
function shreg() {
 var w = 300, h = 170;
 var xc = (window.screen.width - w)/2;
 var yc = (window.screen.height - h)/2;
 var args = "resizable=no,status=no,scrollbars=no,width=" + w + ",height=" + h;
 win = window.open ("register.php","" + new Date().getTime(), args);
 win.moveTo(xc, yc);
}

function showpasswd() {
 var w = 300, h = 170;
 var xc = (window.screen.width - w)/2;
 var yc = (window.screen.height - h)/2;
 var args = "resizable=no,status=no,scrollbars=no,width=" + w + ",height=" + h;
 win = window.open ("chpasswd.php","" + new Date().getTime(), args);
 win.moveTo(xc, yc);
}

function pinput() {
 var w = 320, h = 120;
 var xc = (window.screen.width - w)/2;
 var yc = (window.screen.height - h)/2;
 var args = "resizable=no,status=no,scrollbars=no,width=" + w + ",height=" + h;
 win = window.open ("padd.php","" + new Date().getTime(), args);
 win.moveTo(xc, yc);
}
function shlist() {
 var w = 250, h = 140;
 var xc = (window.screen.width - w)/2;
 var yc = (window.screen.height - h)/2;
 var args = "resizable=no,status=no,scrollbars=no,width=" + w + ",height=" + h;
 win = window.open ("list.php","" + new Date().getTime(), args);
 win.moveTo(xc, yc);
}
function pbulk() {
 if(trim(document.getElementById("message").value).length == 0) {
  showmsg("First compose a message", "red"); 
  document.getElementById("message").focus();
  setTimeout("clrmsg()", 3000); return;
 }
 if(trim(document.getElementById("sender").value).length == 0) {
  showmsg("Sender not set", "red"); 
  document.getElementById("sender").focus();
  setTimeout("clrmsg()", 3000); return;
 }
 var w = 220, h = 180;
 var xc = (window.screen.width - w)/2;
 var yc = (window.screen.height - h)/2;
 var args = "resizable=no,status=no,scrollbars=no,width=" + w + ",height=" + h;
 win = window.open ("bulk.php","" + new Date().getTime(), args);
 win.moveTo(xc, yc);
}
function shphbk() {
 var w = 600, h = 500;
 var xc = (window.screen.width - w) - 30;
 var yc = (window.screen.height - h)/2;
 var args = "resizable=no,status=no,scrollbars=no,width=" + w + ",height=" + h;
 win = window.open ("phonebk.php","" + new Date().getTime(), args);
 win.moveTo(xc, yc);
}

function pgroups() {
 var w = 500, h = 230;
 var xc = (window.screen.width - w)/2;
 var yc = (window.screen.height - h)/2;
 var args = "resizable=no,status=no,scrollbars=no,width=" + w + ",height=" + h;
 win = window.open ("groups.php","" + new Date().getTime(), args);
 win.moveTo(xc, yc);
}

function paddgroup() {
 var w = 310, h = 140;
 var xc = (window.screen.width - w)/2;
 var yc = (window.screen.height - h)/2;
 var args = "resizable=no,status=no,scrollbars=no,width=" + w + ",height=" + h;
 win = window.open ("addgroup.php","" + new Date().getTime(), args);
 win.moveTo(xc, yc);
}

function paddcontact() {
 var w = 310, h = 140;
 var xc = (window.screen.width - w)/2;
 var yc = (window.screen.height - h)/2;
 var args = "resizable=no,status=no,scrollbars=no,width=" + w + ",height=" + h;
 win = window.open ("addcontact.php","" + new Date().getTime(), args);
 win.moveTo(xc, yc);
}

function peditcontact(p) {
 var w = 310, h = 160;
 var xc = (window.screen.width - w)/2;
 var yc = (window.screen.height - h)/2;
 var args = "resizable=no,status=no,scrollbars=no,width=" + w + ",height=" + h;
 win = window.open ("editcontact.php?p=" + p, "" + new Date().getTime(), args);
 win.moveTo(xc, yc);
}

