﻿/*******************
 * constant values *
 *******************/
 valCellExp =/^(\+\d{2,3} )?\(\d{3,4}\) \d{7,11}$/;
 valFaxExp = /^(\+\d{2,3} )?\(\d{3,5}\) \d{5,9}$/;
 valTelExp = /^(\+\d{2,3} )?\(\d{3,5}\) \d{5,9}( Ext: \d{1,5})?$/;
 //valEmailExp= /^([a-zA-Z0-9\-]+(\.{1}[a-zA-Z0-9\-])?)+\@{1}[a-zA-Z0-9\-]+(\.{1}[a-zA-Z0-9\-]+(\.{1}[a-zA-Z0-9\-])?)+$/;
 valEmailExp= /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
 // /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
 

//check to see if the required field has a value
function requiredField(val,msg){
    val = val == undefined ? '' : val;
    if(trim(val).length==0){
    parent.showPopup(2, document.getElementById('hPath').value + 'UtilityPages/Message.aspx?Message=' + msg + '&type=E&Button=OK&ButtonAction=parent.showPopup();', 'Mandatory Field', 120, 250);
        
        return false;
    }
    else{return true;}  
};

// check if a valid cell number
function isValidInput(val,type){
    val = val==undefined?'':val;
    if(trim(val).length > 0)
    {
        switch(type){
            case 'cell':
                return MatchValue(valCellExp, val, 'Cellphone number is not valid! ex: +27 (082) 1234567');
                break;
            case 'fax':
                return MatchValue(valFaxExp, val, 'Fax number is not valid! ex: +27 (012) 1234567');
                break;
            case 'tel':
                return MatchValue(valTelExp, val, 'Telephone number is not valid! ex: +27 (012) 1234567 Ext: 123');
                break;
            case 'email':
                return MatchValue(valEmailExp, val, 'Email Address is not valid!');
                break;
        }
    }
    else{return true;}
};

function requiredDropDown(index,msg){
    if(index<1){
        parent.showPopup(2, document.getElementById('hPath').value + 'UtilityPages/Message.aspx?Message=' + msg + '&type=E&Button=OK&ButtonAction=parent.showPopup();', 'Mandatory Field', 120, 250);
               
        return false;
    }
    else{return true;}  
};


/***************************
 * Generic Methods         *
 ***************************/
 
 
//validate a value with an expression
//exp - The expression to use
//val - The value to validate
//msg - The message to display if the validation fail
function MatchValue(exp, val, msg){ 
    var re = new RegExp(exp);
    if (!val.match(re)) {
    
                parent.showPopup(2, document.getElementById('hPath').value + 'UtilityPages/Message.aspx?Message=' + msg + '&type=E&Button=OK&ButtonAction=parent.showPopup();', 'Invalid Input', 120, 250);
        return false;
    }
    else{return true;}
};



function Equals(val1, val2, msg){ 
    if (val1 != val2) {
    
                parent.showPopup(2, document.getElementById('hPath').value + 'UtilityPages/Message.aspx?Message=' + msg + '&type=E&Button=OK&ButtonAction=parent.showPopup();', 'Fields do not Match', 120, 250);
        return false;
    }
    else{return true;}
};



//trim a value and return the trimmed value
// val - the value to trim
function trim(val){
    return val.replace(/^\s*/, '').replace(/\s*$/, '');

};

function keyStroke(e){
    if (e.keyCode == 13) {e.keyCode = 9;}
};


function isNumeric(val, msg){
     if(trim(val).length > 0 && isNaN(val))
     {
        parent.showPopup(2, document.getElementById('hPath').value + 'UtilityPages/Message.aspx?Message=' + msg + '&type=E&Button=OK&ButtonAction=parent.showPopup();', 'Mandatory Field', 120, 250);
        return false;
     }
     
     return true;
};

// check to see if input was numeric
// call --> onkeypress(event);
function checkNumeric(e){
    var key;
    var keychar;
    var reg;

    if(window.event){key = e.keyCode;} // IE
    else if(e.which){key = e.which;} // Netscape/Firefox/Opera
    
    // if backspace or delete return true
    if(key==8){return true;}
    
    keychar = String.fromCharCode(key);
    reg = /\d/;
    
    return reg.test(keychar);
};


function format(val){
    for(i = 1; i < arguments.length; i++){
        val = val.replace('{' + (i - 1) + '}', arguments[i]);
    }
    
    return val;
};