﻿/*
This javascript file contains custom validator functions.
*/
/* Update Details
* 06-07-2011 - Added new javascript functions CheckNotAllowedCharsForAlphaNumeric(e), ValidateNotAllowedCharsForAlphaNumeric(), CheckNotAllowedCharsForAlphabets(e)  & ValidateNotAllowedCharsForAlphabets(sender, args) in User side pages for validation of NickName,FirstName, SurName, City etc fields for mantis item # 0013565  
*/

var EN_NotAllowedCharsAlphaNumericSpace = "[0-9!@#$,%^&*+=|()><~?;:_'\"()/,.-]";
var NL_NotAllowedCharsAlphaNumericSpace = "[0-9ü!@#$,%^&*+=|()><~?;:_'\"()/,.-]";
var FR_NotAllowedCharsAlphaNumericSpace = "[0-9Ä!@#$,%^&*+=|()><~?;:_'\"()/,.-]";
var BE_NotAllowedCharsAlphaNumericSpace = "[0-9!@#$,%^&*+=|()><~?;:_'\"()/,.-]";

var EN_NotAllowedCharsAlphabetsWithSpaces = "[0-9!@#$,%^&*+=|()><~?;:_'\"()/,._-]";
var NL_NotAllowedCharsAlphabetsWithSpaces = "[0-9ü!@#$,%^&*+=|()><~?;:_'\"()/,._-]";
var FR_NotAllowedCharsAlphabetsWithSpaces = "[0-9Ä!@#$,%^&*+=|()><~?;:_'\"()/,._-]";
var BE_NotAllowedCharsAlphabetsWithSpaces = "[0-9!@#$,%^&*+=|()><~?;:_'\"()/,._-]";


var varURL, varPass, varConfPass, varCulture;

// Disallow Invalid characters for AlphaNumeric for NickName,FirstName, SurName, City etc for mantis item # 0013565 
function CheckNotAllowedCharsForAlphaNumeric(e) {
if ($("#hdnCultureid").val() != undefined)
varCulture = $("#hdnCultureid").val();
else
varCulture = 'EN';
var keychar;
var numcheck;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;
keychar = String.fromCharCode(keycode)

if (window[varCulture + '_NotAllowedCharsAlphaNumericSpace'] == 'undefined')
result = keychar.match(window['EN_NotAllowedCharsAlphaNumericSpace']);
else
    result = keychar.match(window[varCulture + '_NotAllowedCharsAlphaNumericSpace']);
//alert(result);
if (result == null) {
return true;
}
else {
e.returnValue = false;
e.cancelBubble = true;
$(".Ctainer").focus();
return false;
}
}


// Validate Invalid characters for AlphaNumeric (i.e on submit button click)for NickName,FirstName, SurName, City etc for mantis item # 0013565 
function ValidateNotAllowedCharsForAlphaNumeric(sender, args) {
if ($("#hdnCultureid").val() != undefined)
varCulture = $("#hdnCultureid").val();
else
varCulture = 'EN';
var result = null;
if (window[varCulture + '_NotAllowedCharsAlphaNumericSpace'] == 'undefined')
result = args.Value.match(window['EN_NotAllowedCharsAlphaNumericSpace']);
else
result = args.Value.match(window[varCulture + '_NotAllowedCharsAlphaNumericSpace']);
if (result == null)
args.IsValid = true;
else
args.IsValid = false;
}



// Disallow Invalid characters for Alphabets for NickName,FirstName, SurName, City etc for mantis item # 0013565 
function CheckNotAllowedCharsForAlphabets(e) {
if ($("#hdnCultureid").val() != undefined)
varCulture = $("#hdnCultureid").val();
else
varCulture = 'EN';
var keychar;
var numcheck;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;
keychar = String.fromCharCode(keycode)
if (window[varCulture + '_NotAllowedCharsAlphaNumericSpace'] == 'undefined')
result = keychar.match(window['EN_NotAllowedCharsAlphaNumericSpace']);
else
result = keychar.match(window[varCulture + '_NotAllowedCharsAlphaNumericSpace']);
if (result == null) {
return true;
}
else {
e.returnValue = false;
e.cancelBubble = true;
$(".Ctainer").focus();
return false;
}
}

