/**
*  The common.js
*  Ducont FZ LLC, Dubai Internet City
*  
*  The file contains most of the commonly used javascript function.
*/

/*
This method is to block all the characters which are not listed in the object 'goods'
*/
function goodchars(e, goods) {
    var key, keychar;
    key = getkey(e);
    if (key == null) return true;
    // get character
    keychar = String.fromCharCode(key);
    keychar = keychar.toLowerCase();
    goods = goods.toLowerCase();
    // check goodkeys
    if (goods.indexOf(keychar) != -1)
        return true;
    // control keys
    if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
        return true;
    return false;
}

function getkey(e)
{
    if (window.event)
        return window.event.keyCode;
    else if (e)
        return e.which;
    else
        return null;
}

function checkMobile(formField) {
    var formval = trim(formField.value);
    var msg = "";
    
    if(formval.length == 0) {
        msg = "Enter Mobile Number.\nMobile Number should begin with 974nnnnnnn.\n";
    } else if(formval.length < 9) {
    msg = "Invalid Mobile Number. \nMobile Number should not be less than 9 digits.\n";
} else if(formval.substr(0,3) != "974") {
msg = "Invalid Mobile Number. \nMobile Number should begin with 974nnnnnnn.\n";
} else if (isNaN(formval)) {
msg = "Invalid Mobile Number. \nMobile Number should be numeric.\n";
}
return msg;
}
function checkCountryMobile(formCountryCode, formField) {
    var country = trim(formCountryCode.value);
    var formval = trim(formField.value);
    var msg = "";
    formval = country + formval;
    if(formval.length == 0) {
        msg = "Enter Mobile Number.\nMobile Number should begin with 974nnnnnnn.\n";
    } else if(formval.length < 9) {
    msg = "Invalid Mobile Number. \nMobile Number should not be less than 12 digits.\n";
} else if(formval.substr(0,3) != "974") {
msg = "Invalid Mobile Number. \nMobile Number should begin with 974nnnnnnn.\n";
} else if (isNaN(formval)) {
msg = "Invalid Mobile Number. \nMobile Number should be numeric.\n";
}
return msg;
}
/***
This function will disable  CTRL-C 

*/
function clearClipBoard()
{
    window.clipboardData.clearData();
    return true;
}

function verifyMobile(formField) {
    var formval = trim(formField.value);
    var msg = "";
    if (!isNaN(formval)) {
        msg = "Invalid Mobile Number. \nMobile Number should be numeric.\n";
    }
    return msg;
}

function textCounter(total,field, countfield, maxlimit) {
    
    val = field.value;
    //alert(val.length + ':' + val.trim());
    /*
    for (i = 0; i <= val.length; i++)
        {
    valchar = val.charAt(i);
    if ( valchar == "%")
        {
    if (val.charAt(i+2) == "%")
        {
    val1 = val.substr(0,i);
    val2 = val.substr(i+3,val.length-1);
    val = val1 + val2;
    }
    else if (val.charAt(i+3) == "%")
        {
    val1 = val.substr(0,i);
    val2 = val.substr(i+4,val.length-1);
    val = val1 + val2;	     
    }
    }
    }
    //Code ends
    if (val.length > maxlimit) // if too long...trim it!
        val = val.substring(0, maxlimit);
    else 
        countfield.value = maxlimit - val.length - total.value - 1;
    */
    countfield.value = maxlimit - val.length;
}

function checkSubmit(){
    return true;
}

