﻿
function getClientWidth ()
{
	var width = 0;
	if( typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
		width = window.innerWidth;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		//IE 6+ in 'standards compliant mode'
		width = document.documentElement.clientWidth;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight )) 
	{
		//IE 4 compatible
		width = document.body.clientWidth;
	}
	
	return width;
}

function getClientHeight()
{
	var height = 0;
	if( typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
		height = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		//IE 6+ in 'standards compliant mode'
		height = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight )) 
	{
		//IE 4 compatible
		height = document.body.clientHeight;
	}
	
	return height;
}

function trim(str)
{ 
	//删除左右两端的空格
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

function ltrim(str)
{ 
	//删除左边的空格
	return str.replace(/(^\s*)/g,"");
}

function rtrim(str)
{ 
	//删除右边的空格
	return str.replace(/(\s*$)/g,"");
}

function reload()
{
    if (window.ActiveXObject)
    {
        window.location.reload();
    }
    else
    {
        window.location.href = "";
    }
}

$(document).ready(function()
{       
     $(".init_text_input").each(function(){
        setInputControlInitText($(this));
     });
});

function setInputControlInitText(obj)
{
    var initText = obj.attr('title');
          
    if (obj.val() == '' || obj.val() == initText)
        obj.css('color', '#999999').val(initText);
    
    obj.focus(function(){
      if (obj.val() == initText)
      {
          obj.val('').css('color', '');
      }
    });

    obj.blur(function(){
      if ('' + obj.val() == '')
      {
          obj.val(initText).css('color', '#999999');
      }
    });
}

function syncAjaxRequest(mothed, url, params) 
{
    var result = null;
    
    $.ajax({
        async:  false,
        cache:  false,
        type:   mothed,
        url:    url,
        data:   params,
        success: function(data) 
        {
            result = data;
        }
    });
    
    if (result == null)
        throw "网络通信错误或者服务器错误。";
    
    return result;
}

function ajax_getDialogUI(params)
{
    var result = "";
    try 
    {
        result = syncAjaxRequest("get", "/ajax_dialog_ui.aspx", params);	            
    }
    catch(ex) {
        result = ex;
    }
    
    return result;
}

function ajax_isLogined()
{
    var result = "";
    try 
    {
        result = syncAjaxRequest("get", "/ajax_login.aspx", "action=check");	            
    }
    catch(ex) {
        result = ex;
    }
    
    return result;
}

function ajax_login(username, password)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
            "/ajax_login.aspx", 
            { "action": "login", "username": username, "password": password});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_logout()
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
            "/ajax_login.aspx", 
            { "action": "logout"});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function initMessageDialog()
{
    if ($("#message_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"message_dialog"}));
        $("#message_dialog").dialog({
		    autoOpen: false,
		    width: 340,
		    height: 200,
            resizable: false,
            bgiframe: true,		
            modal: true
        });   
    }
}

function showMessageDialog(msg, completeFunction, param)
{
    initMessageDialog();
    
    $("#message_dialog").dialog("option", "open", function(){
         $("#message_dialog_message").text(msg);
    });
    
    $("#message_dialog").dialog("option", "buttons", {
		 " 确定 ": function() {
			$("#message_dialog").dialog("close");
			if (completeFunction)
			    completeFunction(param);
		 }
    });
    
    $("#message_dialog").dialog("open");
}

function initConfirmDialog()
{
    if ($("#confirm_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"confirm_dialog"}));
        $("#confirm_dialog").dialog({
		    autoOpen: false,
		    width: 340,
		    height: 200,
            resizable: false,	
            bgiframe: true,		
            modal: true
        });
    }
}

function showConfirmDialog(msg, okFunction, okParam, cancelFunction, cancelParam)
{
    initConfirmDialog();
    
    $("#confirm_dialog").dialog("option", "buttons", {
		 " 否 ": function() {
		    $("#confirm_dialog").dialog("close");
		    if (cancelFunction)
		        cancelFunction(cancelParam);
		 },
		 " 是 ": function() {
			$("#confirm_dialog").dialog("close");
			if (okFunction)
			    okFunction(okParam);
		 }
    });
    
    $("#confirm_dialog").dialog("option", "open", function(){
         $("#confirm_dialog_message").text(msg);
    });
    
    $("#confirm_dialog").dialog("open");
}

function initLoginDialog()
{
    if ($("#login_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"login_dialog"}));
        
        $("#login_dialog").dialog({
		    autoOpen: false,
		    width: 400,
		    height: 280,
            resizable: false,	
            bgiframe: true,		
            modal: true
	    });
	}
}

function openLoginDialog(completeFunction, param)
{
    initLoginDialog();
    
    $("#login_dialog").dialog("option", "buttons", {
		 " 取消 ": function() {
				$("#login_dialog").dialog("close");
		    },
	    " 确定 ": function() {
	        var result = ajax_login($("#login_user_name").val(), $("#login_password").val());
	        if (result == "1")
            {
                $("#login_dialog").dialog("close");
                
                rememberLoginInfo($("#login_user_name").val(), $("#login_password").val(), $("#login_remember_me").attr("checked") ? "1" : "0")
                
                if (completeFunction)
                    completeFunction(param);
            }
            else
            {
                $("#login_dialog_validate_tips").text(result).css("color", "red");
            }
        }
    });
    
    $("#login_dialog").dialog("option", "open", function(){
     	$("#login_dialog_validate_tips").text("请输入用户名和密码登录").css("color", "");
	    
	    if ($("#login_user_name").val() == "")
	        $("#login_user_name").val("" + $.cookie("login_user_name"));
		    
	    $("#login_password").val("");
	    
	    if ($("#login_user_name").val() == "")
	        $("#login_user_name").focus();
	    else
	        $("#login_password").focus();
    });
    
    $("#login_dialog").dialog("open");
}

function setLoginName(userNameInput)
{
    var username = "" + $.cookie("login_user_name");    
    if (userNameInput != null && username != "")
    {
        userNameInput.val(username);
    }

    return "0";
}

function formLogin()
{
    if ("" + $("#login_user_name").val() == "")
    {
        showMessageDialog("请输入用户名。");
        $("#login_user_name").focus();
        return;    
    }
    
    if ("" + $("#login_password").val() == "")
    {
        showMessageDialog("请输入密码。");
        $("#login_password").focus();
        return;    
    }
    
    if (ajax_login($("#login_user_name").val(), $("#login_password").val()) == "1")
    {
        rememberLoginInfo($("#login_user_name").val(), 
            $("#login_password").val(), 
            $("#login_remember_me").attr("checked") ? "1" : "0");
        
        reload();
    }
    else
        window.location.href = "/login";
}

function specialLogin()
{
    if ("" + $("#login_user_name").val() == "")
    {
        showMessageDialog("请输入用户名。");
        $("#login_user_name").focus();
        return;    
    }
    
    if ("" + $("#login_password").val() == "")
    {
        showMessageDialog("请输入密码。");
        $("#login_password").focus();
        return;    
    }
    
    var result = ajax_login($("#login_user_name").val(), $("#login_password").val());
    if (result == "1")
    {
        rememberLoginInfo($("#login_user_name").val(), 
            $("#login_password").val(), 
            $("#login_remember_me").attr("checked") ? "1" : "0");
        
        window.location.href = $("#redirection_url").val();
    }
    else
    {    
        showMessageDialog(result);
    }
}

function rememberLoginInfo(username, password, autoLogin)
{
    var domain = null;
    if (document.domain.toLowerCase().indexOf("yaoliwang.com") > 0)
        domain = "yaoliwang.com";
    else if (document.domain.toLowerCase().indexOf("yaoleetest.com") > 0)
        domain = "yaoleetest.com";
        
    $.cookie("login_user_name", username, {"path":"/", "expires": 365, "domain": domain});
    
    if (autoLogin == "1")
        $.cookie("login_password", password, {"path":"/", "expires": 30, "domain": domain});
    else
        $.cookie("login_password", password, {"path":"/", "expires": 0.5, "domain": domain});
}

function clearLoginInfo()
{
    var domain = null;
    if (document.domain.toLowerCase().indexOf("yaoliwang.com") > 0)
        domain = "yaoliwang.com";
    else if (document.domain.toLowerCase().indexOf("yaoleetest.com") > 0)
        domain = "yaoleetest.com";
        
    $.cookie("login_password", "", {"path":"/", "expires": 365, "domain": domain});
}

//
var gou_initUserHopeMedicineDescription = '您有什么想法和建议，请在这里输入。';

function initUserHope()
{
    var obj2 = $('#medicineNote');
    obj2.val(gou_initUserHopeMedicineDescription).css('color', '#999999');

    obj2.focus(function(){
        if (obj2.val() == gou_initUserHopeMedicineDescription)
        {
            obj2.val('').css('color', '');
        }
    });
    obj2.blur(function(){
        if ('' + obj2.val() == '')
        {
            obj2.val(gou_initUserHopeMedicineDescription).css('color', '#999999');
        }
    });
}

function ajax_addUserHope(medicineNote)
{
    var result = "";
    try
    {
            result = syncAjaxRequest("post", 
            "/ajax_buy_userhope.aspx", 
            { "action": "adduserhope", "name":"","note":medicineNote });
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function addUserHope()
{
    if ("" + $("#medicineNote").val() == gou_initUserHopeMedicineDescription ||
        "" + $("#medicineNote").val == "")
    {
        showMessageDialog("您有什么想法和建议，请在这里输入。");
        return;  
    }
    
    var result = ajax_addUserHope($("#medicineNote").val());
    if (result == "1")
    {
        showMessageDialog("您希望团购的药品己经提交到我们的数据库，我们将会认真考虑您的意见。谢谢您的参与。")
        initUserHope();
        return;
    }
    else
    {
        showMessageDialog(result);
    }
}
//用户调查
function addUserSurvey()
{
    var str="";
    $("[name='survey_checkbox']").each(function(){
      if($(this).attr("checked")==true)
      {
         if(str != "")
            str = str + ",";
         str+=$(this).val();  
      }
    });
    if(str == "")
    {
        showMessageDialog("您还没有选择药品种类。")
        return;
    }
    
    var result = ajax_addUserHope("yaolee2010_gou_survey",str);
    if (result == "1")
    {
        showMessageDialog("您希望团购的药品种类己经提交到我们的数据库，我们将会认真考虑您的意见。谢谢您的参与。")
        $("[name='survey_checkbox']").removeAttr("checked");
        return;
    }
    else
    {
        showMessageDialog(result);
    }
}

function showOrHidePrintCouponButton()
{
    var count = 0;

    $("input[name='store_select_cb']").each(function(){
          if($(this).attr("checked"))
          {             
             count++;
          }
    });

}

function selectMedicineSubbranch()
{
    var count = 0;
    var str = "";
    $("input[name='store_select_cb']").each(function(){
          if($(this).attr("checked"))
          {
             if(str != "")
                str = str + "|";
             str += $(this).val();
             
             count++;
          }
    });
    
    $("#subbranch_id").val(str);
    $("#subbranch_id2").val(str);
    
     $("input[name='store_select_cb']").each(function(){
          if(count >= 3)
          {
            if (!$(this).attr("checked"))
                $(this).attr("disabled", true);
          }
          else
          {
            $(this).attr("disabled", false);
          }
    });
    
}

function submitPrintCoupon()
{
    var subbranchIds = $("#subbranch_id").val();
    if (subbranchIds.length < 1)
    {
        showMessageDialog("请选择至少一家门店。");
        return false;
    }
    var arrIds = subbranchIds.split("|");
    if (arrIds.length < 1)
    {
        showMessageDialog("请选择至少一家门店。");
        return false;
    }
    
    if (arrIds.length > 3)
    {
        showMessageDialog("请不要选择超过三家门店。");
        return false;
    }
    
    return true;
}

function submitPrintCoupon_step2()
{
    document.forms["print_coupon_form"].submit();
    reload();
}

function ajax_checkScoreMedicine(medicineId)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
            "/ajax_buy_medicine_score.aspx", 
            { "action": "check", "medicineId": medicineId});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_scoreMedicine(medicineId, score1, score2, score3)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
            "/ajax_buy_medicine_score.aspx", 
            {   "action": "add", 
                "medicineId": medicineId, 
                "score1" : score1, 
                "score2" : score2,
                "score3" : score3
             });
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function initGouMedicineScoreDialog()
{
    if ($("#gou_medicine_score_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"gou_medicine_score_dialog"}));
        
         $("#gou_medicine_score_dialog_medicine_score .item1").rate({
            currentRate:5,
            clickCallBack:function(currRate,id){
                $("#gou_medicine_score_dialog_medicine_score_text_1").text("（" + currRate + " / 5）");
                $("#gou_medicine_score_dialog_medicine_score_text_1").attr("value", currRate);
            }
        });
        
	    $("#gou_medicine_score_dialog_medicine_score .item2").rate({
            currentRate:5,
            clickCallBack:function(currRate,id){
                $("#gou_medicine_score_dialog_medicine_score_text_2").text("（" + currRate + " / 5）");
                $("#gou_medicine_score_dialog_medicine_score_text_2").attr("value", currRate);
            }
        });
        
	    $("#gou_medicine_score_dialog_medicine_score .item3").rate({
            currentRate:5,
            clickCallBack:function(currRate,id){
                $("#gou_medicine_score_dialog_medicine_score_text_3").text("（" + currRate + " / 5）");
                $("#gou_medicine_score_dialog_medicine_score_text_3").attr("value", currRate);
            }
        });
        
        $("#gou_medicine_score_dialog").dialog({
		    autoOpen: false,
		    width: 370,
		    height: 260,
            resizable: false,	
            bgiframe: true,		
            modal: true
	    });
	}
}

function openGouMedicineScoreDialog(medicineId, completeFunction, param)
{
    initGouMedicineScoreDialog();
    
    $("#gou_medicine_score_dialog").dialog("option", "buttons", {
		 " 取消 ": function() {
				$("#gou_medicine_score_dialog").dialog("close");
		    },
	    " 提交 ": function() {
	    
	        score1 = $("#gou_medicine_score_dialog_medicine_score_text_1").attr("value");
	        score2 = $("#gou_medicine_score_dialog_medicine_score_text_2").attr("value");
	        score3 = $("#gou_medicine_score_dialog_medicine_score_text_3").attr("value");
	        
	        var result = ajax_scoreMedicine(medicineId, score1, score2, score3);
	        if (result == "1")
	        {
	            if (completeFunction)
	                completeFunction(param);
	        }
	        else
	        {
	            $("#gou_medicine_score_dialog_validate_tips").text(result).css("color", "red");
	        }
        }
    });
    
    $("#gou_medicine_score_dialog").dialog("option", "open", function(){
     	$("#gou_medicine_score_dialog_validate_tips").text("请对这个药品进行打分").css("color", "");
    });
    
    $("#gou_medicine_score_dialog").dialog("open");
}

function scoreGouMedicine(medicineId)
{
    if (ajax_isLogined() == "1")
    {
        scoreGouMedicine_step2(medicineId);
    }
    else
    {
        openLoginDialog(scoreGouMedicine_step2, medicineId);
    }
}

function scoreGouMedicine_step2(medicineId)
{
    var result = ajax_checkScoreMedicine(medicineId);
    if (result == "1")
    {
        scoreGouMedicine_step3(medicineId);
    }
    else
    {
        showMessageDialog(result);
    }
}

function scoreGouMedicine_step3(medicineId)
{
    openGouMedicineScoreDialog(medicineId, reload);
}

function ajax_commentMedicine(medicineId, content)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_buy_medicine_comment.aspx", 
            {   "action": "add", 
                "medicineId": medicineId, 
                "content" : content
             });
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function initGouMedicineCommentDialog()
{
    if ($("#gou_medicine_comment_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"gou_medicine_comment_dialog"}));
                
        $("#gou_medicine_comment_dialog").dialog({
		    autoOpen: false,
		    width: 450,
		    height: 340,
            resizable: false,	
            bgiframe: true,		
            modal: true
	    });
	}
}

function openGouMedicineCommentDialog(medicineId, completeFunction, param)
{
    initGouMedicineCommentDialog();
    
    $("#gou_medicine_comment_dialog").dialog("option", "buttons", {
		 " 取消 ": function() {
				$("#gou_medicine_comment_dialog").dialog("close");
		    },
	    " 提交 ": function() {
            
            var content = $("#gou_medicine_comment_content").val();
            if (content.length == 0)
            {
                $("#gou_medicine_comment_dialog_validate_tips").text("请输入评论内容。").css("color", "red");
                return;
            }
            if (content.length > 500)
            {
                $("#gou_medicine_comment_dialog_validate_tips").text("评论内容不能多于500个字符。").css("color", "red");
                return;
            }
            
	        var result = ajax_commentMedicine(medicineId, content);
	        if (result == "1")
	        {
	            if (completeFunction)
	                completeFunction(param);
	        }
	        else
	        {
	            $("#gou_medicine_comment_dialog_validate_tips").text(result).css("color", "red");
	        }
        }
    });
    
    $("#gou_medicine_comment_dialog").dialog("option", "open", function(){
     	$("#gou_medicine_comment_dialog_validate_tips").text("请对这个药品进行评论").css("color", "");
    });
    
    $("#gou_medicine_comment_dialog").dialog("open");
}

function commentGouMedicine(medicineId)
{
    if (ajax_isLogined() == "1")
    {
        commentGouMedicine_step2(medicineId);
    }
    else
    {
        openLoginDialog(commentGouMedicine_step2, medicineId);
    }
}

function commentGouMedicine_step2(medicineId)
{
    openGouMedicineCommentDialog(medicineId, reload);
}
//药品评价
function ajax_commentBuyMedicine(medicineId, content)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_buy_medicine_comment.aspx", 
            {   "action": "add", 
                "medicineId": medicineId, 
                "content" : content
             });
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function initBuyMedicineCommentDialog()
{



    if ($("#buy_medicine_comment_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"buy_medicine_comment_dialog"}));
              
        $("#buy_medicine_score_dialog_medicine_score .item1").rate({
            currentRate:5,
            clickCallBack:function(currRate,id){
                $("#buy_medicine_score_dialog_medicine_score_text_1").text("（" + currRate + " / 5）");
                $("#buy_medicine_score_dialog_medicine_score_text_1").attr("value", currRate);
            }
        });
        
	    $("#buy_medicine_score_dialog_medicine_score .item2").rate({
            currentRate:5,
            clickCallBack:function(currRate,id){
                $("#buy_medicine_score_dialog_medicine_score_text_2").text("（" + currRate + " / 5）");
                $("#buy_medicine_score_dialog_medicine_score_text_2").attr("value", currRate);
            }
        });
        
	    $("#buy_medicine_score_dialog_medicine_score .item3").rate({
            currentRate:5,
            clickCallBack:function(currRate,id){
                $("#buy_medicine_score_dialog_medicine_score_text_3").text("（" + currRate + " / 5）");
                $("#buy_medicine_score_dialog_medicine_score_text_3").attr("value", currRate);
            }
        });
        
        $("#buy_medicine_score_dialog_medicine_score .item4").rate({
            currentRate:5,
            clickCallBack:function(currRate,id){
                $("#buy_medicine_score_dialog_medicine_score_text_4").text("（" + currRate + " / 5）");
                $("#buy_medicine_score_dialog_medicine_score_text_4").attr("value", currRate);
            }
        });
        
        $("#buy_medicine_score_dialog_medicine_score .item5").rate({
            currentRate:5,
            clickCallBack:function(currRate,id){
                $("#buy_medicine_score_dialog_medicine_score_text_5").text("（" + currRate + " / 5）");
                $("#buy_medicine_score_dialog_medicine_score_text_5").attr("value", currRate);
            }
        });
        
        $("#buy_medicine_score_dialog_medicine_score .item6").rate({
            currentRate:5,
            clickCallBack:function(currRate,id){
                $("#buy_medicine_score_dialog_medicine_score_text_6").text("（" + currRate + " / 5）");
                $("#buy_medicine_score_dialog_medicine_score_text_6").attr("value", currRate);
            }
        });
                
        $("#buy_medicine_comment_dialog").dialog({
		    autoOpen: false,
		    width: 480,
		    height: 500,
            resizable: false,	
            bgiframe: true,		
            modal: true
	    });
	}
}

function openBuyMedicineCommentDialog(medicineId, completeFunction, param)
{
    initBuyMedicineCommentDialog();
    
    $("#buy_medicine_comment_dialog").dialog("option", "buttons", {
		 " 取消 ": function() {
				$("#buy_medicine_comment_dialog").dialog("close");
		    },
	    " 提交 ": function() {
            
            var content = $("#buy_medicine_comment_content").val();
            if (content.length == 0)
            {
                $("#buy_medicine_comment_dialog_validate_tips").text("请输入评论内容。").css("color", "red");
                return;
            }
            if (content.length > 500)
            {
                $("#buy_medicine_comment_dialog_validate_tips").text("评论内容不能多于500个字符。").css("color", "red");
                return;
            }
            
	        var result = ajax_commentBuyMedicine(medicineId, content);
	        if (result == "1")
	        {
	            if (completeFunction)
	                completeFunction(param);
	        }
	        else
	        {
	            $("#buy_medicine_comment_dialog_validate_tips").text(result).css("color", "red");
	        }
        }
    });
    
    $("#buy_medicine_comment_dialog").dialog("option", "open", function(){
     	$("#buy_medicine_comment_dialog_validate_tips").text("请对这个药品进行评论").css("color", "");
    });
    
    $("#buy_medicine_comment_dialog").dialog("open");
}

function commentBuyMedicine(medicineId)
{
      openBuyMedicineCommentDialog(medicineId, reload);
}


//发送手机号码
function initBuyMedicineSnedMobileDialog()
{
    if ($("#buy_medicine_send_mobile_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"buy_medicine_send_mobile_dialog"}));
                
        $("#buy_medicine_send_mobile_dialog").dialog({
		    autoOpen: false,
		    width: 320,
		    height: 250,
            resizable: false,	
            bgiframe: true,		
            modal: true
	    });
	}
}

function openBuyMedicineSnedMobileDialog(recordId, completeFunction, param)
{
    initBuyMedicineSnedMobileDialog();
    
    $("#buy_medicine_send_mobile_dialog").dialog("option", "buttons", {
		 " 取消 ": function() {
				$("#buy_medicine_send_mobile_dialog").dialog("close");
		    },
	    " 提交 ": function() {
            
            var content = $("#buy_medicine_send_mobile").val();
            var sure_content = $("#sure_buy_medicine_send_mobile").val();
            if (content.length == 0)
            {
                $("#buy_medicine_send_mobile_dialog_validate_tips").text("请输入手机号码。").css("color", "red");
                return;
            }

            if (content.length != 11)
            {
                $("#buy_medicine_send_mobile_dialog_validate_tips").text("手机号码格式有误，请检查。").css("color", "red");
                return;
            }
            if(content.substring(0,2) != 13 && content.substring(0,2) != 15 && content.substring(0,2) != 18)
            {
                $("#buy_medicine_send_mobile_dialog_validate_tips").text("手机号码格式有误，请检查。").css("color", "red");
                return;
            }
            
            if(content != sure_content)
            {
                $("#buy_medicine_send_mobile_dialog_validate_tips").text("两次输入手机号码不一致，请检查。").css("color", "red");
                return;
            }
            
            reg=/^(\+\d{2,3}\-)?\d{11}$/;    
            if(!reg.test(content))
            {   
               $("#buy_medicine_send_mobile_dialog_validate_tips").text("请输入正确的手机号码。").css("color", "red");
               return;
            }
            
	        var result = ajax_sendBuyMedicineMobile(recordId, content);
	        if (result == "1")
	        {
	            $("#buy_medicine_send_mobile_dialog").dialog("close");
	            
	            if (completeFunction)
	                completeFunction(param);
	        }
	        else
	        {
	            $("#buy_medicine_send_mobile_dialog_validate_tips").text(result).css("color", "red");
	        }
        }
    });
    
    $("#buy_medicine_send_mobile_dialog").dialog("option", "open", function(){
     	$("#buy_medicine_send_mobile_dialog_validate_tips").text("注册用户每天每款药品只能下载一条优惠券，请准确填写手机号码。").css("color", "");
    });
    
    $("#buy_medicine_send_mobile_dialog").dialog("open");
}

function sendBuyMedicineMobile(recordId)
{
    if (ajax_isLogined() == "1")
    {
        sendBuyMedicineMobile_step2(recordId);
    }
    else
    {
        openLoginDialog(sendBuyMedicineMobile_step2, recordId);
    }
}

function sendBuyMedicineMobile_step2(recordId)
{
    var getMobile = "" + ajax_getBuyMedicineMobile(recordId);
    
    if(getMobile.length > 10)
    {
        sendBuyMedicineMobile2_step2(recordId);
    }
    else
    {
        openBuyMedicineSnedMobileDialog(recordId, sendBuyMedicineMobile2_step3);
    }
}

function ajax_sendBuyMedicineMobile(recordId, content)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_buy_medicine_send_mobile.aspx", 
            {   "action": "add", 
                "recordId": recordId, 
                "content" : content
             });
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}


//再次发送手机号码
function initBuyMedicineSnedMobileDialog2()
{
    if ($("#buy_medicine_send_mobile_dialog2").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"buy_medicine_send_mobile_dialog2"}));
                
        $("#buy_medicine_send_mobile_dialog2").dialog({
		    autoOpen: false,
		    width: 320,
		    height: 180,
            resizable: false,	
            bgiframe: true,		
            modal: true
	    });
	}
}

