/***********************************************
* Switch Menu script- by Martial B of http://getElementById.com/
* Modified by Dynamic Drive for format & NS4/IE4 compatibility
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var persistmenu="yes" //"yes" or "no". Make sure each SPAN content contains an incrementing ID starting at 1 (id="sub1", id="sub2", etc)
var persisttype="sitewide" //enter "sitewide" for menu to persist across site, "local" for this page only

if (document.getElementById){ //DynamicDrive.com change
    document.write('<style type="text/css">\n')
    document.write('.submenu{display: none;}\n')
    document.write('</style>\n')
}

function SwitchMenu(obj){
    if(document.getElementById){
        var el = document.getElementById(obj);
        var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
        if(el.style.display != "block"){ //DynamicDrive.com change
            for (var i=0; i<ar.length; i++){
                if (ar[i].className=="submenu") //DynamicDrive.com change
                    ar[i].style.display = "none";
            }
            el.style.display = "block";
        }else{
            el.style.display = "none";
        }
    }
}

function get_cookie(Name) { 
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        if (offset != -1) {
            offset += search.length
            end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            returnvalue=unescape(document.cookie.substring(offset, end))
        }
    }
    return returnvalue;
}

function onloadfunction(){
    if (persistmenu=="yes"){
        var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
        var cookievalue=get_cookie(cookiename)
        if (cookievalue!="")
            document.getElementById(cookievalue).style.display="block"
    }
}


function testMessageResult(responseText){

    document.testForm.output.value = responseText;

}



function savemenustate(){
    var inc=1, blockid=""
    while (document.getElementById("sub"+inc)){
        if (document.getElementById("sub"+inc).style.display=="block"){
            blockid="sub"+inc
            break
        }
        inc++
    }
    var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
    var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
    document.cookie=cookiename+"="+cookievalue
}

if (window.addEventListener)
    window.addEventListener("load", onloadfunction, false)
else if (window.attachEvent)
    window.attachEvent("onload", onloadfunction)
else if (document.getElementById)
    window.onload=onloadfunction

if (persistmenu=="yes" && document.getElementById)
    window.onunload=savemenustate



function showFileFormat(){
    var win = window.open('/websmpp/fileformat?width=400,height=275,scrollbars=yes');
}

function showFileFormatByName(){
    var win = window.open('/websmpp/fileformatByName?width=400,height=275,scrollbars=yes');
}

function validTo()
{

    var x= document.smsForm.destinationNumber;
    document.write(x);
    return true;
}




function msgCounter(messageType, message, charCount,specialMessage){
    var maxMsgLen = 0;
    var count=0;
    var tempMsg="";
    charCount.value = message.value.length;
    if(messageType.value=='English')
    {
        var msg=message.value;

        if (msg!="" || msg!=null)
        {

            //string = name;
            for (var i=0;i<msg.length;i++)
            {
                var asciiNum = msg.charCodeAt(i); //find ascci value of each character at i
                //check the ascii value  to see if it is to be counted double
                //alert(asciiNum);
               /* if (asciiNum=='224' || asciiNum=='232' || asciiNum=='233' || asciiNum=='12' ||asciiNum=='13'||asciiNum=='94' || asciiNum=='123' || asciiNum=='125' || asciiNum=='92' || asciiNum=='91' || asciiNum=='93' || asciiNum=='124' || asciiNum=='164' || asciiNum=='12' || asciiNum=='126' )
                {
                    count++;
                } */

                if (asciiNum=='12' ||asciiNum=='13'||asciiNum=='94' || asciiNum=='123' || asciiNum=='125' || asciiNum=='92' || asciiNum=='91' || asciiNum=='93' || asciiNum=='124' || asciiNum=='164' || asciiNum=='12' || asciiNum=='126' )
                {
                    count++;
                }

             /*   if (asciiNum=='224'){
                    tempMsg=tempMsg+"~";
                }else if(asciiNum=='232'){
                    tempMsg=tempMsg+"^";
                }else if(asciiNum=='233'){
                    tempMsg=tempMsg+"`";
                }else{
                   tempMsg=tempMsg+msg.charAt(i);
                } */

            }
        }
        charCount.value=msg.length+count;

        if( (x=(charCount.value)/160)<=1)
        {
            charCount.value=charCount.value +"  = " +"1 sms1";
        }
        else
        {
            x=(charCount.value)/153;
            var remender=(charCount.value)%153;
            if(parseInt(x)>0)
            {
                if(remender>0){
                    x=x+1;
                }
                charCount.value=charCount.value +"  = " +parseInt(x)+ "sms";
            }
        }
       specialMessage.value= tempMsg;
      // alert(tempMsg);

    }
	
    if(messageType.value=='Arabic')
    {
        if( (x=(charCount.value)/70)<=1)
        {
            charCount.value=charCount.value +"  = " +" 1 sms";
        }
        else
        {
            x=(charCount.value)/67;
            var remender=(charCount.value)%67;
            if(parseInt(x)>0)
            {
                if(remender>0){
                    x=x+1;
                }
                charCount.value=charCount.value +"  = " +parseInt(x)+ "sms";
            }
        }
  
    }
    if(messageType.value=='Unicode')
    {
        if( (x=(charCount.value)/280)<=1)
        {
            charCount.value=charCount.value +"  = " +" 1 sms";
        }
        else
        {
            x=(charCount.value)/268;
            var remender=(charCount.value)%268;
            if(parseInt(x)>0)
            {
                if(remender>0){
                    x=x+1;
                }
                charCount.value=charCount.value +"  = " +parseInt(x)+ "sms";
            }
        }
  
    }

    if(messageType.value=='Flash')
    {
        var msg=message.value;

        if (msg!="" || msg!=null)
        {

            //string = name;
            for (var i=0;i<msg.length;i++)
            {
                var asciiNum = msg.charCodeAt(i); //find ascci value of each character at i
                //check the ascii value  to see if it is to be counted double
                if (asciiNum=='224' || asciiNum=='232' || asciiNum=='233' || asciiNum=='94' || asciiNum=='123' || asciiNum=='125' || asciiNum=='92' || asciiNum=='91' || asciiNum=='93' || asciiNum=='124' || asciiNum=='164' || asciiNum=='12' || asciiNum=='126')
                {
                    count++;
                }
            }


        }




        charCount.value=msg.length+count;




        if (charCount.value>=161)
        {
            charCount.value="Length Exceeded !!";
        }
    }



    if(messageType.value=='Picture' || messageType.value=='BCard' || messageType.value=='WAPPush' || messageType.value=='RingTone')
    {
        if( (x=(charCount.value)/266)<=1)
        {
            charCount.value=charCount.value +"  = " +"1 sms1";
        }
        else
        {
            x=(charCount.value)/256;
            var remender=(charCount.value)%256;
            if(parseInt(x)>0)
            {
                if(remender>0){
                    x=x+1;
                }
                charCount.value=charCount.value +"  = " +parseInt(x)+ "sms";
            }
        }

    }
    if(messageType.value=='Logo')
    {
        if( (x=(charCount.value)/256)<=1)
        {
            charCount.value=charCount.value +"  = " +" 1 sms";
        }
        else
        {
            x=(charCount.value)/256;
            var remender=(charCount.value)%256;
            if(parseInt(x)>0)
            {
                if(remender>0){
                    x=x+1;
                }
                charCount.value=charCount.value +"  = " +parseInt(x)+ "sms";
            }
        }

    }



    if(messageType.value=='SpecialChar')
    {
        var msg=message.value;
        if (msg!="" || msg!=null)
        {
		
            //string = name;
            for (var i=0;i<msg.length;i++)
            {
                var asciiNum = msg.charCodeAt(i); //find ascci value of each character at i
                //check the ascii value  to see if it is to be counted double
                if (asciiNum=='224' || asciiNum=='232' || asciiNum=='233' || asciiNum=='94' || asciiNum=='123' || asciiNum=='125' || asciiNum=='92' || asciiNum=='91' || asciiNum=='93' || asciiNum=='124' || asciiNum=='164' || asciiNum=='12' || asciiNum=='126' )
                {
                    count++;
                }
            }
            charCount.value=msg.length+count;
            if( (x=(charCount.value)/160)<=1)
            {
                charCount.value=charCount.value +"  = " +"1 sms ";
            }
            else
            {
                x=(charCount.value)/153;
                var remender=(charCount.value)%153;
                if(parseInt(x)>0)
                {
                    if(remender>0){
                        x=x+1;
                    }
                    charCount.value=charCount.value +"  = " +parseInt(x)+ "sms";
                }
            }
        
        }

    }
}


