// JavaScript Document


function validateAll(id) {	

	form = document.getElementById(id);
	
	item_number = 0;
	
	document.getElementById("inputs").innerHTML = '';
	
	valid = true;
	
	//Validate First Name
	 if (validateField(form.fname.value, 'First Name', 'fnametxt') == false )
	 	valid = false;
	
	//Validate Last Name
	 if (validateField(form.lname.value, 'Last Name', 'lnametxt') == false )
	 	valid = false;
	
	//Validate City
	 if (validateField(form.city.value, 'City', 'citytxt') == false )
	 	valid = false;
	
	//Validate State
	 if (validateField(form.state.value, 'State', 'statetxt') == false )
	 	valid = false;
	
	//Validate Phone
	 if (validateNumber(form.phone1.value, form.phone2.value, form.phone3.value, 'Phone Number', 'phonetxt') == false )
	 	valid = false;
	
	//Validate email
	 if (validateEmail(form.email.value, true, true, 'emailtxt') == false )
	 	valid = false;
		
	//Validate Registration	
	document.getElementById('regtxt').innerHTML = '' 
	
	if (form.power_gifts_couple.checked == true) {
		
		if (form.spouse.value == '' || form.spouse.value == null) {
			document.getElementById('spousetxt').innerHTML = 'Spouse is required when registering as couple.' ;
	 		valid = false;	
		}
		if ((form.power_gifts_couple.checked == true) && (form.power_gifts_single.checked == true ) ) {
			document.getElementById('regtxt').innerHTML = 'Cannot register for both singel and couple.' ;
	 		valid = false;	
		}
	}	

	if (form.luncheon2.checked == true) {
		
		if (form.spouse.value == '' || form.spouse.value == null) {
			document.getElementById('spousetxt').innerHTML = 'Spouse is required when registering as couple.' ;
	 		valid = false;	
		}
		if ((form.luncheon2.checked == true) && (form.luncheon.checked == true ) ) {
			document.getElementById('regtxt').innerHTML = 'Cannot register for both single and couple.' ;
	 		valid = false;	
		}
	}
	
	if ( valid == true ) {
		document.getElementById("registration").submit();
	}
	
	return valid;

}

function validateField(name,type,id) {	

	document.getElementById(id).innerHTML = '<img style="margin:0px; padding=0px;" src="http://www.treeoflifeintl.com/images/success.png" alt="success">' ;

	if (name == '' || name == null) {
	   document.getElementById(id).innerHTML = type+' is required';
	   return false;
	}
	return true;

}

function validateNumber(phone1, phone2, phone3,type,id) {	

	document.getElementById(id).innerHTML = '<img style="margin:0px; padding=0px;" src="http://www.treeoflifeintl.com/images/success.png" alt="success">' ;

	if (phone1 == '' || phone1 == null || phone2 == '' || phone2 == null || phone3 == '' || phone3 == null) {
	   document.getElementById(id).innerHTML = type+' is required';
	   return false;
	}
	
	if (phone1.length != 3 || phone2.length != 3 || phone3.length != 4) {
	   document.getElementById(id).innerHTML = type+' is invalid';
	   return false;
	}
	
	return true;

}

function validateEmail(addr,man,db,id) {
	
	document.getElementById(id).innerHTML = '<img style="margin:0px; padding=0px;" src="http://www.treeoflifeintl.com/images/success.png" alt="success">' ;
	
	if (addr == '' && man) {
	   if (db) 
	   document.getElementById(id).innerHTML = 'email address is required';
	   return false;
	}
	if (addr == '') return true;
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  if (db) 
		  document.getElementById(id).innerHTML = 'email address contains invalid characters';
		  return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
		  if (db)
		  document.getElementById(id).innerHTML = "email address contains non ascii characters.";
		  return false;
	   }
	}
	
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   if (db)
	   document.getElementById(id).innerHTML = 'email address must contain an @';
	   return false;
	}
	if (atPos == 0) {
	   if (db)
	   document.getElementById(id).innerHTML = 'email address must not start with @';
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   if (db) 
	   document.getElementById(id).innerHTML = 'email address must contain only one @';
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   if (db) 
	   document.getElementById(id).innerHTML = 'email address must contain a period in the domain name';
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   if (db) 
	   document.getElementById(id).innerHTML = 'period must not immediately follow @ in email address';
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   if (db) 
	   document.getElementById(id).innerHTML = 'period must not immediately precede @ in email address';
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	   if (db) 
	   document.getElementById(id).innerHTML = 'two periods must not be adjacent in email address';
	   return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	   if (db) 
	   document.getElementById(id).innerHTML = 'invalid primary domain in email address';
	   return false;
	}
	return true;
}