function shedit(gname, gdesc) {
 var w = 300, h = 120;
 var xc = (window.screen.width - w)/2;
 var yc = (window.screen.height - h)/2;
 var args = "resizable=no,status=no,scrollbars=no,width=" + w + ",height=" + h;
 var url = "editgrp.php?gname=" + encode(gname) + "&gdesc=" + encode(gdesc);
 win = window.open (url, "" + new Date().getTime(), args);
 win.moveTo(xc, yc);
}

function setlist() {
 var numbers = window.opener.document.getElementById("numbers").value;
 numbers = trim(numbers.substring(1, numbers.length-1));
 if(numbers.length == 1) return;
 var list = document.getElementById("list");
 numbers = numbers.split(",");
 for(var i=0; i<numbers.length; i++)
  list.options[list.options.length] = new Option(numbers[i]);
}

function remove() {
 var list = document.getElementById("list");
 var numbers = new Array();
 for(var i=0, j=0; i<list.options.length; i++)
  if(!list.options[i].selected) numbers[j++] = list.options[i].text;
 list.options.length = 0;
 for(var i=0; i<numbers.length; i++)
  list.options[list.options.length] = new Option(numbers[i]);
 /*update list*/
 numbers = "-";
 for(var i=0; i<list.options.length; i++)
  numbers += list.options[i].text + ",";
 window.opener.document.getElementById("numbers").value = numbers;
}

function clrmsg() {
 document.getElementById("notice").innerHTML = "&nbsp;";
}

function count(f, limit) {
 if(f.value.length > limit)
  f.value = f.value.substring(0, limit);
 else
  document.getElementById("chars").value = limit - f.value.length;
}

function ch(f){
 for(var i = 0; i < f.value.length; i++) {
  var ch = f.value.charAt(i);
  var t = ch =='0'|| ch =='1'|| ch =='2'|| ch =='3'|| ch =='4'||
          ch =='5'|| ch =='6'|| ch =='7'|| ch =='8'|| ch =='9';
  if(!t){ f.value = f.value.substring(0, i); return; }  
 }
}

/*function setedit(p, n, g) {
 window.opener.document.getElementById("editphone").value = p;
 window.opener.document.getElementById("newphone").value = p;
 window.opener.document.getElementById("editnames").value = n;
 var grps = window.opener.document.getElementById("editgname");
 for(var i=0; i<grps.options.length; i++){
  if(grps.options[i].text == g) {
   grps.options[i].selected = true;
   break;
  }
 }
 window.opener.document.getElementById("newphone").focus();
}*/

function addtolist(){
 var rows = document.getElementById("ctable").getElementsByTagName("tr");
 var numbers = "", c = 0;
 for(var i=1; i<rows.length -1; i++) {
  var cells = rows[i].getElementsByTagName("td");
  var tags = cells.item(0).getElementsByTagName("input");
  var cbox = tags.item(0);
  if(cbox.checked == true) { numbers += cbox.id + ","; c++; }
 }
 
 if(numbers.length == 0){ window.self.close(); return;} 
 numbers = numbers.substring(0, numbers.length-1);
 
 var field = window.opener.document.getElementById('phone');
 if(!field.disabled) {
  if(field.value.length > 0) {
   var str = trim(field.value);
   if(str.charAt(str.length-1) == ',')
    str = str.substr(0, str.length -1);
   field.value = str + ", " + numbers;
  }
  else field.value = numbers;
 }
 else {
  field = window.opener.document.getElementById('numbers');
  if(field.value.length > 0) {
   var str = trim(field.value);
   if(str.charAt(str.length-1) == ',')
    str = str.substr(0, str.length -1);
   field.value = str + ", " + numbers;
  }
  else field.value = numbers;  
 }
 showmsg("Added <font color=red>" + c + "</font> recipients to message", "green");
 setTimeout("window.self.close()", 2000);
}

