// JavaScript Document

function funSubmit(varDisplay) {
	var varObj = document.getElementById("divAbstract").style;
	varObj.display=varDisplay;
	var formId = document.regForm;
	formId.title.value = "";
	formId.authors.value = "";
	formId.affilAbst.value = "";
	formId.abstract.value = "";
	formId.grants.value = "";
	formId.keyword1.value = "";
	formId.keyword2.value = "";
	formId.keyword3.value = "";
	formId.keyword4.value = "";
	formId.keyword5.value = "";
	formId.payment.value = "";
}

function funPayment(varDisplay) {
	var varObj = document.getElementById("divPayment").style;
	varObj.display=varDisplay;
}

function ValidateForm(){
	var formId = document.regForm;
	var emailID = formId.email;
	var aws = true;
	var msg = "";
		
	if ((formId.name.value==null) || (formId.name.value=="")) {
		aws = false;
		msg = msg + "\n- Please fill in your name";
		formId.name.focus;
	}
		
	if ((formId.affil.value==null) || (formId.affil.value=="")) {
		aws = false;
		msg = msg + "\n- Please fill in your affiliation";
		formId.affil.focus;
	}
	
	if ((emailID.value==null)||(emailID.value=="")){
		aws = false;
		msg = msg + "\n- Please fill in your email";
		emailID.focus();
	} else {
		if (echeck(emailID.value)==false){
			aws = false;
			emailID.value="";
			emailID.focus();
			msg = msg + "\n- Please fill in a valid e-mail address";
		}
	}
	
	if ((formId.address.value==null) || (formId.address.value=="")) {
		aws = false;
		msg = msg + "\n- Please fill in your address";
		formId.address.focus;
	}
	
	var checkCateg = false;
	for (var loop=0;loop<formId.category.length;loop++) {
	  if(formId.category[loop].checked) {
		checkCateg = true;
	  }
	}
	if(!checkCateg){msg = msg + "\n- Please choose a registration category";}
	
	varCountry = 1;
	for (i=0;i<formId.country.length-1;i++) {
		if (formId.country[i].checked) {
			varCountry = i;
		}
	}
	if (varCountry == 0) {
		if ((formId.payment.value==null) || (formId.payment.value=="")) {
			aws = false;
			msg = msg + "\n- If you live in Brazil, please attach a copy of your payment order";
			formId.payment.focus;
		}
	}

	varAbstSub = 1;
	for (i=0;i<formId.abstSub.length-1;i++) {
		if (formId.abstSub[i].checked) {
			varAbstSub = i;
		}
	}
	
	if (varAbstSub == 0) {

		if ((formId.title.value==null) || (formId.title.value=="")) {
			aws = false;
			msg = msg + "\n- Please fill in the title of your abstract";
			formId.title.focus;
		} else {
			numWords = formId.title.value.split(" ");
			if (numWords.length > 12) {
				aws = false;
				formId.title.focus();
				msg = msg + "\n- The title must have up to 12 words";
   			}
		
		}
		
		if ((formId.authors.value==null) || (formId.authors.value=="")) {
			aws = false;
			msg = msg + "\n- Please fill in the authors of your abstract";
			formId.authors.focus;
		}
		
		if ((formId.affilAbst.value==null) || (formId.affilAbst.value=="")) {
			aws = false;
			msg = msg + "\n- Please fill in  the affilition of the authors of your abstract";
			formId.affilAbst.focus;
		}
		
		if ((formId.abstract.value==null) || (formId.abstract.value=="")) {
			aws = false;
			msg = msg + "\n- Please fill in your abstract";
			formId.abstract.focus;
		} else {
			if (formId.abstract.value.length > 1500) {
				aws = false;
				formId.abstract.focus();
				msg = msg + "\n- The abstract must have up to 1500 characters";
   			}
		
		}
		
		if ((formId.grants.value==null) || (formId.grants.value=="")) {
			aws = false;
			msg = msg + "\n- Please fill in the grants of your abstract";
			formId.authors.focus;
		}
		
		countKeys = 0;
		for(i=1;i<6;i++) {
			isFilled = document.getElementById("keyword"+i).value;
			if ((isFilled != null)&&(isFilled != "")) {
				countKeys++;
			}
		}
		
		if (countKeys<2) {
			aws = false;
			msg = msg + "\n- Please fill in at least 2 keywords";
		}
		

	}
	
	if (aws) {
		return true;
	} else {
		alert("Please, review the following errors:\n"+msg);
		return false;	
	}	
}
	 
	 
function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		return false
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	
	if (str.indexOf(at,(lat+1))!=-1){
		return false
	}
	
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	}
	
	if (str.indexOf(dot,(lat+2))==-1){
		return false
	}
			
	if (str.indexOf(" ")!=-1){
		return false
	}
	
	return true					
}

function printReceipt(){  
	window.open('','MaRGA','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=510,height=480');  
	document.receiptForm.submit();  
}