// JavaScript Document

function switchpayment(identifier)
	{
		if (identifier == 'check')
			{
				if(document.getElementById('checkpayment'))
					document.getElementById('checkpayment').style.display = "block";
				if(document.getElementById('creditpayment'))
					document.getElementById('creditpayment').style.display = "none";
				if(document.getElementById('check'))
				document.getElementById('check').checked = true;
			}
		else
			{
				if(document.getElementById('creditpayment'))
					document.getElementById('creditpayment').style.display = "block";
				if(document.getElementById('checkpayment'))
					document.getElementById('checkpayment').style.display = "none";
			}
		
	}



function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=225,height=300,screenX=150,screenY=150,top=150,left=150')
}

//------------------------------------------
//Date Validation
function dob(dobby)
	{
		//get rid of the slashes	
		var newstr = dobby.replace(/\//g, "");
		//then make sure that the new string is numeric
		if (!IsNumeric(newstr))
			{
				return false;
			}
		//now that we know it is numeric
		//run checks against the postion of the '/"
		if (dobby.substring(3,2) != "/")
			{
				return false;
			}
		else if(dobby.substring(6,5) != "/")
			{
				return false;
			}
		else if(dobby.length != 10)
			{
				return false;
			}
		else
			{
				//validated
				return true;
			}
	}
	
	
//------------------------------------------
//Citation Amount Total
function citationamounttotal(amount)
	{
		//get rid of the slashes
		var splitter = amount.split(".");
		var newstr = amount.replace(/\,/g, "");
		var newstrtotal = newstr;
		
		// BUG #3159 - Paul Cleaveland 11/12/2009
		// DB-retreived fields to calculate the Online Fee
		var feePercentage = document.getElementById('feePercentage').value;
		var feeDollarAmount = parseFloat(document.getElementById('feeDollarAmount').value);
		var newstr = newstr * (feePercentage / 100);
		var chargeamount = feeDollarAmount;
		
		if(amount == "" || !IsNumeric(newstrtotal))
		{
			if(document.getElementById('field19'))
				document.getElementById('field19').value = "";
			document.getElementById('field20').value = "";
			return;
		}
		
		if (newstr > chargeamount.toFixed(2))
			{
				document.getElementById('field18').value = parseFloat(newstrtotal).toFixed(2);
				if(document.getElementById('field19'))
					document.getElementById('field19').value = newstr.toFixed(2);
				var finaltotal = parseFloat(newstr.toFixed(2)) + parseFloat(newstrtotal);
				document.getElementById('field20').value = finaltotal.toFixed(2);
			}
		else
			{
				document.getElementById('field18').value = parseFloat(newstrtotal).toFixed(2);
				if(document.getElementById('field19'))
					document.getElementById('field19').value = chargeamount.toFixed(2);
				var finaltotal = parseFloat(chargeamount.toFixed(2)) + parseFloat(newstrtotal);
				document.getElementById('field20').value = finaltotal.toFixed(2);
			}
		
		
	}
//------------------------------------------	

//------------------------------------------
//Citation Amount Validation
function citationamount(amount)
	{
		
		//get rid of the slashes
		var splitter = amount.split(".");
		var newstr = amount.replace(/\./g, "");
		var newstr = newstr.replace(/\,/g, "");
		//then make sure that the new string is numeric
		if (!IsNumeric(newstr) || parseFloat(newstr) == 0)
			{
				return false;
			}
		else if(splitter.length > 2)
			{
				return false;
			}
		else if(!splitter[1] || splitter[1].length > 2)
			{
				return false;
			}
		else
			{
				return true;
			}
	}
//------------------------------------------


//------------------------------------------
//Numeric Validation
function IsNumeric(sText)
	{
	   var ValidChars = "0123456789.";
	   var IsNumber=true;
	   var Char;
	
	 
	   for (i = 0; i < sText.length && IsNumber == true; i++) 
		  { 
		  Char = sText.charAt(i); 
		  if (ValidChars.indexOf(Char) == -1) 
			 {
			 IsNumber = false;
			 }
		  }
	   return IsNumber;
   
    }
//-------------------------------------------


//-------------------------------------------
function noDashes(e)
{
	var charCode = (e.which) ? e.which : event.keyCode
	
	if(charCode == 45)
		return false;
	
	return true;
}
//-------------------------------------------

//------------------------------------------
//Email validation
function checkEmail(myForm)
	{
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm))
			{
				return (true)
			}
		else
			{
				return (false)
			}
	}
//-------------------------------------------		


//------------------------------------------
//Phone validation
function checkPhone(phone_number)
	{
	  // Make sure there are ten digits in the phone number
		if (/(\D*\d\D*){10,}/.test(phone_number))
			{
				return (true)
			}
		else
			{
				return (false)
			}
	}
