/*****************************************************
Function: copy data from one element to the other
		copy data from one select to another
		set text to an element
		enable/disable form element
******************************************************/

function copyData(from,to){
	var tempFrom = document.getElementById(from);
	var tempTo = document.getElementById(to);

	tempTo.value = tempFrom.value;
}

function copyDataSelect(form,from,to){
	document.forms[form][to].options.selectedIndex = document.forms[form][from].selectedIndex;
}

function setFormElementData(element,text){
	document.getElementById(element).value = text;
}

function enableDisableFormElement(form,element,type){ // type: true = disable ; false = enable
	var theForm = document.forms[form];
	var theElement = theForm[element];
	
	if(type){
		theElement.disabled = true;
	}else{
		theElement.disabled = false;
	}
}

/******************************************************
Function: check fields to make sure there is 
		something in it!  If fieldlist containts
		email, check to make sure the email address
		is valid!
******************************************************/
function CheckEmail(sEmail){
	str = document.getElementById(sEmail).value;
	var Errors = false;
	var Emailerrors = false;
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if(str == ""){
		Emailerrors = true;	
	}
	if (str.indexOf(at)==-1){
	  Emailerrors = true;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   Emailerrors = true;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		Emailerrors = true;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		Emailerrors = true;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		Emailerrors = true;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		Emailerrors = true;
	 }
	
	 if (str.indexOf(" ")!=-1){
		Emailerrors = true;
	 }
	 if(Emailerrors){
		alert("Sorry, your email address is invalid!")
		return false;
	 }else{
		return true;	 
	 }
}
function CheckFields(sFields){
	var errors = false;
	var ErrorMsg = "Please enter your:\n\n";
	var temp = 0;
	ElementArr = sFields.split("|");
	for(i=0; i<ElementArr.length; i++){
		if(!document.getElementById(ElementArr[i]).value){
			ErrorMsg += ElementArr[i] + "\n";
			errors = true;
		}
	}
	if(errors == true){
		alert(ErrorMsg)
		return false;
	}else{
		if(sFields.indexOf("Email") != -1){
			var validEmail = CheckEmail("Email");
			if(!validEmail){
				return false;	
			}else{
				return true;	
			}
		}else{	
			return true;
		}
	}
}

/******************************************************
Function: custom function for echelon
		submit the registration form
******************************************************/
function submitRegistration(){
	var fieldsCheck = CheckFields("FirstName|Email");
	
	if(fieldsCheck){ //all required fields have been filled in
		//get all vars
		var Title = document.getElementById("Title").value.stripTags();
		var FirstName = document.getElementById("FirstName").value.stripTags();
		var LastName = document.getElementById("LastName").value.stripTags();
		var Address = document.getElementById("Address").value.stripTags();
		var Subrub = document.getElementById("Suburb").value.stripTags();
		var Postcode = document.getElementById("PostCode").value.stripTags();
		var Phone = document.getElementById("Phone").value.stripTags();
		var Mobile = document.getElementById("Mobile").value.stripTags();
		var Email = document.getElementById("Email").value.stripTags();
		var CommentsQuestions = document.getElementById("CommentsQuestions").value.stripTags();
		var PreferredContactPhone = document.getElementById("PreferredContactPhone").checked;
		var PreferredContactEmail = document.getElementById("PreferredContactEmail").checked;
		
		if(PreferredContactPhone){
			Preferred = "Phone";
		}else{
			Preferred = "Email";
		}
		
		var vars = "Action=RegisterSubmitted&Title="+Title+"&FirstName="+FirstName+"&LastName="+LastName+"&Address="+Address+"&Suburb="+Subrub;
		    vars += "&Postcode="+Postcode+"&Phone="+Phone+"&Mobile="+Mobile+"&Email="+Email+"&Comments="+CommentsQuestions+"&ContactMethod="+Preferred;
		
		submitForm(vars);
	}else{
		return false;	
	}
}