﻿function checkValidation() {
    var valImgs = new Array();
    valImgs[0] = "txtName_ValidatorOk";
    valImgs[1] = "txtSurname_ValidatorOk";
    valImgs[2] = "txtContact_ValidatorOk";
    valImgs[3] = "txtEmail_ValidatorOk";
    valImgs[4] = "txtEnquiry_ValidatorOk";
    valImgs[5] = "txtDate_ValidatorOk";
    valImgs[6] = "ddnVehicleModel_ValidatorOk";
    valImgs[7] = "ddnDealer_ValidatorOk";
    valImgs[8] = "txtUsername_ValidatorOk";
    valImgs[9] = "txtPassword_ValidatorOk";
    valImgs[10] = "txtSalesName_ValidatorOk";
    valImgs[11] = "txtImpression_ValidatorOk";
    valImgs[12] = "txtComments_ValidatorOk";
    valImgs[13] = "txtCaptcha_ValidatorOk";
    valImgs[14] = "txtIdNumber_ValidatorOk";
    valImgs[15] = "txtServiceManager_ValidatorOk";
    valImgs[16] = "txtCell_ValidatorOk";
    valImgs[17] = "ddnVehicleRange_ValidatorOk";
    valImgs[18] = "ddnRegion_ValidatorOk";
    valImgs[18] = "ddnTown_ValidatorOk";
    valImgs[19] = "ddlServiceInterval_ValidatorOk";
    valImgs[20] = "txtServiceTimeFrame_ValidatorOk";
    valImgs[21] = "txtConfirmPassword_ValidatorOk";
    valImgs[22] = "txtDestinationForLift_ValidatorOk";
    valImgs[23] = "txtVehicleYear_ValidatorOk";
    valImgs[24] = "txtVehicleRegistration_ValidatorOk";
    valImgs[25] = "txtAnswer_ValidatorOk";
    valImgs[26] = "ddlQuestion_ValidatorOk";
    valImgs[27] = "txtRegistration_ValidatorOk";
    valImgs[28] = "cklrblOwnerStatus_ValidatorOk";
    valImgs[29] = "ddnVehicleRegNumber_ValidatorOk";
    valImgs[30] = "txtFinanceHelpOptional_ValidatorOk";
    valImgs[31] = "rblAirportTransfers_ValidatorOk";
    valImgs[32] = "txtFirstName_ValidatorOk";
    valImgs[33] = "txtQuestion_ValidatorOk";
    valImgs[34] = "txtCell_ValidatorOk";
    valImgs[34] = "txtCellphone_ValidatorOk";
    
    for (var i = 0; i <= (valImgs.length + 1); i++) {
        if (document.getElementById(valImgs[i]) != null) {
            var img = document.getElementById(valImgs[i]).src;
            if (img.substr((img.lastIndexOf("/") + 1)) == "icoInfo_off.png") {
                document.getElementById(valImgs[i]).style.display = 'none';
            }
        }
    }
}

function FireCustomValidation(fldobj) {
    if (typeof (Page_ClientValidate) == 'function') {
        var passed = true;
        for (i = 0; i < Page_Validators.length; i++) {
            if (Page_Validators[i].controltovalidate == fldobj.id) {
                ValidatorValidate(Page_Validators[i]);
                if (!Page_Validators[i].isvalid)
                    passed = false;
            }
        }
        var str = fldobj.id;
        var finstr = str.substr((str.lastIndexOf("_") + 1));
        if (passed) {



            document.getElementById(finstr + '_ValidatorOk').src = 'images/iconCorrect.png';
            document.getElementById(finstr + '_ValidatorOk').style.display = '';

            //Set top progress bar colour
            if (document.getElementById(finstr + '_top_left') != null) {
                document.getElementById(finstr + '_top_left').src = 'images/leftGreen.jpg';
            }
                if (document.getElementById(finstr + '_top_mid') != null) {
                    document.getElementById(finstr + '_top_mid').src = 'images/middleGreen.jpg';
                }
                    if (document.getElementById(finstr + '_top_right') != null) {
                        document.getElementById(finstr + '_top_right').src = 'images/rightGreen.jpg';
                    }

                    return 0;
        }
        else {
            document.getElementById(finstr + '_ValidatorOk').style.display = 'none';

            //Set top progress bar colour
            if (document.getElementById(finstr + '_top_left') != null) {
                document.getElementById(finstr + '_top_left').src = 'images/leftRed.jpg';
            }            
            
            if (document.getElementById(finstr + '_top_mid') != null) {
                document.getElementById(finstr + '_top_mid').src = 'images/middleRed.jpg';
            }

            if (document.getElementById(finstr + '_top_right') != null) {
                document.getElementById(finstr + '_top_right').src = 'images/rightRed.jpg';
            }
            return 1;
        }
    }
}

//function OnlyAlphabet(fldobj) {
//    var regexNum = /\d/;
//    var regexLetter = /[a-zA-z]/;

//    alert(document.getElementById(fldobj.id).value); 
//    if (!regexNum.test(document.getElementById(fldobj.id).value) && regexLetter.test(document.getElementById(fldobj.id).value)) {
//        alert(document.getElementById(fldobj.id).value); 
//        return false;
//    }
//    alert(document.getElementById(fldobj.id).value); 
//    return true;
//}