//-------------------------------------------		


//-------------------------------------------
//Credit Card Number Validation
function isCreditCard( CC )
{
	if (CC.length > 19)
		return (false);

	sum = 0; mul = 1; l = CC.length;
	for (i = 0; i < l; i++)
	{
		digit = CC.substring(l-i-1,l-i);
		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)
	{
		return (true);
	}
	else
	{
		return (false);
	}
}

function isVisa( cc )
{
	if( (cc.substring(0,1) == 4) && (cc.length == 16)
	|| (cc.length == 13) )
	{
		return isCreditCard( cc );
	}
	
	return (false);
}

function isDiscover( cc )
{
	if( (cc.substring(0,4) == 6011) && (cc.length == 16))
	{
		return isCreditCard( cc );
	}

	return (false);
}

function isMC( cc )
{
	if( (cc.length == 16) && (cc.substring(0,2) == 51)
	|| (cc.substring(0,2) == 52) || (cc.substring(0,2) == 53)
	|| (cc.substring(0,2) == 54) || (cc.substring(0,2) == 55) )
	{
		return isCreditCard( cc );
	}
	
	
	return (false);
}

function isAMEX(cc)
{
	var firstDigits = cc.substring(0, 2);
	if(cc.length == 15 && (firstDigits == 34 || firstDigits == 37)) {
		return isCreditCard(cc);
	}
	
	return false;
}
//-------------------------------------------

//-------------------------------------------
//Routing Number Validation
function checkRouting(s)
	{
		var i, n, t;

  		// First, remove any non-numeric characters.

		  t = "";
		  for (i = 0; i < s.length; i++) {
			c = parseInt(s.charAt(i), 10);
			if (c >= 0 && c <= 9)
			  t = t + c;
		  }
		
		  // Check the length, it should be nine digits.
		
		  if (t.length != 9)
			return false;

		 // Run through each digit and calculate the total.
		  n = 0;
		  for (i = 0; i < t.length; i += 3) 
		  	{
				n += parseInt(t.charAt(i),     10) * 3
				  +  parseInt(t.charAt(i + 1), 10) * 7
				  +  parseInt(t.charAt(i + 2), 10);
		  	}
		
		  // If the resulting sum is an even multiple of ten (but not zero),
		  // the aba routing number is good.
	
		  if (n != 0 && n % 10 == 0)
		  	{
				return true;
			}
		  else
		  	{
				return false;
			}
	}
//-------------------------------------------

