// JavaScript Document
function validateform()
{	
	var count = 0;
	
	removeallerrors();
	
	if ((document.getElementById('name_lbl')!= null) && (verifyname(document.forms.myform.name.value , document.getElementById('name_lbl')) == false)) {count+=1;}
	
	if ((document.getElementById('mailaddr_lbl')!= null) && (verifyaddress(document.forms.myform.address.value , document.getElementById('mailaddr_lbl')) == false)) {count+=1;}
	
	if ((document.getElementById('state_lbl')!= null) && (verifystate(document.forms.myform.state.options[document.forms.myform.state.selectedIndex].value , document.getElementById('state_lbl')) == false)) {count+=1;}
	
	if ((document.getElementById('city_lbl')!= null) && (verifycity(document.forms.myform.city.value , document.getElementById('city_lbl')) == false)) {count+=1;}
	
	if ((document.getElementById('zipcode_lbl')!= null) && (verifyzipcode(document.forms.myform.zipcode.value , document.getElementById('zipcode_lbl')) == false)) {count+=1;}
	
	if ((document.getElementById('phone_lbl')!= null) && (verifyphone(document.forms.myform.phone.value , document.getElementById('phone_lbl')) == false)) {count+=1;}
	
	if ((document.getElementById('gifts_lbl')!= null) && (verify_checkboxes() == false)) {count+=1;}
	
	if ((document.getElementById('quantity_lbl')!= null) && (verifyticketnum(document.forms.myform.quantity.value , document.getElementById('quantity_lbl')) == false)) {count+=1;}
	
	if ((document.getElementById('guests_lbl')!= null) && (verifyguests_names() == false)) {count+=1; alert("\nPlease make sure the number of the tickets is\n\nthe same with the number of the entered names!\n  "); var flag='yes'}
	
	if ((document.getElementById('email_lbl')!= null) && (verifyemail(document.forms.myform.email.value , document.getElementById('email_lbl')) == false)) {count+=1;}
	
	if ((document.getElementById('ad_selection_lbl')!= null) && (verifyad_selection(document.forms.myform.ad_selection.options[document.forms.myform.ad_selection.selectedIndex].value , document.getElementById('ad_selection_lbl')) == false)) {count+=1;}
	
	
	if (count>0 ) 
	{
		if (count == 1 && flag == "yes")
		    { }
		else
			document.getElementById('formerror').style.display = 'block';
		return false; 
	}
	else if (flag == 'yes')
		return false;
	else
	{
		return true;
	}		
}

function removeallerrors()
{	
	if (document.getElementById('name_lbl')!= null)
		document.getElementById('name_lbl').style.color = '#666666';
	
	if (document.getElementById('mailaddr_lbl')!= null)
		document.getElementById('mailaddr_lbl').style.color = '#666666';
	
	if (document.getElementById('city_lbl')!= null)
		document.getElementById('city_lbl').style.color = '#666666';
	
	if (document.getElementById('state_lbl')!= null)
		document.getElementById('state_lbl').style.color = '#666666';
	
	if (document.getElementById('zipcode_lbl')!= null)
		document.getElementById('zipcode_lbl').style.color = '#666666';
	
	if (document.getElementById('phone_lbl')!= null)
		document.getElementById('phone_lbl').style.color = '#666666';
		
	if (document.getElementById('email_lbl')!= null)
		document.getElementById('email_lbl').style.color = '#666666';
		
	if (document.getElementById('gifts_lbl')!= null)
		document.getElementById('gifts_lbl').style.display = 'none';
		
	if (document.getElementById('gifts_lbl2')!= null)
		document.getElementById('gifts_lbl2').style.display = 'none';
	
	if (document.getElementById('guests_lbl')!= null)
		document.getElementById('guests_lbl').style.color = '#666666';
		
	if (document.getElementById('ad_selection_lbl')!= null)
		document.getElementById('ad_selection_lbl').style.color = '#666666';
	
	if (document.getElementById('quantity_lbl')!= null)
		document.getElementById('quantity_lbl').style.color = '#666666';
	
	document.getElementById('formerror').style.display = 'none';
}

function verifyemail(obj, lbl){
	var em = obj;  
	if (trim(em)=='') {
			lbl.style.color = 'red';
			return false;
			}
	var re = new RegExp();
	re = /.+@.+\..+/
	if (em != "") 
	{
		if (!(re.test(em))) 
		{
			lbl.style.color = 'red';
			return false;
			
		}		
		return true;		
	}
	lbl.style.color = 'red';
	return false;
}

