﻿var focusOut = false;
var maxCCNumber = 16;
var maxCCSecurity = 3;
var setNoFocus = "false";



function ValidateTerms(source, clientside_arguments)
{
    var id = source.id.replace(/Val_/, "");
    validateControl(id, source, clientside_arguments, "Terms");
}

function Validate(source, clientside_arguments)
{
    var id1 = source.id.split("_lblerr");
    var id2 = id1[1].split("_Focus_");
    var validation = id2[0].replace(/_XXYY_/, "");
    setNoFocus = id2[1];
    
    //var id = source.id.split("_lblerr");
    //var validation = id[1].replace(/_XXYY_/, "");
    
//    var ctrlNoFocus = 
//        noFocusSet = false;

    validateControl(id1[0], source, clientside_arguments, validation);
}


function ValidateStates(source, clientside_arguments)
{
    var id = source.id.split("_Validator");
    var w = document.getElementById(id[0]).selectedIndex;
    var selected_text = document.getElementById(id[0]).options[w].value;
    
    if (selected_text == "") clientside_arguments.IsValid = false;
    else clientside_arguments.IsValid = true;
}


function VerifyInput(source, clientside_arguments)
{
    var id = source.id.split("_lblerr");
    var validation = id[1].replace(/_XXYY_/, "");
    
    var val = document.getElementById(id[0]).value;
    if (val != "" || val != null)
    {    
        validateControl(id[0], source, clientside_arguments, validation);
    }
}



function SetCCMaxValues(number, security)
{
    maxCCNumber = number;
    maxCCSecurity = security
}


function verifyEmail(str)
{
    var at = "@";
    var dot = ".";
    var lat = str.indexOf(at);
    var lstr = str.length;
    var ldot = str.indexOf(dot);    
    if (str.indexOf(at) == -1)
    {
       return false;
    }
    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr)
    {
       return false;
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
    {
        return false;
    }
    if (str.indexOf(at,(lat+1))!=-1)
    {
       return false;
    }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
    {
       return false;
    }
    if (str.indexOf(dot,(lat+2))==-1)
    {
       return false;
    }
    if (str.indexOf(" ")!=-1)
    {
       return false;
    }
    return true;
}


function CCLuhnCheck(s)
{
    var i, n, c, r, t;

    // First, reverse the string and remove any non-numeric characters.
    r = "";
    for (i = 0; i < s.length; i++)
    {
    c = parseInt(s.charAt(i), 10);
    if (c >= 0 && c <= 9)
      r = c + r;
    }
    // Check for a bad string.
    if (r.length <= 1) return false;

    // Now run through each single digit to create a new string. Even digits
    // are multiplied by two, odd digits are left alone.
    t = "";
    for (i = 0; i < r.length; i++)
    {
    c = parseInt(r.charAt(i), 10);
    if (i % 2 != 0) c *= 2;
    t = t + c;
    }

    // Finally, add up all the single digits in this string.
    n = 0;
    for (i = 0; i < t.length; i++)
    {
    c = parseInt(t.charAt(i), 10);
    n = n + c;
    }

    // If the resulting sum is an even multiple of ten (but not zero), the
    // card number is good.
    if (n != 0 && n % 10 == 0) return true;
    else return false;
}









function validateControl(ctrl, source, clientside_arguments, validationType)
{
    var xCtrl = document.getElementById(ctrl);
    if (focusOut == false)
    {
        if (setNoFocus == "false")
            xCtrl.focus();
    }    
    var value = xCtrl.value;
    
    if (validationType == "Email")
    {
        if (verifyEmail(value) == false)
        {
            xCtrl.value = ""; 
            clientside_arguments.IsValid = false;
            focusOut = true;    
        }
        else
        {
            clientside_arguments.IsValid = true;
            focusOut = false;
        }
    }
    
    if (validationType == "Void")
    {    
        if (value == "")
        {
            clientside_arguments.IsValid = false;
            focusOut = true;
        }
        else
        {
            clientside_arguments.IsValid = true;
            focusOut = false;
        }
    }
    
    if (validationType == "None")
    {    
        if (value == "")
        {
            clientside_arguments.IsValid = false;
            focusOut = true;
        }
        else
        {
            clientside_arguments.IsValid = true;
            focusOut = false;
        }
    }
    if (validationType == "UsZip")
    {
        var reg = /^[0-9]{5}$/;
        if (reg.test(value))
        {
            clientside_arguments.IsValid = true;
            focusOut = false;
        }
        else
        {
            clientside_arguments.IsValid = false;
            focusOut = true;
        }  
    }
    
    
    if (validationType == "UsPhone")
    {
        var valLength = value.length;
        if (valLength < 14)
        {
            clientside_arguments.IsValid = false;
            focusOut = true;
        }
        else
        {
            clientside_arguments.IsValid = true;
            focusOut = false;
        }
    }
    
    
    if (validationType == "UsDate")
    {       
        var isMonth = false; var isDay = false; var isYear = false;
        var val = value.split("/");
        if (val[0] > 0 && val[0] < 13) isMonth = true;        
        if (val[1] > 0 && val[1] < 32) isDay = true;
        if (val[2] > 1850 && val[2] < 3000) isYear = true;
        
        if (isMonth && isDay && isYear)
        {
            clientside_arguments.IsValid = true;
            focusOut = false;
        }
        else
        {
            clientside_arguments.IsValid = false;
            focusOut = true;
        }   
    }    
    
    if (validationType == "CreditCardNumber" || validationType == "CreditCardLuhnAlgorithm")
    {
        var valLength = value.length;
        if (valLength < maxCCNumber)
        {        
            clientside_arguments.IsValid = false;
            focusOut = true;
        }
        else
        {
            if (validationType == "CreditCardLuhnAlgorithm")
            {
                if (CCLuhnCheck(value))
                {
                    clientside_arguments.IsValid = true;
                    focusOut = false;                
                }
                else
                {
                    clientside_arguments.IsValid = false;
                    focusOut = true;                
                }
            }
            else
            {
                clientside_arguments.IsValid = true;
                focusOut = false;
            }
        }
    }
    
    
    if (validationType == "CreditCardSecurityCode")
    {
        var valLength = value.length;
        if (valLength < maxCCSecurity)
        {
            clientside_arguments.IsValid = false;
            focusOut = true;
        }
        else
        {
            clientside_arguments.IsValid = true;
            focusOut = false;
        }
    }
    
    
    if (validationType == "CheckRouting")
    {
        var valLength = value.length;
        if (valLength < 9)
        {
            clientside_arguments.IsValid = false;
            focusOut = true;
        }
        else
        {
            clientside_arguments.IsValid = true;
            focusOut = false;
        }
    }
    
    
    if (validationType == "CheckAccount")
    {
        var valLength = value.length;
        
        if (valLength > 3 && valLength < 18)
        {
            clientside_arguments.IsValid = true;
            focusOut = false;
        }
        else
        {
            clientside_arguments.IsValid = false;
            focusOut = true;
        }     
    }
    
    
    if (validationType == "Terms")
    {
        if (xCtrl.checked == false)
        {
            clientside_arguments.IsValid = false;
            focusOut = true;
        }
        else
        {
            clientside_arguments.IsValid = true;
            focusOut = false;
        }
    }    


            
           
    
}
