﻿function checkAll(Val)
{
  var ValChecked = Val.checked;
  var ValId = Val.id;
  var frm = document.forms[0];
  // Loop through all elements
  for (i = 0; i < frm.length; i++)
  {
    // Look for Header Template's Checkbox
    //As we have not other control other than checkbox we just check following statement
    //alert(frm.elements[i].id);
    if (this != null)
    {
      elm = frm.elements[i];
      elmid = elm.id;
      if (ValId.indexOf('CheckAll') !=  - 1)
      {
        // Check if main checkbox is checked,
        // then select or deselect datagrid checkboxes
        if (ValChecked)
        {
            if (elmid.indexOf('CheckBox') !=  - 1)
            {
                elm.checked = true;
            }
        }
        else
        {
            if (elmid.indexOf('CheckBox') !=  - 1)
            {
                elm.checked = false;
            }
        }
      }
      else if (ValId.indexOf('CheckBox') !=  - 1)
      {            
        // Check if any of the checkboxes are not checked, and then uncheck top select all checkbox
        if (frm.elements[i].checked == false)
        {
            document.getElementById("CheckAll").checked = false;
        }
      }              
    } // if
  } // for
} // function
function openFormatDiv(divID)
{
  document.getElementById(divID).style.display="block";
  document.getElementById(divID).style.textAlign="center";
}
function setPosition(ContainerDiv,w,h,w1,h1)
{
  if (document.body.scrollWidth == "1004")
  {
    document.getElementById(ContainerDiv).style.left=w1;
    document.getElementById(ContainerDiv).style.top=h1;
  }
  else
  {
    document.getElementById(ContainerDiv).style.left=w;
    document.getElementById(ContainerDiv).style.top=h;
  }
}
function copyValue(obj,val)
{
  document.getElementById(obj).value=val;
}

function makeContacts(obj,val)
{
    if (document.getElementById(obj).value == '')
        document.getElementById(obj).value = val;
    else
        document.getElementById(obj).value = document.getElementById(obj).value + ',' + val;
}

function showNoOfContacts()
{
    document.getElementById('ctl00_ContentPlaceHolder1_NoOfContactsLabel').innerText = document.getElementById('ctl00_ContentPlaceHolder1_GroupsDropDownList').value  + ' Contacts';
    //alert(document.getElementById('ctl00_ContentPlaceHolder1_NoOfContactsLabel').innerText);
}