function selectall(t) {
 var table = document.getElementById(t);
 var rows = table.getElementsByTagName("tr");
 for(var i=1; i<rows.length - 1; i++) /*last row has buttons*/{
  var cells = rows[i].getElementsByTagName("td");
  var tags = cells.item(0).getElementsByTagName("input");
  var cbox = tags.item(0);/*the only checkbox.*/
  cbox.checked = true;
 }
}

function ldphonebk() {
 document.getElementById("groups").selectedIndex=1;
 loadentries();
}

function chbtn (btn) { btn.style.backgroundColor = "#666666"; btn.style.color="#ffffff"}
function chbtn2 (btn) { btn.style.backgroundColor = "#ffffff"; btn.style.color="#666666"}

function uncheckall(t) {
 var table = document.getElementById(t);
 var rows = table.getElementsByTagName("tr");
 for(var i=1; i<rows.length - 1; i++) /*last row has buttons*/{
  var cells = rows[i].getElementsByTagName("td");
  var tags = cells.item(0).getElementsByTagName("input");
  var cbox = tags.item(0).checked = false;
 }
}

function chin(f)
{
 for(var i = 0; i < f.value.length; i++)
 {
  var ch = f.value.charAt(i);
  var t = ch =='0'|| ch =='1'|| ch =='2'|| ch =='3'|| ch =='4'||
          ch =='5'|| ch =='6'|| ch =='7'|| ch =='8'|| ch =='9' || ch ==',' || ch ==' ';
  if(t)
  {
   if(ch == ',')
   {
    if(f.value.charAt(i-1) == ',' ||  f.value.charAt(i-1) == ' ')
     f.value = f.value.substring(0, i);
   }
   else if(ch == ' ' && f.value.charAt(i-1) != ',')
    f.value = f.value.substring(0, i);
  }
  else
   f.value = f.value.substring(0, i);
 }
}

function input() {
 var input = "<span style='font-size: 10px; color: #317ADA'>separate with commas</span> | <a href='javascript:add()'>Add</a> | "+
 "<a href='javascript:hide()'>Hide</a> <br />"+
          "<input type='text' class='line' id='numbers' size='31' "+
		  "onkeydown='chin(this)' onkeyup='chin(this)' onblur='chin(this)'/>";
 document.getElementById("input").innerHTML = input;
}

function hide() {
 var input = "<a href='javascript:input()'>Add Contacts [+]</a>";
 if(document.getElementById("list").options.length > 0)
  input += " | <a href='javascript:remove()'>Remove [-]</a>";
 document.getElementById("input").innerHTML = input;
}

function add() {
 var t = trim(document.getElementById("numbers").value);
 if(t.charAt(t.length - 1) == ',') t = t.substring(0, t.length-1);
 var numbers = t.split(","), list = "", c = 0; 
 if(t.length == 0) { 
  showmsg("Input some numbers", "red"); 
  document.getElementById("numbers").focus();
  setTimeout("clrmsg()", 2000); return;
 }
 for(var i=0, j=0; i<numbers.length; i++) {
  var number = trim(numbers[i]);
  list += number + ","; c++;
  if(number.length < 10 || number.length > 15) {
   alert("Number not valid: " + number + "\nPlease correct it"); return;
  }
 }
 list = list.substring(0, list.length-1);
 var arr = list.split(",");
 list = document.getElementById("list");
 for(var i=0; i < arr.length; i++)
  list.options[list.options.length] = new Option(arr[i]);
 document.getElementById("numbers").value = "";
 document.getElementById("numbers").focus();
}

