﻿if (typeof (jQuery) == 'undefined') {
    //alert('set');
    var s = document.createElement('script');
    s.setAttribute('type', 'text/javascript');
    s.setAttribute('src', '../Common/Js/jquery.min.js');
    var head = document.getElementsByTagName('head')[0];
    if (head) head.appendChild(s);
    else document.body.appendChild(s);

}

jQuery(function($) {    
    AutoIFrameHeight();    
    SetIFrameBk();    
    setTimeout(AutoIFrameHeight, 1000);    
});

function AutoIFrameHeight() {
    var objIframe = $(".autoHeight");
    for (var i = 0; i < objIframe.length; i++) {
        dyniframesize(objIframe[i]);
    }    
    /*
        $(this).height($(this).contents().find("body").height() + 50);
    */
}

function GetHeight(obj) {
    var h = obj.contents().find("body").height() + 50;
    if (h = 50) return 400;
    return h;
}

function dyniframesize(pTar) {
    try {
        if (pTar && !window.opera) {
            if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight) {
                //ns6 syntax
                pTar.height = pTar.contentDocument.body.offsetHeight + 50;
            }
            else {
                if (pTar.Document && pTar.Document.body.scrollHeight) {
                    //ie5+ syntax
                    pTar.height = pTar.Document.body.scrollHeight + 50;
                }
            }
        }
    } catch (Error) {
    }
}

function SetIFrameBk() {
    if (location.hash.length > 0) {
        var objIframe = $(".autoHeight");        
        objIframe.attr("src", function() {
            this.src = this.src + location.hash;
            //alert(this.src);
        });
    }
}


function SetShow(value,objItem,speed){
  //alert(value.html());
  if (value.html().length > 0){
    objItem.fadeIn(speed);
  } else {
    objItem.fadeOut(speed);
  }
}

function SetTextRequired(value,obj){
  if (value.html().length > 0){
    obj.addClass("Required");
  } else {
    obj.removeClass("Required");
  }
}

function SetShowHide(valueStr, objItem) {
  if ((valueStr.length <= 0) || (valueStr == "0") || (valueStr == "False") || (valueStr == false)){
    objItem.hide();
  } else {
    objItem.show();
  }
}

//看需要檢查的欄位，是不是都有值
function isRequired() {
    //要找class=Required type="text"
    var errMsg = GetRequiredErrMsg();
    obj = $("[class*='ReqDate']");
    obj.removeClass("ReqShowStyle");
    for (i = 0; i < obj.length; i++) {
        //alert(obj[i].value);
        if (obj[i].value.length > 0) {
            if (!isDate(obj[i].value)) {
                if (errMsg.length <= 0) {
                    obj[i].focus();
                }
                errMsg += "．日期格式錯誤!請依照格式輸入日期：[ yyyy/MM/dd ]";
                $("#" + obj[i].id).addClass("ReqShowStyle");
            }
        }
    }

    if (errMsg.length > 0) {        
        alert("親愛的賣家要記得填以下的欄位才可以繼續哦！\n\n" + errMsg);
        return false;
    } else {
        return true;
    }
}

function isHaveErrWord(word) {
  return (word.search("<") >= 0) || (word.search(">") >= 0);
}

function ClassHaveErrWord(classTag) {    
    var obj = $(classTag);
    var isHave = false;
    obj.removeClass("ReqShowStyle");
    for (i = 0; i < obj.length; i++) {
        if (isHaveErrWord(obj[i].value)) {
            $("#" + obj[i].id).addClass("ReqShowStyle");
            isHave = true;
        }
    }
    return isHave;
}

