﻿<!--
/*---

----------------------------Instructions on how to make this validation to work
----------------------------
----------------------------    Items needed:
----------------------------        this script file
----------------------------        the ValidatePurchaseForm.vb class 
----------------------------
----------------------------For each Item you want to validate, do the following 
----------------------------    - add the field name you want to appear in the alert as a css class of the field itself, replacing spaces with "_" and slashes(/) with "~". 
----------------------------        (i.e. "Jesse Trimble/Kayla Stevens" would be entered as "Jesse_Trimble~Kayla_Stevens") 
----------------------------    - if you're taking any information where there is more than one field for that info (i.e. four credit card fields), add "rep" as a css class to each of those fields.
----------------------------
----------------------------Then do the following
----------------------------    - in the code behind, do a RegisterClientScriptBlock with the attachJSArray function from the class ValidatePurchaseForm.vb as the script parameter.  
----------------------------        the attachJSArray function takes a WebControl array of all of the fields you want to validate as a parameter
----------------------------        (i.e. attachJSArray(new WebControl() {me.txtField, me.cboField})    )
----------------------------    - in the code behind, do a RegisterClientScriptBlock with the attachCBOValues function from the class ValidatePurchaseForm.vb as the script parameter.
----------------------------        the attachCBOValues function takes a DropDownList array of all of the dropdowns you want to validate as a parameter
----------------------------        (i.e. attachCBOValues(new DropDownList() {me.ddl1, me.ddl2})    )
----------------------------    - in the code behind, add the following: "return doValidation(formElements);" to the "onclientclick" event of the submit button or link (must be an asp:Button or asp:LinkButton)
----------------------------
----------------------------    Validation should work from there!!

---*/
    
var valForm = function(arr){ //currently validates for the following controls: asp:TextBox, asp:DropDownList, asp:RadioButtonList, RadDatePicker, RadEditor
    var rarr = new Array();
    for (x in arr){
        if (arr[x][1] == "TextBox"){
            var box = document.getElementById(arr[x][0]);
            if (!(checkString(box.value))){
                addArrayItem(rarr, box);
            }
        } else if(arr[x][1] == "DropDownList"){
            var list = document.getElementById(arr[x][0]);
            if (list.options[list.selectedIndex].text == getDefaultCBOValue(list)){
                addArrayItem(rarr, list);
            }
        } else if(arr[x][1] == "RadioButtonList"){
            var list = document.getElementById(arr[x][0]);
            var m = list.childNodes;
            var selected = false;
            for (x in m){
                var elem = m[x];
                if (elem.tagName){
                    if (elem.tagName.toUpperCase() == "INPUT"){
                        if (elem.checked){
                            selected = true;
                            break;  
                        }
                    }
                }
            }
            if (!(selected)){
                addArrayItem(rarr, list);
            }
        } else if(arr[x][1] == "RadDatePicker"){
            var calendar = eval(arr[x][0]);            
            if (calendar.IsEmpty()){
                addArrayItem(rarr, document.getElementById(calendar.ClientID).parentNode);
            }
        } else if (arr[x][1] == "RadEditor"){
            var editor = GetRadEditor(arr[x][0]);
            if (!(checkString(editor.GetText()))){
                addArrayItem(rarr, editor);
            }      
        }
    }    
    var invalidContentMessage = valContent(arr);
    var invalidPasswordMessage = valPassword(arr);
    
    if (rarr.length > 0){
        var alertstring = "Please fill out the following required field(s)  :\n\t";
        for (x in rarr){
            alertstring += "- " + rarr[x] + "\n\t";
        }
        if (typeof(invalidContentMessage) == "string") alertstring = alertstring + "\n\n" + invalidContentMessage;
        return alertstring;
    } else if (typeof(invalidContentMessage) == "string"){
        return invalidContentMessage;
    } else {
        return true;
    }
    return false;
}

