﻿
var PreFix = "#ctl00_ContentPlaceHolder1_";

var drpMsgType = PreFix +"drpMessageType";
var txtFirstName = PreFix +"txtFirstName";
var txtLastName = PreFix + "txtLastName";
var txtEmail = PreFix + "txtEmail";
var txtComments = PreFix + "txtComments";
var txtValidateCode = PreFix + "txtValidateCode";   

$(document).ready(
    function (){
            $(drpMsgType).blur(function(){DoMessageForEmpty(drpMsgType,"divMessageType",varMsgType);}).focus(function(){DoMessageForEmpty(drpMsgType,"divMessageType",varMsgType);});
            $(txtFirstName).blur(function(){DoMessageForEmpty(txtFirstName,"divFirstName",varFirstName);}).focus(function(){DoMessageForEmpty(txtFirstName,"divFirstName",varFirstName);});
            $(txtLastName).blur(function(){DoMessageForEmpty(txtLastName,"divLastName",varLastName);}).focus(function(){DoMessageForEmpty(txtLastName,"divLastName",varLastName);});            
            $(txtEmail).blur(function(){DoMessageForEmail();}).focus(function(){DoMessageForEmail();});
            $(txtComments).blur(function(){DoMessageForEmpty(txtComments,"divComments",varComments);}).focus(function(){DoMessageForEmpty(txtComments,"divComments",varComments);});            
            $(txtValidateCode).blur(function(){DoMessageForEmpty(txtValidateCode,"divValidateCode",varValidateCode);}).focus(function(){DoMessageForEmpty(txtValidateCode,"divValidateCode",varValidateCode);});
    }
);
//验证表单
function ValidateForm()
{
    if(!CheckSelector(drpMsgType,"divMessageType",varMsgType))
        return false;  
    if(!CheckText(txtFirstName,"divFirstName",varFirstName))
        return false; 
    else if(!CheckText(txtLastName,"divLastName",varLastName))
        return false;   
    else if(!CheckEmail())
        return false; 
    else if(!CheckText(txtComments,"divComments",varComments))
        return false;    
    else if(!CheckText(txtValidateCode,"divValidateCode",varValidateCode))
        return false; 
        
    return true;
}
//检测下拉列表是否为空，并且操作相应的DIV提示
function CheckSelector(drp,divMsg,strMsg)
{
    if($.trim($(drp).children('option:selected').attr('value'))=="")
    {
        $("#"+divMsg).html(strMsg).show();
        return false;
    }
    else
    {
        $("#"+divMsg).hide();
        return true;
    }
}
//下拉列表获得、失去焦点时是否显示消息
function DoMessageForEmpty(drp,divMsg,strMsg)
{
    if($.trim($(drp).children('option:selected').attr('value'))=="")
    {
        $("#"+divMsg).html(strMsg).show();
    }
    else
    {
        $("#"+divMsg).hide();
    }
}


//检测文本框是否为空，并且操作相应的DIV提示
function CheckText(txt,divMsg,strMsg)
{
    if($.trim($(txt).val())=="")
    {
        $("#"+divMsg).html(strMsg).show();
        return false;
    }
    else
    {
        $("#"+divMsg).hide();
        return true;
    }
}
//文本框获得、失去焦点时是否显示消息
function DoMessageForEmpty(txt,divMsg,strMsg)
{
    if($.trim($(txt).val())=="")
    {
        $("#"+divMsg).html(strMsg).show();
    }
    else
    {
        $("#"+divMsg).hide();
    }
}

//验证邮箱地址
function CheckEmail()
{
    var reg = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    if($.trim($(txtEmail).val())=="")
    {
        $("#divEmail").html(varEmail).show();
        return false;
    }
    else if(!reg.exec($.trim($(txtEmail).val())))
    {
        $("#divEmail").html(varEmailFormat).show();
        return false;
    }
    else
    {
        $("#divEmail").hide();
        return true;
    }
}
//验证邮箱地址
function DoMessageForEmail()
{
    var reg = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    if($.trim($(txtEmail).val())=="")
    {
        $("#divEmail").html(varEmail).show();
    }
    else if(!reg.exec($.trim($(txtEmail).val())))
    {
        $("#divEmail").html(varEmailFormat).show();
    }
    else
    {
        $("#divEmail").hide();
    }
}