function openBuyMedicineSnedMobileDialog2(recordId, completeFunction, param)
{
    initBuyMedicineSnedMobileDialog2();
    
    $("#buy_medicine_send_mobile_dialog2").dialog("option", "buttons", {
		 " 取消 ": function() {
				$("#buy_medicine_send_mobile_dialog2").dialog("close");
		    },
	    " 提交 ": function() {
            
            var content = $("#buy_medicine_send_mobile_2").html();
            
	        var result = ajax_sendBuyMedicineMobile(recordId, content);
	        if (result == "1")
	        {
	            $("#buy_medicine_send_mobile_dialog2").dialog("close");
	            
	            if (completeFunction)
	                completeFunction(param);
	        }
	        else
	        {
	            $("#buy_medicine_send_mobile_dialog2_validate_tips").text(result).css("color", "red");
	        }
        }
    });
    
    $("#buy_medicine_send_mobile_dialog2").dialog("option", "open", function(){
     	$("#buy_medicine_send_mobile_dialog2_validate_tips").text("注册用户每天每款药品只能下载一条优惠券，点击提交按钮发送手机短信。").css("color", "");
     	
     	var getMobile = "" + ajax_getBuyMedicineMobile(recordId);
        
        $("#buy_medicine_send_mobile_2").html(getMobile);
        
    });
    

    
    $("#buy_medicine_send_mobile_dialog2").dialog("open");
}