//-------------------------------------------
//Main Form Validation
function validateform(numOfCustomFields)
	{
		//grab the date for the cc exp validation
		var now = new Date();
		var year = now.getFullYear();
		var year = year.toString();
		var month = now.getMonth()+1;
		var month = month.toString();
	
		if (month.length == 1)
			{
				var month = "0" + month;
			}
		
		//---------------------------------------------------------------
		//Validate Information 
		//Make an array to know where to set the focus
			var inputfields = new Array();
			//initiate focus counter
			var x = 0;
			//alert error message
			//intialize error message
			var errorstring = "" 
			//begin for loop for inpute fields
			//validate zip
			var checkzip = IsNumeric(document.getElementById('field5').value);
			//check email validation
			if (document.getElementById('field6').value != "")
				{
					var emailer = checkEmail(document.getElementById('field6').value);
				}
			//check phone validation
			if (document.getElementById('field21').value != "")
				{
					var checkphone = checkPhone(document.getElementById('field21').value);
				}
			
			if(document.getElementById('checkpayment'))
			{
				//validate routing number (numeric)
				var checkroutingnumeric = IsNumeric(document.getElementById('field8').value);
				//validate routing number
				var checkroutenum = checkRouting(document.getElementById('field8').value);
				//validate account number (numeric)
				var checkaccountnumeric = IsNumeric(document.getElementById('field10').value);
				//validate check number (numeric)
				var checknumnumeric = IsNumeric(document.getElementById('field12').value);
			}
			
			//validate cc number between Visa and MC - VISA
			if(document.getElementById('creditpayment'))
			{
				if (document.getElementById('cardtype').value == "Visa")
					{
						//validate cc number between Visa and MC - VISA
						var isvisa = isVisa(document.getElementById('field13').value);
						var invalid = "Visa";
					}
				else if (document.getElementById('cardtype').value == "Discover")
					{
						var isdiscover = isDiscover(document.getElementById('field13').value);
						var invalid = "Discover";
					}
				else if(document.getElementById('cardtype').value == 'AMEX')
					{
						var isamex = isAMEX(document.getElementById('field13').value);
						var invalid = "American Express";
					}
				else
					{
						//validate cc number between Visa and MC - MC
						var ismc = isMC(document.getElementById('field13').value);
						var invalid = "MasterCard";
					}
			}
			
			if(document.getElementById('field17'))
				var checkdob = dob(document.getElementById('field17').value);		
			var checkcitation = citationamount(document.getElementById('field18').value);
			
			for(i=1; i <=23; i++)
				{
				  // if the current field cannot be found, skip it
				  if( !document.getElementById('field'+i) )
						{
							continue;
						}
				
					//field 3 is not required
					if (i == 3 || i == 12 || i == 19 || i == 20)
						{
							continue;
						}
						
						
					if (document.getElementById('check') && document.getElementById('check').checked)
		 				{
							//if we are on check, skip credit card validation
							if (i == 13 || i==14){
								continue;
							}
						}
					else
						{
							//if we are on credit, skip check validation
							if (i == 8 || i==9 || i==10 || i==11 || i==12 || i==14){
								continue;
							}
							
						}
					
						
					// BUG #3833 - Paul Cleaveland
					// Ignore field12 (check number) as it is now optional
					//check for blank
					if (
                                                // If the user left a required field blank, make a note of it
                                                // and don't let them submit the form until they correct the problem
                                                document.getElementById('field'+i).value == "" &&
                                                document.getElementById('field'+i).className.indexOf('required') != -1
                                        )
						{
							//set first instance to the focus array
							inputfields[x]= i;
							//default color if not complete
							document.getElementById('field'+i).style.backgroundColor = '#fef1d6'; 
							//increment focus array
							x++;
						}
					else
						{
							//set color back to white if corrected
							document.getElementById('field'+i).style.backgroundColor = '#FFF'; 
						}
						
					//check zip validation 
				 	if (i == 5){
					if (!checkzip && document.getElementById('field5').className.indexOf('required') != -1)
						{
							//if a return is not made from IsNumeric
							errorstring += "--Zip Code must be Numeric. Please Re-Enter.--"+ "\r\n";
							document.getElementById('field'+i).value = "";
							document.getElementById('field5').focus();
							document.getElementById('field'+i).style.backgroundColor = '#fef1d6'; 
						}
					}//end if i == 5
					
					//---------------------------------------------------------------
					//Check Emails
					if (i == 6 && document.getElementById('field6').className.indexOf('required') != -1){
					if(document.getElementById('field6').value != "" && !emailer )
						{
							errorstring += "--Invalid E-mail Address! Please re-enter.--" + "\r\n";
							document.getElementById('field6').focus();
							document.getElementById('field6').style.backgroundColor = '#fef1d6'; 
						}
					}//end if i == 6
					
					if (i == 7){
					if (document.getElementById('field6').value != document.getElementById('field7').value && document.getElementById('field7').className.indexOf('required') != -1)
						{
							 errorstring += '--Email Addresses must match.--' + "\r\n";
							 document.getElementById('field6').focus();
							 document.getElementById('field6').style.backgroundColor = '#fef1d6'; 
							 document.getElementById('field7').style.backgroundColor = '#fef1d6'; 
						}
					}//end if i == 7
					//---------------------------------------------------------------
						
					//check phone validation 
				 	if (i == 21){
					if (!checkphone && document.getElementById('field21').className.indexOf('required') != -1)
						{
							errorstring += "--Complete ten-digit Phone Number must be entered.--"+ "\r\n";
							document.getElementById('field'+i).value = "";
							document.getElementById('field'+i).focus();
							document.getElementById('field'+i).style.backgroundColor = '#fef1d6'; 
						}
					}//end if i == 21
					


					//check phone validation 
				 	if (i == 23){
					if (document.getElementById('field23') != null && document.getElementById('field23').value == '' && document.getElementById('field23').className.indexOf('required') != -1)
						{
							errorstring += "--Charges / Offense Field must be completed.--"+ "\r\n";
							document.getElementById('field'+i).value = "";
							document.getElementById('field'+i).focus();
							document.getElementById('field'+i).style.backgroundColor = '#fef1d6'; 
						}
					}//end if i == 21
					
					
					//---------------------------------------------------------------
					//Validate Payment Options
					if (document.getElementById('check') && document.getElementById('check').checked)
		 				{

							if (i == 8){
							if (!checkroutingnumeric)
								{
									//if a return is not made from IsNumeric
									errorstring += '--Rouing Number must be Numeric. Please Re-Enter.--' + "\r\n";
									document.getElementById('field8').value ="";
									document.getElementById('field8').focus();
									document.getElementById('field8').style.backgroundColor = '#fef1d6'; 
								}
							}//end if == 8
							
							//validate routing number
							if (i == 8){
							if (!checkroutenum)
								{
									//if a return is not made from checkRouting
									errorstring += '--Invalid Routing Number. Please Re-Enter.--' + "\r\n";
									document.getElementById('field8').focus();
									document.getElementById('field8').style.backgroundColor = '#fef1d6'; 
								}
							}//end if == 8
							
							
							//check routing confirm 
							if (i == 9){
							if (document.getElementById('field8').value != document.getElementById('field9').value)
								{
									//if the routing numbers do not match 
									errorstring += '--Routing Numbers must match. Please Re-Enter.--' + "\r\n";
									document.getElementById('field8').focus();
									document.getElementById('field8').style.backgroundColor = '#fef1d6'; 
									document.getElementById('field9').style.backgroundColor = '#fef1d6'; 
								}
							}//end if == 9
							
							//validate account number (numeric)
							if (i == 10){
							if (!checkaccountnumeric)
								{
									//if a return is not made from IsNumeric
									errorstring += '--Account Number must be Numeric. Please Re-Enter.--' + "\r\n";
									document.getElementById('field10').value ="";
									document.getElementById('field10').focus();
									document.getElementById('field10').style.backgroundColor = '#fef1d6'; 
								}
							}//end if == 10
							
							
							//check account confirm 
							if (i == 11){
							if (document.getElementById('field10').value != document.getElementById('field11').value)
								{
									//if the account numbers do not match 
									errorstring += '--Account Numbers must match. Please Re-Enter.--' + "\r\n";
									document.getElementById('field10').focus();
									document.getElementById('field10').style.backgroundColor = '#fef1d6'; 
									document.getElementById('field11').style.backgroundColor = '#fef1d6'; 
								}
							}//end if == 11
						}//end if check
					else
						{
							//credit
							//validate cc number between Visa and MC - VISA
							if (i == 13){
							if(!(isvisa || isdiscover || ismc || isamex))
								{
									errorstring += '--Invalid '+invalid+' Card Number. Please Re-Enter.--' + "\r\n";
									document.getElementById('field13').focus();
									document.getElementById('field13').style.backgroundColor = '#fef1d6'; 
								}
							
							
							//check exp date on cc - YEAR
							if (document.getElementById('selectyear').value < year)
								{
									errorstring += '--Credit Card Year is Invalid.--' + "\r\n";
								}
							//check exp date on cc - MONTH	
							if (document.getElementById('selectmonth').value < month && 
													document.getElementById('selectyear').value == year)
								{
									//alert(document.getElementById('selectmonth').value);
									errorstring += '--Credit Card Month is Invalid.--' + "\r\n";
								}
							}//end if == 13}//end if == 13		
							
						}//end credit
					//---------------------------------------------------------------
					
					
					//---------------------------------------------------------------
					//Validate Citation 
						//check to see if License State has been selected
						if (i == 16 && document.getElementById('field16')){
						
						if (document.getElementById('lic_select').value == 'selectstate')
							{
								errorstring += '--Please Select License State.--' + "\r\n";
							}
							
						if(document.getElementById('field16').value == '')
							{
                                var fieldText = document.getElementById('licenseName').innerHTML;
                                fieldText = fieldText.substr(0, fieldText.length-2);
                                errorstring += '--Please Enter ' + fieldText + '.--' + "\r\n";				   
							}
						
						}//end if i == 16
						
						if (i == 17 && document.getElementById('field17')){
						if(!checkdob)
							{
								errorstring += '--Date of Birth must follow mm/dd/yyyy format.--' + "\r\n";
								document.getElementById('field17').focus();
								document.getElementById('field17').style.backgroundColor = '#fef1d6'; 
							}
						}//end if i == 17
						
						if (i == 18){
						if(!checkcitation)
							{
								errorstring += '--Citation Amount is not valid.--' + "\r\n";
								document.getElementById('field18').focus();
								document.getElementById('field18').style.backgroundColor = '#fef1d6'; 
							}
						}//end if i == 18
					//---------------------------------------------------------------
					
				}
				
			//check the customfields if there are any
			if(numOfCustomFields > 0)
			{
				var i = 0;
				for(i=0; i < numOfCustomFields; i++)
				{
					if(document.getElementById('customField'+i).className == 'required' && document.getElementById('customField'+i).value == '')
					{
						var label = document.getElementById('customFieldLabel'+i).innerHTML;
						label = label.substring(0, label.length - 2); //removes the colon from the label
						errorstring += '--'+label+' is required--' + "\r\n";
						document.getElementById('customField'+i).focus();
						document.getElementById('customField'+i).style.backgroundColor = '#fef1d6'; 	
					}
				}
				
			}//end if
				
			if (inputfields[0])
				{
					errorstring += '--Required User Information was not entered.--' + "\r\n";
					//set our focus to first instance 
					document.getElementById('field'+inputfields[0]).focus();
				}
			if (errorstring != "")
				{
					alert(errorstring);
					return false;
				}
			else
				{
					document.citationpay.submit();
				}
			
		//---------------------------------------------------------------
	}

