//RegExp for e-mail address validation
re1 = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})$/;

//RegExp for web address validation
re5 = /^[Ww]{3}\.\S+\.\S+$/;

//RegExp for simple address validation - no PO Box addresses
re4 = /^.*[Pp]{1}[\. ]*[Oo]{1}[\. ]*[Bb]{1}[Oo]{1}[Xx]{1}.*$/;

//RegExp for zip codes
re2 = /^\d{5}$/;

//RegExp for names
re3 = /^[a-zA-Z].*$/;

function IsNumeric(val)
{
	//Uses a Regular Expression to determine if a string represents a numeric value

	reNum = /^\d+[\.]?\d*$/;

    if (reNum.test(val))
    	return true;
    else
		return false;
}

function setBkgColor(objectID,strColor)
{
 	document.getElementById(objectID).style.backgroundColor = strColor;
}

function selectDDLOptionByValue(ddlName,selectValue)
{
	//Locates a DDL option by "value" and selects it

    if(document.getElementById(ddlName))
    {
    	ddlOptions = document.getElementById(ddlName);

        for (x = 0; x < ddlOptions.length; x++)
        {
        	if(ddlOptions.options[x].value == selectValue)
            {
            	ddlOptions.options[x].selected = true;
                break;
            }
        }
    }
}

function selectDDLOptionByText(ddlName,selectText)
{
	//Locates a DDL option by its "text" string and selects it
    if(document.getElementById(ddlName))
    {
        ddlOptions = document.getElementById(ddlName);

        for (x = 0; x < ddlOptions.length; x++)
        {
        	if(ddlOptions.options[x].text == selectText)
            {
            	ddlOptions.options[x].selected = true;
                break;
            }
        }
    }
}

function writeNumberedOptions(startVal, endVal, skipVal, textBefore, textAfter)
{
	//Writes <option> tags for lists that use a range of sequential numbers as values
    /*
    	startVal	- The starting number to include in the option list
        endVal 		- The last number to include in the option list
        skipVal     - Specified the increment amount between each number
        textBefore	- The displayable text to print before the number
        textAfter	- The displayable text to display after the number
    */
	var htmlText = "\n";

    for(x = startVal; x <= endVal; x += skipVal)
    {
    	if (x < 10)
        	strVal = "0" + x;
        else
        	strVal = x;

    	htmlText += "<option value='" + strVal + "'>" + textBefore + strVal + textAfter + "</option>\n";
    }

    return htmlText;
}

    function isCheckoutFormValid(accountForm)
    {
        if(accountForm.txtFName.value == "" ||!re3.test(accountForm.txtFName.value))
        {
        	alert("Please provide your First Name.");
            accountForm.txtFName.focus();
            return false;
        }
        if(accountForm.txtLName.value == "" || !re3.test(accountForm.txtLName.value))
        {
        	alert("Please provide your Last Name.");
            accountForm.txtLName.focus();
            return false;
        }
    	//Validation for the form during the checkout process
        if(accountForm.txtEMail.value == "" || !re1.test(accountForm.txtEMail.value))
        {
        	alert("Please provide a valid e-mail address.");
            accountForm.txtEMail.focus();
            return false;
        }
        if(accountForm.txtAddress.value == "")
        {
        	alert("Please provide your Billing Street Address.");
            accountForm.txtAddress.focus();
            return false;
        }

        if(accountForm.txtCity.value == "")
        {
        	alert("Please provide the name of your City.");
            accountForm.txtCity.focus();
            return false;
        }
        if(accountForm.selState.value == "--")
        {
        	alert("Please select a State.");
            accountForm.selState.focus();
            return false;
        }
        if(accountForm.txtZip.value == "" || !re2.test(accountForm.txtZip.value))
        {
        	alert("Please enter a valid Zip Code.");
            accountForm.txtZip.focus();
            return false;
        }
        if(accountForm.selCardType.value == "--")
        {
        	alert("Please select the Credit Card Type");
            accountForm.selCardType.focus();
            return false;
        }
        if(accountForm.txtCardNumber.value.length < 13)
        {
        	alert("Please enter a valid Credit Card Number.");
            accountForm.txtCardNumber.focus();
            return false;
        }
        if(accountForm.selCCMonth.value == "mm" || (accountForm.selCCYear.value == "yy"))
        {
        	alert("Please specify your Credit Card Expiration Date.");
            accountForm.selCCMonth.focus();
            return false;
        }
        if(accountForm.txtCVV2.value.length < 3)
        {
        	alert("Please enter the CVV2 / CID number as it appears on your card.");
            accountForm.txtCVV2.focus();
            return false;
        }
        if(!IsNumeric(accountForm.txtAmount.value))
        {
        	alert("Please enter a valid Payment Amount");
            accountForm.txtAmount.focus();
            return false;
        }

        //If all checks pass, the form is valid
        return true;
    }