function sendBuyMedicineMobile2_step2(recordId)
{
    openBuyMedicineSnedMobileDialog2(recordId, sendBuyMedicineMobile2_step3);
}

function sendBuyMedicineMobile2_step3()
{
    showMessageDialog('短信已成功发送，系统繁忙时会有3-5分钟的发送延迟，请不要在短时间内重复发送。', reload);
}

function ajax_getBuyMedicineMobile(recordId)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
            "/ajax_buy_medicine_send_mobile.aspx", 
            {   "action": "getmobile", 
                "recordId": recordId
             });
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

//发送邮件
function initBuyMedicineSnedEmailDialog()
{
    if ($("#buy_medicine_send_email_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"buy_medicine_send_email_dialog"}));
                
        $("#buy_medicine_send_email_dialog").dialog({
		    autoOpen: false,
		    width: 320,
		    height: 210,
            resizable: false,	
            bgiframe: true,		
            modal: true
	    });
	}
}

function openBuyMedicineSnedEmailDialog(medicineId,type, completeFunction, param)
{
    initBuyMedicineSnedEmailDialog();
    
    $("#buy_medicine_send_email_dialog").dialog("option", "buttons", {
		 " 取消 ": function() {
				$("#buy_medicine_send_email_dialog").dialog("close");
		    },
	    " 提交 ": function() {
            
            var content = $("#buy_medicine_send_email").val();
            
            if (content.length == 0)
            {
                $("#buy_medicine_send_email_dialog_validate_tips").text("请输入邮箱地址。").css("color", "red");
                return;
            }
            if (content.length > 200)
            {
                $("#buy_medicine_send_email_dialog_validate_tips").text("邮箱地址过长。").css("color", "red");
                return;
            }
            ////
            reg=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;    
            if(!reg.test(content))
            {   
               $("#buy_medicine_send_email_dialog_validate_tips").text("邮箱格式有误，请输入正确的邮箱地址。").css("color", "red");
               return;
            }
            
	        var result = ajax_sendBuyMedicineEmail(medicineId, content, type);
	        if (result == "1")
	        {
	            $("#buy_medicine_send_email_dialog").dialog("close");
	            
	            if (completeFunction)
	                completeFunction(param);
	        }
	        else
	        {
	            $("#buy_medicine_send_email_dialog_validate_tips").text(result).css("color", "red");
	        }
        }
    });
    
    $("#buy_medicine_send_email_dialog").dialog("option", "open", function(){
        
        if(type == "type")
        {
            $("#buy_medicine_send_email_dialog_validate_tips").text("注册用户每天每款药品只能下载一条优惠券，请准确填写邮箱地址500返60。").css("color", "");
        }
        else
        {
            $("#buy_medicine_send_email_dialog_validate_tips").text("注册用户每天每款药品只能下载一条优惠券，请准确填写邮箱地址100返10。").css("color", "");
        }
    
     	var getEmail = "" + ajax_getBuyMedicineEmail();
        
        $("#buy_medicine_send_email").val(getEmail);
        
    });
    
    $("#buy_medicine_send_email_dialog").dialog("open");
}

//onlyEmail
function openBuyMedicineSnedOnlyEmailDialog(type, completeFunction, param)
{
    initBuyMedicineSnedEmailDialog();
    
    $("#buy_medicine_send_email_dialog").dialog("option", "buttons", {
		 " 取消 ": function() {
				$("#buy_medicine_send_email_dialog").dialog("close");
		    },
	    " 提交 ": function() {
            
            var content = $("#buy_medicine_send_email").val();
            
            if (content.length == 0)
            {
                $("#buy_medicine_send_email_dialog_validate_tips").text("请输入邮箱地址。").css("color", "red");
                return;
            }
            if (content.length > 200)
            {
                $("#buy_medicine_send_email_dialog_validate_tips").text("邮箱地址过长。").css("color", "red");
                return;
            }
            ////
            reg=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;    
            if(!reg.test(content))
            {   
               $("#buy_medicine_send_email_dialog_validate_tips").text("邮箱格式有误，请输入正确的邮箱地址。").css("color", "red");
               return;
            }
            
	        var result = ajax_sendBuyMedicineOnlyEmail(content, type);
	        if (result == "1")
	        {
	            $("#buy_medicine_send_email_dialog").dialog("close");
	            
	            if (completeFunction)
	                completeFunction(param);
	        }
	        else
	        {
	            $("#buy_medicine_send_email_dialog_validate_tips").text(result).css("color", "red");
	        }
        }
    });
    
    $("#buy_medicine_send_email_dialog").dialog("option", "open", function(){
        
        if(type == "type")
        {
            $("#buy_medicine_send_email_dialog_validate_tips").text("注册用户每天每款药品只能下载一条优惠券，请准确填写邮箱地址500返60。").css("color", "");
        }
        else
        {
            $("#buy_medicine_send_email_dialog_validate_tips").text("注册用户每天每款药品只能下载一条优惠券，请准确填写邮箱地址100返10。").css("color", "");
        }
    
     	var getEmail = "" + ajax_getBuyMedicineEmail();
        
        $("#buy_medicine_send_email").val(getEmail);
        
    });
    
    $("#buy_medicine_send_email_dialog").dialog("open");
}

function sendBuyMedicineEmail(medicineId,type)
{
    if (ajax_isLogined() == "1")
    {
        sendBuyMedicineEmail_step2(medicineId,type);
    }
    else
    {
        openLoginDialog(sendBuyMedicineEmail_step2, medicineId,type);
    }
}

//onlyEmail
function sendBuyMedicineOnlyEmail(type)
{
    if (ajax_isLogined() == "1")
    {
        sendBuyMedicineOnlyEmail_step2(type);
    }
    else
    {
        openLoginDialog(sendBuyMedicineOnlyEmail_step2,type);
    }
}

function sendBuyMedicineEmail_step2(medicineId,type)
{
    openBuyMedicineSnedEmailDialog(medicineId,type, reload);
}

//onlyEmail
function sendBuyMedicineOnlyEmail_step2(type)
{
    openBuyMedicineSnedOnlyEmailDialog(type, sendBuyMedicineOnlyEmail_step3);
}

//onlyEmail
function sendBuyMedicineOnlyEmail_step3()
{
    showMessageDialog('邮件已经发送成功。', reload);
}


function ajax_sendBuyMedicineEmail(medicineId, content, type)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_buy_medicine_send_email.aspx", 
            {   "action": "add", 
                "medicineId": medicineId, 
                "content" : content,
                "type":type
             });
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