function uploadComplete(responseText){
	
    var tokens = responseText.split(",");
    for(var index=0; index<tokens.length; index++){
        var	newOption = new Option(tokens[index], tokens[index], false, false);
        document.bulkUploadForm.elements['numberlist'].options[index] = newOption;
    }
    countNums();
}


function removeDuplicateNum(){
    var numar = new Array();
    var temp = document.bulkUploadForm.elements['numberlist'].options.length - 1;

    while(temp >= 0)
    {
        if(FastArraySearch(numar,document.bulkUploadForm.elements['numberlist'].options[temp].value) == -1)
        {
            numar.push(document.bulkUploadForm.elements['numberlist'].options[temp].value);
            numar.sort();
        }
        else
        {
            document.bulkUploadForm.elements['numberlist'].options[temp] = null;
        }
        temp--;
    }
    countNums();
}

function removeDuplicateNumBulkContact(){
    var numar = new Array();
    var temp = document.bulkContactUploadForm.elements['numberlist'].options.length - 1;
    
    while(temp >= 0)
    {
        if(FastArraySearch(numar,document.bulkContactUploadForm.elements['numberlist'].options[temp].value) == -1)
        {
            numar.push(document.bulkContactUploadForm.elements['numberlist'].options[temp].value);
            numar.sort();
        }
        else
        {
            document.bulkContactUploadForm.elements['numberlist'].options[temp] = null;
        }
        temp--;
    }
    countNumsBulkContact();
}


