﻿// JScript File
function void_GeneralFormValidation_resetBackgrounds(string_controlName)
{
    for (i=0; i < form_GeneralUtilities_GetForm().elements.length; i++)
    {
        var currentObject = form_GeneralUtilities_GetForm().elements[i];
    
        if (currentObject.style.backgroundColor == "rgb(255, 174, 174)" || currentObject.style.backgroundColor.toUpperCase() == "#FFAEAE")
        {
            if (string_controlName != null)
            {
                if (currentObject.id.indexOf(string_controlName) >= 0)
                {
                    currentObject.style.backgroundColor = "white";
                }
            }
            else
                currentObject.style.backgroundColor = "white";
        }
    }
}

function bool_GeneralFormValidation_fieldExists(string_objectName)
{
    return object_GeneralUtilities_GetObject(string_objectName) != null;
}

function bool_GeneralFormValidation_checkBoxIsSelected(string_objectName, bool_highlightObject, bool_moveFocus)
{
    var object_currentFormObject = object_GeneralUtilities_GetObject(string_objectName);

    bool_success = object_currentFormObject.checked;

    if (!bool_success)
    {
        if (bool_highlightObject)
        {
            var string_friendlyName = object_currentFormObject.getAttribute("friendlyName");
            var string_promptPosition = object_currentFormObject.getAttribute("promptPosition");

            // Since ASP.net checkboxes put their arbitrary attribute in a wrapping <span>, check the parent of the checkbox if no name attribute is found on the checkbox itself
            if (string_friendlyName == null)
            {
                string_friendlyName = object_currentFormObject.parentNode.getAttribute("friendlyName");
                string_promptPosition = object_currentFormObject.parentNode.getAttribute("promptPosition");
            }             
                
            $(object_currentFormObject).parents("form:first").validationEngine({promptPosition: object_currentFormObject.getAttribute("promptPosition")=="" ? "topRight" : object_currentFormObject.getAttribute("promptPosition"),  inlineValidation: false});
            $.validationEngine.buildPrompt(object_currentFormObject, string_friendlyName, "error");
        }

        if (bool_moveFocus)
            object_currentFormObject.focus; 
    }   

    return bool_success;
}


function bool_GeneralFormValidation_fieldContainsText(string_objectName, bool_highlightObject, bool_moveFocus)
{
    //Check if bool_moveFocus was passed in
    if(bool_moveFocus == null)
        bool_moveFocus = false;
        
    var bool_success = true;
    var object_currentFormObject = object_GeneralUtilities_GetObject(string_objectName);
    
    if (object_currentFormObject.value.trim() == "")
    {
        bool_success = false;
        
        if (bool_highlightObject)
        {
            var string_friendlyName = object_currentFormObject.getAttribute("friendlyName");
                
            $(object_currentFormObject).parents("form:first").validationEngine({promptPosition: object_currentFormObject.getAttribute("promptPosition")=="" ? "topRight" : object_currentFormObject.getAttribute("promptPosition"),  inlineValidation: false});
            $.validationEngine.buildPrompt(object_currentFormObject, "Please enter " + string_GeneralUtilities_AttemptToGetArticleForWord(string_friendlyName) + " " + string_friendlyName, "error");
        }
            
        if (bool_moveFocus)
            object_currentFormObject.focus;     
    }
    else
    {
        $.validationEngine.closePrompt(object_currentFormObject);
    }
    
    return bool_success;
}

function bool_GeneralFormValidation_fieldContainsTextOfSpecifiedLength(string_objectName, int_requiredLength, bool_highlightObject, bool_moveFocus)
{
    //Check if bool_moveFocus was passed in
    if(bool_moveFocus == null)
        bool_moveFocus = false;

    var bool_success = true;
    var object_currentFormObject = object_GeneralUtilities_GetObject(string_objectName);    

    if (object_currentFormObject.value.length < int_requiredLength)
    {
        bool_success = false;
        
        if (bool_highlightObject)
        {
             var string_friendlyName = object_currentFormObject.getAttribute("friendlyName");
                
            $(object_currentFormObject).parents("form:first").validationEngine({promptPosition: object_currentFormObject.getAttribute("promptPosition")=="" ? "topRight" : object_currentFormObject.getAttribute("promptPosition"), inlineValidation: false});
            $.validationEngine.buildPrompt(object_currentFormObject, "Please enter at least " + int_requiredLength + " letters for your " + string_friendlyName, "error");
        }
            
        if (bool_moveFocus)
            object_currentFormObject.focus;     
    }
    else
    {
        $.validationEngine.closePrompt(object_currentFormObject);
    }
    
    return bool_success;
}