//onlyemail
function ajax_sendBuyMedicineOnlyEmail(content, type)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_buy_medicine_send_email.aspx", 
            {   "action": "onlyEmail", 
                "content" : content,
                "type":type
             });
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_getBuyMedicineEmail()
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
            "/ajax_buy_medicine_send_email.aspx", 
            {   "action": "getemail"
             });
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

//订单药品数量限制
function checkInputMedicineCount(upper_count)
{
    if ("" + $("#medicine_count_txt").val() == "")
    {
        showMessageDialog("请输入药品购买数量。");
        $("#medicine_count_txt").focus();
        return;    
    }
    
    reg=/^[0-9]*[1-9][0-9]*$/;    
    if(!reg.test("" + $("#medicine_count_txt").val()))
    {   
        showMessageDialog("请正确输入药品购买的数量。");
        $("#medicine_count_txt").focus();
        return;    
    }
    
    if("" + $("#medicine_count_txt").val() > upper_count)
    {
        showMessageDialog("本单限购"+ upper_count +"盒。" );
        $("#medicine_count_txt").focus();
        return;    
    }
    else
    {
        getMedicineTotalCount();
    }
    
}

function getMedicineTotalCount()
{
    $("#medicine_total_price").html(Math.round(($("#medicine_unit_price").html() * $("#medicine_count_txt").val())*100)/100);
}