function countNums()
{
    document.getElementById("totalNumbers").value = document.bulkUploadForm.elements['numberlist'].options.length;
}

function countNumsBulkContact()
{
    document.getElementById("totalNumbers").value = document.bulkContactUploadForm.elements['numberlist'].options.length;
}

function removeOneNum(){ 
    var temp = document.bulkUploadForm.elements['numberlist'].options.length - 1;
    while(temp >= 0)
    {
        if (document.bulkUploadForm.elements['numberlist'].options[temp].selected==true) {
            document.bulkUploadForm.elements['numberlist'].options[temp] = null;
        }
        temp--;
    }
    countNums();
}

function removeOneNumBulkContact(){
    var temp = document.bulkContactUploadForm.elements['numberlist'].options.length - 1;
    while(temp >= 0)
    {
        if (document.bulkContactUploadForm.elements['numberlist'].options[temp].selected==true) {
            document.bulkContactUploadForm.elements['numberlist'].options[temp] = null;
        }
        temp--;
    }
    countNumsBulkContact();
}

function removeAllNum(){
    var temp = document.bulkUploadForm.elements['numberlist'].options.length - 1;
    while(temp >= 0){
        document.bulkUploadForm.elements['numberlist'].options[temp] = null;
        temp--;
    }
    countNums();
}

function removeAllNumBulkContact(){
    var temp = document.bulkContactUploadForm.elements['numberlist'].options.length - 1;
    while(temp >= 0){
        document.bulkContactUploadForm.elements['numberlist'].options[temp] = null;
        temp--;
    }
    countNumsBulkContact();
}

