/*
This script includes common funtions.
*/
function captureEmail() {
    name = document.captureEmailInfoForm.name.value;
    sendCopy = document.captureEmailInfoForm.sendCopy.value;

    if (document.captureEmailInfoForm.sendCopy.checked) {
        document.captureEmailInfoForm.sendParam.value = "ON";
    } else {
        document.captureEmailInfoForm.sendParam.value = "OFF";
    }


    fromEmail = document.captureEmailInfoForm.fromEmail.value;
    toEmail = document.captureEmailInfoForm.toEmail.value;

    if (fromEmail == "") {
        alert("Please Specify E-mail Address");
        document.captureEmailInfoForm.fromEmail.focus();
        return;
    }


    if (checkEmail(fromEmail) == false) {
        document.captureEmailInfoForm.fromEmail.focus();
        return;
    }

    if (toEmail == "") {
        alert("Please Specify E-mail Address");
        document.captureEmailInfoForm.toEmail.focus();
        return;
    }



    var emailList = toEmail.split(',');

    for (var i = 0; i < emailList.length; i++) {
        var emailAdr = emailList[i];
        //alert(emailAdr);
        if (checkEmail(emailAdr) == false) {
            document.captureEmailInfoForm.toEmail.focus();
            return;
        }

    }

    document.captureEmailInfoForm.submit();
}

function captureSFInfo() {

    fromEmail = document.captureSFInfoForm.fromEmail.value;
    confEmail = document.captureSFInfoForm.confEmail.value;

    if (checkEmail(fromEmail) == false) {
        document.captureSFInfoForm.fromEmail.focus();
        return;
    }

    if (checkEmail(confEmail) == false) {
        document.captureSFInfoForm.confEmail.focus();
        return;
    }

    if (fromEmail != confEmail) {
        alert("Email addresses do not match.");
        return;
    }

    //uncomment if zip field is mandatory
    //        if (document.captureSFInfoForm["zip"].value == "") {
    //            alert("Please enter a zip code!");
    //            return;
    //        }

    fullName = document.captureSFInfoForm.full_name.value;
    arrayOfNames = fullName.split(" ");

    //comment out if first name and last name are NOT mandatory
    if (fullName == "" || arrayOfNames.length == 0 || arrayOfNames.length == 1) {
        alert("Please enter a first name and a last name!");
        return;
    }

    firstName = arrayOfNames[0];

    lastName = arrayOfNames[arrayOfNames.length - 1];

    middleName = "";

    for (num = 1; num < arrayOfNames.length - 1; num++) {

        middleName = middleName + arrayOfNames[num] + " ";
    }


    document.captureSFInfoForm["first_name"].value = firstName;
    document.captureSFInfoForm["middle_name"].value = middleName;
    document.captureSFInfoForm["last_name"].value = lastName;


    var interestDD = document.getElementById('00N40000001rGee');
    var isInterestDDSelected = false;
    var len = interestDD.length;

    for (var counter = 0; counter < len; counter++) {
        tmpOptionObj = interestDD.options[counter];

        if (tmpOptionObj.selected == true && tmpOptionObj.value != "-1") {
            isInterestDDSelected = true;
        }

        if (tmpOptionObj.selected == true && tmpOptionObj.value == "Other") {
            otherValue = document.captureSFInfoForm["00N40000001rPIr"].value;
            if (otherValue == "") {
                alert("Please enter other passion");
                return;
            }
        }
    }

    if (!isInterestDDSelected) {
        alert("Please select your passions");
        return;
    }


    var hearAboutDD = document.getElementById('00N40000001rGej');

    if (hearAboutDD.value == "-1") {
        alert("Please enter a value for how you heard about us");
        return;
    }

    if (hearAboutDD.value == "Other") {
        otherValue = document.captureSFInfoForm["00N40000001rGek"].value;
        if (otherValue == "") {
            alert("Please enter a value for how you heard about us");
            return;
        }

    }

    SetCookie("registered", "Y", 30);

    document.captureSFInfoForm.submit();
}


function cancelCaptureEmail() {
    document.captureEmailInfoForm.reset();
    hideDiv("inviteFriendsBlock");
}

function cancelCaptureSFInfo() {
    document.captureSFInfoForm.reset();
}

function DivShowOrHide(divId) {
    var divstyle = new String();

    divstyle = document.getElementById(divId).style.display;

    if (divstyle == '' || divstyle == 'none') {
        showDiv(divId);
    } else {
        hideDiv(divId);
    }
}


function showDiv(divId) {

    var objDivStyle = document.getElementById(divId).style;

    objDivStyle.display = 'block';
    objDivStyle.visibility = 'visible';
}

function hideDiv(divId) {

    var objDivStyle = document.getElementById(divId).style;

    objDivStyle.display = 'none';
    objDivStyle.visibility = 'hidden';
}

function checkEmail(str) {

    str = Trim(str);

    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {
        alert("Invalid E-mail Address")
        return false
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        alert("Invalid E-mail Address")
        return false
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        alert("Invalid E-mail Address")
        return false
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        alert("Invalid E-mail Address")
        return false
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        alert("Invalid E-mail Address")
        return false
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        alert("Invalid E-mail Address")
        return false
    }

    if (str.indexOf(" ") != -1) {
        alert("Invalid E-mail Address")
        return false
    }

    return true
}

function Trim(str) {
    while (str.charAt(0) == (" ")) {
        str = str.substring(1);
    }
    while (str.charAt(str.length - 1) == " ") {
        str = str.substring(0, str.length - 1);
    }
    return str;
}

function sendRequest(divName, actionClass, form) {
    new Ajax.Updater(divName, actionClass, { method: 'post',
        parameters: $(form).serialize()
    });
}

function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds) {
            break;
        }
    }
}

function SetCookie(cookieName, cookieValue, nDays) {
    var today = new Date();
    var expire = new Date();
    if (nDays == null || nDays == 0) nDays = 1;
    expire.setTime(today.getTime() + 3600000 * 24 * nDays);
    document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString() + "; path=/";
}

function ReadCookie(cookieName) {
    var theCookie = "" + document.cookie;
    var ind = theCookie.indexOf(cookieName);
    if (ind == -1 || cookieName == "") return "";
    var ind1 = theCookie.indexOf(';', ind);
    if (ind1 == -1) ind1 = theCookie.length;
    var val = unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
    return val;
}
function redirectToSignUp() {
    var val = ReadCookie("registered");
    if (val == null || val == "")
        location.href = "sign-me-up.aspx";
}

function IsMandatory(control)
    {
	    if (Trim(control.value) == "")
	    {
		    return false;
	    }
	    else
	    {
		    return true;
	    }
    }
    
    function CancelForm()
    {
        document.forms[0].reset();
    }