﻿function getSelected(e) {
    var src = e.target ? e.target : e.srcElement;
    //if(window.event.srcElement.value == "phone")  - Firefox doesn't like window.event
    if (src.value == "phone") {
        document.getElementById("phonediv").style.display = 'block';
        document.getElementById("emaildiv").style.display = 'none';
    }
    else {
        document.getElementById("phonediv").style.display = 'none';
        document.getElementById("emaildiv").style.display = 'block';
    }
}

function validate_email(str) {
    var email = document.getElementById(GetClientId("emailtext"));
    if (str == "") {
        alert("Please enter your e-mail address");
        email.focus();
        //document.aspnetForm.Contact1_email.focus();
        return false;
    }

    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")
        email.focus();
        //document.aspnetForm.Contact1_email.focus();
        return false
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        alert("Invalid e-mail address")
        email.focus();
        return false
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        alert("Invalid e-mail address")
        email.focus();
        return false
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        alert("Invalid e-mail address")
        email.focus();
        return false
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        alert("Invalid e-mail address")
        email.focus();
        return false
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        alert("Invalid e-mail address")
        email.focus();
        return false
    }

    if (str.indexOf(" ") != -1) {
        alert("Invalid e-mail address")
        email.focus();
        return false
    }
    return true
}

function stripCharsInPhone(s, specChar) {
    var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (specChar.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function validate_phone(p1) {
    var phone1 = document.getElementById(GetClientId("phone1"));
    var s;
    //if ((p1 == "") || (p2 == "") || (p3 == ""))
    if (p1 == "") {
        alert("Please enter your complete phone number");
        phone1.focus();
        //document.aspnetForm.Contact1_phone1.focus();
        return false;
    }

    rePhoneNumber = new RegExp(/^[\+]?[1-9]/);
    //if ((!rePhoneNumber.test(p1)) || (!rePhoneNumber.test(p2)) || (!rePhoneNumber.test(p3)))
    if (!rePhoneNumber.test(p1)) {
        alert("Invalid phone number");
        phone1.focus();
        //document.aspnetForm.Contact1_phone1.focus();
        return false;
    }

    s = stripCharsInPhone(p1, "+- ");
    if (s.length < 10) {
        alert("Phone number is too short");
        phone1.focus();
        //document.aspnetForm.Contact1_phone1.focus();
        return false;
    }

    return true;
}

function $(id) {
    return document.getElementById(id);
}

//function validate_form(email,phone1,fname,lname,interest) {
function validate_form() {
    var bemail = document.getElementById(GetClientId("email"));
    var bphone = document.getElementById(GetClientId("phone"));
    var email = document.getElementById(GetClientId("emailtext"));
    var phone1 = document.getElementById(GetClientId("phone1"));
    var fname = document.getElementById(GetClientId("firstname"));
    var lname = document.getElementById(GetClientId("lastname"));
    var interest = document.getElementById(GetClientId("interest"));
    var institution = document.getElementById(GetClientId("institution"));

    //alert("emailtext" + bemail.id);
    //alert("phone1" + bphone.id);

    // var bphone=$('("<%=phone1.ClientID%>")');
    // var bemail=$('("<%=email.ClientID%>")');
    //alert(bphone);

    //var bphone=$('Contact1_Phone');
    //var bemail=$('Contact1_email');


    //var bphone=(bphone=='True');
    //var bemail=(bemail=='True');

    if (interest.selectedIndex == 0) {
        alert('Please select an area of interest');
        interest.focus();
        //document.aspnetForm.getElementById("<%=interest.ClientID%>").focus();
        //document.aspnetForm.Contact1_interest.focus();
        return false;
    }

    if (fname.value == "") {
        alert('Please enter your first name');
        fname.focus();
        //document.aspnetForm.getElementById("<%=firstname.ClientID%>").focus();
        //document.aspnetForm.Contact1_firstname.focus();
        return false;
    }
    if (lname.value == "") {
        alert('Please enter your last name');
        lname.focus();
        //document.aspnetForm.getElementById("<%=lastname.ClientID%>").focus();
        //document.aspnetForm.Contact1_lastname.focus();
        return false;
    }

    if (institution.value == "") {
        alert('Please enter your institution/company name');
        institution.focus();
        //document.aspnetForm.getElementById("<%=lastname.ClientID%>").focus();
        //document.aspnetForm.Contact1_lastname.focus();
        return false;
    }
    
    if (bemail.checked) {
        var validEmail = validate_email(email.value);
        if (!validEmail) {
            return false;
        }
    }
    if (bphone.checked) {
        var validphone = validate_phone(phone1.value);
        if (!validphone) {
            return false;
        }
    }

    //alert("Your information has been sent to a Peterson's representative and you will be contacted within 24 hours.  Thank you for your interest.")
    alert("Thank you for your inquiry. Someone from Peterson’s will contact you shortly.")
    return true;
}