function FastArraySearch(vArray,vValue)
{
    var iStartIndex, iEndIndex, iMiddleIndex;

    iStartIndex = 0;
    iMiddleIndex = -1;
    iEndIndex = vArray.length - 1;
		
    while (true)
    {
        iMiddleIndex = iStartIndex + Math.ceil((iEndIndex - iStartIndex) / 2);
		
        switch(vValue)
        {
            case vArray[iStartIndex]:
                return iStartIndex;
            case vArray[iMiddleIndex]:
                return iMiddleIndex;
            case vArray[iEndIndex]:
                return iEndIndex;
        }
		
        if (iStartIndex == iMiddleIndex || iEndIndex == iMiddleIndex) return -1;
		
        if (vValue > vArray[iMiddleIndex])
        {
            iStartIndex = iMiddleIndex + 1;
        }
        else if (vValue < vArray[iMiddleIndex])
        {
            iEndIndex = iMiddleIndex - 1;
        }
		
    }
    return -1;
}


function showAddressBook(form)
{
    if(form=='smsForm')var y=setRequest('smsForm');
    if(form=='bulkUploadForm') var y=setRequest('bulkUploadForm');
    var url='/websmpp/action/showAddBookEntryList'+y;
    location.href=url;
} 	


function setRequest(form){ 
    if (form=='smsForm'){
        var from=document.smsForm.from.options.selectedIndex;
        var senderId=document.smsForm.senderId.value;
        var to=document.smsForm.destinationNumber.value;
        var msgType= document.smsForm.messageType.options.selectedIndex;
        var message=document.smsForm.message.value;
        var x='?from='+from+'&senderId='+senderId+'&to='+to+'&msgType='+
        msgType+'&message='+message+'&back=setupSendSmsForm';
        return x;

    }
    else if (form=='bulkUploadForm')
    {
        var from=document.bulkUploadForm.from.options.selectedIndex;

        var to=document.bulkUploadForm.destinationNumberFile.value;
        var senderId=document.bulkUploadForm.senderId.value;
        var msgType= document.bulkUploadForm.messageType.options.selectedIndex;
        var message=document.bulkUploadForm.message.value;
        var x='?from='+from+'&senderId='+senderId+'&to='+to+'&msgType='+
        msgType+'&message='+message+'&back=setupBulkUploadForm';
        ;
        return x;
    }


}

function showTemplates(form)
{
    alert(form);
    if(form=='smsForm')var y=setRequest('smsForm');
    if(form=='bulkUploadForm') var y=setRequest('bulkUploadForm');
    if(form=='bulkUploadForm1') var y=setRequest('bulkUploadForm1');
    if(form=='bulkContactUploadForm') var y=setRequest('bulkContactUploadForm');
    alert(y);
    var url='/websmpp/action/templatesList'+y;
    location.href=url;
} 	

function setValue(form)
{
    if(form=='smsForm'){
        var param=location.search.length;
        if(param==0){}
        else
        {
            num=findValue("num");
            if(num.length == 0){
                document.smsForm.destinationNumber.value=findValue("to");
                document.smsForm.message.value=findValue("msg");
            }
            else {
                document.smsForm.destinationNumber.value=findValue("num");
                document.smsForm.message.value=findValue("message");

            }

            document.smsForm.from.options.selectedIndex=findValue("from");
            document.smsForm.senderId.value=findValue("senderId");
            document.smsForm.messageType.options.selectedIndex=findValue("messageType");
        }
    }
    else if (form=='bulkUploadForm')
    {
        var param=location.search.length;
        if(param==0){}
        else
        {
            document.bulkUploadForm.message.value=findValue("msg");
            document.bulkUploadForm.destinationNumberFile.value=findValue("to");
            document.bulkUploadForm.from.options.selectedIndex=findValue("from");
            document.bulkUploadForm.senderId.value=findValue("senderId");
            document.bulkUploadForm.messageType.options.selectedIndex=findValue("messageType");
        }
    }
    else if (form=='bulkContactUploadForm')
    {
        var param=location.search.length;
        if(param==0){}
        else
        {
            document.bulkUploadForm.message.value=findValue("msg");
            document.bulkUploadForm.numberlist.value=findValue("to");
            document.bulkUploadForm.from.options.selectedIndex=findValue("from");
            document.bulkUploadForm.senderId.value=findValue("senderId");
            document.bulkUploadForm.messageType.options.selectedIndex=findValue("messageType");
        }
    }
}

