// JavaScript Document

var step1=false;
var step2=false;
var step3=false;

var ELstep1;
var ELstep1C;
var ELstep2;
var ELstep2C;
var ELstep3;
var ELstep3C;

//P1

var nume;
var telefonORO;
var telefonContact;
var email;
var tipClient;
var tipPersRow;
var tipPers;

//P2

var destinatar;
var cat_date;
var cat_scppd;
var cat_scppy;
var cat_scnonoro;

//P3

var msg_title;
var msg_body;
var captcha;

var currentStep=1;

function setElements()
{
	ELstep1=document.getElementById("step1");
	ELstep1C=document.getElementById("step1Content");
	ELstep2=document.getElementById("step2");
	ELstep2C=document.getElementById("step2Content");
	ELstep3=document.getElementById("step3");
	ELstep3C=document.getElementById("step3Content");
	
	//P1
	nume=document.frmSurvey.nume.value;
	telefonORO=document.frmSurvey.numartelefon.value;
	telefonContact=document.frmSurvey.telefoncontact.value;
	email=document.frmSurvey.email;
	tipClient=document.frmSurvey.tip_client1;
	tipPersRow=document.getElementById("tipPersoana");
	tipPers=document.frmSurvey.tip_client2;
	
	//P2
	
	destinatar=document.frmSurvey.destinatar_partial;
	cat_date=document.frmSurvey.message_type;
	cat_scppd=document.frmSurvey.cat_scppd;
	cat_scppy=document.frmSurvey.cat_scppy;
	cat_scnonoro=document.frmSurvey.cat_scnonoro;
	selectFaqs("none");
	
	//P3
	
	msg_title=document.frmSurvey.subiect;
	msg_body=document.frmSurvey.mesaj;
	captcha=document.frmSurvey.j_captcha_response;
	
}

function selectFaqs(faq)
{
	var faqDiv=document.getElementById("faqDiv");
	var faqs=faqDiv.getElementsByTagName("div");
	for(i=0;i<faqs.length;i++)
	{
		if(faqs[i].id==faq)
		{
			faqs[i].style.display="block";
		}
		else
		{
			faqs[i].style.display="none";
		}
	}
	if(!document.getElementById(faq) && faq.indexOf("altele")==-1)
	{
		faqDiv.getElementById("none").style.display="block";
	}
}

function showFaqs(category)
{
	if(category!="none")
	{
		/*if(category=="cat_scppy")
		{
			selectFaqs(cat_scppy.value);
		}
		else if(category=="cat_scppd")
		{
			selectFaqs(cat_scppd.value);
		}
		else if(category=="cat_scnonoro")
		{
			selectFaqs(cat_scnonoro.value);
		}*/
		switch(category)
		{
			case 'cat_scppy':		selectFaqs(cat_scppy.value); break;
			case 'cat_scppd':		selectFaqs(cat_scppd.value); break;
			case 'cat_scnonoro':	selectFaqs(cat_scnonoro.value); break;
			case 'mag_online':		selectFaqs(destinatar.value); break;
			//default:				selectFaqs(destinatar.value); break;
		}
	}
}

function setSteps()
{
	
	if(step1)
	{
		ELstep1.className="stepCompleted";
	}
	else
	{
		ELstep1.className="step";
	}
	
	if(step1)
	{
		ELstep2.className="stepCompleted";
	}
	else
	{
		step2=false;
		ELstep2.className="step";
	}
	if(step2)
	{
		ELstep3.className="stepCompleted";
	}
	else
	{
		step3=false;
		ELstep3.className="step";
	}
}

function selectCategory(category)
{
	divCategorii=document.getElementById("categorii");
	categories=divCategorii.getElementsByTagName("select");
	for(i=0;i<categories.length;i++)
	{
		categories[i].style.display="none";
	}
	if(category!="none")
	{
		divCategorii.style.display="block";
		document.getElementById(category).style.display="block";
	}
	else
	{
		divCategorii.style.display="none";
	}
	showFaqs(category);
}

function nextStep(step, showErrors)
{
	switch(step-1)
	{
		case 1:	if(validS1(showErrors)){selectStep(step);}
				break;
		case 2:	if(validS2(showErrors)){selectStep(step);}
				break;
		case 3: if(validS3(showErrors)){selectStep(step);}
	}
}

