/**
 * Form Validation
 * By Xavier Beurois
 * Copyright Arumtec 2009
 */
 
function semValidation(error){
	form=document.getElementById('commentForm');
	inputs=form.getElementsByTagName('input');
	selects=form.getElementsByTagName('select');
	labels=form.getElementsByTagName('label');

	var errorgen=false;
	for(i=0,j=0;i<inputs.length;j++,i++){
		if(/^[a-zA-Z0-9 ]*required[a-zA-Z0-9 ]*$/.test(inputs[i].className)){
			labels[j].className="";
			inputs[i].className=inputs[i].className.replace(/error/g,"");
			if(/^[a-zA-Z0-9 ]*email[a-zA-Z0-9 ]*$/.test(inputs[i].className)){
				if(!isValidEmail(inputs[i].value)){
					labels[j].className="error";
					inputs[i].className+=" error";
					errorgen=true;
				}
			}else{
				if(inputs[i].value=="" || inputs[i].value.length==0){
					labels[j].className="error";
					inputs[i].className+=" error";
					errorgen=true;
				}
			}
		}
		if(inputs[i].type=="checkbox")
			j--;
	}
	for(i=0;i<selects.length;i++){
		if(/^[a-zA-Z0-9 ]*required[a-zA-Z0-9 ]*$/.test(selects[i].className)){
			span=document.getElementById("select"+selects[i].id);
			span.style.backgroundColor="transparent";
			span.style.border="none";
			if(selects[i].value=="" || selects[i].value.length==0){
				span.style.backgroundColor="#990000";
				span.style.border="1px dotted #990000";
				errorgen=true;
			}				
		}
	}
	errgen=document.getElementById('errorgen');
	errgen.innerHTML="";
	if(errorgen)
		errgen.appendChild(document.createTextNode(error));
	else{
		errgen.innerHTML="";
		ctt="vw="+mthCrypt1.encode(form.vw.value);
		elt=document.getElementById('subbtnspan');
		elt.innerHTML="";
		myIMG=document.createElement('img');
		myIMG.setAttribute('src','/assets/templates/images/misc/ajax-loader.gif');
		myIMG.setAttribute('align','absmiddle');
		elt.appendChild(myIMG);
		myTXT=document.createTextNode('  Validation ...');
		elt.appendChild(myTXT);
		sendSemInfos("send_vw","c="+mthCrypt1.encode(ctt))
	}
}