function userMedicineOrderInformationSubmit(upper_count)
{
    
    if ("" + $("#medicine_count_txt").val() == "")
    {
        showMessageDialog("请输入药品购买数量。");
        $("#medicine_count_txt").focus();
        return false;    
    }
    
    reg=/^[0-9]*[1-9][0-9]*$/;    
    if(!reg.test("" + $("#medicine_count_txt").val()))
    {   
        showMessageDialog("请正确输入药品购买的数量。");
        $("#medicine_count_txt").focus();
        return false;    
    }
    
    if("" + $("#medicine_count_txt").val() > upper_count)
    {
        showMessageDialog("本单限购"+ upper_count +"盒。" );
        $("#medicine_count_txt").focus();
        return false; 
    }

    if ("" + $("#realName_txt").val() == "")
    {
        showMessageDialog("请输入姓名。");
        $("#realName_txt").focus();
        return false;
    }
    
    if ("" + $("#telephone_txt").val() == "")
    {
        showMessageDialog("请输入联系电话。");
        $("#telephone_txt").focus();
        return false;
    }
    
    if ("" + $("#email_txt").val() == "")
    {
        showMessageDialog("请输入电子邮件。");
        $("#email_txt").focus();
        return false;
    }
    
    reg=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;    
    if(!reg.test("" + $("#email_txt").val()))
    {   
       showMessageDialog("电子邮件格式有误，请输入正确的电子邮件地址。");
       $("#email_txt").focus();
       return false;
    }
    
    return true;
}