function selectStep(step)
{
	switch(step)
	{
		case 2: if(!step1) return; break;
		case 3: if(!step2) return; break;
	}
	
	ELstep1C.style.display="none";
	ELstep2C.style.display="none";
	ELstep3C.style.display="none";
	
	setSteps();
	
	switch(step)
	{
		case 1: ELstep1.className="stepActive"; ELstep1C.style.display="block"; break;
		case 2: if(step1){ELstep2.className="stepActive"; ELstep2C.style.display="block";} break;
		case 3: if(step2){ELstep3.className="stepActive"; ELstep3C.style.display="block";} break;
	}
	
	currentStep=step;
}

function validS1(showErrors)
{
	valid=true;
		
	if(nume.search(/[^a-zA-Z -]/)!="-1" || nume.length==0)
	{
		if(showErrors){document.getElementById("err_nume").style.display="block";}
		valid=false;
	}
	else
	{
		document.getElementById("err_nume").style.display="none";
	}
	if((telefonORO.search(/[^0-9]/)!="-1" || telefonORO.length<10) && !tipClient[2].checked)
	{
		if(showErrors){document.getElementById("err_telefonoro").style.display="block";}
		valid=false;
	}
	else
	{
		document.getElementById("err_telefonoro").style.display="none";
	}
	
	if(telefonContact.search(/[^0-9]/)!="-1" || telefonContact.length<10)
	{
		if(showErrors){document.getElementById("err_telefoncontact").style.display="block";}
		valid=false;
	}
	else
	{
		document.getElementById("err_telefoncontact").style.display="none";
	}
	
	with(email)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<=1 || dotpos-apos<3 || value.length==0 || value.length-dotpos-1<2) 
		{
			if(showErrors){document.getElementById("err_email").style.display="block";};
			valid=false;
		}
		else 
		{
			document.getElementById("err_email").style.display="none";
		}
	}
	
	if(tipClient[0].checked )
	{
		
		tipPersRow.style.display="block";
	}
	else
	{
		tipPersRow.style.display="none";
	}
	
	if(!tipClient[0].checked && !tipClient[1].checked && !tipClient[2].checked)
	{
		if(showErrors){document.getElementById("err_tip_client").style.display="block";};
		valid=false;
	}
	else
	{
		document.getElementById("err_tip_client").style.display="none";
	}
	
	if(tipClient[0].checked && !tipPers[0].checked && !tipPers[1].checked)
	{
		if(showErrors){document.getElementById("err_tip_pers").style.display="block";};
		valid=false;
	}
	else
	{
		document.getElementById("err_tip_pers").style.display="none";
	}
	
	if(tipClient[2].checked && (telefonORO=="" && telefonORO.length<10))
	{
		document.frmSurvey.numartelefon.value=telefonContact;
	}
	
	if(valid)
	{
		step1=true;
		return true;
	}
	else
	{
		step1=false;
	}
}

function validS2(showErrors)
{
	valid=true;
	if(destinatar.value=="none")
	{
		if(showErrors){document.getElementById("err_destinatar").style.display="block";}
		valid=false;
	}
	else
	{
		document.getElementById("err_destinatar").style.display="none";
	}
	if(destinatar.value=="sc" && tipClient[0].checked)
	{
		selectCategory("cat_scppd");
		if(cat_scppd.value=="none")
		{
			if(showErrors){document.getElementById("err_categorie").style.display="block";}
			valid=false;
		}
		else
		{
			document.getElementById("err_categorie").style.display="none";
		}
	}
	else if(destinatar.value=="sc" && tipClient[1].checked)
	{
		selectCategory("cat_scppy");
		if(cat_scppy.value=="none")
		{
			if(showErrors){document.getElementById("err_categorie").style.display="block";}
			valid=false;
		}
		else
		{
			document.getElementById("err_categorie").style.display="none";
		}
	}
	else if(destinatar.value=="sc" && tipClient[2].checked)
	{
		selectCategory("cat_scnonoro");
		if(cat_scnonoro.value=="none")
		{
			if(showErrors){document.getElementById("err_categorie").style.display="block";}
			valid=false;
		}
		else
		{
			document.getElementById("err_categorie").style.display="none";
		}
	}
	else if(destinatar.value=="sd")
	{
		selectCategory("s9");
	}
	else if(destinatar.value=='mag_online')
	{
		selectCategory("none");
		showFaqs(destinatar.value);
	}
	else
	{
		selectCategory("none");
	}
	
	if(destinatar.value=='mag_online') document.getElementById('nextS2').style.display="none";
	else document.getElementById('nextS2').style.display="block";
	
	if(valid)
	{
		step2=true;
		return true;
	}
	else
	{
		step2=false;
	}
}