//var maxChars = 4000;
function charsleft(InputBox, maxChars) {
    var InputBoxId = InputBox.id;
    var CountBoxId = InputBox.id.substr((InputBox.id.lastIndexOf("_") + 1));
    theInputBox = document.getElementById(InputBoxId);
    theCountBox = document.getElementById(CountBoxId + '_Chars');
    var currentChars = theInputBox.value.length;
    theCountBox.innerHTML = Math.max((maxChars - currentChars), 0).toString();
    if (currentChars > maxChars) {
        theInputBox.value = theInputBox.value.substring(0, maxChars);
    }
}

//This script is based on the javascript code of Roman Feldblum (web.developer@programmer.net)
//Original script : http://javascript.internet.com/forms/format-phone-number.html
//Original script is revised by Eralper Yilmaz (http://www.eralper.com)
//Revised script : http://www.kodyaz.com

var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 13;
var phonevalue1;
var phonevalue2;
var cursorposition;

function ParseForNumber1(object) {
    phonevalue1 = ParseChar(object.value, zChar);
}
function ParseForNumber2(object) {
    phonevalue2 = ParseChar(object.value, zChar);
}

function backspacerUP(object, e) {
    if (e) {
        e = e
    } else {
        e = window.event
    }
    if (e.which) {
        var keycode = e.which
    } else {
        var keycode = e.keyCode
    }

    ParseForNumber1(object)

    if (keycode >= 48) {
        ValidatePhone(object)
    }
}

function backspacerDOWN(object, e) {
    if (e) {
        e = e
    } else {
        e = window.event
    }
    if (e.which) {
        var keycode = e.which
    } else {
        var keycode = e.keyCode
    }
    ParseForNumber2(object)
}

function GetCursorPosition() {

    var t1 = phonevalue1;
    var t2 = phonevalue2;
    var bool = false
    for (i = 0; i < t1.length; i++) {
        if (t1.substring(i, 1) != t2.substring(i, 1)) {
            if (!bool) {
                cursorposition = i
                bool = true
            }
        }
    }
}

function ValidatePhone(object) {

    var p = phonevalue1

    p = p.replace(/[^\d]*/gi, "")

    if (p.length < 3) {
        object.value = p
    } else if (p.length == 3) {
        pp = p;
        d4 = p.indexOf('(')
        d5 = p.indexOf(')')
        if (d4 == -1) {
            pp = "(" + pp;
        }
        if (d5 == -1) {
            pp = pp + ")";
        }
        object.value = pp;
    } else if (p.length > 3 && p.length < 7) {
        p = "(" + p;
        l30 = p.length;
        p30 = p.substring(0, 4);
        p30 = p30 + ")"

        p31 = p.substring(4, l30);
        pp = p30 + p31;

        object.value = pp;

    } else if (p.length >= 7) {
        p = "(" + p;
        l30 = p.length;
        p30 = p.substring(0, 4);
        p30 = p30 + ")"

        p31 = p.substring(4, l30);
        pp = p30 + p31;

        l40 = pp.length;
        p40 = pp.substring(0, 8);
        p40 = p40 + "-"

        p41 = pp.substring(8, l40);
        ppp = p40 + p41;

        object.value = ppp.substring(0, maxphonelength);
    }

    GetCursorPosition()

    if (cursorposition >= 0) {
        if (cursorposition == 0) {
            cursorposition = 2
        } else if (cursorposition <= 2) {
            cursorposition = cursorposition + 1
        } else if (cursorposition <= 5) {
            cursorposition = cursorposition + 2
        } else if (cursorposition == 6) {
            cursorposition = cursorposition + 2
        } else if (cursorposition == 7) {
            cursorposition = cursorposition + 4
            e1 = object.value.indexOf(')')
            e2 = object.value.indexOf('-')
            if (e1 > -1 && e2 > -1) {
                if (e2 - e1 == 4) {
                    cursorposition = cursorposition - 1
                }
            }
        } else if (cursorposition < 11) {
            cursorposition = cursorposition + 3
        } else if (cursorposition == 11) {
            cursorposition = cursorposition + 1
        } else if (cursorposition >= 12) {
            cursorposition = cursorposition
        }

        var txtRange = object.createTextRange();
        txtRange.moveStart("character", cursorposition);
        txtRange.moveEnd("character", cursorposition - object.value.length);
        txtRange.select();
    }

}

function ParseChar(sStr, sChar) {
    if (sChar.length == null) {
        zChar = new Array(sChar);
    }
    else zChar = sChar;

    for (i = 0; i < zChar.length; i++) {
        sNewStr = "";

        var iStart = 0;
        var iEnd = sStr.indexOf(sChar[i]);

        while (iEnd != -1) {
            sNewStr += sStr.substring(iStart, iEnd);
            iStart = iEnd + 1;
            iEnd = sStr.indexOf(sChar[i], iStart);
        }
        sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

        sStr = sNewStr;
    }

    return sNewStr;
}

function validateSAID(elementID) {
    var SAIDRegEx = /^(((\d{2}((0[13578]|1[02])(0[1-9]|[12]\d|3[01])|(0[13456789]|1[012])(0[1-9]|[12]\d|30)|02(0[1-9]|1\d|2[0-8])))|([02468][048]|[13579][26])0229))(( |-)(\d{4})( |-)(\d{3})|(\d{7}))/;
    if (SAIDRegEx.test(document.getElementById(elementID).value)) return true;
    return false;
}