﻿//验证邮箱
function verifyEmail(txtEmail) {
    var regex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
    return regex.test(txtEmail);
}
//验证电话号码（可验证27种格式）
//详见：http://www.cnblogs.com/mephisto/archive/2009/01/15/841757.html 
function verifyPhone(txtPhone) {
    var regex = /(^(\d{2,4}[-_－—]?)?\d{3,8}([-_－—]?\d{3,8})?([-_－—]?\d{1,7})?$)|(^0?1[35]\d{9}$)/;
    return regex.test(txtPhone);
}
//验证是否正整数
function verifyNumber(txtNumber) {
    var regex = /^[0-9]*[1-9][0-9]*$/;
    return regex.test(txtNumber);
}
//function $(id)
//{
//    return document.getElementById(id);
//}
//验证个人信息输入是否合法
function verifyPersonalInfo() {
    if ($("#ctl00_ContentPlaceHolder1_UserName")[0].value == "") {
        alert("UserName is required!");
        $("#ctl00_ContentPlaceHolder1_UserName").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_fName")[0].value == "") {
        alert("FirstName is required!");
        $("#ctl00_ContentPlaceHolder1_fName").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_mInitial")[0].value == "") {
        alert("Middle Initial is required!");
        $("#ctl00_ContentPlaceHolder1_mInitial").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_lName")[0].value == "") {
        alert("LastName is required!");
        $("#ctl00_ContentPlaceHolder1_lName").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_email")[0].value == "") {
        alert("Email is required!");
        $("#ctl00_ContentPlaceHolder1_email").focus();
        return false;
    }
    if (!verifyEmail($("#ctl00_ContentPlaceHolder1_email")[0].value)) {
        alert("Invalid email!");
        $("#ctl00_ContentPlaceHolder1_email").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_Password")[0] != "undefined") {
        if ($("#ctl00_ContentPlaceHolder1_Password")[0].value == "") {
            alert("Password is required!");
            $("#ctl00_ContentPlaceHolder1_Password").focus();
            return false;
        } 
    }
    if ($("#ctl00_ContentPlaceHolder1_ConfPassword")[0] != "undefined") {

        if ($("#ctl00_ContentPlaceHolder1_ConfPassword")[0].value == "") {
            alert("PasswordConfirmation is required!");
            $("#ctl00_ContentPlaceHolder1_ConfPassword").focus();
            return false;
        }
    }
    if ($("#ctl00_ContentPlaceHolder1_ConfPassword")[0] != "undefined") {
        if ($("#ctl00_ContentPlaceHolder1_ConfPassword")[0].value != $("#ctl00_ContentPlaceHolder1_Password")[0].value) {
            alert("Two password is not consistent ");
            $("#ctl00_ContentPlaceHolder1_ConfPassword").focus();
            return false;
        }
    }
    if ($("#ctl00_ContentPlaceHolder1_DealerName")[0].value == "") {
        alert("Dealer Name is required!");
        $("#ctl00_ContentPlaceHolder1_DealerName").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_Phone")[0].value == "") {
        alert("phone number is required!");
        $("#ctl00_ContentPlaceHolder1_Phone").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_Phone")[0].value != "") {
        if (!verifyPhone($("#ctl00_ContentPlaceHolder1_Phone")[0].value)) {
            alert("Invalid phone number!");
            $("#ctl00_ContentPlaceHolder1_Phone").focus();
            return false;
        }
    }
    if ($("#ctl00_ContentPlaceHolder1_Zip")[0].value != "") {
        if (!verifyNumber($("#ctl00_ContentPlaceHolder1_Zip")[0].value)) {
            alert("Invalid zip number!");
            $("#ctl00_ContentPlaceHolder1_Zip").focus();
            return false;
        } 
    }
    return true;
}

//validate product info
function verifyProductInfo() {
    if ($("#ctl00_ContentPlaceHolder1_ProductRegistration_tbFirstname")[0].value == "") {
        alert("FirstName is required!");
        $("#ctl00_ContentPlaceHolder1_ProductRegistration_tbFirstname").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_ProductRegistration_tbLastname")[0].value == "") {
        alert("LastName is required!");
        $("#ctl00_ContentPlaceHolder1_ProductRegistration_tbLastname").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_ProductRegistration_tbPosition")[0].value == "") {
        alert("Position is required!");
        $("#ctl00_ContentPlaceHolder1_ProductRegistration_tbPosition").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_ProductRegistration_tbCompany")[0].value == "") {
        alert("Company is required!");
        $("#ctl00_ContentPlaceHolder1_ProductRegistration_tbCompany").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_ProductRegistration_tbAddress1")[0].value == "") {
        alert("Address1 is required!");
        $("#ctl00_ContentPlaceHolder1_ProductRegistration_tbAddress1").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_ProductRegistration_tbCity")[0].value == "") {
        alert("City is required!");
        $("#ctl00_ContentPlaceHolder1_ProductRegistration_tbCity").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_ProductRegistration_tbZip")[0].value == "") {
        alert("Zip is required!");
        $("#ctl00_ContentPlaceHolder1_ProductRegistration_tbZip").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_ProductRegistration_tbCountry")[0].value == "") {
        alert("Country is required!");
        $("#ctl00_ContentPlaceHolder1_ProductRegistration_tbCountry").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_ProductRegistration_tbEmail")[0].value == "") {
        alert("Email is required!");
        $("#ctl00_ContentPlaceHolder1_ProductRegistration_tbEmail").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_ProductRegistration_tbEmail")[0].value != "") {
        if (!verifyEmail($("#ctl00_ContentPlaceHolder1_ProductRegistration_tbEmail")[0].value)) {
            alert("Invalid email!");
            $("#ctl00_ContentPlaceHolder1_ProductRegistration_tbEmail").focus();
            return false;
        }
    }
    if ($("#ctl00_ContentPlaceHolder1_ProductRegistration_tbPhone")[0].value == "") {
        alert("Phone is required!");
        $("#ctl00_ContentPlaceHolder1_ProductRegistration_tbPhone").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_ProductRegistration_tbSN")[0].value == "") {
        alert("Serial Number is required!");
        $("#ctl00_ContentPlaceHolder1_ProductRegistration_tbSN").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_ProductRegistration_txt_PurchaseDate")[0].value == "") {
        alert("Purchase Date is required!");
        $("#ctl00_ContentPlaceHolder1_ProductRegistration_txt_PurchaseDate").focus();
        return false;
    }
    return true;
}

function InvalidLoginInfo() {
    if ($("#ctl00_ContentPlaceHolder1_tbUsrName")[0].value == "") {
        alert("UserName is required!");
        $("#ctl00_ContentPlaceHolder1_tbUsrName").focus();
        return false;
    }
    if ($("#ctl00_ContentPlaceHolder1_tbPWD")[0].value == "") {
        alert("Password is required!");
        $("#ctl00_ContentPlaceHolder1_tbPWD").focus();
        return false;
    }
}

function InvalidSearch() {
    if ($("#ctl00_ContentPlaceHolder1_serkeyword")[0].value == "Serial Number Here") {
        alert("serial number is required!");
        $("#ctl00_ContentPlaceHolder1_serkeyword").focus();
        return false;
    }
}