//Validate Invalid characters for Alphabets (i.e on submit button click) for NickName,FirstName, SurName, City etc for mantis item # 0013565 
function ValidateNotAllowedCharsForAlphabets(sender, args) {
if ($("#hdnCultureid").val() != undefined)
varCulture = $("#hdnCultureid").val();
else
varCulture = 'EN';
var result = null;
if (window[varCulture + '_NotAllowedCharsAlphabetsWithSpaces'] == 'undefined')
result = args.Value.match(window['EN_NotAllowedCharsAlphabetsWithSpaces']);
else
result = args.Value.match(window[varCulture + '_NotAllowedCharsAlphabetsWithSpaces']);
if (result)
args.IsValid = false;
else
args.IsValid = true;
}

// Validate passed inputbox(source) value as AlphaNumeric
function ValidateAlphaNumericSpace(sender, args) {
var filter = /^[a-zA-Z0-9.,\s-_]+$/;
args.IsValid = filter.test(args.Value);
}

// Validate passed inputbox(source) value have only alphabets with spaces.
function ValidateAlphabetsWithSpaces(sender, args) {
var filter = /^[a-zA-Z ]+$/i;
args.IsValid = filter.test(args.Value);
}

// Validate passed inputbox(source) value as email address and accordingly animate it.
function ValidateEmail(sender, args) {
if (args.Value != "") {
args.IsValid = isValidEmailAddress(args.Value);
}
else {
//$("#" + sender.getAttributeNode("controltovalidate").value + "").css({ "background-color": "#ffffff" });
args.IsValid = true;
}
}

// Validate passed parameter string as a email address and return true if valid and false in reverse case.
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}

//Validate passed inputbox(source) value for max limit.
function ValidateMaxChars(sender, msg) {

var len = $("#" + sender.id + "").val().length;
if (len >= sender.maxLength) {
$("#" + sender.id + "").fadeIn("slow", function() {
$(this).after("<span class=\"" + sender.id + "\">" + msg + ":" + sender.maxLength + "</span>");
$("." + sender.id + "").fadeOut(500);
jQuery.Event("after").stopPropagation();
});
}
}

// Validate passed inputbox(source) value for min limit.
function ValidateMinChars(sender, minvalue) {
var len = $("#" + sender.id + "").val().length;
if (len < minvalue) {
$("#" + sender.id + "").css({ "background-color": "#ffdfdf" });
return;
}
else {
$("#" + sender.id + "").css({ "background-color": "#ffffff" });
return true;
}
}

// Validate passed inputbox(source) value have no space.
function ValidateNoSpaces(sender, args) {
var result = args.Value.indexOf(' ');
if (result == -1) {
args.IsValid = true;
}
else {
args.IsValid = false;
}
}