function bool_GeneralFormValidation_dropDownIsSelected(string_objectName, bool_highlightObject, bool_moveFocus, bool_validateValue)
{    
    //Check if bool_moveFocus was passed in
    if(bool_moveFocus == null)
        bool_moveFocus = false;
        
    var bool_success = true;
    var object_currentFormObject = object_GeneralUtilities_GetObject(string_objectName); 
       
    if (object_currentFormObject.options.selectedIndex <= 0 || (bool_validateValue && object_currentFormObject.options[object_currentFormObject.options.selectedIndex].value < 0))
    {
        bool_success = false;
        
        if (bool_highlightObject)
        {
             var string_friendlyName = object_currentFormObject.getAttribute("friendlyName");
                
            $(object_currentFormObject).parents("form:first").validationEngine({promptPosition: object_currentFormObject.getAttribute("promptPosition")=="" ? "topRight" : object_currentFormObject.getAttribute("promptPosition"), inlineValidation: false});
            $.validationEngine.buildPrompt(object_currentFormObject, "Please choose " + string_GeneralUtilities_AttemptToGetArticleForWord(string_friendlyName) + " " + string_friendlyName, "error");
        }
            
        if (bool_moveFocus)
            object_currentFormObject.focus;     
    }
    else
    {
        $.validationEngine.closePrompt(object_currentFormObject);
    }

    return bool_success;
}

function bool_GeneralFormValidation_ValidateNameField(string_objectName, bool_highlightObject, bool_moveFocus)
{
    return (bool_GeneralFormValidation_fieldContainsTextOfSpecifiedLength(string_objectName, 2, bool_highlightObject, bool_moveFocus));         
}

function bool_GeneralFormValidation_ValidateZipField(string_objectName, bool_highlightObject, bool_moveFocus)
{
    //Check if bool_moveFocus was passed in
    if(bool_moveFocus == null)
        bool_moveFocus = false;
        
    var bool_success = true;
    var object_currentFormObject = object_GeneralUtilities_GetObject(string_objectName); 

    if (!bool_GeneralStringValidation_StringIsValidZipCode(object_currentFormObject.value.trim()))
    {
        bool_success = false;
        
        if (bool_highlightObject)
        {
            var string_friendlyName = object_currentFormObject.getAttribute("friendlyName");
                
            $(object_currentFormObject).parents("form:first").validationEngine({promptPosition: object_currentFormObject.getAttribute("promptPosition")=="" ? "topRight" : object_currentFormObject.getAttribute("promptPosition"), inlineValidation: false});
            $.validationEngine.buildPrompt(object_currentFormObject, "Please enter a valid " + string_friendlyName + ". (Ex: xxxxx or xxxxx-xxxx)", "error");
        }
            
        if (bool_moveFocus)
            object_currentFormObject.focus;     
    }
    else
    {
        $.validationEngine.closePrompt(object_currentFormObject);
    }

    return bool_success;
}

function bool_GeneralFormValidation_ValidatePhoneField(string_objectName, bool_highlightObject, bool_moveFocus)
{
    //Check if bool_moveFocus was passed in
    if(bool_moveFocus == null)
        bool_moveFocus = false;

    var bool_success = true;
    var object_currentFormObject = object_GeneralUtilities_GetObject(string_objectName); 

    if (!bool_GeneralStringValidation_StringIsValidPhoneNumber(object_currentFormObject.value.trim(), true))
    {
        bool_success = false;
        
        if (bool_highlightObject)
        {
            var string_friendlyName = object_currentFormObject.getAttribute("friendlyName");
                
            $(object_currentFormObject).parents("form:first").validationEngine({promptPosition: object_currentFormObject.getAttribute("promptPosition")=="" ? "topRight" : object_currentFormObject.getAttribute("promptPosition"), inlineValidation: false});
            $.validationEngine.buildPrompt(object_currentFormObject, "Please enter a valid " + string_friendlyName + ". (Ex: xxx-xxx-xxxx)", "error");
        }
            
        if (bool_moveFocus)
            object_currentFormObject.focus;     
    }
    else
    {
        $.validationEngine.closePrompt(object_currentFormObject);
    }

    return bool_success;
}

