﻿var controls = ['none'];
var passwords = ['none'];
var directory = '../';

function validationObject(control,type,image,blankValue){
    this.control = document.getElementById(control);
    this.type = type.toLowerCase();
    this.image = document.getElementById(image);  
    this.blankValue = blankValue;
    if(this.control.value == ''){this.control.value = blankValue;}
    if(blankValue != ''){
        this.image.src = directory + 'Images/accept.png';
    }else{
        this.image.src = directory + 'Images/blank.png';
    }
}

function passwordValidationObject(control, control2,image, image2,minLength){
    this.control = document.getElementById(control);
    this.control2 = document.getElementById(control2);
    this.image = document.getElementById(image);
    this.image2 = document.getElementById(image2);  
    this.minLength = minLength;
    this.image.src = directory + 'Images/blank.png';
    this.image2.src = directory + 'Images/blank.png';
}

function addValidation(control,type,image, blankValue){
    var id = controls.push(new validationObject(control,type,image,blankValue));
    document.getElementById(control).onblur = function(){validate(id);}
}

function addPasswordValidation(control, control2, image, image2, minLength){
    var id = passwords.push(new passwordValidationObject(control, control2, image, image2, minLength));
    document.getElementById(control).onblur = function(){validatePass(id);}
    document.getElementById(control2).onblur = function(){validatePass(id);}
}


function validate(id){
    var ob = controls[id-1];
    var valid = false;
    var badMessage ='';
    ob.control.value = trimString(ob.control.value);
    if(ob.control.value == ''){ob.control.value = ob.blankValue;}
    if(ob.control.value == ob.blankValue && ob.control.value != ''){
        valid = true;
    }else{
        switch(ob.type){
            case 'required':
                valid = valRequired(ob.control.value);
                badMessage = "Must be entered!";
                break;
            case 'date':
                valid = valDate(ob.control.value);
                badMessage = "Must be valid date!";
                break;
            case 'email':
                valid = valEmail(ob.control.value);
                badMessage = "Must be a proper email!";
                break;
            case 'postcode':
                valid = valPostcode(ob.control.value.toUpperCase());
                badMessage = "Must be valid postcode!";
                if(valid)ob.control.value = ob.control.value.toUpperCase();
                break;
            case 'price' :
                valid = valPrice(ob.control);
                badMessage = "Must be a valid price!";
                break;
            default:
                if(ob.type.substr(0,6).toLowerCase() == "maxlen"){
                    valid = valMaxLen(ob.type.substr(6,ob.type.length-6),ob.control.value);  
                }
                break;
        }
    }
    
    if(valid){
        ob.image.src = directory + "Images/accept.png";
    }else{
        ob.image.src = directory + "Images/exclamation.png";
        ob.image.title = badMessage;
        ob.image.setAttribute("style","cursor:help");
    }
    return valid;
}

function validatePass(id){
    var ob = passwords[id-1];
    if(ob.control.value.length >= ob.minLength){
       ob.image.src = directory + "Images/accept.png"; 
    }else{
        ob.image.src = directory + "Images/exclamation.png"; 
        return false;
    }

    if(ob.control2.value == ob.control.value){
        ob.image2.src = directory + "Images/accept.png"; 
        return true;
    }else{
        ob.image2.src = directory + "Images/exclamation.png"; 
        return false;
    }
    
}

function trimString(string){
    if(string.length > 0){
        while(string.substr(0,1) == ' '){
            string = string.substr(1,string.length-1);
        }
        if(string.length > 0){
           while(string.substr(string.length-1,1) == ' '){
                string = string.substr(0,string.length-1);
            }
        }
    }
    return string;
}



function valRequired(value){
    if(value != ""){
        return true
    }else{
        return false;
    }
}

function valMaxLen(max,string){
    return (parseInt(string.length) <= parseInt(max))
}

function valDate(dateStr)
{
   return (/^(((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))$/).test(dateStr);
}

function valPostcode(postcodeStr){
    postcode = new RegExp('(((^[BEGLMNS][1-9]\\d?)|(^W[2-9])|(^(A[BL]|B[ABDHLNRST]|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]|M[EKL]|N[EGNPRW]|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\\d\\d?)|(^W1[A-HJKSTUW0-9])|(((^WC[1-2])|(^EC[1-4])|(^SW1))[ABEHMNPRVWXY]))(\\s*)?([0-9][ABD-HJLNP-UW-Z]{2}))$|(^GIR\\s?0AA$)');
    if (postcode.exec(postcodeStr) == null){
        return false;
   }else{
        return true;
   }
}

function valEmail(emailStr)
{
   email = new RegExp('^[a-z0-9][a-z0-9_\.-]{0,}[a-z0-9]@[a-z0-9][a-z0-9_\.-]{0,}[a-z0-9][\.][a-z0-9]{2,4}$');
   if (email.exec(emailStr.toLowerCase()) == null){
        return false;
   }else{
        return true;
   }
}

function valPrice(s)
{
    price = new RegExp('^[0-9]+$');
    if(price.exec(s.value.toLowerCase()) != null){
        s.value = s.value + ".00";
        return true;
    }else{
        price = new RegExp('^[0-9]+\.[0-9]{2}$');
        if(price.exec(s.value.toLowerCase()) != null){
            return true;
        }
    }
    
    return false;
}


function validateAll(){
    var valid = true;
    for(i=1;i<controls.length;i++){
        if(!(validate(i+1))){
            valid = false;
        }
    }
    for(i=1;i<passwords.length;i++){
        if(!(validatePass(i+1))){
            valid = false;
        }
    }
    return valid;
}

function setDirectory(dir){
    directory = dir;
}