function createForms(c1,c2,c3){
	form=document.getElementById("train_insc_form");
	selects=form.getElementsByTagName("select");
	var errorgen=false;
	for(i=0;i<selects.length;i++){
		span=document.getElementById("select"+selects[i].name);
		span.style.backgroundColor="transparent";
		span.style.border="none";
		if(selects[i].value=="" || selects[i].value.length==0){
			span.style.backgroundColor="#990000";
			span.style.border="1px dotted #990000";
			errorgen=true;
		}
	}
	if(!errorgen){
		document.getElementById('valBtn').style.display='none';
		options=document.getElementById("nbrpart");
		options=options.getElementsByTagName("option");
		for(d=0;d<options.length;d++){
			if(options[d].selected==true)
				nbrpart=option[d].childNodes[0].nodeValue;
		}
		mainContainer=document.getElementById("others_contacts");
		mainContainer.innerHTML="";
		if(nbrpart!="" && nbrpart!=0){
			myDIV=document.createElement("div");
			myDIV.className="linetitle";
			mySPAN=document.createElement("span");
			mySPAN.className="title";
			mySPAN.appendChild(document.createTextNode(c1));
			myDIV.appendChild(mySPAN);
			mainContainer.appendChild(myDIV);
			mainTab=document.createElement("table");
			mainTab.setAttribute("cellpadding","0");
			mainTab.setAttribute("cellspacing","0");
			left=true;
			c3=c3.split("|");
			for(i=1;i<parseInt(nbrpart)+1;i++){
				if(left==true){
					myTR=document.createElement("tr");
					myTR.className="line3";
					tdCol1=document.createElement("td");
					tdCol1.className="col1";
					cellTab=document.createElement("table");
					cellTab.setAttribute("cellpadding","0");
					cellTab.setAttribute("cellspacing","0");
					cellTabtr=document.createElement("tr");
					cellTabtrtd=document.createElement("td");
					cellTabtrtd.setAttribute("colspan","2");
					cellTabtrtd.className="title";
					cellTabtrtd.appendChild(document.createTextNode(c2+i));
					cellTabtr.appendChild(cellTabtrtd);
					cellTab.appendChild(cellTabtr);
					for(j=0;j<5;j++){
						cellTabtr=document.createElement("tr");
						cellTabtrtd=document.createElement("td");
						cellTabtrtd.className="col1";
						cellTabtrtdlbl=document.createElement("label");
						cellTabtrtdlbl.setAttribute("for","c"+c3[j]+i);
						cellTabtrtdlbl.appendChild(document.createTextNode(c3[j]));
						cellTabtrtdlblem=document.createElement("em");
						cellTabtrtdlblem.appendChild(document.createTextNode("*"));
						cellTabtrtdlbl.appendChild(cellTabtrtdlblem);
						cellTabtrtd.appendChild(cellTabtrtdlbl);
						cellTabtr.appendChild(cellTabtrtd);
						cellTabtrtd=document.createElement("td");
						if(i==parseInt(nbrpart))
							cellTabtrtd.className="col2 last";
						else
							cellTabtrtd.className="col2";
						cellTabtrtdinp=document.createElement("input");
						cellTabtrtdinp.setAttribute("type","text");
						cellTabtrtdinp.setAttribute("id","c"+c3[j]+i);
						cellTabtrtdinp.setAttribute("name",c3[j]+i);
						if(c3[j]=="EMAIL")
							cellTabtrtdinp.setAttribute("class","required email");
						else
							cellTabtrtdinp.setAttribute("class","required");
						cellTabtrtd.appendChild(cellTabtrtdinp);
						cellTabtr.appendChild(cellTabtrtd);
						cellTab.appendChild(cellTabtr);
					}
					tdCol1.appendChild(cellTab);
					myTR.appendChild(tdCol1);
				}else{
					tdCol2=document.createElement("td");
					tdCol2.className="col2";
					cellTab=document.createElement("table");
					cellTab.setAttribute("cellpadding","0");
					cellTab.setAttribute("cellspacing","0");
					cellTabtr=document.createElement("tr");
					cellTabtrtd=document.createElement("td");
					cellTabtrtd.setAttribute("colspan","2");
					cellTabtrtd.className="col1 title";
					cellTabtrtd.appendChild(document.createTextNode(c2+i));
					cellTabtr.appendChild(cellTabtrtd);
					cellTab.appendChild(cellTabtr);
					for(j=0;j<5;j++){
						cellTabtr=document.createElement("tr");
						cellTabtrtd=document.createElement("td");
						cellTabtrtd.className="col1";
						cellTabtrtdlbl=document.createElement("label");
						cellTabtrtdlbl.setAttribute("for","c"+c3[j]+i);
						cellTabtrtdlbl.appendChild(document.createTextNode(c3[j]));
						cellTabtrtdlblem=document.createElement("em");
						cellTabtrtdlblem.appendChild(document.createTextNode("*"));
						cellTabtrtdlbl.appendChild(cellTabtrtdlblem);
						cellTabtrtd.appendChild(cellTabtrtdlbl);
						cellTabtr.appendChild(cellTabtrtd);
						cellTabtrtd=document.createElement("td");
						cellTabtrtd.className="col2";
						cellTabtrtdinp=document.createElement("input");
						cellTabtrtdinp.setAttribute("type","text");
						cellTabtrtdinp.setAttribute("id","c"+c3[j]+i);
						cellTabtrtdinp.setAttribute("name",c3[j]+i);
						if(c3[j]=="EMAIL")
							cellTabtrtdinp.setAttribute("class","required email");
						else
							cellTabtrtdinp.setAttribute("class","required");
						cellTabtrtd.appendChild(cellTabtrtdinp);
						cellTabtr.appendChild(cellTabtrtd);
						cellTab.appendChild(cellTabtr);
					}
					tdCol2.appendChild(cellTab);
					myTR.appendChild(tdCol2);
				}
				mainTab.appendChild(myTR);
				left=!left;
			}
			mainContainer.appendChild(mainTab);
		}
	}
}

