var errorcolor = "#FCFCC2"
var normalcolor = "#FFFFFF"
var errormessage = ""

function CheckNum(cardnum)
{
    if (cardnum == '')
	{
	    return 0;
	}
    if (isNaN(cardnum))
	{
	    return 0;
	}
    if (!CheckLUHN(cardnum))
	{
	    return 0;
	}
    return 1;
}

function CheckLUHN(cardnum)
{
    var RevNum = new String(cardnum);
    RevNum = Reverse(RevNum);

    var total = new Number(0);
    for ( var i = 0; i < RevNum.length; i += 1 )
	{
	    var temp = 0;
	    if (i % 2)
		{
		    temp = RevNum.substr(i, 1) * 2;
		    if (temp >= 10)
			{
			    var splitstring = new String(temp);
			    temp = parseInt(splitstring.substr(0, 1)) + parseInt(splitstring.substr(1, 1));
			}
		}
	    else
		{
		    temp = RevNum.substr(i, 1);
		}
	    total += parseInt(temp);
	}
    // if there's no remainder, we return 1 (true)
    return (total % 10) ? 0 : 1;
}

function Reverse(strToReverse)
{
    var strRev = new String;
    var i = strToReverse.length;

    while (i--)
	{
	    strRev += strToReverse.charAt(i);
	}
    return strRev;
}

function  d_validate()
{
    errormessage = ""
	if (CheckNum(document.cust_info.card_number.value) == 0)
	{
        errormessage = errormessage + "Please enter a valid Credit Card Number\n" ;
        document.cust_info.card_number.focus();
        document.cust_info.card_number.style.backgroundColor = errorcolor;
    }
    else
    {
        document.cust_info.card_number.style.backgroundColor = normalcolor;
	    var cDate = new Date();
	    var curMonth = cDate.getMonth() + 1;
	    var curYear  = cDate.getFullYear();
        var frmMonth = document.cust_info.card_month.options[document.cust_info.card_month.selectedIndex].value;
        var frmYear =  document.cust_info.card_year.options[document.cust_info.card_year.selectedIndex].value;

		if(frmYear < curYear || (curMonth > frmMonth && frmYear == curYear))
		{
            errormessage = errormessage + "Please select a valid expiration date\n" ;
            document.cust_info.card_year.style.backgroundColor = errorcolor;
		}
        else
        {
            document.cust_info.card_year.style.backgroundColor = normalcolor;
        }

        if  (document.cust_info.cvv2.value.length < 3)
        {
            errormessage = errormessage + "Please enter a valid Cvv2\n";
            document.cust_info.cvv2.style.backgroundColor = errorcolor;
        }
        else
        {
            document.cust_info.cvv2.style.backgroundColor = normalcolor;
        }

        
        validate_billing();
        
        if  (!validate_email())
        {
            errormessage= errormessage + "Please enter a valid email address\n" ;
            document.cust_info.email_address.style.backgroundColor = errorcolor;
        }
		else
		{
			document.cust_info.email_address.style.backgroundColor = normalcolor;
		}
		
        
        if(document.getElementById("shipping_billing_different").checked == true)
        {
           validate_shipping()
        }
    }
    
    if (errormessage != "")
    {
        var errormessage2 = "Please make this corrections before proceeding\n";
        errormessage = errormessage2 + errormessage;
        alert(errormessage);
   	    return false;
    }

    document.getElementById('Submit1').style.display = "none";
    document.getElementById('submitProcess').style.display = "block";
    return true;
}

function validate_email() 
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address = document.getElementById("email_address").value
	if(reg.test(address) == false)
	{
		return false;
	}
	return true;
}

function toggle_shipping_info()
{
    alert
    if(document.getElementById("shipping_billing_different").checked == true)
    {
        document.getElementById("shipping_info").style.display = ""
    }
    else
    {
        document.getElementById("shipping_info").style.display = "none"
    }
}