function GetRequiredErrMsg() {
    //要找class=Required type="text" 
    var errMsg = "";

    var obj = $("input[type$='text'][class*='Required']");
    //var obj = $("input[type$='text'],.Required"); 
    obj.removeClass("ReqShowStyle");
    for (i = 0; i < obj.length; i++) {
        if (obj[i].value.length <= 0) {
            if (errMsg.length <= 0) {
                obj[i].focus();
            }
            errMsg += GetRequiredWord(obj[i].id);
            $("#" + obj[i].id).addClass("ReqShowStyle");
        } else if (isHaveErrWord(obj[i].value)) {
        errMsg += GetRequiredWord(obj[i].id).replace("\n", "") + "請勿輸入特殊字元，例如：<,>\n";
            $("#" + obj[i].id).addClass("ReqShowStyle");
        }
    }

    //CheckHtmlValue
    var obj = $("[class*='CheckValue']");
    //var obj = $("input[type$='text'],.Required");
    obj.removeClass("ReqShowStyle");
    for (i = 0; i < obj.length; i++) {
        //alert(obj[i]);
         if (isHaveErrWord(obj[i].value)) {
             errMsg += GetRequiredWord(obj[i].id).replace("\n", "") + "請勿輸入特殊字元，例如：<,>\n";
            $("#" + obj[i].id).addClass("ReqShowStyle");
        }
    }
    
    //select
    obj = $("select[class*='Required']");
    obj.removeClass("ReqShowStyle");
    for (i = 0; i < obj.length; i++) {
        if ((obj[i].length > 1) && (obj[i].selectedIndex <= 0)) {
            if (errMsg.length <= 0) {
                obj[i].focus();
            }            
            errMsg += GetRequiredWord(obj[i].id);
            $("#" + obj[i].id).addClass("ReqShowStyle");
        }
    }

    //radio 確認所有的radio，
    try {
        obj = $("span[class*='Required']>input[type=radio]");
        if (obj.length > 0) {
            var oldName = "";
            for (i = 0; i < obj.length; i++) {
                if (oldName != obj[i].name) {
                    var chkObj = $("span[class*='Required']>input[name=" + obj[i].name + "]:checked").val();
                    oldName = obj[i].name;
                    if (chkObj == undefined) {
                        var id = obj[i].id.replace("_0", "");
                        errMsg += GetRequiredWord(id);
                        $("#" + id).addClass("ReqShowStyle");
                    } else {
                        var id = obj[i].id.replace("_0", "");
                        $("#" + id).removeClass("ReqShowStyle");
                    }
                }
            }            
        }
    } catch (e) {
        alert(e);
    }

    obj = $("span[class*='Required']>input[type=hidden]");
    if (obj.length > 0) {
        for (i = 0; i < obj.length; i++) {
            var id = obj[i].id.replace("_0", "");
            if (obj[i].value.length == 0) {                
                errMsg += GetRequiredWord(id);
                $("." + id).addClass("ReqShowStyle");                
            } else {            
               $("." + id).removeClass("ReqShowStyle");
            }                   
        }
    }
    return errMsg;
}


function GetRequiredWord(id){
 var word = $("#" + id + "Required").text();
 if (word != ""){
   return "．" + word + "\n";
 } else {
   return "." + id + "\n";
 }
}

function isNetRequired(source, arguments) {
    if (GetRequiredErrMsg().length == 0) {
        arguments.IsValid = true;
    } else {
        arguments.IsValid = false;
    }
}