function bool_GeneralFormValidation_ValidateEmailField(string_objectName, bool_highlightObject, bool_moveFocus)
{
    //Check if bool_moveFocus was passed in
    if(bool_moveFocus == null)
        bool_moveFocus = false;
        
    var bool_success = true;
    var object_currentFormObject = object_GeneralUtilities_GetObject(string_objectName); 

    if (!bool_GeneralStringValidation_StringIsValidEmailAddress(object_currentFormObject.value.trim()))
    {
        bool_success = false;
        
        if (bool_highlightObject)
        {
            var string_friendlyName = object_currentFormObject.getAttribute("friendlyName");
                
            $(object_currentFormObject).parents("form:first").validationEngine({promptPosition: object_currentFormObject.getAttribute("promptPosition")=="" ? "topRight" : object_currentFormObject.getAttribute("promptPosition"), inlineValidation: false});
            $.validationEngine.buildPrompt(object_currentFormObject, "Please enter a valid " + string_friendlyName, "error");
        }
            
        if (bool_moveFocus)
            object_currentFormObject.focus;     
    }
    else
    {
        $.validationEngine.closePrompt(object_currentFormObject);
    }
    
    return bool_success;
}

function bool_GeneralFormValidation_ValidateAddressField(string_objectName, bool_highlightObject, bool_moveFocus)
{
    //Check if bool_moveFocus was passed in
    if(bool_moveFocus == null)
        bool_moveFocus = false;

    var bool_success = true;
    var object_currentFormObject = object_GeneralUtilities_GetObject(string_objectName); 

    if (!bool_GeneralStringValidation_StringIsValidStreetAddress(object_currentFormObject.value.trim()))
    {
        bool_success = false;
        
        if (bool_highlightObject)
        {
            var string_friendlyName = object_currentFormObject.getAttribute("friendlyName");
                
            $(object_currentFormObject).parents("form:first").validationEngine({promptPosition: object_currentFormObject.getAttribute("promptPosition")=="" ? "topRight" : object_currentFormObject.getAttribute("promptPosition"), inlineValidation: false});
            $.validationEngine.buildPrompt(object_currentFormObject, "Please enter a valid " + string_friendlyName + ". (At least some numbers and letters)", "error");
        }
            
        if (bool_moveFocus)
            object_currentFormObject.focus;     
    }
    else
    {
        $.validationEngine.closePrompt(object_currentFormObject);
    }
    
    return bool_success;
}

function bool_GeneralFormValidation_CompareFields(string_objectName1, string_objectName2, bool_highlightObject, bool_moveFocus)
{
    //Check if bool_moveFocus was passed in
    if(bool_moveFocus == null)
        bool_moveFocus = false;

    var bool_success = true;
    var object_currentFormObject1 = object_GeneralUtilities_GetObject(string_objectName1); 
    var object_currentFormObject2 = object_GeneralUtilities_GetObject(string_objectName2);
    
    if (object_currentFormObject1.value.trim() != object_currentFormObject2.value.trim())
    {
        bool_success = false;
        
        if (bool_highlightObject)
        {
            var string_friendlyName1 = object_currentFormObject1.getAttribute("friendlyName");                
                
            $(object_currentFormObject1).parents("form:first").validationEngine({promptPosition: object_currentFormObject1.getAttribute("promptPosition")=="" ? "topRight" : object_currentFormObject1.getAttribute("promptPosition"), inlineValidation: false});
            $.validationEngine.buildPrompt(object_currentFormObject1, string_friendlyName1 + " fields do not match", "error");
        }
            
        if (bool_moveFocus)
        {
            object_currentFormObject1.focus;     
        }
    }
    else
    {
        $.validationEngine.closePrompt(object_currentFormObject2);
    }
    
    return bool_success;
}