function disableMobileField()
{
  if (document.getElementById('ctl00_ContentPlaceHolder1_GroupsDropDownList').value != 0)
  {
    document.getElementById('ctl00_ContentPlaceHolder1_MobileTextBox').disabled=true;
    document.getElementById('ctl00_ContentPlaceHolder1_GroupsDropDownList').disabled=false;
    document.getElementById('ctl00_ContentPlaceHolder1_MobileTextBox').style.backgroundColor="#dddddd";
  }
  else if (document.getElementById('ctl00_ContentPlaceHolder1_MobileTextBox').value != 0)
  {
    document.getElementById('ctl00_ContentPlaceHolder1_MobileTextBox').disabled=false;
    document.getElementById('ctl00_ContentPlaceHolder1_GroupsDropDownList').disabled=true;
    document.getElementById('ctl00_ContentPlaceHolder1_MobileTextBox').style.backgroundColor="#ffffff";
  }
}
function disableGroupField()
{
  if (document.getElementById('ctl00_ContentPlaceHolder1_MobileTextBox').value != "")
  {
    document.getElementById('ctl00_ContentPlaceHolder1_GroupsDropDownList').disabled=true;
  }
  else
  {
    document.getElementById('ctl00_ContentPlaceHolder1_GroupsDropDownList').disabled=false;
  }
}
function countNoOfChars(msgBox)
{
  var msgType = document.getElementById('ctl00_ContentPlaceHolder1_CreditsDropDownList').value;
  var simpleMsgChrs=0;
  var unicodeMsgChrs=0;
  if (msgType == 2)
  {
    simpleMsgChrs=140;
    unicodeMsgChrs=50;
  }
  else if(msgType == 3)
  {
    simpleMsgChrs=160;
    unicodeMsgChrs=70;
  }
  var unicodeFlag = 0;
  var extraChars = 0;
  var msgValue = msgBox.value;
  var strLength = msgValue.length;
  var firstLength = strLength;
    
  for (var i = 0; (!unicodeFlag && (i < firstLength)); i++) 
  {
    //alert(msgValue.charCodeAt(i));
    if ((msgValue.charAt(i) >= '0') && (msgValue.charAt(i) <= '9'))
    {
    }
    else if ((msgValue.charAt(i) >= 'A') && (msgValue.charAt(i) <= 'Z')) 
    {
    }
    else if ((msgValue.charAt(i) >= 'a') && (msgValue.charAt(i) <= 'z')) 
    {
    }
    else if (msgValue.charAt(i) == '@') 
    {
    }
    else if (msgValue.charAt(i) == '£') 
    {
    }
    else if (msgValue.charAt(i) == '$') 
    {
    }
    else if (msgValue.charAt(i) == '¥') 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xE8) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xE9) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xF9) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xEC) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xF2) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xC7) 
    {
    }
    else if (msgValue.charAt(i) == '\r') 
    {
    }
    else if (msgValue.charAt(i) == '\n') 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xD8) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xF8) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xC5) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xE5) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x394) 
    {
    }
    else if (msgValue.charAt(i) == '_') 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x3A6) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x393) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x39B) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x3A9) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x3A0) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x3A8) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x3A3) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x398) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x39E) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xC6) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xE6) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xDF) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xC9) 
    {
    }
    else if (msgValue.charAt(i) == ' ') 
    {
    }
    else if (msgValue.charAt(i) == '!') 
    {
    }
    else if (msgValue.charAt(i) == '\"') 
    {
    }
    else if (msgValue.charAt(i) == '#') 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xA4) 
    {
    }
    else if (msgValue.charAt(i) == '%') 
    {
    }
    else if (msgValue.charAt(i) == '&') 
    {
    }
    else if (msgValue.charAt(i) == '\'') 
    {
    }
    else if (msgValue.charAt(i) == '(') 
    {
    }
    else if (msgValue.charAt(i) == ')') 
    {
    }
    else if (msgValue.charAt(i) == '*') 
    {
    }
    else if (msgValue.charAt(i) == '+') 
    {
    }
    else if (msgValue.charAt(i) == ',') 
    {
    }
    else if (msgValue.charAt(i) == '-') 
    {
    }
    else if (msgValue.charAt(i) == '.') 
    {
    }
    else if (msgValue.charAt(i) == '/') 
    {
    }
    else if (msgValue.charAt(i) == ':') 
    {
    }
    else if (msgValue.charAt(i) == ';') 
    {
    }
    else if (msgValue.charAt(i) == '<') 
    {
    }
    else if (msgValue.charAt(i) == '=') 
    {
    }
    else if (msgValue.charAt(i) == '>') 
    {
    }
    else if (msgValue.charAt(i) == '?') 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xA1) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xC4) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xD6) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xD1) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xDC) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xA7) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xBF) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xE4) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xF6) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xF1) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xFC) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0xE0) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x391) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x392) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x395) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x396) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x397) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x399) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x39A) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x39C) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x39D) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x39F) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x3A1) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x3A4) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x3A5) 
    {
    }
    else if (msgValue.charCodeAt(i) == 0x3A7) 
    {
    }
    else if (msgValue.charAt(i) == '^') 
    {
       strLength++;
    }
    else if (msgValue.charAt(i) == '{') 
    {
       strLength++;
    }
    else if (msgValue.charAt(i) == '}') 
    {
       strLength++;
    }
    else if (msgValue.charAt(i) == '\\') 
    {
       strLength++;
    }
    else if (msgValue.charAt(i) == '[') 
    {
       strLength++;
    }
    else if (msgValue.charAt(i) == '~') 
    {
       strLength++;
    }
    else if (msgValue.charAt(i) == ']') 
    {
       strLength++;
    }
    else if (msgValue.charAt(i) == '|') 
    {
       strLength++;
    }
    else if (msgValue.charCodeAt(i) == 0x20AC) 
    {
       strLength++;
    }
    else 
    {
       unicodeFlag = 1;
    }
 }
 //alert(unicodeFlag);
 if (unicodeFlag == 1)
 {
    document.getElementById('ctl00_ContentPlaceHolder1_MessageType').value = "U";    
    if (strLength == 0)
    {
        document.getElementById('NoOfChars').value = 0;
        document.getElementById('ctl00_ContentPlaceHolder1_NoOfMsgsTextBox').value = 0;
    }    
    else if(strLength < unicodeMsgChrs)
    {
        document.getElementById('NoOfChars').value = (unicodeMsgChrs - strLength);
        document.getElementById('ctl00_ContentPlaceHolder1_NoOfMsgsTextBox').value = 1;
    }
    else if (strLength == unicodeMsgChrs)
    {
        document.getElementById('NoOfChars').value = 0;
        document.getElementById('ctl00_ContentPlaceHolder1_NoOfMsgsTextBox').value = 1;
    }
    else if(strLength > unicodeMsgChrs)
    {
        document.getElementById('NoOfChars').value = 0;
        document.getElementById('ctl00_ContentPlaceHolder1_NoOfMsgsTextBox').value = 1;    
        msgBox.value = msgBox.value.substring(0,unicodeMsgChrs);
    }
    //alert();
  }
  else
  {
    document.getElementById('ctl00_ContentPlaceHolder1_MessageType').value = "N";    
    if (strLength == 0)
    {
        document.getElementById('NoOfChars').value = 0;
        document.getElementById('ctl00_ContentPlaceHolder1_NoOfMsgsTextBox').value = 0;
    }    
    else if(strLength < simpleMsgChrs)
    {
        document.getElementById('NoOfChars').value = (simpleMsgChrs - strLength);
        document.getElementById('ctl00_ContentPlaceHolder1_NoOfMsgsTextBox').value = 1;
    }
    else if (strLength == simpleMsgChrs)
    {
        document.getElementById('NoOfChars').value = 0;
        document.getElementById('ctl00_ContentPlaceHolder1_NoOfMsgsTextBox').value = 1;
    }
    else if(strLength > simpleMsgChrs)
    {    
        document.getElementById('NoOfChars').value = 0;
        document.getElementById('ctl00_ContentPlaceHolder1_NoOfMsgsTextBox').value = 1;
        msgBox.value = msgBox.value.substring(0,simpleMsgChrs);        
    }
  }
}
function countMMChars(msgBox)
{
    var msgValue = msgBox.value;
    if (msgValue.length > 20)
    {
        msgValue = msgValue.substring(0,20);
        msgBox.value = msgValue;
        alert(msgValue.length);
    }
}
function validatePhoneNo(mobileNo)
{
    var mobileNo = document.getElementById(mobileNo).value;
    messageLabel = document.getElementById('ctl00_ContentPlaceHolder1_address1messagelabel');
    if (mobileNo == "")
    {
        messageLabel.innerText = "Please supply mobile no";
        return false;
    }
    else if(mobileNo.length < 9 || mobileNo.substring(0, 1) == "+" || mobileNo.substring(0, 2) == "00" || mobileNo.substring(0, 3) == "+00")
    {
        messageLabel.innerText = "Please enter complete mobile number, including, Country code, Network code and mobile number ie. 447981234567";
        return false;
    }
    else
    {
        return true;
    }    
}
function ShowPanel(panelID)
{
    document.getElementById(panelID).style.display = 'block';
    document.getElementById(panelID).style.left= (screen.width/2-200) + "px";
    document.getElementById(panelID).style.top='0px';
    var dock0=new dockit(panelID, 0);
}
function HidePanel(panelID)
{
    document.getElementById(panelID).style.display = 'none';
}

