function openMail()
{
     width   = screen.width-15;
     height = screen.height-160;
     mailWind = open("http://enmail.ems.org.eg/cgi-bin/engemswebmail?noframes=1","email","toolbar=yes,location=no,scrollbars=yes,directories=no,status=no,menubar=yes,resizable=yes,top=0,left=0,width="+width +",height="+height);
}

function signup()
{
     width   = screen.width-15;
     height = screen.height-160;
     open("http://www.ems.org.eg/email.php","email","toolbar=yes,location=no,scrollbars=yes,directories=no,status=no,menubar=yes,resizable=yes,top=0,left=0,width="+width +",height="+height);
}

function forgotPass()
{
     width   = screen.width-15;
     height = screen.height-160;
     mailWind = open("forgot_pass_frm.php","email","toolbar=yes,location=no,scrollbars=yes,directories=no,status=no,menubar=yes,resizable=yes,top=0,left=0,width="+width +",height="+height+", left=0,right=0");
}

function logToMail(username, password)
{
     width   = screen.width-15;
     height = screen.height-160;
     url = "redirect.php?username=" + username + "&password=" + encode(password);
     open(url ,"email","toolbar=yes,location=no,scrollbars=yes,directories=no,status=no,menubar=yes,resizable=yes,top=0,left=0,width="+width+",height="+height);
}

function validateLogin()
{
   username = trim(document.forms[0].username.value);
   password  = trim(document.forms[0].password.value);
   if (username == '' || password == '')
   {
      alert("من فضلك أدخل إسم المستخدم وكلمة المرور");
      return false;
   }
   logToMail(username, password);
   document.forms[0].username.value = '';
   document.forms[0].password.value = '';
   return false;
}

function trim(string)
{
  var string= new String(string),
  re = /(^\s*)/;
  string = string.replace(re, "");


  re = /\s*$/;
  string = string.replace(re, "");
  //alert("A" + string + "A");
  return string;
}

var base64 = [
       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',   //  0 to  7
       'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',   //  8 to 15
       'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',   // 16 to 23
       'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',   // 24 to 31
       'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',   // 32 to 39
       'o', 'p', 'q', 'r', 's', 't', 'u', 'v',   // 40 to 47
       'w', 'x', 'y', 'z', '0', '1', '2', '3',   // 48 to 55
       '4', '5', '6', '7', '8', '9', '+', '/' ]; // 56 to 63

function charFromCharCode (charCode) {
    return unescape('%' + charCode.toString(16));
}

function reverseBase64 () {
  var r = new Object();
  for (var i = 0; i < 64; i++) {
    r[base64[i]] = i;
  } return r;
}

var reversedBase64 = reverseBase64();

function decode (encStr) {
    var charCodes = new Array();
    var decStr = "";

    /* charCodes contains the index values into the base64 array
       for each character in the encoded string */
    for (var i = 0; i < encStr.length; i++)
        charCodes[i] = reversedBase64[encStr.charAt(i)];

    for (var i = 0; i < encStr.length; i += 4) {
        /* bits24 is 4 groups of 6-bit character indexes */
        var bits24  = ( charCodes [i]     & 0xFF  ) <<  18;
            bits24 |= ( charCodes [i + 1] & 0xFF  ) <<  12;
            bits24 |= ( charCodes [i + 2] & 0xFF  ) <<   6;
            bits24 |= ( charCodes [i + 3] & 0xFF  ) <<   0;

        /* grab the character for the first 8 bits by masking off the
           last 16 bits and then shifting right */
        decStr     += charFromCharCode((bits24 & 0xFF0000) >> 16);

        /* if the next characer is a pad character, there won't be a
charCode
           for it; so charCodes[] will return false and the character
won't
           be decoded. */

        /* grab the character for the second 8 bits by masking off the
           last 8 bits and then shifting right */
        if (charCodes[i + 2])  // check for padding character =
            decStr += charFromCharCode((bits24 &   0xFF00) >>  8);

        /* grab the character for the last 8 bits */
        if (charCodes[i + 3])  // check for padding character =
            decStr += charFromCharCode((bits24 &     0xFF) >>  0);
   } return decStr;
}

function encode (Str) {
    var charCodes = new Array();
    var encStr = "";

    for (var i = 0; i < Str.length; i += 3) {
        /* grab groups of three characters, 24 bits, then split into
           4 groups of 6 bits, and use each group as an index into
           base64[] to get the encoded character */

        /* bits24 is 3 groups of 8-bit characters */
        var bits24  = ( Str.charCodeAt(i)     ) <<  16;

        if ( Str.substr(i + 1, i + 2) ) {
            bits24 |= ( Str.charCodeAt(i + 1) ) <<   8;
        }
        else {
            bits24 |= ( 0 )                     <<   8;
        }

        if ( Str.substr(i + 2, i + 3) ) {
            bits24 |= ( Str.charCodeAt(i + 2) ) <<   0;
        }
        else {
            bits24 |= ( 0 )                     <<   0;
        }

        encStr += base64[ (bits24 >>> 18) & 0x3F ];

        if ( (bits24 >>> 12) & 0x3F ) {
            encStr += base64[ (bits24 >>> 12) & 0x3F ];
        }
        else {
            encStr += '=';
        }

        if ( (bits24 >>> 6) & 0x3F ) {
            encStr += base64[ (bits24 >>>  6) & 0x3F ];
        }
        else {
            encStr += '=';
        }

        if ( (bits24 >>> 0) & 0x3F ) {
            encStr += base64[ (bits24 >>>  0) & 0x3F ];
        }
        else {
            encStr += '=';
        }
   } return encStr;
}