function bool_GeneralFormValidation_ValidateCreditCardNumberField(string_objectName, bool_highlightObject, bool_moveFocus)
{
    //Check if bool_moveFocus was passed in
    if(bool_moveFocus == null)
        bool_moveFocus = false;

    var object_currentFormObject = object_GeneralUtilities_GetObject(string_objectName);
    var string_fieldValue = object_currentFormObject.value.trim(); 
    var string_errorString = string_friendlyName + " is not valid. Use only numbers (no symbols or spaces).\n";
    var string_response = "";
    var bool_success = true;
    
    string_fieldValue = string_GeneralStringManipulation_removeNonNumericCharacters(string_fieldValue);
	
	if (!string_fieldValue.length > 0)
		bool_success = false;
	
	if (!bool_GeneralUtilities_StringIsValidPositiveInteger(string_fieldValue))
		bool_success = false;
	
	if (bool_GeneralStringValidation_StringStartsWithSubString(string_fieldValue, "51") || 
	    bool_GeneralStringValidation_StringStartsWithSubString(string_fieldValue, "52") || 
	    bool_GeneralStringValidation_StringStartsWithSubString(string_fieldValue, "53") || 
	    bool_GeneralStringValidation_StringStartsWithSubString(string_fieldValue, "54") || 
	    bool_GeneralStringValidation_StringStartsWithSubString(string_fieldValue, "55"))
	{
	    if (!(string_fieldValue.length == 16))
	        bool_success = false;
	}
	else if (bool_GeneralStringValidation_StringStartsWithSubString(string_fieldValue, "4"))
	{
	    if (!(string_fieldValue.length == 13 || string_fieldValue.length == 16))
	        bool_success = false;
	}
	else if (bool_GeneralStringValidation_StringStartsWithSubString(string_fieldValue, "34") || 
	         bool_GeneralStringValidation_StringStartsWithSubString(string_fieldValue, "37"))
	{
	    if (!(string_fieldValue.length == 15))
	        bool_success = false;
	}
	else if (bool_GeneralStringValidation_StringStartsWithSubString(string_fieldValue, "6011"))
	{
	    if (!(string_fieldValue.length == 16))
	        bool_success = false;
	}
	else
	{
	    bool_success = false;
	}
	
	if (bool_success)
	{
	    var num1, num2, tempNum;
	    num1 = ""
	    if (!(string_fieldValue.length%2==0)) {
		    for(var j=0; j < string_fieldValue.length; j++) {
			    if ((j+1)%2==0){
				    tempNum = 2 * string_fieldValue.charAt(j);
			    }
			    else {
				    tempNum = 1 * string_fieldValue.charAt(j);
			    }
			    num1 = num1 + tempNum.toString();
		    }
	    }
	    else{
		    for(var j=0; j < string_fieldValue.length; j++){
			    if ((j+1)%2==0){
				    tempNum = 1 * string_fieldValue.charAt(j);
			    }
			    else{
				    tempNum = 2 * string_fieldValue.charAt(j);
			    }
			    num1 = num1 + tempNum.toString();
		    }
	    }
	    num2 = 0;
	    for (var j = 0; j < num1.length; j++) {
		    num2 = num2 + parseInt(num1.charAt(j));
	    }
	    if (num2%10==0) {
		    bool_success = true;
	    }
	    else {
		    bool_success = false;
	    }
	}
	
	if (!bool_success)
	{    	
        if (bool_highlightObject)
        {
            var string_friendlyName = object_currentFormObject.getAttribute("friendlyName");
                
            $(object_currentFormObject).parents("form:first").validationEngine({promptPosition: object_currentFormObject.getAttribute("promptPosition")=="" ? "topRight" : object_currentFormObject.getAttribute("promptPosition"), inlineValidation: false});
            $.validationEngine.buildPrompt(object_currentFormObject, string_friendlyName + " is not valid. Use only numbers (no symbols or spaces).", "error");
        }

        if (bool_moveFocus)
            object_currentFormObject.focus;
    }
    else
    {
        $.validationEngine.closePrompt(object_currentFormObject);
    }
        
    return bool_success; 
}

function bool_GeneralFormValidation_ValidateSSN(string_objectName, bool_highlightObject, bool_moveFocus)
{
    //Check if bool_moveFocus was passed in
    if(bool_moveFocus == null)
        bool_moveFocus = false;

    var object_currentFormObject = object_GeneralUtilities_GetObject(string_objectName);
    var bool_success = false;
    
    var string_SSN = object_currentFormObject.value
    
    if(string_SSN.length == 4)
    {
        if (string_SSN != "0000")
        {
            var filter=/^[\d]{4}$/;
            
            if (filter.test(string_SSN))
            {
                $.validationEngine.closePrompt(object_currentFormObject);
                bool_success = true;
            }
       }
    }    

    if (!bool_success)
    {
        if (bool_moveFocus)
            object_currentFormObject.focus;
        
        $(object_currentFormObject).parents("form:first").validationEngine({promptPosition: object_currentFormObject.getAttribute("promptPosition")=="" ? "topRight" : object_currentFormObject.getAttribute("promptPosition"), inlineValidation: false});
        $.validationEngine.buildPrompt(object_currentFormObject, "The Social Security Number you entered is invalid. Please correct your Social Secuirty Number or select another payment option.", "error");              
    }
        
    return bool_success;
}

