////////////////////////////////////////////////////////////////////////////
//	FUNCTION FOR VALIDATE FIELDS										 //
//																		//
/////////////////////////////////////////////////////////////////////////
	
function validate_Field(field,type,acceptEmpty)
	{
		if (typeof acceptEmpty == 'undefined') 
		{
        	acceptEmpty = 0;
    	}

		var theValue = document.getElementById(field).value;
		switch (type)
		{
			case 'text':
			if(acceptEmpty == 0)
			{
				if(theValue == "" || theValue.replace(/^\s+|\s+#/g, '') == '')
					return false;	
			}
			else
			{
				return true;	
			}
			
			break;
			
			case 'float':
			if(acceptEmpty == 0)
			{
			var reg = /^([+-]?(((\d+(\.)?)|(\d*\.\d+))([eE][+-]?\d+)?))$/;
			   if(reg.test(theValue) == false || theValue == "" || theValue == null || theValue.replace(/^\s+|\s+#/g, '') == '') 
				  return false;
			}
			else
			{
				if(theValue !="")
				{
					var reg = /^([+-]?(((\d+(\.)?)|(\d*\.\d+))([eE][+-]?\d+)?))$/;
			   		if(reg.test(theValue) == false || theValue == "" || theValue == null || theValue.replace(/^\s+|\s+#/g, '') == '') 
				  		return false;	
				}
				else
				{
				 	return true;
				}
			}
			break;
			
			case 'number':
			if(acceptEmpty == 0)
			{
				var intCharCode;
				for (var i = 0; i < theValue.length; i++) 
				{
					intCharCode = theValue.charCodeAt(i);        
					if (!((intCharCode >= 48 && intCharCode <= 57))) 
						return false; 
				}
				if(theValue == "" || theValue == null || theValue.replace(/^\s+|\s+#/g, '') == '')
						return false;
			}
			else
			{
				if(theValue !="")
				{
					var intCharCode;
					for (var i = 0; i < theValue.length; i++) 
					{
						intCharCode = theValue.charCodeAt(i);        
						if (!((intCharCode >= 48 && intCharCode <= 57))) 
							return false; 
					}
					if(theValue == "" || theValue == null || theValue.replace(/^\s+|\s+#/g, '') == '')
							return false;
				}
				else
				{
					return true;
				}	
			}
			break;
			
			case 'email':
			if(acceptEmpty == 0)
			{
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				   if(reg.test(theValue) == false || theValue == "" || theValue == null || theValue.replace(/^\s+|\s+#/g, '') == '') 
					  return false;
			}
			else
			{
				if(theValue !="")
				{
					var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				   	if(reg.test(theValue) == false || theValue == "" || theValue == null || theValue.replace(/^\s+|\s+#/g, '') == '') 
					  return false;
				}
				else
				{
					return true;
				}
			}
			break;
			
			case 'ip':
			
			if(acceptEmpty == 0)
			{
				theValue = theValue.replace( /\s/g, "") //remove spaces for checking
				var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; //regex. check for digits and in
													  //all 4 quadrants of the IP
					if (re.test(theValue)) {
						//split into units with dots "."
						var parts = theValue.split(".");
						//if the first unit/quadrant of the IP is zero
						if (parseInt(parseFloat(parts[0])) == 0) {
							return false;
						}
						//if any part is greater than 255
						for (var i=0; i<parts.length; i++) {
							if (parseInt(parseFloat(parts[i])) > 255){
								return false;
							}
						}
						return true;
					} else {
						return false;
					}
			}
			else
			{
				if(theValue !="")
				{
					theValue = theValue.replace( /\s/g, "") //remove spaces for checking
					var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; //regex. check for digits and in
													  //all 4 quadrants of the IP
					if (re.test(theValue)) {
						//split into units with dots "."
						var parts = theValue.split(".");
						//if the first unit/quadrant of the IP is zero
						if (parseInt(parseFloat(parts[0])) == 0) {
							return false;
						}
						//if any part is greater than 255
						for (var i=0; i<parts.length; i++) {
							if (parseInt(parseFloat(parts[i])) > 255){
								return false;
							}
						}
						return true;
					} else {
						return false;
					}
				}
				else
				{
					return true;
				}
			}
			break;
			
			case 'date':
				if(acceptEmpty == 0)
				{
					var validformat=/^\d{1,2}\/\d{1,2}\/\d{4}$/; //Basic check for format validity
					var returnval=false;
					if (!validformat.test(theValue.value))
						returnval=false;
					else
					{ 
						//Detailed check for valid date ranges
						var dayfield=theValue.value.split("/")[0]
						var monthfield=theValue.value.split("/")[1]
						var yearfield=theValue.value.split("/")[2]
						
						var dayobj = new Date(yearfield, monthfield-1, dayfield)
						if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
							returnval=false;
						else
						{
							returnval=true;
						}
					}
					return returnval;
				}
				else
				{
					if(theValue !="")
					{
						var validformat=/^\d{1,2}\/\d{1,2}\/\d{4}$/; //Basic check for format validity

						if (!validformat.test(theValue))
							returnval=false;
						else
						{ 
							//Detailed check for valid date ranges
							var dayfield=theValue.split("/")[0];
							var monthfield=theValue.split("/")[1];
							var yearfield=theValue.split("/")[2];
							
							var dayobj = new Date(yearfield,monthfield,dayfield);
							var today = new Date();
							
							if (dayobj<today)
								returnval=false;
							else
								returnval=true;
						}
						return returnval;
					}
					else
					{
						return true;
					}
				}

			break;
			
			case 'cardBin':
			if(acceptEmpty == 0)
			{
				var intCharCode;
				for (var i = 0; i < theValue.length; i++) 
				{
					intCharCode = theValue.charCodeAt(i);        
					if (!((intCharCode >= 48 && intCharCode <= 57) || theValue == "N/A")) 
						return false; 
				}
				if(theValue != "N/A" && theValue.length != 6)
						return false;
				if(theValue == "" || theValue == null || theValue.replace(/^\s+|\s+#/g, '') == '')
						return false;
			}
			else
			{
				if(theValue !="")
				{
					var intCharCode;
					for (var i = 0; i < theValue.length; i++) 
					{
						intCharCode = theValue.charCodeAt(i);        
						if (!((intCharCode >= 48 && intCharCode <= 57) || theValue == "N/A")) 
							return false; 
					}
					if(theValue != "N/A" && theValue.length != 6)
						return false;
					if(theValue == "" || theValue == null || theValue.replace(/^\s+|\s+#/g, '') == '')
							return false;
				}
				else
				{
					return true;
				}	
			}
					
			break;
			
			case 'cardNo':
			if(acceptEmpty == 0)
			{
				var intCharCode;
				for (var i = 0; i < theValue.length; i++) 
				{
					intCharCode = theValue.charCodeAt(i);        
					if (!((intCharCode >= 48 && intCharCode <= 57) || theValue == "N/A")) 
						return false; 
				}
				if(theValue != "N/A" && theValue.length != 4)
						return false;
				if(theValue == "" || theValue == null || theValue.replace(/^\s+|\s+#/g, '') == '')
						return false;
			}
			else
			{
				if(theValue !="")
				{
					var intCharCode;
					for (var i = 0; i < theValue.length; i++) 
					{
						intCharCode = theValue.charCodeAt(i);        
						if (!((intCharCode >= 48 && intCharCode <= 57) || theValue == "N/A")) 
							return false; 
					}
					if(theValue != "N/A" && theValue.length != 4)
						return false;
					if(theValue == "" || theValue == null || theValue.replace(/^\s+|\s+#/g, '') == '')
							return false;
				}
				else
				{
					return true;
				}	
			}
					
			break;
		}
	}
	
function submit_Agreement()
	{
		var my_browser = navigator.appName;
		var somethingWrong = false;
		var myWrongs = new Array();
		var myTypes = new Array();
		var arrValues = new Array();
		var name= document.getElementById("inp0").value;
		var amount= document.getElementById("inp1").value;
		
		myTypes[0] = "text";
		myTypes[1] = "float";

		if(document.getElementById("inp_Refund").checked)
						var agree = 1;
					else
					{
						var agree = 0;
						somethingWrong = true;
						myWrongs[2] = 1;
					}
		
		for(i=0;i<=1;i++)
		{
			var validateResult = validate_Field("inp"+i,myTypes[i],0);
			if(validateResult == false)
			{
				somethingWrong = true;
				myWrongs[i] = 1;
			}
		}

		
		if(somethingWrong == true)
		{	
			for(i=0;i<=2;i++)
			{
				if(myWrongs[i] != "" && myWrongs[i] != null)
					document.getElementById("spn"+i).style.display="";
				else
					document.getElementById("spn"+i).style.display="none";
			}
			alert("Error: Some fields contain invalid data or some required fields are empty!");
		}
		else
		{
			if(my_browser == "Microsoft Internet Explorer")
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			else
			{
				xmlhttp = new XMLHttpRequest();
			}	
			xmlhttp.onreadystatechange=function()
			  {
			  if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
					var responseString = xmlhttp.responseText.replace(/^\s+|\s+$/, "");
					alert(responseString);
					if(responseString == "OK")
					{
						document.getElementById("showWarning").style.display="";
						setTimeout("redirectPage()",2000);
					}
				}
			  }
		
			xmlhttp.open("GET","ajax_Request.php?action=agreement&name="+name+"&amount="+amount,true);
			xmlhttp.send();
		}
			
	}
	
function reset_Agreement()
	{
		document.getElementById("inp_Refund").checked = false;
		for(i=0;i<=1;i++)
		{
			document.getElementById("inp"+i).value = "";
		}
	}

function submit_Contact()
	{
		var my_browser = navigator.appName;
		var somethingWrong = false;
		var myWrongs = new Array();
		var myTypes = new Array();
		var arrValues = new Array();
		
		//get the values
		var fname= document.getElementById("inp0").value;
		var lname= document.getElementById("inp1").value;
		var address= document.getElementById("inp2").value;
		var zip= document.getElementById("inp3").value;
		var city= document.getElementById("inp4").value;
		var country= document.getElementById("inp5").value;
		var phone= document.getElementById("inp6").value;
		var email= document.getElementById("inp7").value;
		var comments= document.getElementById("inp8").value;
		
		myTypes[0] = "text";
		myTypes[1] = "text";
		myTypes[2] = "text";
		myTypes[3] = "text";
		myTypes[4] = "text";
		myTypes[5] = "text";
		myTypes[6] = "text";
		myTypes[7] = "email";
		myTypes[8] = "text";
		
		for(i=0;i<=8;i++)
		{
			if(i == 8)
				var validateResult = validate_Field("inp"+i,myTypes[i],1);
			else
				var validateResult = validate_Field("inp"+i,myTypes[i],0);
			if(validateResult == false)
			{
				somethingWrong = true;
				myWrongs[i] = 1;
			}
		}

		
		if(somethingWrong == true)
		{	
			for(i=0;i<=8;i++)
			{
				if(myWrongs[i] != "" && myWrongs[i] != null)
					document.getElementById("spn"+i).style.display="";
				else
					document.getElementById("spn"+i).style.display="none";
			}
			alert("Error: Some fields contain invalid data or some required fields are empty!");
		}
		else
		{
			if(my_browser == "Microsoft Internet Explorer")
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			else
			{
				xmlhttp = new XMLHttpRequest();
			}	
			xmlhttp.onreadystatechange=function()
			  {
			  if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
					var responseString = xmlhttp.responseText.replace(/^\s+|\s+$/, "");
					alert(responseString);
					if(responseString == "OK")
					{
						document.getElementById("showWarning").style.display="";
						setTimeout("redirectPage()",2000);
					}
				}
			  }
		
			xmlhttp.open("GET","ajax_Request.php?action=contact&name=&"+fname+"&lname="+lname+"&address="+address+"&zip="+zip+"&country="+country+"&phone="+phone+"&email="+email+"&comments="+comments+"amount="+amount,true);
			xmlhttp.send();
		}
			
	}
	
function reset_Contact()
	{
		for(i=0;i<=9;i++)
		{
			document.getElementById("inp"+i).value = "";
		}
	}
		
function redirectPage()	
{
	document.location.href = "index.php";	
}

