$(document).ready(function() {
	$("form").submit(function(){
	 	var complete = true;
		//alert($(this).children(".required").attr("id"));
		$(this).find(".required").each( function() {
	        //alert($(this).attr("value"));
	        if($(this).attr("value") == ""){
				alert("Please fill out all required fields");
				$(this).focus();
				complete = false;
				return false;
			}
	    });
		$(this).find(".required:checkbox").each( function() {
	        //alert($(this).attr("value"));
	        if(!$(this).attr("checked")){
				alert("Please check the box to continue");
				$(this).focus();
				complete = false;
				return false;
			}
	    });
		
		if(!complete){
			return false;
		}
	})
		

});



function IsEmpty(textField,msg){
	var strValue=textField.value;
	for (var i = 0; i < strValue.length; i++)
	{
	var c = strValue.charAt(i);
	if ((c != ' ') && (c != '\n') && (c != '\t'))
	return false;
	}
	alert (msg);
	textField.focus();
	textField.select();
	return true;
}
function IsNotMaxLength(textField,maxlen,msg) {
  var strValue=textField.value;
  if (strValue.length < maxlen) {
    alert (msg);
    textField.focus();
	textField.select();
    return true;
  }
  return false;
}
function IsNotSelected(textField,msg) {
  if (textField.options[0].selected) {
    alert (msg);
    textField.focus();
    return true;
  }
  return false;
}
function IsNotNumber(textField,msg) {
  var digits = "0123456789";
  for (i=0; i<=textField.size; i++) {
     if (digits.indexOf(textField.value.charAt(i))== -1) {
       alert (msg);
       textField.focus();
       textField.select(); 
       return true;
     } 
  } 
  return false;
}
function IsNotValidAmount(textField,msg){
   var numValue=textField.value;
	if(isNaN(numValue))
	{
		alert (msg);
	    textField.focus();
	    textField.select(); 
	    return true;
	}
    return false;
}
function fixDecimal(DecimalNum) {
 strNum = "" + DecimalNum;
 if (strNum.indexOf('.') == -1)
  return strNum + '.00';
 seperation = strNum.length - strNum.indexOf('.');
 if (seperation > 3)
  return strNum.substring(0,strNum.length-seperation+3);
 else if (seperation == 2)
  return strNum + '0';
 return strNum;
}
function fixAmount()
{
if (!isNaN(document.frmCCP.pg_total_amount.value))
   document.frmCCP.pg_total_amount.value=fixDecimal(document.frmCCP.pg_total_amount.value)
}
//validate the fields in the form before submitting the data
function ValidatefrmCCP() {
if(IsEmpty(document.frmCCP.pg_total_amount,"Please enter the transaction amount"))
	return false;
if(IsNotValidAmount(document.frmCCP.pg_total_amount,"Invalid transaction amount"))
	return false;
if(IsEmpty(document.frmCCP.ecom_billto_postal_name_first,"Please enter your first name"))
	return false;
if(IsEmpty(document.frmCCP.ecom_billto_postal_name_last,"Please enter your last name"))
	return false;
if(IsEmpty(document.frmCCP.ecom_billto_postal_postalcode,"Please enter your zip code"))
	return false;
if(IsNotNumber(document.frmCCP.ecom_billto_postal_postalcode,"Please enter only digits for the zip code"))
	return false;
if(IsNotMaxLength(document.frmCCP.ecom_billto_postal_postalcode,5,"The zip code should be 5 digits long"))
	return false;
if(IsEmpty(document.frmCCP.ecom_payment_card_name,"Please enter the card holder name"))
	return false;
if(IsNotSelected(document.frmCCP.ecom_payment_card_type,"Please select the card type"))
	return false;
if(IsEmpty(document.frmCCP.ecom_payment_card_number,"Please enter the credit card number"))
	return false;
if(IsNotNumber(document.frmCCP.ecom_payment_card_number,"Please enter only digits for the credit card number"))
	return false;

if(IsNotSelected(document.frmCCP.ecom_payment_card_expdate_month,"Please select the month of the expiration date"))
	return false;
if(IsNotSelected(document.frmCCP.ecom_payment_card_expdate_year,"Please select the year of the expiration date"))
	return false;
	
//check to see if the expiration date is valid
todayDate=new Date()
dteYear=parseInt(document.frmCCP.ecom_payment_card_expdate_year.value,10)
dteMonth=parseInt(document.frmCCP.ecom_payment_card_expdate_month.value,10)-1
if((dteYear<todayDate.getFullYear())||((dteYear==todayDate.getFullYear())&&(dteMonth<todayDate.getMonth())))
{
	alert("Your credit card has already expired")
	return false;
}
return true;
}