function submitTrainInsc(){
	form=document.getElementById('train_insc_form');
	inputs=form.getElementsByTagName('input');
	selects=form.getElementsByTagName('select');
	labels=form.getElementsByTagName('label');
	
	var errorgen=false;
	for(i=0;i<selects.length;i++){
		span=document.getElementById("select"+selects[i].id);
		span.style.backgroundColor="transparent";
		span.style.border="none";
		if(selects[i].value=="" || selects[i].value.length==0){
			span.style.backgroundColor="#990000";
			span.style.border="1px dotted #990000";
			errorgen=true;
		}
	}
	if(!errorgen){
		for(i=0,j=0;i<inputs.length;j++,i++){
			if(/^[a-zA-Z0-9 ]*required[a-zA-Z0-9 ]*$/.test(inputs[i].className) && inputs[i].type=="text"){
				labels[j].className="";
				inputs[i].className=inputs[i].className.replace(/error/g,"");
				if(/^[a-zA-Z0-9 ]*email[a-zA-Z0-9 ]*$/.test(inputs[i].className)){
					if(!isValidEmail(inputs[i].value)){
						labels[j].className="error";
						inputs[i].className+=" error";
						errorgen=true;
					}
				}else{
					if(inputs[i].value=="" || inputs[i].value.length==0){
						labels[j].className="error";
						inputs[i].className+=" error";
						errorgen=true;
					}
				}
			}
			if(inputs[i].type!="text")
				j--;
		}
		if(!errorgen){
			document.getElementById('errorgen').style.display="none";
			ctt="vw="+mthCrypt1.encode(form.captcha.value);
			elt=document.getElementById('valBtnDivCont');
			elt.innerHTML="";
			myIMG=document.createElement('img');
			myIMG.setAttribute('src','/assets/templates/images/misc/ajax-loader.gif');
			myIMG.setAttribute('align','absmiddle');
			elt.appendChild(myIMG);
			myTXT=document.createTextNode('  Validation ...');
			elt.appendChild(myTXT);
			sendTInscInfos("send_vw","c="+mthCrypt1.encode(ctt));
		}else
			document.getElementById('errorgen').style.display="block";
	}
	if(errorgen)
		document.getElementById('errorgen').style.display="block";
}

function submitContactForm(){
	form=document.getElementById('form_information_demand');
	inputs=form.getElementsByTagName('input');
	selects=form.getElementsByTagName('select');
	labels=form.getElementsByTagName('label');
	document.getElementById('forminfoerr').style.display="none";
	
	var errorgen=false;
	span=document.getElementById("selectlistobj");
	span.style.backgroundColor="transparent";
	span.style.border="none";
	labels[0].className="";
	if(selects[0].value=="" || selects[0].value.length==0){
		span.style.backgroundColor="#990000";
		span.style.border="1px dotted #990000";
		labels[0].className="error";
		errorgen=true;
	}
	for(i=0,l=1;i<inputs.length;i++,l++){
		if(labels[l]) labels[l].className="";
		inputs[i].className=inputs[i].className.replace(/error/g,"");
		if(/^[a-zA-Z0-9 ]*email[a-zA-Z0-9 ]*$/.test(inputs[i].className)){
			if(!isValidEmail(inputs[i].value)){
				if(labels[l]) labels[l].className="error";
				inputs[i].className+=" error";
				errorgen=true;
			}
		}else{
			if(inputs[i].value=="" || inputs[i].value.length==0){
				labels[l].className="error";
				inputs[i].className+=" error";
				errorgen=true;
			}
		}
	}
	form.message.className=form.message.className.replace(/error/g,"");
	if(form.message.value=="" || form.message.value.length==0){
		form.message.className+=" error"
		errorgen=true;
	}
	if(errorgen)
		document.getElementById('forminfoerr').style.display="block";
	else{
		document.getElementById('forminfoerr').style.display="none";
		document.getElementById("ctformimgload").style.display="block";
		document.getElementById("subbtn").style.display="none";
		ctt="vw="+mthCrypt1.encode(form.vw.value);
		sendContactForm("send_vw","c="+mthCrypt1.encode(ctt));
	}
}

