//TODO write functions in new format so the code lloks cleaner and more standard
//Sample below
//Benefit
//Function call is like builtin function For E.G myVar = mVar.function(x,y) instead of myVar = function(myVar,x,y)

String.prototype.endsWith = function(str)
{return ( this.match(str+"$")==str )}

String.prototype.trim = function()
{return this.replace(/(^\s*)|(\s$)/g, "");}

Array.prototype.has = function(value) {
var i;
for (var i = 0, loopCnt = this.length; i < loopCnt; i++) {
if (this[i] === value) {return true;}
}
return false;
};

function readFile(file)
{
//
//-----------------------------------------------------------------------------/
//       Check that the user has permission to read the file                   /
//-----------------------------------------------------------------------------/
//
  alert("readFile : "+file);
  var fso = new ActiveXObject("Scripting.FileSystemObject");
  if (!(fso.FileExists(file)))
  {
     return "";
  }
  else
  {
//
//-----------------------------------------------------------------------------/
//       Read the file                                                         /
//-----------------------------------------------------------------------------/
//
     var textStream = fso.OpenTextFile(file);
     var data = textStream.ReadAll();
     alert(data);
     textStream.Close();
     return data;
  }
}
 
function loadFile(fName)
{
  alert("loadFile : "+fName);
  fileName = document.getElementById(fName).value;
  alert("loadFile : "+fileName);
  fContents = readFile(fileName);
  alert("loadFile : "+fContents);
  return false;
}


function selectall(dd) 
{
  var dropdlist = document.getElementById(dd);
  for (i = 0; i < dropdlist.options.length; i++) { 
      dropdlist.options[i].selected=true; 
  }
}
function updateListCount(dd,tb)
{
  document.getElementById(tb).value=document.getElementById(dd).length;  
}

function importEmails(id) 
{
  var menus = ["copyEmails","importEmails"];    
  thisDiv = document.getElementById(menus[id]);
  if (thisDiv) 
  {
    for (i=0; i < menus.length; i++)
    {       
      if (i == id)
        thisDiv.style.display = "block";
      else
        document.getElementById(menus[i]).style.display = "none";
    }
  }
}

function listToArray(list)
{
  var selObj = document.getElementById(list);
  var listArray = new Array();
  for (i=0; i<selObj.options.length; i++) {
    listArray[i]=selObj.options[i].value;
  }

  return listArray;
}

function getCheckedValue(radioObj)
{
  var l = radioObj.length;
  if(l == undefined)
    if(radioObj.checked)
      return radioObj.value
    else
      return "";
  for( var i=0; i<l;i++)
  {
    if(radioObj[i].checked)
      return radioObj[i].value;
  }
}

function checkAndTansfer(tarea, delim, dd, clear)
{
  var lArr = listToArray("selCheck[]");
  //Get contents of textarea
  var del = document.getElementById(delim).value;
  
  //var text_content = document.getElementById(tarea).value;
  var invalid = 0;
  var Egmail = 0
  var Ehotmail = 0;
  var Eyahoo = 0;
  var Eothers = 0;
  //Clear DropDownlist
  if (clear == 1)
    document.getElementById(dd).length = 0

  radioObj = document.getElementById("list");
  var method = getCheckedValue(radioObj);

  if (method == "0")
      text_content = document.getElementById('to').value
  else
      text_content = document.getElementById('h_to').value
  //Store in array - break by delimiter
    if (text_content == "")
      alert("Please Provide Emails");
    else
      var array_content = text_content.split(del);
      
  //Transfer to drop down box
  for (var i=0; i<array_content.length; i++)
  {
    var lemail = array_content[i];

    if (validate(lemail))
    {
    
      if(lArr.has(lemail))
      {
        alert(lemail+" already exists in the email list");
        invalid++;
      }
      else
      {
        if(lemail.endsWith("@gmail.com"))
          Egmail++;
        else if(lemail.endsWith("@hotmail.com"))
          Ehotmail++;
        else if(lemail.endsWith("@yahoo.com"))
          Eyahoo++;
        else
          Eothers++;  
          
        addOption( document.getElementById(dd),lemail,lemail );
        //Add new emails to array for further comparison
        lArr[lArr.length]= lemail; 
      }
    }
    else
      invalid++;
    //Select All
    //selectall(dd);
  }
  //Update Results
  document.getElementById('total').value = array_content.length;
  document.getElementById('invld').value = invalid;
  document.getElementById('vld').value = array_content.length - invalid;
  //Email Break Down
  document.getElementById('gmail').value = Egmail;
  document.getElementById('hotmail').value = Ehotmail;
  document.getElementById('yahoo').value = Eyahoo;
  document.getElementById('other').value = Eothers;
}