function validS3(showErrors)
{
	valid=true;
	if(msg_title.value.length==0)
	{
		if(showErrors){document.getElementById("err_msg_title").style.display="block";}
		valid=false;
	}
	else
	{
		document.getElementById("err_msg_title").style.display="none";
	}
	if(msg_body.value.length==0)
	{
		if(showErrors){document.getElementById("err_msg_body").style.display="block";}
		valid=false;
	}
	else
	{
		document.getElementById("err_msg_body").style.display="none";
	}
	if(captcha.value.length==0 || captcha.value.length<4 || captcha.value.length>4)
	{
		if(showErrors){document.getElementById("err_captcha").style.display="block";}
		valid=false;
	}
	else
	{
		document.getElementById("err_captcha").style.display="none";
	}
	if(valid)
	{
		step3=true;
		return true;
	}
	else
	{
		step3=false;
	}
}

function refreshForm(showErrors)
{
	setElements();
	
	if(validS1(showErrors))
	{
		document.getElementById("nextS1").className="next";
	}
	else
	{
		document.getElementById("nextS1").className="nextInactive";
	}
	
	if(validS2(showErrors))
	{
		document.getElementById("nextS2").className="next";
	}
	else
	{
		document.getElementById("nextS2").className="nextInactive";
	}
	validS3(showErrors);
	
	setSteps();
	selectStep(currentStep);
}



function upperCase(x)
{
	var y=document.getElementById(x).value;
	document.getElementById(x).value=y.toUpperCase();
}

function resetForm()
{
	document.frmSurvey.reset();
	refreshForm(false);
	selectStep(1);
}

function submitForm()
{
	
	var category="";
	var category_value;
	validS3(true);
	if(step3)
	{
		//document.frmSurvey.titluMesaj.value=msg_title.value;
		
		if(document.frmSurvey.message_type.value=="none")
		{
				document.frmSurvey.message_type.value="informatie";
		}
		if(destinatar.value=="sc" && tipClient[0].checked)
		{
			category=cat_scppd.options[cat_scppd.selectedIndex].innerHTML;
			category_value=cat_scppd.options[cat_scppd.selectedIndex].value;
		}
		else if(destinatar.value=="sc" && tipClient[1].checked)
		{
			category=cat_scppy.options[cat_scppy.selectedIndex].innerHTML;
			category_value=cat_scppy.options[cat_scppy.selectedIndex].value;
		}
		else if(destinatar.value=="sc" && tipClient[2].checked)
		{
			category=cat_scnonoro.options[cat_scnonoro.selectedIndex].innerHTML;
			category_value=cat_scnonoro.options[cat_scnonoro.selectedIndex].value;
		}
		else if(destinatar.value=="sd")
		{
			category=cat_date.options[cat_date.selectedIndex].innerHTML;
			category_value=cat_date.options[cat_date.selectedIndex].value;
		}
		else 
		{
			category=destinatar.options[destinatar.selectedIndex].innerHTML;
			category_value=null;
		}
		if(category)
		{
			var tip_persoana=get_client_type();
			document.frmSurvey.subiect_customer.value=tip_persoana+" : "+category+" - "+msg_title.value;
			document.frmSurvey.subiect_autoreply.value=category+" : "+msg_title.value;
			document.frmSurvey.destinatar.value=(category_value)?destinatar.value+"_"+category_value:destinatar.value;
		}
		//alert(document.frmSurvey.subiect_customer.value);
		document.frmSurvey.submit();
	}
}

function get_client_type()
{
	if(tipClient[0].checked)
	{
		if(tipPers[0].checked) return "(PF) Postpaid";
		else if(tipPers[1].checked) return "(PJ) Postpaid";
	}
	else if(tipClient[1].checked)
	{
		return "PrePay";
	}
	else if(tipClient[2].checked)
	{
		return "NON ORO";
	}
	else return "";
}

function try_login()
{
	document.frmSurvey.action="https://www.orange.ro/contul-meu";
	document.frmSurvey.redirect_tt.value="1";
	document.frmSurvey.submit();
}

function recover_password()
{
	document.frmSurvey.action="https://www.orange.ro/contul-meu";
	document.frmSurvey.nextstep.value="WEBSHOP.RECOVER_LOGIN";
	document.frmSurvey.submit();
}