function add2 () {
 var numbers = trim(document.getElementById("phone").value);
 if(numbers.charAt(numbers.length - 1) == ',') numbers = numbers.substring(0, numbers.length-1);
 var arr = numbers.split(","); 
 if(arr.length == 0) return;
 var list = document.getElementById("list");
 for(var i=0; i < arr.length; i++)
  if(arr[i].length > 0)
  list.options[list.options.length] = new Option(trim(arr[i])); 
 document.getElementById("phone").value = '';
 document.getElementById("phone").disabled = true;
 document.getElementById("phone").className = "disabled";
}

function add3 () {
/* var numbers = "";
 var list = document.getElementById("list");
 if(list.options.length > 0) {
  //if(!confirm('The numbers in the list will be discarded. Are you sure?')) return;
  document.getElementById("phone").disabled = false;
  document.getElementById("phone").className = "line";
  for(var i=0; i < list.options.length; i++) {
   numbers += list.options[i].value + ","
  }
  numbers = numbers.substr(0, numbers.length-1);
  document.getElementById('phone').value = numbers;
 }*/
 
 document.getElementById("phone").className = "line";
 document.getElementById("phone").disabled = false;
 document.getElementById("bulk").innerHTML = '';
}

function add4(p) {
 var field = window.opener.document.getElementById('phone');
 if(!field.disabled) { 
  if(field.value.length > 0) {
   var str = trim(field.value);
   if(str.charAt(str.length-1) == ',')
    str = str.substr(0, str.length -1);
   field.value = str + ", " + p;
  }
  else field.value = p;
 }
 else {
  var field = window.opener.document.getElementById('numbers');
  if(field.value.length > 0) {
   var str = trim(field.value);
   if(str.charAt(str.length-1) == ',')
    str = str.substr(0, str.length -1);
   field.value = str + ", " + p;
  }
  else field.value = p;
 }
 window.self.close();
}

function trim(str) {
 while(str.substring(0,1) == ' ')
  str = str.substring(1, str.length);
 while (str.substring(str.length-1, str.length) == ' ')
  str = str.substring(0,str.length-1);
 return str;
}

function validemail(email) {
 var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
 if (filter.test(email))
  return true;
 else return false;
}

function showmsg(msg, color){
 document.getElementById("notice").innerHTML = 
 "<span style='color: " + color + "'>" + msg + "</span>";
}

function inlist(group, groups)
{
 for(var i=0; i<groups.options.length; i++)
  if(group == groups.options[i].text)
   return true;
 return false;
}
/*
function tt() { alert('ok');
var v = 
'<table width="221" border="0" cellpadding="0" cellspacing="0" class="general">\
	    <caption class="caption3">Add/Edit Recipients</caption>\
        <tr>\
          <td width="163" height="5" valign="top"></td>\
          <td width="58" valign="top"></td>\
        </tr>		\
        <tr>\
          <td height="22" valign="middle">\
		    <input name="text" type="text" class="line" id="numbers" onblur="chin(this)" \
		 onkeydown="chin(this)" onkeyup="chin(this)" size="25"/></td>\
        <td valign="middle">\
		    &nbsp;<a href="#" style="color: #0000ff" onclick="add(); return false;">[Add]</a></td>\
        </tr>\
        <tr>\
          <td rowspan="3" valign="top">\
		  <select name="list" multiple="multiple" class="list" id="list">\
          </select></td>\
        <td height="12"></td>\
        </tr>\
        <tr>\
          <td height="20" align="center" valign="middle">\
	        <a href="#" style="color: #ff0000" onclick="remove()">[Remove]</a></td>\
        </tr>\
        <tr>\
          <td height="18"></td>\
        </tr>\
        <tr>\
          <td height="19" colspan="2" valign="middle" nowrap="nowrap">\
	        <a href="#" onclick="shphbk(); return false;" style="color: #0000FF">Use phonebook</a> | \
			<a href="#" style="color: #ff0000">Cancel </a></td>\
        </tr>\
        <tr>
          <td height="7"></td>\
          <td></td>\
        </tr>\
 </table>';
 document.getElementById('bulk').innerHTML = v;
}
*/