<!--

/*-*-*-*-*-* This function is used for the localToolbar in the top page *-*-*-*-*-*/
function mhHover(tbl, idx, cls) {
  var t = document.getElementById(tbl);
  if (t == null) return;
  var d = t.getElementsByTagName("TD");
  if (d == null) return;
  if (d.length <= idx) return;
  d[idx].className = cls;
}

/*-*-*-*-*-* This function is to be used in the show/hide row contents with img *-*-*-*-*-*/
function ToggleInfoRow(Row, Img, From) {
  if (Row.style.display == '') {
    Row.style.display = 'none';
    if (From == 'Clients') {
  		Img.src = '../Images/IDS_Arrow_Down.gif'
    }else {
      Img.src = '../Images/show.jpg'
    }
  }else{
    Row.style.display = '';
		if (From == 'Clients') {
  		Img.src = '../Images/IDS_Arrow_Up.gif'
    }else {
      Img.src = '../Images/hide.jpg'
    }
  }
}

/*-*-*-*-*-* This function is to be used in the show/hide row contents without img *-*-*-*-*-*/
function ShowHideRow(Row) {
  if (Row.style.display == '') {
    Row.style.display = 'none';
  }else{
    Row.style.display = '';
  }
}

// This function is used to search site using google search for any keyword typed by the user
function searchSite() {
  var frm = document.forms[0];
  var keyword;
  var url;
  keyword = frm.site_search.value;
  if (keyword!="") {
    url = "http://www.google.com/custom?q=" + keyword;
    url = url + "&sitesearch=ids.com.lb&ie=windows-1256&oe=windows-1256";
    //url = "http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=";
    //url = url + keyword + " www.ids.com.lb"; //btnG=Google+Search
    window.location.href = url;
  }else{
    return;
  }
}

// This function is used in the login process
function login() {
	// form name = 'frmlogin'
  var frm = document.forms[0];
  if (frm.username.value=="") {
    alert("Please you must enter a username");
    frm.username.focus();
    return;
	}
  if (frm.password.value=="") {
    alert("Please you must enter a password");
    frm.password.focus();
    return;
	}
  frm.useraction.value = "login";
  frm.action = frm.from.value;
  frm.submit();
}

// This function is used to submit form when the user press enter
function submitEnter(myfield,myevent,myaction) {
  var keycode;
  if (window.event) keycode = window.event.keyCode;
  else if (myevent) keycode = myevent.which;
  else return true;

  if (keycode == 13) {
    if (myaction == "login") {
      login();
    }else if (myaction == "search_site") {
      searchSite();
    }else if (myaction == "forgot_password") {
      checkEmail();
    }else{
      myfield.form.submit();
    }
    return false;
  }
  else
     return true;
}

// This function is used to open a new window
function openNewWindow(URL, Title, Width, Height, Resizable) {
	NewWindow  = window.open(URL, Title, "height="+Height+",width="+Width+",toolbar=no,resizable="+Resizable+",scrollbars=yes,status=1,menubar=0,location=0");
	NewWindow.moveTo((screen.width-Width)/2,(screen.height-Height)/2-30);
	NewWindow.focus();
}

// This function is used to check if entered email address
// Used in the forgot password page
function checkEmail() {
  var frm = document.forms[0];
  if (frm.email_address.value=="") {
    alert("Please you must enter an email address!");
    frm.email_address.focus();
    return;
	}
	frm.action = "save_db_home.php";
  frm.submit();
}

// This function is used to check if a user has entered a valid mail address.
// Of course it does not check if the address actually exists.
/*
function isValidMail(email) {
  //var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
  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 isValidMail(emailStr) {
  var checkTLD=1;
  var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
  var emailPat=/^(.+)@(.+)$/;
  var specialChars= "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
  var validChars= "\[^\\s" + specialChars + "\]";
  var quotedUser="(\"[^\"]*\")";
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
  var atom=validChars + '+';
  var word="(" + atom + "|" + quotedUser + ")";
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
  var matchArray=emailStr.match(emailPat);
  if (matchArray==null) {
    alert("Your email address is missing or incorrectly spelled (check @ and .'s)");
    return false;
  }
  var user=matchArray[1];
  var domain=matchArray[2];
  for (i=0; i<user.length; i++) {
    if (user.charCodeAt(i)>127) {
      alert("Your email address contains invalid characters.");
      return false;
     }
  }
  for (i=0; i<domain.length; i++) {
    if (domain.charCodeAt(i)>127) {
      alert("Your  email domain name contains invalid characters.");
      return false;
     }
  }
  if (user.match(userPat)==null) {
    alert("Your email doesn't seem to be valid - check your spelling.");
    return false;
  }
  var IPArray=domain.match(ipDomainPat);
  if (IPArray!=null) {
    for (var i=1;i<=4;i++) {
      if (IPArray[i]>255) {
        alert("Destination IP address is invalid!");
        return false;
      }
    }
    return true;
  }
  var atomPat=new RegExp("^" + atom + "$");
  var domArr=domain.split(".");
  var len=domArr.length;
  for (i=0;i<len;i++) {
    if (domArr[i].search(atomPat)==-1) {
      alert("Your email address does not seem to be valid - check your spelling, including wrong use of commas, or a full point [.] at the end of the address .");
      return false;
     }
  }
  if (checkTLD && domArr[domArr.length-1].length!=2 &&
  domArr[domArr.length-1].search(knownDomsPat)==-1) {
    alert("Your email address must end in a well-known domain or two letter " + "country.");
    return false;
  }
  if (len<2) {
    alert("Your email address is missing a hostname - check spelling.");
    return false;
  }
  return true;
}


// This function is used to subscribe (1) or unsubscribe (0) a certain email address
function subscribeMe(subs) {
  var frm = document.forms[0];
  var email = frm.nl_email.value;
  if (email != "") {
    if (!isValidMail(email)) {
      frm.nl_email.focus();
    }else{
      if (subs == 1) frm.useraction.value = "subscribe";
      else frm.useraction.value = "unsubscribe";
      frm.action = "../Newsletter/subscribtion.php";
      frm.submit();
    }
  }else{
    return false;
  }
}

function checkClientInfo() {
  var frm = document.forms[0];
  if (frm.client_name.value=="") {
    alert("Please you must enter the client name!");
    frm.client_name.focus();
    return;
	}
  if (frm.category_name.value=="-1") {
    alert("Please you must specify the category of this client!");
    frm.category_name.focus();
    return;
	}
	
  frm.action = "save_db.php";
  frm.submit();
}

//-->