var offsetfromedge=0      //offset from window edge when content is "docked". Change if desired.
var dockarray=new Array() //array to cache dockit instances
var dkclear=new Array()   //array to cache corresponding clearinterval pointers

function dockit(el, duration)
{
    this.source=document.all? document.all[el] : document.getElementById(el);
    this.source.height=this.source.offsetHeight;
    this.docheight=truebody().clientHeight;
    this.duration=duration;
    this.pagetop=0;
    this.elementoffset=this.getOffsetY();
    dockarray[dockarray.length]=this;
    var pointer=eval(dockarray.length-1);
    var dynexpress='dkclear['+pointer+']=setInterval("dockornot(dockarray['+pointer+'])",100);';
    dynexpress=(this.duration>0)? dynexpress+'setTimeout("clearInterval(dkclear['+pointer+']); dockarray['+pointer+'].source.style.top=0", duration*1000)' : dynexpress;
    eval(dynexpress);
}

dockit.prototype.getOffsetY=function()
{
    var totaloffset=parseInt(this.source.offsetTop);
    var parentEl=this.source.offsetParent;
    while (parentEl!=null)
    {
        totaloffset+=parentEl.offsetTop;
        parentEl=parentEl.offsetParent;
    }
    return totaloffset;
}

function dockornot(obj)
{
    obj.pagetop=truebody().scrollTop;
    if (obj.pagetop>obj.elementoffset) //detect upper offset
        obj.source.style.top=obj.pagetop-obj.elementoffset+offsetfromedge+"px";
    else if (obj.pagetop+obj.docheight<obj.elementoffset+parseInt(obj.source.height)) //lower offset
        obj.source.style.top=obj.pagetop+obj.docheight-obj.source.height-obj.elementoffset-offsetfromedge+"px";
    else
        obj.source.style.top=0;
}

function truebody()
{
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}
function loadpage(url)
{
    document.getElementById('ctl00_ContentPlaceHolder1_apiframe').src = url;
}

function getposOffset(overlay, offsettype)
{
    var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
    var parentEl=overlay.offsetParent;
    while (parentEl!=null)
    {
        totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
        parentEl=parentEl.offsetParent;
    }
    return totaloffset;
}   
     
function overlay(curobj, subobjstr)
{
    var xpos=getposOffset(curobj, "left");
    var ypos=getposOffset(curobj, "bottom");
    document.getElementById(subobjstr).style.left=(xpos) + "px";
    document.getElementById(subobjstr).style.top=(ypos+5)+ "px";
    document.getElementById(subobjstr).style.display = "block";
}