function verifyphone(strPhone, lbl) {
	// Pattern matches 9999999999, 999-999-9999, (999)-999-9999, (999) 999-9999, (999)999-9999
	var regexp = /^(\d{10}|\d{3}-\d{3}-\d{4}|(\((\d{3})\)|(\d{3}))[- ]?)?(\d{3})[- ]?(\d{4})$/;
	// The exec() method returns an array of matched substrings:
	if (regexp.exec(strPhone)) {
		// At least one of $3 or $4 will be null:
		//var match = RegExp.$3 + RegExp.$4 + RegExp.$5 + RegExp.$6;
		//window.document.simpleForm.phone.value = match;
		return true;
	}
	//document.getElementById('formerror').style.display = 'block';
	lbl.style.color = 'red';
	return false;
}

function verifyname (strname, lbl) {

	var regexp = /^[a-zA-Z''-'\s]{1,40}$/;
	// The exec() method returns an array of matched substrings:
	if (regexp.exec(strname)) {
		
		return true;
	}
	//document.getElementById('formerror').style.display = 'block';
	lbl.style.color = 'red';
	return false;
}

function verifyname2 (strname) {

	var regexp = /^[a-zA-Z''-'\s]{1,40}$/;
	// The exec() method returns an array of matched substrings:
	if (regexp.exec(strname)) {
		
		return true;
	}
	return false;
}

function verifycity (obj, lbl) {

	var regexp = /^[a-zA-Z'-'\s]{3,30}$/;
	// The exec() method returns an array of matched substrings:
	if (regexp.exec(obj)) {
		
		return true;
	}
	//document.getElementById('formerror').style.display = 'block';
	lbl.style.color = 'red';
	return false;
}

function verifyzipcode(obj, lbl) {
    
	var regexp = /^(\d{5}-\d{4}|\d{5}|\d{9})$|^([a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d)$/;
	// The exec() method returns an array of matched substrings:
	if (regexp.exec(obj)) {
		
		return true;
	}
	//document.getElementById('formerror').style.display = 'block';
	lbl.style.color = 'red';
	return false;
	
}


function verifyaddress(obj, lbl) {
	var em = obj;
	
	if (em != "") 
	{
		return true;
		
	}
		//document.getElementById('formerror').style.display = 'block';
		lbl.style.color = 'red';
		return false;
}

function verifystate(obj, lbl)
{
	if (obj != "") 
	{
		return true;		
	}
		//document.getElementById('formerror').style.display = 'block';
	lbl.style.color = 'red';
	return false;
}


function verify_checkboxes()
{
	var lebo_checkbox = document.forms.myform.lebo;
	var blst_checkbox = document.forms.myform.blst;
	var bsst_checkbox = document.forms.myform.bsst;
	var sstm_checkbox = document.forms.myform.sstm;
	var csad_checkbox = document.forms.myform.csad;
	var ggdvd_checkbox = document.forms.myform.ggdvd;
	
	if (lebo_checkbox.checked) 
	{
		var x = document.forms.myform.lebo_amount.value ;
		if (!isInteger(x))
		{
			document.getElementById('gifts_lbl').style.display = 'block';
			return false;
		}
	}
	
	else if (blst_checkbox.checked) 
	{
		var y = document.forms.myform.blst_amount.value ;
		if (!isInteger(y))
		{
			document.getElementById('gifts_lbl').style.display = 'block';
		 	return false;
		}
	}
	
	else if (bsst_checkbox.checked) 
	{
		var z = document.forms.myform.bsst_amount.value ;
		if (!isInteger(z))
		{
			document.getElementById('gifts_lbl').style.display = 'block';
			return false;
		}
	}
	
	else if (sstm_checkbox.checked) 
	{
		var t = document.forms.myform.sstm_amount.value ;
		if (!isInteger(t))
		{
			document.getElementById('gifts_lbl').style.display = 'block';
			return false;
		}
	}
	
	else if (csad_checkbox.checked) 
	{
		var t = document.forms.myform.csad_amount.value ;
		if (!isInteger(t))
		{
			document.getElementById('gifts_lbl').style.display = 'block';
			return false;
		}
	}
	
	else if (ggdvd_checkbox.checked) 
	{
		var t = document.forms.myform.ggdvd_amount.value ;
		if (!isInteger(t))
		{
			document.getElementById('gifts_lbl').style.display = 'block';
			return false;
		}
	}
	
	else
	{
		document.getElementById('gifts_lbl2').style.display = 'block';
		return false;
	}
	
	
}

//trim functions
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function isInteger (s)

{
	var i;

	if (isEmpty(s))
		 if (isInteger.arguments.length == 1) return 0;
		 else return (isInteger.arguments[1] == true);

	 for (i = 0; i < s.length; i++)
	 {
		  var c = s.charAt(i);

		  if (!isDigit(c)) return false;
	 }

	 if (s > 0 )
	 return true;
	 else 
	 return false;
}

function isEmpty(s)
{
	return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
	return ((c >= "0") && (c <= "9"))
}





function submitForm(action) {
	document.myform.action = "actions.php?action=" + action;
	document.myform.submit() 
}