function isUnicode(msgtext) 
{ 
    var intInputlen, charValue, charCode ; 
    intInputlen = msgtext.length ; 
    for (var intCtr = 0; intCtr <= intInputlen; intCtr++)
        { 
            charValue = msgtext.charAt(intCtr) ; 
            charCode = msgtext.charCodeAt(intCtr) ; 
            if(charCode > 127)
                return true;
        } 
        return false; 
    } 
    function disableRightClick(e)
    {
        var message = "Right click disabled";
        if(!document.rightClickDisabled) // initialize
            {
                if(document.layers) 
                    {
                        document.captureEvents(Event.MOUSEDOWN);
                        document.onmousedown = disableRightClick;
                    }
                    else document.oncontextmenu = disableRightClick;
                    return document.rightClickDisabled = true;
                }
                if(document.layers || (document.getElementById && !document.all))
                    {
                        if (e.which==2||e.which==3)
                            {
                                // alert(message);
                                return false;
                            }
                        }
                        else
                            {
                                //  alert(message);
                                return false;
                            }
                            
                        }
                        //disableRightClick();
                        
                        /***
                        This function will disable  CTRL-C 
                        
                        */
                        function onKeyDown() {
                            // current pressed key
                            var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();
                            
                            if (event.ctrlKey && (pressedKey == "c" || 
                                pressedKey == "v")) {
                            // disable key press porcessing
                            event.returnValue = false;
                        }
                        
                    } // onKeyDown
                    
                    /***
                    This function will check alphanumeric 
                    
                    */
                    function  CheckAlpha(str) {
                        
                        var valid = 0;
                        var valid1 = 0;
                        var valid2 = 0;
                        var GoodCharsNumber = '0123456789';
                        var GoodChars = 'abcdefghijklmnopqrstuvwxyz';
                        str = str.toLowerCase();
                        
                        var i = 0;
                        if (str=='') {
                            // Return false if number is empty
                            valid = '0';
                        }
                        for (i =0; i <= str.length -1; i++) {
                            if (GoodChars.indexOf(str.charAt(i)) >=0) {
                                valid1 = '1';
                                break;
                            } // End if statement
                        } // End for loop
                        for (i =0; i <= str.length - 1; i++) {
                            if (GoodCharsNumber.indexOf(str.charAt(i)) >=0) {
                                valid2 = '1';
                                break;
                            } // End if statement
                        } // End for loop
                        if ( (valid1)==1 && (valid2)==1 ){
                            valid = '1';
                        }
                        return valid;
                    }
                    /**
                    *  This function will create and return a multi diamentional array
                    *  to using in the javascript
                    *
                    */
                    function MultiDimensionalArray(iRows,iCols) 
                    { 
                        var i; 
                        var j; 
                        var a = new Array(iRows); 
                        for (i=0; i < iRows; i++) 
                            { 
                                a[i] = new Array(iCols); 
                                for (j=0; j < iCols; j++) 
                                    { 
                                        a[i][j] = ""; 
                                    } 
                                } 
                                return(a); 
                            } 
                            
                            function trim(inputString) 
                            {
                                var returnString = inputString;
                                removeChar = ' ';
                                if (removeChar.length)
                                    {
                                        while('' + returnString.charAt(0) == removeChar){
                                            returnString = returnString.substring(1, returnString.length);
                                        }
                                        while('' + returnString.charAt(returnString.length - 1) == removeChar) {
                                            returnString = returnString.substring(0, returnString.length - 1);
                                        }
                                    }
                                    return returnString;
                                }
                                
                                function endsWith(messageText, pattern) {
                                    if(pattern == (messageText.substring((messageText.length - (pattern.length))))) {
                                        return true;
                                    }else {
                                    return false;
                                }
                            }
                            
                            function validateMobileNumber(){
                                if(document.forms[0].mobileNumber.value == ''){
                                    setMessage('ERR1', 'userIdMessage', 'errormsg', 'Please enter the MobileNumber', null);
                                    return false;
                                }
                                else{
                                    searchByMobile();
                                }
                            }
                            function validateAccountNumber(){
                                if(document.forms[0].accountNumber.value == ''){
                                    setMessage('ERR1', 'userIdMessage', 'errormsg', 'Please enter the AccountNumber', null);
                                    return false;
                                }
                                else{
                                    searchByAccount();
                                }
                            }
                            
                            function echeck(str) {
                                
                                var at="@"
                                var dot="."
                                var lat=str.indexOf(at)
                                var lstr=str.length
                                var ldot=str.indexOf(dot)
                                if (str.indexOf(at)==-1){
                                    //alert("Invalid E-mail ID");
                                    return "Invalid E-mail ID"
                                }
                                
                                if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
                                    
                                    return "Invalid E-mail ID"
                                }
                                
                                if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
                                    //  alert("Invalid E-mail ID")
                                    return "Invalid E-mail ID";
                                }
                                
                                if (str.indexOf(at,(lat+1))!=-1){
                                    //  alert("Invalid E-mail ID")
                                    return "Invalid E-mail ID";
                                }
                                
                                if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
                                    //alert("Invalid E-mail ID")
                                    return "Invalid E-mail ID"
                                }
                                
                                if (str.indexOf(dot,(lat+2))==-1){
                                    //alert("Invalid E-mail ID")
                                    return "Invalid E-mail ID"
                                }
                                
                                if (str.indexOf(" ")!=-1){
                                    //alert("Invalid E-mail ID")
                                    return "Invalid E-mail ID"
                                }
                                
                                return "ERR0";					
                            }
                            
                            function ValidateForm(){
                                var emailID=document.frmSample.txtEmail
                                
                                if ((emailID.value==null)||(emailID.value=="")){
                                    alert("Please Enter your Email ID")
                                    emailID.focus()
                                    return false
                                }
                                if (echeck(emailID.value)==false){
                                    emailID.value=""
                                    emailID.focus()
                                    return false
                                }
                                return true
                            }
                            
                            
                            /*******************************************************************************
                            ===============================AJAX RELATED CODES===============================
                            *******************************************************************************/
                            /**
                            *  This method will return the XML Http Object to make the http request from
                            *  browser without submiting the page to the server, do the processing and return
                            *  the results in XML format.
                            *  Here, the objects are created after validating for the browser. If the browser
                            *  using is opera, netscape, moizilla, the line
                            *           xmlObject = new XMLHttpRequest();
                            *  will be executed. For IE 6 or later, xmlObject = new ActiveXObject("Msxml2.XMLHTTP");
                            *  and for earlier versions IE will make use of xmlObject = new ActiveXObject("Microsoft.XMLHTTP");
                            *
                            */
                            
                            function getXMLObject(){
                                var xmlObject = null;
                                var agent = navigator.userAgent.toLowerCase();
                                if (window.XMLHttpRequest) {
                                    xmlObject = new XMLHttpRequest();
                                } else if (window.ActiveXObject) {
                                if(agent.indexOf("msie 5")!=-1) {
                                    xmlObject = new ActiveXObject("Microsoft.XMLHTTP");
                                }else {
                                xmlObject = new ActiveXObject("Msxml2.XMLHTTP");
                            }
                        }
                        return xmlObject;
                    }
                    
                    /**
                    *  This will submit the request to the server and based upon the status change of the
                    *  XML Http object, this will call a local javascript function called 'callback'.
                    *  This function should be available in the local JSP file from where the AJAX call
                    *  is initiated.
                    *
                    */
                    
                    function sendRequest(xmlObject, Url) {
                        xmlObject.open("GET", Url, true);
                        xmlObject.onreadystatechange = callback;
                        xmlObject.send(null);
                    }
                    /**
                    *  This will submit the request to the server and based upon the status change of the
                    *  XML Http object, this will call a local javascript function called 'callback'.
                    *  This function should be available in the local JSP file from where the AJAX call
                    *  is initiated.
                    *
                    */
                    
                    function sendPostRequest(xmlObject, Url) {
                        xmlObject.open("POST", Url, true);
                        xmlObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                        xmlObject.onreadystatechange = callback;
                        xmlObject.send(null);
                    }
                    
                    /**
                    *  This is the method where the parsing for the search Customer number is happening.
                    *  This is directly getting the Tag Value of the tag 'message'. 
                    *  If there is multiple tag with same name, it can be processed in a loop which is
                    *  similiar to a normal for loop
                    */
                    
                    function parseMessage(xmlObject, divname, divclass, errormsg, object) {
                        message = xmlObject.responseXML.getElementsByTagName("message")[0];
                        setMessage(message.childNodes[0].nodeValue, divname, divclass, errormsg,object);
                    }
                    
                    /**
                    *  This method will compare the tag value passed. If the value is ERR1, this will
                    *  set the error message value to the div provided otherwise, it will set the value of the
                    *  variable to the object provided
                    */
                    
                    function setMessage(message, divname, divclass, errormsg,object) {
                        mdiv = document.getElementById(divname);
                        mdiv.innerHTML = "";
                        if (message == 'ERR1') {
                            mdiv.innerHTML = "<div class=\"" + divclass + "\"><strong>" + errormsg + "</strong></div><br>";
                        } else {
                        object.value = message;
                        document.forms[0].submit();
                    }
                }
                
                /**
                *  This function will clear the content of the specified div tag which is used to display
                *  either the error messages or the message contents. This can be call for any div tag 
                *  which is having an id set
                */
                
                function clearMessage(divname) {
                    mdiv = document.getElementById(divname);
                    if(mdiv.innerHTML.length > 0){
                        mdiv.innerHTML = "";
                    }
                }
                
                function MM_findObj(n, d) { //v4.01
                    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
                        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
                    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
                    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
                    if(!x && d.getElementById) x=d.getElementById(n); return x;
                }
                
                function MM_showHideLayers() { //v6.0
                    var i,p,v,obj,args=MM_showHideLayers.arguments;
                    for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
                        if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
                        obj.visibility=v; }
                }
                
                function copyRight() {
                    var time=new Date();
                    var year=time.getYear();
                    if (year<100) year="19" + time.getYear();
                    else year=time.getYear();
                    bodyText = '';
                    bodyText = '<table width="90%">' + 
                    '<TR> ' +
                    '<TD height=10 colSpan=2 bgColor="#082B8B"><img name="" src="" width="1" height="1" alt=""></STRONG></FONT></TD>' +
                    '</TR>' + 
                    '<tr><td><font size="2" face="Verdana" class="menu_current"><img border="0" src="images/coppyright.jpg" hspace="5" vspace="1" width="171" height="20" /></font>' + 
                    '</td>' + 
                    '<td align="right">' + 
                    '<font face="Verdana, Arial, Helvetica, sans-serif" size="1" >' + 
                    'Copyright © 2000-' + year + ' Ducont FZ-LLC. All rights reserved.</font>' + 
                    '</td>' +
                    '</tr>' +
                    
                    '<tr><td><font size="2" face="Verdana" class="menu_current"><img border="0" src="images/ducontLogo.gif" hspace="5" vspace="1" width="171" height="20" /></font>' + 
                    '</td>' + 
                    '<td align="center">' + 
                    '<font face="Verdana, Arial, Helvetica, sans-serif" size="2" >' + 
                    'For SMS pull alerts, please send service code along with keyword to <B>92610</B>."<br>eg.To get balance send BAL <Account Nickname> to 92610.' + 
                    '</td>' +
                    '</tr>' +
                    
                    
                    '</table>';
                    return bodyText;
                }
                
                function copyRight() {
                    var time=new Date();
                    var year=time.getYear();
                    if (year<100) year="19" + time.getYear();
                    else year=time.getYear();
                    bodyText = '';
                    bodyText = '<table width="90%">' + 
                    '<TR> ' +
                    '<TD height=10 colSpan=2 bgColor="#082B8B"><img name="" src="" width="1" height="1" alt=""></STRONG></FONT></TD>' +
                    '</TR>' + 
                    '<tr><td><!--<font size="2" face="Verdana" class="menu_current"><img border="0" src="images/coppyright.jpg" hspace="5" vspace="1" width="171" height="20" /></font> -->' + 
                    '</td>' + 
                    '<td align="right">' + 
                    '<!-- <font face="Verdana, Arial, Helvetica, sans-serif" size="1" >' + 
                    'Copyright © 2000-' + year + ' Ducont FZ-LLC. All rights reserved.</font> -->' + 
                    '</td>' +
                    '</tr>' +
                    
                    '<tr><td><font size="2" face="Verdana" class="menu_current"><img border="0" src="images/ducontLogo.gif" hspace="5" vspace="1" width="171" height="20" /></font>' + 
                    '</td>' + 
                    '<td align="center">' + 
                    '<font face="Verdana, Arial, Helvetica, sans-serif" size="2" >' + 
                    'For SMS pull alerts, please send service code along with keyword to <B>92610</B>."<br>eg.To get balance send BAL <Account Nickname> to 92610.' + 
                    '</td>' +
                    '</tr>' +
                    
                    
                    '</table>';
                    return bodyText;
                }
                
                function copyRight_old(color) {
                    var time=new Date();
                    var year=time.getYear();
                    if (year<100) year="19" + time.getYear();
                    else year=time.getYear();
                    bodyText = '';
                    bodyText = '<table width="90%">' + 
                    '<TR> ' +
                    '<TD height=10 colSpan=2 bgColor='+color+'><img name="" src="" width="1" height="1" alt=""></STRONG></FONT></TD>' +
                    '</TR>' + 
                    '<tr><td><font size="2" face="Verdana" class="menu_current"><img border="0" src="images/coppyright.jpg" hspace="5" vspace="1" width="171" height="20" /></font>' + 
                    '</td>' + 
                    '<td align="right">' + 
                    '<font face="Verdana, Arial, Helvetica, sans-serif" size="1" >' + 
                    'Copyright © 2000-' + year + ' Ducont FZ-LLC. All rights reserved.</font>' + 
                    '</td>' +
                    '</tr>' +
                    
                    '<tr><td><font size="2" face="Verdana" class="menu_current"><img border="0" src="images/ducontLogo.gif" hspace="5" vspace="1" width="171" height="20" /></font>' + 
                    '</td>' + 
                    '<td align="center">' + 
                    '<font face="Verdana, Arial, Helvetica, sans-serif" size="2" >' + 
                    'For SMS pull alerts, please send service code along with keyword to <B>92610</B>."<br>eg.To get balance send BAL <Account Nickname> to 92610.' + 
                    '</td>' +
                    '</tr>' +
                    
                    
                    '</table>';
                    return bodyText;
                }
                /*
                
                /*
                *This function validate customerNumber and account Number
                */
                
                function ValidateCustomer(object,flag){
                    if(flag == 1){
                        if(object.customerNo.value == '0'){
                            alert('Please provide a valid customer Number')
                            return false;
                        }else if(isNaN(object.customerNo.value)){
                        alert('Please provide a valid customer Number')
                        return false;
                    }else if(object.customerNo.value == ''){
                    alert('Please provide a valid customer Number')
                    return false;
                }else
                return true;
            }else{
            if(object.accountNumber.value == '0'){
                alert('Please provide a valid account Number')
                return false;
            }else if(isNaN(object.accountNumber.value)){
            alert('Please provide a valid account Number')
            return false;
        }else if(object.accountNumber.value == ''){
        alert('Please provide a valid accountNumber Number')
        return false;
    }else
    return true;
}

}



