
window.onerror = null;

function checkMailForm(thisForm) {
    var newWin;

    if (!thisForm.recipient.value) {
        thisForm.recipient.focus();
        loadWindow("You did not enter the e-mail address of the recipient.","E-mail Error", "error");
        return false;
    } else if (thisForm.recipient.value && !checkEmail(thisForm.recipient.value)) {
        thisForm.recipient.focus();
        loadWindow("The recipient's e-mail address is not valid.","E-mail Error", "error");
        return false;
    } else if (!thisForm.sender.value) {
        thisForm.sender.focus();
        loadWindow("You did not enter your e-mail address.","E-mail Error", "error");
        return false;
    } else if (thisForm.sender.value && !checkEmail(thisForm.sender.value)) {
        thisForm.sender.focus();
        loadWindow("Your e-mail address is not valid.","E-mail Error", "error");
        return false;
    }

    return true;
}

function checkEmail(addr) {
    addr        = addr.toLowerCase();
    atIndex     = addr.indexOf('@');
    dotIndex    = addr.lastIndexOf('.');
    legalNChars  = "abcdefghijklmnopqrstuvwxyz1234567890._-";
    legalDChars  = "abcdefghijklmnopqrstuvwxyz1234567890-";

    if (atIndex == -1 || atIndex == 0 || dotIndex == -1 || atIndex > dotIndex) {
        return false;
    }

    extension = addr.substring(dotIndex+1);
    if (extension.length < 2) {
        return false;
    }

    nameStr = addr.substring(0, atIndex);
    for (i = 0; i < nameStr.length; i++) {
        if (legalNChars.indexOf( nameStr.charAt(i) ) == -1) {
            return false;
        }

    }

    domainStr = addr.substring(atIndex+1, dotIndex);
    for (i = 0; i < domainStr.length; i++) {
        if (legalDChars.indexOf(domainStr.charAt(i)) == -1) {
            return false;
        }
    }


    return true;
}


var newWindow;
function loadWindow(msg, title, msgType) {
    var winWidth = 302;
    var winHeight = 174;
    
    var winX = (screen.width - winWidth)/2;
    var winY = (screen.height - winHeight)/2;
    var imgTag = "<img src='/images/panel/" + msgType + ".gif'" + " width=34 height=34 border=0>";

    //Specifiying image width and height as below speeds image rendering
    if (!newWindow || newWindow.closed) {
        var winString = "<html><head><title>" + title + "</title></head>\n";
        winString += "<body bgcolor='#ffe78f' text='#663300' marginwidth=10 marginheight=10 topmargin=10 rightmargin=10 leftmargin=10 bottommargin=10 onBlur='self.focus()'>\n";
        winString += "<form>\n\n";
        winString += "<table cellspacing=10 cellpadding=0, border=0 height=" + (winHeight-15) + ">\n";
        winString += "  <tr align=left valign=top>\n";
        winString += "    <td>" + imgTag + "</td>\n";
        winString += "    <td><font face='helvetica'>" + msg + "</font></td>\n";
        winString += "  </tr>\n";
        winString += "  <tr align=center valign=bottom>\n";
        winString += "    <td colspan=2><input type='button' type='submit' value='  OK  ' onClick='window.close()'></td>\n";
        winString += "  </tr>\n";
        winString += "</table>\n\n";
        winString += "</form>\n";
        winString += "</body></html>\n";

        // All attributes in third param are automatically turned off if one is specified:
        var winProps = "width=" + winWidth + ",height=" + winHeight + "screenX=" + winX + ",screenY=" + winY + ",left=" + winX + ",top=" + winY;
        newWindow = window.open("","newWin",winProps);
        newWindow.document.write(winString);
        newWindow.document.close();

        if (newWindow.opener == null) {
            newWindow.opener = self;
        }
    } else {
        newWindow.focus();
    }
}