function SetDivRound(className){
 //$("[class*='" + className + "']").corner("5px");
  //$("['." + className +"']").corner("round");
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function AddMoneyCommom(num){
num = num+"";
var re=/(-?\d+)(\d{3})/
while(re.test(num))
{
num=num.replace(re,"$1,$2")
}
return num; 
}

function onCartNextClick(){
    //如果值超過一個，不能選 0  
    //var Sels = window.document.getElementsByTagName("SELECT");
    var Sels = $("input[class*='Required']");
    for(var i = 0; i < Sels.length; i++)
    {        
        if ((Sels[i].length <= 0)  || (parseInt(Sels[i].value,0) <= 0) || (isNaN(parseInt(Sels[i].value)))){
          alert('請輸入正確的購買數量！');
          return false;
	    }
    }
    Sels = $(".NumEnd");
    if (Sels.length > 0){
      alert('有物品已售完，請刪除該物品！');
      return false;
    }
    return true;
}

function optionsClear(object)  
{   
    var length = object.options.length;
    if (length <= 0) return;
    for(var i=length-1;i>=0;i--){
        e.options.remove(i);
    }
}

function addOption(object)
{  
    object.add(new Option(label,value));
    //使用options集合中最後一項獲取焦點
    //object.selectedIndex = object.lentht-1;
}

function SyncHeight(sName, toName) {    
    $("#" + toName).height($("#" + sName).height());
}

function ClickSetRadio(id) {
  $("#" + id).get(0).checked = true;
}

var win = null;
function newWindow(mypage,myname,w,h,features) {
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',width=' + w + ',';
  settings  += 'top=' +  wint +  ',';
  settings  += 'left=' +  winl +  ',' +features;
  win = window.open(mypage,myname,settings);
  win.window.focus();
}


function AskReturn() {
    return confirm('這筆訂單已退貨過，確定還有其它物品要退貨嗎？');
}

function getCookie(c_name) {    
    if (document.cookie.length > 0) {
        var c_list = document.cookie.split("\;");        
        for (i in c_list) {
            var cook = c_list[i].split("=");            
            if (cook[0].replace(' ','') == c_name) {
                return unescape(cook[1]);
            }
        }
    }
    return null
}

function setCookie(c_name, value, expiredays)   
{
    var exdate = new Date()                        
    exdate.setDate(exdate.getDate() + expiredays)  
    document.cookie = c_name + "=" + escape(value) +  
    ((expiredays == null) ? "" : ";expires=" + exdate)
}

function QueryString(name) {    
    var AllVars = window.location.search.substring(1);
    var Vars = AllVars.split("&");
    for (i = 0; i < Vars.length; i++) {
        var Var = Vars[i].split("=");
        if (Var[0] == name) return Var[1];
    }
    return "";
}

function openDoor(page,param)
{ 
  window.open(page,"doorSel",param);
}

function cvsOpenDoor(){
  openDoor("../Common/CvsDoorSelDlg.aspx","height=650, width=850, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no");
}

function SevenOpenDoor() {
    openDoor("../Common/SevenDoorSelDlg.aspx", "height=580, width=1000, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no");
}

function isEmail(emailtoCheck) {    
    var regExp = /^[^@^\s]+@[^\.@^\s]+(\.[^\.@^\s]+)+$/;    
    if (emailtoCheck.match(regExp)) 
        return true;
    else
        return false;
}

function CheckAuid(obj) {
    obj.removeClass("ReqShowStyle");  
    var value = obj[0].value.trim();
    if (value.length <= 0) {
        obj.addClass("ReqShowStyle");  
        alert('請輸入拍賣代號');
        return false;
    }
    var re = /(^Y|^y)\d{10}$/;
	 if (!re.test(value)){
	 obj.addClass("ReqShowStyle"); 
	  alert("請輸入正確的拍賣代號");
	  return false;
	 }
    return true;
}

function bookmarksite() {
    if (document.all) {
        // For IE
        window.external.AddFavorite(location.href, document.title);
    }
    else if (window.sidebar) {
        // For Firefox
        window.sidebar.addPanel(document.title, location.href, "");
    }
}


function ImgLoad(imgLoad, imgTar, url) {
    imgTar.addClass("LoadingIcon");
    imgTar.attr("src", "../common/Images/shop/loadingPhoto.gif");    
    imgLoad.unbind("load");
    imgLoad.show();
    imgLoad.one("load",function() {
        imgTar.removeClass("LoadingIcon");                
        imgTar.attr("src", url);        
        imgLoad.hide();
    });
    imgLoad.attr("src", url);
}