var valContent = function(arr){
    var numarr = new Array();
    var pcearr = new Array();
    var emlarr = new Array();
    var passwordMatch = true;
    var passwordLength = true;
    var passwordContent = null;
    var retstring = "";
    for (x in arr){
        if (arr[x][1] == "TextBox"){
            var box = document.getElementById(arr[x][0]);
            var cls = box.className.split(" ");
            for (m in cls){
                if (cls[m] == "num" && !(isNumeric(box.value)) && !(box.value.replace(" ", "") == "")){                   
                    addArrayItem(numarr, box);                    
                } else if (cls[m] == "pce" && !(isNumeric(box.value.replace("$", ""))) && !(box.value.replace(" ", "") == "")){
                    addArrayItem(pcearr, box);
                } else if (cls[m] == "eml" && !(box.value.match(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/)) && !(box.value.replace(" ", "") == "")){
                    addArrayItem(emlarr, box);   
                } else if (cls[m] == "pass" && box.value.length > 0){                    
                    passwordLength = box.value.length >= 6?true:false;                  
                    if (passwordContent){
                        passwordMatch = box.value == passwordContent?true:false;
                    } else {
                        passwordContent = box.value;
                    }
                } //
            }
        }
    }    
    if (numarr.length > 0){
        retstring = "The values for the following field(s) need to contain only numbers:\n\t";
        for (x=0; x<numarr.length; x++){
            retstring += "- " + numarr[x];
            if (x == numarr.length - 1){
                retstring += "\n\n";
                break;
            }
            retstring += "\n\t";
        }
    }
    if (pcearr.length > 0){
        retstring += "The following field(s) must be a whole-dollar amount:\n\t";
        for (x=0; x<pcearr.length; x++){
            retstring += "- " + pcearr[x];
            if (x == pcearr.length - 1){
                retstring += "\n\n";
                break;
            }
            retstring += "\n\t";
        }
    }
    if (emlarr.length > 0){
        retstring += "The following field(s) do not contain a valid email address:\n\t";
        for (x = 0; x < emlarr.length; x++){
            retstring += "- " + emlarr[x];
            if (x == emlarr.length - 1){
                retstring += "\n\n";
                break;
            }
            retstring += "\n\t";
        }
    }
    if (!(passwordLength)){
        retstring += "Your password needs to be at least 6 characters in length";
    }
    if (!(passwordMatch) && passwordLength){
        retstring += "The values entered into the 'Select Password' and 'Confirm Password' fields do not match\n\n";
    }
    if (retstring != ""){
        return retstring;
    } else {
        return true;
    }        
}

var valPassword = function(arr){

}
//-->

var doValidation = function(arr){
    var result = valForm(arr);
    if (typeof(result) == "string"){
        alert(result);
        return false;            
    } else if (typeof(result) == "boolean"){
        if (result){
            return true;    
        } else {
            alert('Please fill out all required fields');
            return false;
        }
    }
}

var formatString = function(item){
    var it = item;
    do{
        it = it.replace("_", " ");
    } while (!(it.indexOf("_") == -1));
    
    do{
        it = it.replace("~", "/");
    } while (!(it.indexOf("~") == -1));
    return it;
}

var addArrayItem = function(arr, element){
    var exists = false;
    if (element.ClassName){
        this.m = element.ClassName.split(" ");
    } else {
        this.m = element.className.split(" ");
    }
    var theName = formatString(m[0])
    for (x in m){
        if (exists) break;
        if (m[x] == "rep"){
            for (b in arr){
                if (arr[b] == theName){
                    exists = true;
                    break;
                }
            }
        }
    }
    if (!(exists)){
        arr.push(theName);
    }
}

var getDefaultCBOValue = function(element){
    var m = cboValues;
    for (x in m){
        if (m[x][0] == element.id){
            return m[x][1];
        }
    }
}

var isNumeric = function(s){
    var validChars = "0123456789";
    var validNumber = "";
    for (i=0; i<s.length; i++){
        var currentChar = s.charAt(i);
        if (validChars.indexOf(currentChar) == -1){
            return false;
        }        
    }
    return true;
}

var checkString = function(stringtocheck){
    var isValidString = false;
    for (i=0; i<stringtocheck.length;i++){
        if (!(stringtocheck.charAt(i) == " ")){
            isValidString = true;
            break;
        }
    }
    return isValidString;
}