function bool_GeneralFormValidation_ValidateDateOfBirth(string_monthObjectName, string_dayObjectName, string_yearObjectName, int_minAge)
{
    var date_currentDate = new Date();
    var string_birthDay = (object_GeneralUtilities_GetObject(string_dayObjectName)).options[object_GeneralUtilities_GetObject(string_dayObjectName).selectedIndex].value;
    var string_birthMonth = (object_GeneralUtilities_GetObject(string_monthObjectName)).options[object_GeneralUtilities_GetObject(string_monthObjectName).selectedIndex].value;
    var string_birthYear = (object_GeneralUtilities_GetObject(string_yearObjectName)).options[object_GeneralUtilities_GetObject(string_yearObjectName).selectedIndex].value;
    var dropDown_month = object_GeneralUtilities_GetObject(string_monthObjectName)


    if (string_birthMonth > 0 && string_birthDay > 0 && string_birthYear > 0)
    {
        if (date_currentDate.getFullYear() - string_birthYear > int_minAge)
        {
            $.validationEngine.closePrompt(dropDown_month);
            return true;
        }
        else if (date_currentDate.getFullYear() - string_birthYear < int_minAge)
        {
            $(dropDown_month).parents("form:first").validationEngine({promptPosition: dropDown_month.getAttribute("promptPosition")=="" ? "topRight" : dropDown_month.getAttribute("promptPosition"), inlineValidation: false});
            $.validationEngine.buildPrompt(dropDown_month, "The date of birth you entered indicates that you are less than " + int_minAge + " years of age. Please correct your date of birth or select another payment option.", "error");   

            return false;
        }
        else
        {
            if (date_currentDate.getMonth() - (string_birthMonth - 1) > 0)
            {
                $.validationEngine.closePrompt(dropDown_month);

                return true;
            }
            else if (date_currentDate.getMonth() - (string_birthMonth - 1) < 0)
            {
                $(dropDown_month).parents("form:first").validationEngine({promptPosition: dropDown_month.getAttribute("promptPosition")=="" ? "topRight" : dropDown_month.getAttribute("promptPosition"), inlineValidation: false});
                $.validationEngine.buildPrompt(dropDown_month, "The date of birth you entered indicates that you are less than " + int_minAge + " years of age. Please correct your date of birth or select another payment option.", "error");   

                return false;
            }
            else
            {
                if (date_currentDate.getDate() - (string_birthDay) >= 0)
                {
                    $.validationEngine.closePrompt(dropDown_month);

                    return true;
                }
                else 
                {
                    $(dropDown_month).parents("form:first").validationEngine({promptPosition: dropDown_month.getAttribute("promptPosition")=="" ? "topRight" : dropDown_month.getAttribute("promptPosition"), inlineValidation: false});
                    $.validationEngine.buildPrompt(dropDown_month, "The date of birth you entered indicates that you are less than " + int_minAge + " years of age. Please correct your date of birth or select another payment option.", "error");   
                
                    return false;
                }
            }
        }    
    }
    else
    {
        $(dropDown_month).parents("form:first").validationEngine({promptPosition: dropDown_month.getAttribute("promptPosition")=="" ? "topRight" : dropDown_month.getAttribute("promptPosition"), inlineValidation: false});
        $.validationEngine.buildPrompt(dropDown_month, "Please enter your date of birth", "error");   
                
        return false;           
    }  
}

function void_GeneralFormValidation_OnlyAllowIntsInTextBox(object_textBox)
{
    if(object_textBox.value.length > 0)
    {
        object_textBox.value = object_textBox.value.replace(/[^\d]+/g, ''); 
    }
}

function bool_GeneralFormValidation_ValidateStateIsShippingEnabled(string_objectName)
{
    var dropDown_state = object_GeneralUtilities_GetObject(string_objectName);
    var string_nonShippingStates = ['AK', 'HI', 'AS', 'FM', 'GU', 'MH', 'MP', 'PW', 'PR', 'VI'];   
    var string_stateValue = dropDown_state.options[object_GeneralUtilities_GetObject(string_objectName).selectedIndex].value;
    
    for (i = 0; i < string_nonShippingStates.length; i++)
    {
        if (string_nonShippingStates[i] == string_stateValue)
        {
            $(dropDown_state).parents("form:first").validationEngine({promptPosition: dropDown_state.getAttribute("promptPosition")=="" ? "topRight" : dropDown_state.getAttribute("promptPosition"), inlineValidation: false});
            $.validationEngine.buildPrompt(dropDown_state, "We are unable to ship to the state/territory you have chosen. Please call 1-888-293-2339 (toll free) or 1-724-444-5300 (international) or place a shipping quote <a style='color:white' href='cs_intShippingQuote.aspx'>here</a>.", "error");   
            
            return false;
        }  
    } 
    
    $.validationEngine.closePrompt(dropDown_state);
    
    return true;       
}