// Validate passed inputbox(source) value havn't special character.
function ValidateNoSpecialChars(sender, args) {
var result = args.Value.match("[!@#$%^&*()><~?;:'\"()/,]");
if (result) {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}

// Validate passed inputbox(source) value have only alphabets.
function ValidateAlphabetsOnly(sender, args) {
var filter = /^[a-zA-Z]+$/;
args.IsValid = filter.test(args.Value); ;
}



// Validate passed inputbox(source) value as Phone number,
function ValidatePhoneNumber(sender, args) {
args.IsValid = checkInternationalPhone(args.Value);
}

// Validate passed inputbox(source) value as numeric
function ValidateNumericOnly(sender, args) {
var filter = /^[0-9]+$/;
args.IsValid = filter.test(args.Value);
}

// Validate passed inputbox(source) value as AlphaNumeric
function ValidateAlphaNumericOnly(sender, args) {
var filter = /^[a-zA-Z0-9]+$/;
args.IsValid = filter.test(args.Value);
}

// Validate passed inputbox(source) value have 2 decimal points.
function ValidatefloatWith2decimalpoints(sender, args) {
var filter = /^\d{0,2}(\.\d{1,2})?$/;
args.IsValid = filter.test(args.Value);
}

// Validate passed checkbox checked.
function ValidateCheckedCheckBox(sender, args) {
// args.IsValid = $("#" + sender.getAttributeNode("checked").value + "");    
}

// Validate passed checkboxlist contains atleast one checked value.
function ValidateCheckedCheckBoxList(sender, args) {
var chkBoxList = document.getElementById($("#" + sender.getAttributeNode("controltovalidate").value + ""));
var count = CountCheckedItems(chkBoxList);
if (count > 0)
args.IsValid = true;
else
args.IsValid = false;
}
function CountCheckedItems(checkBoxList) {
var numCheckBoxItems = checkBoxList.cells.length;
var numItemsChecked = 0;
for (i = 0; i < numCheckBoxItems; i++) {
//Get the checkboxlist item  
var checkBox = document.getElementById(checkBoxList.id + '_' + [i]);
//Check if the checkboxlist item exists, and if it is checked  
if (checkBox != null && checkBox.checked) { numItemsChecked = numItemsChecked + 1; }
}
return numItemsChecked;
}

/* Phone Validation functions */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

// Check passed value as valid integer.
function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

//trim the whitespace and return the string.
function trim(s) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
// Strip characters and return the string.
function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

// Validate passed value as phone number.
function checkInternationalPhone(strPhone) {
    var bracket = 3
    strPhone = trim(strPhone)
    if (strPhone.indexOf("+") > 1) return false
    if (strPhone.indexOf("-") != -1) bracket = bracket + 1
    if (strPhone.indexOf("(") != -1 && strPhone.indexOf("(") > bracket) return false
    var brchr = strPhone.indexOf("(")
    if (strPhone.indexOf("(") != -1 && strPhone.charAt(brchr + 2) != ")") return false
    if (strPhone.indexOf("(") == -1 && strPhone.indexOf(")") != -1) return false
    s = stripCharsInBag(strPhone, validWorldPhoneChars);
    return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

/*Password Validation*/

function hasUpperCaseCharacter(password) {
    return password.match("[A-Z]") != null;
}

function hasLowerCaseCharacter(password) {
    return password.match("[a-z]") != null;
}

function hasNumericCharacter(password) {
    return password.match("[0-9]") != null;
}

function hasSpecialCharacter(password) {
    return password.match("[!@#$%^&*()><~?;:'\"()/,]") != null;
}

function CheckPasswordStrength(sender, args) {
    if (args.Value.length >= 6 && args.Value.length <= 20) {
        args.IsValid = true;
    }
    else {
        args.IsValid = false;
    }
}

// Validate contact us page text from alpha numeric and special characters.
function ValidateText(sender, args) {
    if (hasUpperCaseCharacter(args.Value) || hasLowerCaseCharacter(args.Value) || hasNumericCharacter(args.Value) || hasSpecialCharacter(args.Value)) {
        args.IsValid = true;
    }
    else {
        args.IsValid = false;
    }
}



function ensureSigningChecked(sender, args) {
    var cb = document.getElementById('ctl00_M_chkSigning');
    if (cb.checked == true) {
        args.IsValid = true;
    } else {
        args.IsValid = false;
    }
}
// Validate passed inputbox value.
//Type : 1- Password ,2 : Confirm Password,3: Email,4:Post-Code
function ShowingValidateImage(cntID, dvID, type) {

    var varRight = '<img src=' + varURL + '/images/RightInput.png>';
    var varWrong = '<img src=' + varURL + '/images/WrongInput.png>';
    var varCnt = document.getElementById(cntID);
    var varDiv = document.getElementById(dvID);
    if (varCnt.value.length == 0) { varDiv.innerHTML = ''; return true; }
    if (type == 1) {
        if (varCnt.value.length >= 6 && varCnt.value.length <= 20) {
            varDiv.innerHTML = varRight;
        }
        else {
            varDiv.innerHTML = varWrong;
        }
    }
    if (type == 2) {
        var newPass = document.getElementById(varPass).value;
        var ConfPass = document.getElementById(varConfPass).value;
        if (varCnt.value.length >= 6 && varCnt.value.length <= 20 && newPass == ConfPass) {
            varDiv.innerHTML = varRight;
        }
        else {
            varDiv.innerHTML = varWrong;
        }
    }
    if (type == 3) {
        if (isValidEmailAddress(varCnt.value))
        { varDiv.innerHTML = varRight; }
        else { varDiv.innerHTML = varWrong; }

    }
    if (type == 4) {

        var result;
        var currentLang = document.getElementById("ctl00_M_hdnCountryCode").value;
        var filter = /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/;
        if (currentLang == "NL") {
            result = filter.test(varCnt.value);
        }
        else {
            result = true;
        }
        if (result == true) {
            varDiv.innerHTML = varRight;
        }
        else {
            varDiv.innerHTML = varWrong;
        }
    }
}


// Begin Sitestat4 code 
function sitestat(ns_l)
{
    ns_l+='&amp;ns__t='+(new Date()).getTime();
    ns_pixelUrl=ns_l;
    ns_0=document.referrer;
    ns_0=(ns_0.lastIndexOf('/')==ns_0.length-1)?ns_0.substring(ns_0.lastIndexOf('/'),0):ns_0;
    if(ns_0.length>0)ns_l+='&amp;ns_referrer='+escape(ns_0);
    if(document.images)
    {
	    ns_1=new Image();
	    ns_1.src=ns_l;
    }
    else
    document.write('<img src="'+ns_l+'" width="1" height="1" alt="">');
}

//-->
//End Sitestat4 code 