function validate(email) 
{
   var status = true;
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   
   if(reg.test(email) == false) {
      status = false;
   }

   return status;
}

function addOption(selectbox,text,value )
{
  var optn = document.createElement("OPTION");
  optn.text = text;
  optn.value = value;
  selectbox.options.add(optn);
}

function addtoEndOption(dd)
{
  //
  selectbox = document.getElementById(dd);
  var optn = document.createElement("OPTION");
  optn.text = document.getElementById("to").value;
  optn.value = document.getElementById("to").value;
  selectbox.options.add(optn);
}

function removeOption(dd)
{
  selectbox = document.getElementById(dd);
  var i;
  var s = document.getElementById("esize").value;
  for(i=selectbox.options.length-1;i>=0;i--)
  {
    if(selectbox.options[i].selected)
    {
      selectbox.remove(i);
      s--;
    }
  }
}

function editOption(dd)
{
  selectbox = document.getElementById(dd);
  var i;
  for(i=selectbox.options.length-1;i>=0;i--)
  {
    if(selectbox.options[i].selected)
    {
      document.getElementById("updateText").value = selectbox.options[i].text;
    }
  }
}

function editList(dd)
{
  selectbox = document.getElementById(dd);
  var i;
  for(i=selectbox.options.length-1;i>=0;i--)
  {
    if(selectbox.options[i].selected)
    {
      var id = selectbox.options[i].value;
      location.href='index.php?email=edit&id='+id;
    }
  }
}

function editMessage(dd)
{
  selectbox = document.getElementById(dd);
  var i;

  for(i=selectbox.options.length-1;i>=0;i--)
  {
    
    if(selectbox.options[i].selected)
    {
      var id = selectbox.options[i].value;
      location.href='index.php?mess=edit&id='+id;
    }
  }
}

function changeOptionValue(dd)
{
  selectbox = document.getElementById(dd);
  var i;
  for(i=selectbox.options.length-1;i>=0;i--)
  {
    if(selectbox.options[i].selected)
    {
      selectbox.options[i].text = document.getElementById("updateText").value;
    }
  }
}

function test()
{
  alert(1);
}

function ddOnChange(dropdown,tf)
{
  var i = dropdown.selectedIndex;
  
  if(i == 0)
    var selVal = '';
  else
    var selVal = dropdown.options[i].text;
  
  document.getElementById(tf).value = selVal;
}

function setQueueVars(dropdown)
{
  var i = dropdown.selectedIndex;
  
  if(i == 0)
    var selVal = '';
  else
    var selVal = dropdown.options[i].value;
  
  document.getElementById("txtSize").value = selVal.split("-").slice(1);
}

function loadXmlHttp(url, id) {
var f = this;
f.xmlHttp = null;
/*@cc_on @*/ // used here and below, limits try/catch to those IE browsers that both benefit from and support it
/*@if(@_jscript_version >= 5) // prevents errors in old browsers that barf on try/catch & problems in IE if Active X disabled
try {f.ie = window.ActiveXObject}catch(e){f.ie = false;}
@end @*/
if (window.XMLHttpRequest&&!f.ie||/^http/.test(window.location.href))
f.xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method
else if (/(object)|(function)/.test(typeof createRequest))
f.xmlHttp = createRequest(); // ICEBrowser, perhaps others
else {
f.xmlHttp = null;
// Internet Explorer 5 to 6, includes IE 7+ when local //
/*@cc_on @*/
/*@if(@_jscript_version >= 5)
try{f.xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
catch (e){try{f.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){f.xmlHttp=null;}}
@end @*/
}
if(f.xmlHttp != null){
f.el = document.getElementById(id);
f.xmlHttp.open("GET",url,true);
f.xmlHttp.onreadystatechange = function(){f.stateChanged();};
f.xmlHttp.send(null);
}
}


loadXmlHttp.prototype.stateChanged=function () {
if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !/^http/.test(window.location.href)))
this.el.innerHTML = this.xmlHttp.responseText;
}

var requestTime = function(){
new loadXmlHttp('time.php', 'timeDiv');
setInterval(function(){new loadXmlHttp('time.php?t=' + new Date().getTime(), 'timeDiv');}, 5000);
}

if (window.addEventListener)
window.addEventListener('load', requestTime, false);
else if (window.attachEvent)
window.attachEvent('onload', requestTime);