function validateServices(object) {
    var serviceFlag = true;
    var pullCounter = 0;
    for(var i = 0; i < object.enabledService.length;i++) {
        if(object.categoryId.value == 5) {
            if(object.enabledService[i].checked) {
                if(object.serviceCodes[pullCounter].value == ''){
                    object.serviceCodes[pullCounter].focus();
                    alert('Service code should not be blank for the selected pull services');
                    serviceFlag = false;
                    break;
                }else if(checkDupicate(object,object.serviceCodes[pullCounter].value)){
                object.serviceCodes[pullCounter].focus();
                alert('Service code cannot be same');
                serviceFlag = false;
                break;
            }
        }
        pullCounter++;
    }
}
return serviceFlag;
}

function checkDupicate(object,value){
    var i;
    var ctr = 0;
    for (i=0; i < object.serviceCodes.length; i++) {
        if (object.serviceCodes[i].value.toUpperCase() == value.toUpperCase()  ) {
            ctr++;
            if(ctr > 1)
                return true;
        }
    }
    return false;
}
function mt_pagetop()
{
    window.scrollTo(document.body.scrollLeft,0);
}  
function mt_pagedown()
{
    window.scrollBy(0, document.body.clientHeight);
}
function  convertToUCS2(dataToConvert){
    var ucs2='';
    var utf8 = dataToConvert.split("&#");
    var len = utf8.length;
    for(i=1; i<len; i++) {
        var str = utf8[i].substring(0,4);
        var j = parseInt(str)
        str = (j).toString(16)
        ucs2 = ucs2 + paddingUCS2(str);
    }
    return ucs2;
}
function paddingUCS2(data){
    if (data.length < 4){           
        for (var j=0;j<4-data.length ; j++){
            data = '0'+ data;
        }
    }
    return data;
}
function convertArabicTOUCS2(message){
    var ucs2='';
    for (var i = 0 ;i<message.length;i++){
        ucs2 = ucs2 + paddingUCS2(message.charCodeAt(i).toString(16));
    }
    return ucs2;
}

function openArabicKeyBoard(langaugeCode){
    langaugeCode = langaugeCode.split('~')[0]
    if(langaugeCode.split('~')[0] == 'AR'){
        windowobj = window.open("common/arabickeyboard.jsp?frmMessage=frmMessage", 'winKeyboard', 'left=300,top=200,width=438,height=188,locationbar=false,menubar=false,alwaysRaised=0');
        if (windowobj != null) {
            windowobj.focus();
        }
    }else{
    if (windowobj != null) {
        windowobj.close();
    }
}
}
function formatDates(x) {
    x = parseInt(x)
    return(x<0||x>9?"":"0")+x
}


function getCurrentDate(){
    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    curr_month++;//Month starts with 0
    var curr_year = d.getFullYear();
    
    return formatDates(curr_date) + "/" + formatDates(curr_month) + "/" + curr_year;   
}
function forwardError(){
           window.open("ErrorPage.do", "_top");
    
}