function validate_billing()
{
    if  (document.cust_info.billing_firstname.value.length < 2)
        {
            errormessage= errormessage + "Please enter a valid billing first name\n" ;
            document.cust_info.billing_firstname.style.backgroundColor = errorcolor;
        }
        else
        {
            document.cust_info.billing_firstname.style.backgroundColor = normalcolor;
        }
        
        if  (document.cust_info.billing_lastname.value.length < 2)
        {
            errormessage= errormessage + "Please enter a valid billing last name\n" ;
            document.cust_info.billing_lastname.style.backgroundColor = errorcolor;
        }
        else
        {
            document.cust_info.billing_lastname.style.backgroundColor = normalcolor;
        }
        
        if  (document.cust_info.billing_address1.value.length < 3)
        {
            errormessage = errormessage + "Please enter a valid billing address\n" ;
            document.cust_info.billing_address1.style.backgroundColor = errorcolor;
        }
        else
        {
            document.cust_info.billing_address1.style.backgroundColor = normalcolor;
        }
        
        if  (document.cust_info.billing_city.value.length < 3)
        {
            errormessage = errormessage + "Please enter a valid billing city\n" ;
            document.cust_info.billing_city.style.backgroundColor = errorcolor;
        }
        else
        {
            document.cust_info.billing_city.style.backgroundColor = normalcolor;
        }
        
        if  (document.cust_info.billing_state.value == "select")
        {
            errormessage = errormessage + "Please select a valid billing state\n" ;
            document.cust_info.billing_state.style.backgroundColor = errorcolor;
        }
        else
        {
            document.cust_info.billing_state.style.backgroundColor = normalcolor;
        }
        
        if  (document.cust_info.billing_zip.value.length != 5)
        {
            errormessage= errormessage + "Please enter a valid billing zip\n" ;
            document.cust_info.billing_zip.style.backgroundColor = errorcolor;
        }
        else
        {
            document.cust_info.billing_zip.style.backgroundColor=normalcolor
        }
        
        if(document.cust_info.billing_phone.value.length < 10)
		{
			errormessage= errormessage + "Please enter a valid phone number\n" ;
            document.cust_info.billing_phone.style.backgroundColor = errorcolor;
		}
		else if(isNaN(document.cust_info.billing_phone.value))
		{
			errormessage= errormessage + "Please enter a valid phone number (numbers only)\n" ;
            document.cust_info.billing_phone.style.backgroundColor = errorcolor;
		}
        else
        {
            document.cust_info.billing_phone.style.backgroundColor = normalcolor;
        }
}

function validate_shipping()
{
    if(document.getElementById("shipping_firstname").value.length < 2)
    {
        errormessage= errormessage + "Please enter a valid shipping first name\n" ;
        document.getElementById("shipping_firstname").style.backgroundColor = errorcolor;
    }
    else
    {
        document.getElementById("shipping_firstname").style.backgroundColor = normalcolor;
    }
        
    if(document.getElementById("shipping_lastname").value.length < 2)
    {
        errormessage= errormessage + "Please enter a valid shipping last name\n" ;
        document.getElementById("shipping_lastname").style.backgroundColor = errorcolor;
    }
    else
    {
        document.getElementById("shipping_lastname").style.backgroundColor = normalcolor;
    }
        
    if(document.getElementById("shipping_address1").value.length < 3)
    {
        errormessage = errormessage + "Please enter a valid shipping address\n" ;
        document.getElementById("shipping_address1").style.backgroundColor = errorcolor;
    }
    else
    {
        document.getElementById("shipping_address1").style.backgroundColor = normalcolor;
    }
        
    if(document.getElementById("shipping_city").value.length < 3)
    {
        errormessage = errormessage + "Please enter a valid shipping city\n" ;
        document.getElementById("shipping_city").style.backgroundColor = errorcolor;
    }
    else
    {
        document.getElementById("shipping_city").style.backgroundColor = normalcolor;
    }
        
    if(document.getElementById("shipping_state").value == "select")
    {
        errormessage = errormessage + "Please select a valid shipping state\n" ;
        document.getElementById("shipping_state").style.backgroundColor = errorcolor;
    }
    else
    {
        document.getElementById("shipping_state").style.backgroundColor = normalcolor;
    }
        
    if(document.getElementById("shipping_zip").value.length != 5 && !isNaN(document.getElementById("shipping_zip").value))
    {
        errormessage= errormessage + "Please enter a valid shipping zip\n" ;
        document.getElementById("shipping_zip").style.backgroundColor = errorcolor;
    }
    else
    {
        document.getElementById("shipping_zip").style.backgroundColor=normalcolor
    }
}