var errors = "";
var formname = "";


function validate_required(field) {

	with (field) {
		if (value==null||value=="") {			
			style.backgroundColor = "#eef6ff";			
			return false;
		} else {
			style.backgroundColor = "#fff";	
			return true;
		}
	}
}

function validate_required_length(field, length) {

	with (field) {
		if (value==null||value=="" || value.length > length) {			
			style.backgroundColor = "#eef6ff";			
			return false;
		} else {
			style.backgroundColor = "#fff";	
			return true;
		}
	}
}


function validate_select_required(field) {
	
	if (field.selectedIndex == 0) {
		field.style.backgroundColor = "#eef6ff";
		return false;
	} else {
		field.style.backgroundColor = "#fff";
		return true;
	}
}

function validate_email_required(field) {

	with (field) {
		apos = value.indexOf("@");
		dotpos = value.lastIndexOf(".");
		
		if (apos < 1 || dotpos-apos < 2) {
			style.backgroundColor = "#eef6ff";	
			return false;

		} else {
			style.backgroundColor = "#fff";	
			return true;
		}
	}
}

function validate_numeric_required(field){
		
	str = field.value;
		
	if(str==null || str=="") {		
		field.style.backgroundColor = "#eef6ff";	
		return false;
	}
	
	numdecs = 0;
	for (i = 0; i < str.length; i++) {
			mychar = str.charAt(i);
			if ((mychar >= "0" && mychar <= "9") || mychar == ".") {
				if (mychar == ".")
					numdecs++;
			} else {
				field.style.backgroundColor = "#eef6ff";	
				return false;
			}
	}
	
	if (numdecs > 1) {
		field.style.backgroundColor = "#eef6ff";	
		return false;
	}
	
	field.style.backgroundColor = "#fff";	
	return true;
}

function validate_numeric(field){
			
	str = field.value;			

	if(str==null || str=="") {		
		field.style.backgroundColor = "#fff";	
		return true;
	}
	
	numdecs = 0;
	for (i = 0; i < str.length; i++) {
			mychar = str.charAt(i);
			if ((mychar >= "0" && mychar <= "9") || mychar == "." || mychar == "-") {
				if (mychar == ".")
					numdecs++;
			} else {
				field.style.backgroundColor = "#eef6ff";	
				return false;
			}
	}
	
	if (numdecs > 1) {
		field.style.backgroundColor = "#eef6ff";	
		return false;
	}
	
	field.style.backgroundColor = "#fff";	
	return true;
}

function validate_creditcard(field)
{
	strInput = field.value;
	
	// Check if value is empty
	if (strInput.length == 0) {
		field.style.backgroundColor = "#eef6ff";
		return (false);
	}
	
	// Encoding only works on cards with less than 19 digits		
	if (strInput.length > 19) {
		field.style.backgroundColor = "#eef6ff";
		return (false);  
	}
	
	var sum = 0; 
	var mul = 1; 
	var l = strInput.length;
	
	for (i = 0; i < l; i++)
	{
		var digit    = strInput.substring(l-i-1,l-i);
    		var tproduct = parseInt(digit ,10) * mul;

	    	if (tproduct >= 10) 
		{ sum += (tproduct % 10) + 1; }
    		else
		{ sum += tproduct; }

    		if (mul == 1)       
		{ mul++; }
	    	else
		{ mul--; }
  	}	

  	if ((sum % 10) == 0) {
		field.style.backgroundColor = "#fff";
		return (true);
	
	} else {
		field.style.backgroundColor = "#eef6ff";
		return (false);
	}
}



function validate_form(form_name, id) {

	
	errors = "";
	if (id == null) id=0;
	
	formname = form_name;
	
	var elem = document.getElementById(formname).elements;
	
	for(var i = 0; i < elem.length; i++) {
		if (elem[i].className == "required") {
			if (validate_required(elem[i])==false) {				
				errors =  "There is a problem with information you have entered.<br><br>Please check the highlighted fields and try again.";
			}	
		
		} else if (elem[i].className == "required_12") {
			if (validate_required_length(elem[i], 12)==false) {				
				errors =  "There is a problem with information you have entered.<br><br>Please check the highlighted fields and try again.";
			}
		
		} else if (elem[i].className == "required_50") {
			if (validate_required_length(elem[i], 50)==false) {				
				errors =  "There is a problem with information you have entered.<br><br>Please check the highlighted fields and try again.";
			}
		
		} else if (elem[i].className == "required_100") {
			if (validate_required_length(elem[i], 100)==false) {				
				errors =  "There is a problem with information you have entered.<br><br>Please check the highlighted fields and try again.";
			}
		
		} else if (elem[i].className == "required_255") {
			if (validate_required_length(elem[i], 255)==false) {				
				errors =  "There is a problem with information you have entered.<br><br>Please check the highlighted fields and try again.";
			}
		
		} else if (elem[i].className == "required_numeric") {
			if (validate_numeric_required(elem[i])==false) {				
				errors =  "There is a problem with information you have entered.<br><br>Please check the highlighted fields and try again.";
			}	
		

		} else if (elem[i].className == "required_email") {			
			if (validate_email_required(elem[i])==false) {				
				errors =  "There is a problem with information you have entered.<br><br>Please check the highlighted fields and try again.";
			}	
		
		} else if (elem[i].className == "required_select") {			
			if (validate_select_required(elem[i])==false) {				
				errors =  "There is a problem with information you have entered.<br><br>Please check the highlighted fields and try again.";
			}	
		
		} else if (elem[i].className == "required_card") {			
			if (validate_creditcard(elem[i])==false) {				
				errors =  "There is a problem with information you have entered.<br><br>Please check the highlighted fields and try again.";
			}					
		
		
		} else if (elem[i].className == "numeric") {			
			if (validate_numeric(elem[i])==false) {				
				errors =  "There is a problem with information you have entered.<br><br>Please check the highlighted fields and try again.";
			}					
		}
		
	}
	
	
	if (errors != "") {
	
		document.getElementById("error_message").innerHTML = errors;	
		document.getElementById("error_message").style.display = "block";	

		return false;
	}
	
	document.getElementById(formname).submit ();	

}