function findValue(name)
{
    var start=location.search.indexOf("?"+name+"=");
    if (start<0) start=location.search.indexOf("&"+name+"=");
    if (start<0) return '';
    start += name.length+2;
    var end=location.search.indexOf("&",start)-1;
    if (end<0) end=location.search.length;
    var result=location.search.substring(start,end);
    var result='';
    for(var i=start;i<=end;i++) {
        var c=location.search.charAt(i);
        result=result+(c=='+'?' ':c);
    }
    return unescape(result);
}


function checkMessageLength(){
    var index = document.bulkUploadForm.messageType.selectedIndex;
    var type = document.bulkUploadForm.messageType[index].value;
    var typeChack = document.getElementById("typeChack");
    if((type=='WAPPush')||(type=='Picture')||(type=='Logo')||(type=='BCard')||(type=='RingTone')){
        typeChack.style.display="none";
    }else{
        typeChack.style.display="";
    }
}

///////////////////For Smsc Testing////////////////////////

function countNums1()
{
    document.getElementById("totalNumbers").value = document.personalisedSMSForm.elements['numberlist'].options.length;
}
function removeOneNum1(){
    var temp = document.personalisedSMSForm.elements['numberlist'].options.length - 1;
    while(temp >= 0)
    {
        if (document.personalisedSMSForm.elements['numberlist'].options[temp].selected==true) {
            document.personalisedSMSForm.elements['numberlist'].options[temp] = null;
        }
        temp--;
    }
    countNums1();
}

function removeAllNum1(){
    var temp = document.personalisedSMSForm.elements['numberlist'].options.length - 1;
    while(temp >= 0){
        document.personalisedSMSForm.elements['numberlist'].options[temp] = null;
        temp--;
    }
    countNums1();
}

function removeDuplicateNum1(){
    var numar = new Array();
    var temp = document.personalisedSMSForm.elements['numberlist'].options.length - 1;

    while(temp >= 0)
    {
        if(FastArraySearch(numar,document.personalisedSMSForm.elements['numberlist'].options[temp].value) == -1)
        {
            numar.push(document.personalisedSMSForm.elements['numberlist'].options[temp].value);
            numar.sort();
        }
        else
        {
            document.personalisedSMSForm.elements['numberlist'].options[temp] = null;
        }
        temp--;
    }
    countNums1();
}

function personalInfo(numberList,hashList,activate,destStatus,countRecord){
    //alert(destStatus);
    if((destStatus=="true")||(destStatus=="TRUE")){
        alert("File Format is not Correct.\n ERROR IN LINE NUMBER :::"+countRecord);
    }else{

        document.personalisedSMSForm.tempHashList.value = hashList;
        var tokens = numberList.split(",");

        for(var index=0; index<tokens.length; index++){
            var	newOption = new Option(tokens[index], tokens[index], false, false);
            document.personalisedSMSForm.elements['numberlist'].options[index] = newOption;
            if (index==0)
                document.getElementById("testing").value= tokens[index];
        }
        document.getElementById("totalNumbers").value = document.personalisedSMSForm.elements['numberlist'].options.length;
        // alert(""+activate);
        var col = activate.split(",");
        for(var i=0; i<col.length; i++)
        {
            //alert(col[i]);
            if (col[i]=='Name')
                document.getElementById("add_one").disabled=false;
            if (col[i]=='colOne')
                document.getElementById("add_two").disabled=false;
            if (col[i]=='colTwo')
                document.getElementById("add_three").disabled=false;

        }
    }
}

function checkMessageLength1(){
    var index = document.personalisedSMSForm.messageType.selectedIndex;
    var type = document.personalisedSMSForm.messageType[index].value;
    var typeChack = document.getElementById("typeChack");
    if((type=='WAPPush')||(type=='Picture')||(type=='Logo')||(type=='BCard')||(type=='RingTone')){
        typeChack.style.display="none";
    }else{
        typeChack.style.display="";
    }
}
	


