// JavaScript Document

function open_subscribe_window(elem,url,ref)
{
        url += '?email='+elem+'&ref='+ref;
        subscribewin = window.open(url,'_email_subscribe','height=565,width=490');
        subscribewin.focus();
}

function setEmail()
{
        var url = window.location.href;

        // Lets clean up the url and put the variables in an array
        url=url.replace(/%20/g,' ');
        url=url.substr(url.indexOf('?')+1);
        var url_arr=url.split('&');

        //Default values
        var email = 'Email Address';
        var source = 'education.org';

        for (var x in url_arr)
        {
            if (url_arr[x].match(/email=./)) {
                email = url_arr[x].substr(url_arr[x].indexOf('=')+1);
            }
            else if (url_arr[x].match(/ref=./)) {
                source = url_arr[x].substr(url_arr[x].indexOf('=')+1);
            }
        }

        document.getElementById('email').value = email;
        document.getElementById('source').value = source;

}

function doCheck() {
    var errorString = "";
    if (!document.emailCollectionForm.email.value) errorString += "Please enter value for \"Email Address\".\n";
    if (document.emailCollectionForm.email.value) {
        regExprEmail = new RegExp("^\\w+[a-zA-Z0-9\\.\\&\\+\\-\\%]*[a-zA-Z0-9]+\\&*\\+*\\@{1}[a-zA-Z0-9\\-\\_]+[\\.]+[a-zA-Z0-9\\.\\-\\_]*[a-zA-Z0-9]+$");
        returnEmailTest = regExprEmail.test(document.emailCollectionForm.email.value);
        if (!(returnEmailTest)) errorString += "Your email address is not valid. Please use your full email address.\n";
    }

    if (document.emailCollectionForm.email2.value != document.emailCollectionForm.email.value) errorString += "Please verify your \"Email Address\".\n";
    if (!document.emailCollectionForm.privacy.checked) errorString += "Please read our privacy policy and accept it\n";

    if (document.emailCollectionForm.zip.value) {
        regExprZip = /^\d{5}([\-]\d{4})?$/;
        returnZipTest = regExprZip.test(document.emailCollectionForm.zip.value);
        if (!(returnZipTest)) errorString += "Your zip is not valid.\n";
    }

    return errorString;
}

function handleSubmit () {
    var errorText = doCheck();
    if (errorText) {
        alert(errorText);
        return false;
    }
    else {
        return true;
    }
}