//Operations
function isValidEmail(d){
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(d);
}

function vwtrue(t){
	switch(t){
		case "sem":
			form=document.getElementById('commentForm');
			content="email="+mthCrypt1.encode(form.email.value)+"&name="+mthCrypt1.encode(form.name.value)+"&fname="+mthCrypt1.encode(form.fname.value)+"&company="+mthCrypt1.encode(form.company.value)+"&office="+mthCrypt1.encode(form.office.value)+"&phone="+mthCrypt1.encode(form.phone.value)+"&postaladr="+mthCrypt1.encode(form.postaladr.value)+"&cp="+mthCrypt1.encode(form.cp.value)+"&city="+mthCrypt1.encode(form.city.value)+"&listsem="+mthCrypt1.encode(form.listsem.value);
			if(form.prog.checked)
				content+="&prog="+mthCrypt1.encode("true");
			if(form.nl.checked)
				content+="&nl="+mthCrypt1.encode("true");
			if(form.listsem2.value.length!=0)
				content+="&listsem2="+mthCrypt1.encode(form.listsem2.value);
			sendSemInfos("send_sem","c="+mthCrypt1.encode(content));
		break;
		case "tinsc":
			form=document.getElementById('train_insc_form');
			content="session="+mthCrypt1.encode(form.session.value)+"&nbrpart="+mthCrypt1.encode(form.nbrpart.value);
			inputs=form.getElementsByTagName("input");
			for(i=0;i<inputs.length;i++){
				if(inputs[i].type=="text" && inputs[i].name!="captcha")
					content+="&"+inputs[i].name+"="+mthCrypt1.encode(inputs[i].value);
			}
			if(form.comment.value.length!=0)
				content+="&comment="+mthCrypt1.encode(form.comment.value);
			if(i==12)
				document.getElementById('errornoparts').style.display="block";
			else
				sendTInscInfos("send_tinsc","c="+mthCrypt1.encode(content));
		break;
		case "contact":
			form=document.getElementById('form_information_demand');
			content="object="+mthCrypt1.encode(form.listobj.value)+"&company="+mthCrypt1.encode(form.company.value)+"&name="+mthCrypt1.encode(form.name.value)+"&fname="+mthCrypt1.encode(form.fname.value)+"&email="+mthCrypt1.encode(form.email.value)+"&office="+mthCrypt1.encode(form.office.value)+"&postaladr="+mthCrypt1.encode(form.postaladr.value)+"&cp="+mthCrypt1.encode(form.cp.value)+"&city="+mthCrypt1.encode(form.city.value)+"&phone="+mthCrypt1.encode(form.phone.value)+"&message="+mthCrypt1.encode(form.message.value);
			sendContactForm("send_contact","c="+mthCrypt1.encode(content));
		break;
	}
}

function vwfalse(){
	captcha=document.getElementById('captchadiv');
	img=captcha.getElementsByTagName('img')[0];
	lbl=captcha.getElementsByTagName('label')[0];
	inp=captcha.getElementsByTagName('input')[0];
	img.src="/manager/includes/veriword.php?rand="+Math.random();
	lbl.className="error";
	inp.className+=" error";
}

