function trim(sString)
{
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

function validateCheckout(country)
{
    
    if($("selPaymentType").value == "Credit_Card")
    {
        if(validateBilling(country) && validateCreditCard())
        {
            $("spanErrorMessage").innerHTML = "Your order is being processed. Please wait.";
            $("Place Your Order").disabled = true;
            $("Place Your Order").src = "../../menu_buttons/place_your_order_hover.jpg";
            document.body.style.cursor = 'wait';
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        if (validateBilling(country))
        {
            $("spanErrorMessage").innerHTML = "Your order is being processed. Please wait.";
            $("Place Your Order").disabled = true;
            $("Place Your Order").src = "../../menu_buttons/place_your_order_hover.jpg";
            document.body.style.cursor = 'wait';
            return true;
        }
        else
        {
            return false;
        }
    }
}

function validateBilling(country)
{    
    $("txtCardName").value = trim($("txtCardName").value);
    $("txtCCNumber").value = trim($("txtCCNumber").value);
    $("txtCCSecurityCode").value = trim($("txtCCSecurityCode").value);
    $("txtBillingAddress1").value = trim($("txtBillingAddress1").value);
    $("txtBillingAddress2").value = trim($("txtBillingAddress2").value);
    $("txtBillingCity").value = trim($("txtBillingCity").value);
    $("txtBillingState").value = trim($("txtBillingState").value);
    $("txtBillingCode").value = trim($("txtBillingCode").value);
    $("txtBillingCountry").value = trim($("txtBillingCountry").value);
	
		

	var right_now=new Date();
	var the_year=right_now.getYear();
	if (navigator.appName != "Microsoft Internet Explorer") 
	{
        the_year=the_year + 1900;
    }
    var the_month=right_now.getMonth() + 1;
    
    var expire_month = $("selExpireMonth").value;
    var expire_year = $("selExpireYear").value;


	var valid = true;
	
	
	if(country == "usa" || country == "USA")
	{    
	    if($("txtBillingCode").value == "" )
	    {	
		    $("txtBillingCode").style.borderColor = "red";
		    $("txtBillingCode").style.borderWidth = "1px";
		    valid = false;
	    }
	    else
	    {
		    $("txtBillingCode").style.borderColor = "";
		    $("txtBillingCode").style.borderWidth = "";
	    }
	    
	    if($("txtBillingState").value == "" )
	    {	
		    $("txtBillingState").style.borderColor = "red";
		    $("txtBillingState").style.borderWidth = "1px";
		    valid = false;
	    }
	    else
	    {
		    $("txtBillingState").style.borderColor = "";
		    $("txtBillingState").style.borderWidth = "";
	    }	
	}
	
	
	

       
    if($("selPaymentType").value == "Credit_Card")
    {
        if($("txtCardName").value == "" )
	    {	
		    $("txtCardName").style.borderColor = "red";
		    $("txtCardName").style.borderWidth = "1px";
		    valid = false;
	    }
	    else
	    {
		    $("txtCardName").style.borderColor = "";
		    $("txtCardName").style.borderWidth = "";
	    }
        
	    if($("txtCCNumber").value == "" )
	    {	
		    $("txtCCNumber").style.borderColor = "red";
		    $("txtCCNumber").style.borderWidth = "1px";
		    valid = false;
	    }
	    else
	    {
		    if (isNaN( $("txtCCNumber").value) )
            {
                $("txtCCNumber").style.borderColor = "red";
                $("txtCCNumber").style.borderWidth = "1px";
		        valid = false;
            }
            else
            {
                $("txtCCNumber").style.borderColor = "";
                $("txtCCNumber").style.borderWidth = "";
            }
	    }

	    if($("txtCCSecurityCode").value == "" )
	    {	
		    $("txtCCSecurityCode").style.borderColor = "red";
		    $("txtCCSecurityCode").style.borderWidth = "1px";
		    valid = false;
	    }
	    else
	    {
		    if (isNaN( $("txtCCSecurityCode").value) )
            {
                $("txtCCSecurityCode").style.borderColor = "red";
                $("txtCCSecurityCode").style.borderWidth = "1px";
		        valid = false;
            }
            else
            {
                $("txtCCSecurityCode").style.borderColor = "";
                $("txtCCSecurityCode").style.borderWidth = "";
            }
	    }
	    
    }

	if($("txtBillingAddress1").value == "" )
	{	
		$("txtBillingAddress1").style.borderColor = "red";
		$("txtBillingAddress1").style.borderWidth = "1px";
		valid = false;
	}
	else
	{
		$("txtBillingAddress1").style.borderColor = "";
		$("txtBillingAddress1").style.borderWidth = "";
	}


	if($("txtBillingCity").value == "" )
	{	
		$("txtBillingCity").style.borderColor = "red";
		$("txtBillingCity").style.borderWidth = "1px";
		valid = false;
	}
	else
	{
		$("txtBillingCity").style.borderColor = "";
		$("txtBillingCity").style.borderWidth = "";
	}

    if($("txtBillingCountry").value == "" )
	{	
		$("txtBillingCountry").style.borderColor = "red";
		$("txtBillingCountry").style.borderWidth = "1px";
		valid = false;
	}
	else
	{
		$("txtBillingCountry").style.borderColor = "";
		$("txtBillingCountry").style.borderWidth = "";
	}
	

    
    if(valid && $("selPaymentType").value == "Credit_Card")
    {
        if(expire_year < the_year )
        {
            $("spanErrorMessage").innerHTML = "Please enter valid expiration date.";    
            valid = false;
        }
        else
        {
            //year is valid, now check month
            if(expire_month < the_month && expire_year <= the_year)
            {
                $("spanErrorMessage").innerHTML = "Please enter valid expiration date.";    
                valid = false;
            }
            else
            {
                $("spanErrorMessage").innerHTML = "";    
            }        
        }
        
    }
    else
    {
        $("spanErrorMessage").innerHTML = "Please enter all required fields";
    }
    return valid;
}

function validateShipping(country)
{
	$("txtFirstName").value = trim($("txtFirstName").value);
    $("txtMiddleInitial").value = trim($("txtMiddleInitial").value);
    $("txtLastName").value = trim($("txtLastName").value);
    $("txtCompany").value = trim($("txtCompany").value);
    $("txtShippingAddress1").value = trim($("txtShippingAddress1").value);
    $("txtShippingAddress2").value = trim($("txtShippingAddress2").value);
    $("txtShippingCity").value = trim($("txtShippingCity").value);
    $("txtShippingState").value = trim($("txtShippingState").value);
    $("txtShippingCode").value = trim($("txtShippingCode").value);
    $("txtShippingCountry").value = trim($("txtShippingCountry").value);
    $("txtPhone").value = trim($("txtPhone").value);
    $("txtEmailAddress").value = trim($("txtEmailAddress").value);
    $("txtComments").value = trim($("txtComments").value);
	var valid = true;
	if($("txtFirstName").value == "" )
	{	
		$("txtFirstName").style.borderColor = "red";
		$("txtFirstName").style.borderWidth = "1px";
		valid = false;
	}
	else
	{
	    $("txtFirstName").style.borderColor = "";
	    $("txtFirstName").style.borderWidth = "";
	}
    
	if($("txtLastName").value == "" )
	{	
		$("txtLastName").style.borderColor = "red";
		$("txtLastName").style.borderWidth = "1px";
		valid = false;
	}
	else
	{
		$("txtLastName").style.borderColor = "";    
		$("txtLastName").style.borderWidth = "";
	}

	if($("txtEmailAddress").value == "" )
	{	
		$("txtEmailAddress").style.borderColor = "red";
		$("txtEmailAddress").style.borderWidth = "1px";
		valid = false;
	}
	else
	{
		$("txtEmailAddress").style.borderColor = "";
		$("txtEmailAddress").style.borderWidth = "";
	}

	if($("txtShippingAddress1").value == "" )
	{	
		$("txtShippingAddress1").style.borderColor = "red";
		$("txtShippingAddress1").style.borderWidth = "1px";
		valid = false;
	}
	else
	{
		$("txtShippingAddress1").style.borderColor = "";
		$("txtShippingAddress1").style.borderWidth = "";
	}


	if($("txtShippingCity").value == "" )
	{	
		$("txtShippingCity").style.borderColor = "red";
		$("txtShippingCity").style.borderWidth = "1px";
		valid = false;
	}
	else
	{
		$("txtShippingCity").style.borderColor = "";
		$("txtShippingCity").style.borderWidth = "";
	}
	
	if($("txtShippingCountry").value == "" )
	{	
		$("txtShippingCountry").style.borderColor = "red";
		$("txtShippingCountry").style.borderWidth = "1px";
		valid = false;
	}
	else
	{
		$("txtShippingCountry").style.borderColor = "";
		$("txtShippingCountry").style.borderWidth = "";
	}
	
	if(country == "usa" || country == "USA")
	{
	    if($("txtShippingCode").value == "" )
	    {	
		    $("txtShippingCode").style.borderColor = "red";
		    $("txtShippingCode").style.borderWidth = "1px";
		    valid = false;
	    }
	    else
	    {
		    $("txtShippingCode").style.borderColor = "";
		    $("txtShippingCode").style.borderWidth = "";
	    }
	    if($("txtShippingState").value == "" )
	    {	
		    $("txtShippingState").style.borderColor = "red";
		    $("txtShippingState").style.borderWidth = "1px";
		    valid = false;
	    }
	    else
	    {
		    $("txtShippingState").style.borderColor = "";
		    $("txtShippingState").style.borderWidth = "";
	    }
	}
	
	if(valid)
    {
		$("spanErrorMessage").innerHTML = "";    
    }
    else
    {
        $("spanErrorMessage").innerHTML = "Please enter all required fields";
    }
    return valid;
}












function validateCreditCard() {
  myCardNo = document.getElementById('txtCCNumber').value;
  myCardType = document.getElementById('selCardType').value;

    var valid = false;
  if (checkCreditCard (myCardNo,myCardType)) {
    //$("creditError").innerHTML = ("Credit card has a valid format") + "<br />";
    $("creditError").innerHTML = "";
    $("txtCCNumber").style.borderColor = "";
    valid = true;
  } 
  else {
	$("creditError").innerHTML = ccErrors[ccErrorNo] + "<br />";
	$("txtCCNumber").style.borderColor = "red";
	valid = false;
  }
    return valid;
}