// JScript File
   var t;
   var isNeck = false;
   var mWidth = 95;
   var mHeight = 80;
   var radio = 2.3;
    
  function resetPreview()
  {
       if(t != null)
       {
	    //Event.unloadCache();
	      
            //t.previewImg.src = 'ProImgbyCode.aspx?ProductCode=' + document.getElementById("Ori_ProductCode").value + '&PicType='+ document.getElementById("enlargeType").value;

            t.reset();
        }
      
    
  }
  
  var imgW = 310;
  var imgH = 310;
  
  /*function setAreaCoords(_20, _21, _22, _23, _24) {
	if (_21) {
		var _25 = _20.x2 - _20.x1;
		var _26 = _20.y2 - _20.y1;
		if (_20.x1 < 0) {
			_20.x1 = 0;
			_20.x2 = _25;
		}
		if (_20.y1 < 0) {
			_20.y1 = 0;
			_20.y2 = _26;
		}
		if (_20.x2 > imgW) {
			_20.x2 = imgW;
			_20.x1 = imgW - _25;
		}
		if (_20.y2 > imgH) {
			_20.y2 = imgH;
			_20.y1 = imgH - _26;
		}
	} else {
		if (_20.x1 < 0) {
			_20.x1 = 0;
		}
		if (_20.y1 < 0) {
			_20.y1 = 0;
		}
		if (_20.x2 > imgW) {
			_20.x2 = imgW;
		}
		if (_20.y2 > imgH) {
			_20.y2 = imgH;
		}
		if (_23 != null) {
			if (this.ratioX > 0) {
				this.applyRatio(_20, {x:this.ratioX, y:this.ratioY}, _23, _24);
			} else {
				if (_22) {
					this.applyRatio(_20, {x:1, y:1}, _23, _24);
				}
			}
			var _27 = [this.options.minWidth, this.options.minHeight];
			var _28 = [this.options.maxWidth, this.options.maxHeight];
			if (_27[0] > 0 || _27[1] > 0 || _28[0] > 0 || _28[1] > 0) {
				var _29 = {a1:_20.x1, a2:_20.x2};
				var _2a = {a1:_20.y1, a2:_20.y2};
				var _2b = {min:0, max:this.imgW};
				var _2c = {min:0, max:this.imgH};
				if ((_27[0] != 0 || _27[1] != 0) && _22) {
					if (_27[0] > 0) {
						_27[1] = _27[0];
					} else {
						if (_27[1] > 0) {
							_27[0] = _27[1];
						}
					}
				}
				if ((_28[0] != 0 || _28[0] != 0) && _22) {
					if (_28[0] > 0 && _28[0] <= _28[1]) {
						_28[1] = _28[0];
					} else {
						if (_28[1] > 0 && _28[1] <= _28[0]) {
							_28[0] = _28[1];
						}
					}
				}
				if (_27[0] > 0) {
					this.applyDimRestriction(_29, _27[0], _23.x, _2b, "min");
				}
				if (_27[1] > 1) {
					this.applyDimRestriction(_2a, _27[1], _23.y, _2c, "min");
				}
				if (_28[0] > 0) {
					this.applyDimRestriction(_29, _28[0], _23.x, _2b, "max");
				}
				if (_28[1] > 1) {
					this.applyDimRestriction(_2a, _28[1], _23.y, _2c, "max");
				}
				_20 = {x1:_29.a1, y1:_2a.a1, x2:_29.a2, y2:_2a.a2};
			}
		}
	}
	areaCoords = _20;
  }*/
  
  
  
 function createPreview(web_pic_rul) {  
        if(t == null)
        {
            t = new Cropper.ImgWithPreview( 
                    'testImage', 
                    { 
	                    radio: radio,
	                    minWidth: mWidth, 
	                    minHeight: mHeight,
	                    maxWidth:mWidth,
	                    maxHeight: mHeight,
	                    previewWrap: 'previewArea',
			    		previewSrc: web_pic_rul
                    } 
                ); 
       }
	} 
// example with a preview of crop results, must have minimumm dimensions
	function observe_pic()
	{
	  /* Event.stopObserving( 
			document.getElementById("pbigImg"), 
			'mouseover', createPreview()
		);*/
	   // Event.unloadCache();
	   //t.previewWrap.style.display = "block";
	   Event.observe(
	        window,'load',createPreview
	   );
	   
		Event.observe( 
			document.getElementById("testImage"), 
			'mouseover', createPreview
		);
	}
	
	
	
    function setPreviewWrapPos()
    {
       var previewArea =  document.getElementById("previewArea");
       var bigImg = document.getElementById("pbigImg");
       var pleft = 0;
       var ptop = 0;
       var obj = bigImg.offsetParent;
       while(obj.tagName != "BODY")
       {
            pleft = pleft + obj.offsetLeft;
            ptop = ptop + obj.offsetTop;
            obj = obj.offsetParent;
       }
       //此处用于调整放大镜效果 处于左边还是右边
       pleft = pleft + 310/2+document.getElementById("testImage").width/2;
     //pleft = pleft-890/2+document.getElementById("testImage").width/2;   
       ptop = ptop + document.getElementById("testImage").height - parseInt(previewArea.style.height);

       previewArea.style.top = ptop + "px";
       previewArea.style.left = pleft + "px";
    }
    
    function chooseMonogram(){
        if(document.getElementById("Monogram").checked){
            showMonogram();
            $j("#status").slideUp('fast');
            $j("#monogramLayer").slideDown('fast');
        }
        else{
            if(document.getElementById("IsCompleted").value=="T"){
                document.getElementById("IsCompleted").value = "F";
                document.getElementById("status").innerHTML = "个性化绣花已删除";
                $j("#status").slideDown('fast');
            }
            $j("#monogramLayer").slideUp('fast');
        }
        
    }
    
     function monogramApply(){
        var strPosition = document.getElementById("position").value;
        var fontsize    = document.getElementById("fontsize").value;
        var monogramTxt = document.getElementById("monogramTxt").value;
        var price       = document.getElementById("monogramPrice").innerHTML;
        
        var id = document.getElementById("ApID").value;
        
        if (strPosition == "")
        {
            alert("请输入您要绣花的位置");
            return;
        }
        
        if (fontsize == "")
        {
            alert("请输入您要绣花的字体");
            return;
        }
        
        if(monogramTxt.replace(/(^\s*)|(\s*$)/g, "")!=""){
            if(ValidateTxt(monogramTxt, fontsize, strPosition))
            {
                document.getElementById("monogramLayer").innerHTML = "<br />数据传送中......<br /><img src='../img/Package/ajax-loader.gif'>";
                $j.post("MonogramHandle.aspx", { ShowMonogram: "Submit",ApID: id,Position:strPosition,fontSize:fontsize,MTxt:monogramTxt,Price:price}, function(data){ document.getElementById("monogramLayer").innerHTML= data;} );
                document.getElementById("IsCompleted").value = "T";
             }
        }
        else{
            alert("请输入您要绣花的文字!");
            return;
        }
    } 
    
    function ValidateTxt(str, size, pos)
    {
        //首先判断字符合法性
        var isValid = 0;
        var arr_str = "";//1为大写字，0为小写字
        
        for(var i = 0; i < str.length; i++)
        {
            var ascValue = str.charCodeAt(i);
            if((ascValue > 64 && ascValue < 91)) 
            {
                 arr_str = arr_str + "1";
                isValid = 1;
            }
            else if ((ascValue > 96 && ascValue < 123))
            {
                arr_str = arr_str + "0";
                isValid = 1;
            }
            else if ( ascValue == 32 || ascValue == 46)//空格和点
            {
                arr_str = arr_str + "0";
                isValid = 1;
            }
            else
            {
                isValid = 0;
                break;
            }
        }
        if(!isValid)
        {
            alert("您输入的文字含有非字母的其他符号，请重新输入");
            return isValid;
        }
        
       
        //判断其余是否小写
       var isAllUp;
       if(arr_str.lastIndexOf(1) > 0)
       {
            isAllUp = 1;
       }
       else
       {
            isAllUp = 0;
       }
       
       var maxLength = getMaxLength(pos, size, isAllUp);
      
       if(str.length > maxLength)
       {
          alert("您输入的文字长度超过该字体在该位置的最大长度:"+ maxLength +"，请重新输入");
          isValid = 0;
          return isValid;
       }
       
       return 1;
    }
    
  function clearClass(tabId)
    {
        var tab = document.getElementById(tabId);
        for(var i = 0;i < tab.rows.length;i++)
        {
            if(i != 0 )
            {
                var row = tab.rows[i];
                for(var j=0;j < row.cells.length;j++)
                {
                    var cel = row.cells[j];
                    if(document.getElementById("color"+ cel.id) != null)
                        document.getElementById("color" + cel.id).className = "normal";
                }
            }
        }
    }
   
   //type=1, 尺寸触发 type=2 数量触发
    function display_balance(type)
    {
       var sizeList = document.getElementById("sizeList");
       if (sizeList == null)
           return;
       var size = sizeList.options[sizeList.selectedIndex].value;
       if ( type == 1)
       {
            var OriProductCodeCtrl = document.getElementById("Ori_ProductCode");
            var strOriProductCode = "";
            if (OriProductCodeCtrl!=null)
             strOriProductCode = OriProductCodeCtrl.value;

             if(searchType == null || (searchType != "4" && searchType != "2"))
             {
                   $j.post("ClearanceProductPriceHandler.aspx", {ProductID: size, OriProductCode:strOriProductCode}, function(data)
                   {
                        if (data != null && data != "")
                            document.getElementById("pcurprice").innerHTML= data;
                   });
               }
               else
               {
                   $j.post("ClearanceProductPriceHandler.aspx", {ProductID: size, OriProductCode:strOriProductCode,searchType:searchType}, function(data)
                   {
                        if (data != null && data != "")
                            document.getElementById("pcurprice").innerHTML= data;
                   });
               }
       }
       var amount = document.getElementById("Amount").value;
       if(checkInt(amount))
       {
       // document.getElementById("statusLayer").innerHTML = "<br />数据读取中......<br /><img src='../img/Package/ajax-loader.gif'>";
           if (size < 0)
           {
                document.getElementById("statusLayer").innerHTML="";
           }
           else
           {
               $j.post("ProductStatusHandler.aspx", {ProductID: size, Amount: amount,SearchType:searchType}, function(data)
               { 
                    var arr = data.split("||");
                    document.getElementById("statusLayer").innerHTML= arr[0];
                    if(document.getElementById("productStatus") != null)
                        document.getElementById("productStatus").value = Trim(arr[1]);
               } );
           }
       }
      
    }
    
   function Trim(value){
	var res = String(value).replace(/^[\s]+|[\s]+$/g,'');
	return res;
}

    function checkInt(str) 
    { 
        var i; 
        var len = str.length; 
        
        if(len <= 0)
            return false;   
            
        var chkStr = "1234567890"; 
        if (len == 1) 
        { 
            if (chkStr.indexOf(str.charAt(i)) < 0) 
            { 
                return false; 
            } 
        } 
        else 
        { 
            if ((chkStr.indexOf(str.charAt(0)) < 0)) 
            { 
                 return false; 
            } 
            for (i = 1; i < len; i++) 
            { 
                 if (chkStr.indexOf(str.charAt(i)) < 0) 
                 { 
                    return false; 
                 } 
            } 
        } 
        return true; 
    }

 function getMaxLength(pos, size, isAllUp)
    {
       var maxLength = 0;
        
      
       if((pos == "左袖口绣花" || pos == "右袖口绣花" || pos == "后背绣花") )
       {
            if(isAllUp == 1)
            {
                switch(size)
                {
                    case "1":
                        maxLength = 6;
                        break;
                    case "2":
                        maxLength = 4;
                        break;
                    case "3":
                        maxLength = 5;
                        break;
                    case "4":
                        maxLength = 7;
                        break;
                    case "5":
                        maxLength = 5;
                        break;
                    case "6":
                        maxLength = 4;
                        break;
                    case "7":
                        maxLength = 5;
                        break;
                    case "8":
                        maxLength = 10;
                        break;
                    case "9":
                        maxLength = 4;
                        break;
                }
           }
           else
           {
                
                switch(size)
                {
                    case "1":
                        maxLength = 7;
                        break;
                    case "2":
                        maxLength = 5;
                        break;
                    case "3":
                        maxLength = 7;
                        break;
                    case "4":
                        maxLength = 10;
                        break;
                    case "5":
                        maxLength = 6;
                        break;
                    case "6":
                        maxLength = 5;
                        break;
                    case "7":
                        maxLength = 6;
                        break;
                    case "8":
                        maxLength = 13;
                        break;
                    case "9":
                        maxLength = 6;
                        break;
               }
           }
       }
       else
       {
            if(isAllUp == 1)
            {
                switch(size)
                {
                    case "1":
                        maxLength = 10;
                        break;
                    case "2":
                        maxLength = 7;
                        break;
                    case "3":
                        maxLength = 6;
                        break;
                    case "4":
                        maxLength = 10;
                        break;
                    case "5":
                        maxLength = 6;
                        break;
                    case "6":
                        maxLength = 6;
                        break;
                    case "7":
                        maxLength = 7;
                        break;
                    case "8":
                        maxLength = 15;
                        break;
                    case "9":
                        maxLength = 6;
                        break;
                }
            }
            else
            {
                switch(size)
                {
                    case "1":
                        maxLength = 11;
                        break;
                    case "2":
                        maxLength = 8;
                        break;
                    case "3":
                        maxLength = 9;
                        break;
                    case "4":
                        maxLength = 13;
                        break;
                    case "5":
                        maxLength = 8;
                        break;
                    case "6":
                        maxLength = 7;
                        break;
                    case "7":
                        maxLength = 8;
                        break;
                    case "8":
                        maxLength = 16;
                        break;
                    case "9":
                        maxLength = 8;
                        break;
                 }
            }
       }
     
        return maxLength;
    }
   
     function positionChange(){
        var strPosition = document.getElementById("position").value;
        if(strPosition == "左袖口绣花" || strPosition == "右袖口绣花"){
            document.getElementById("monogramPrice").innerHTML="30.00";
        }
        else if (strPosition == "")
        {
            document.getElementById("monogramPrice").innerHTML = "";
        }
        else
        {
            document.getElementById("monogramPrice").innerHTML="10.00";
        }
    }
   
   
       function showMonogram(){
        var id = document.getElementById("ApID").value;
        
        document.getElementById("monogramLayer").innerHTML = "<br />数据读取中......<br /><img src='../img/Package/ajax-loader.gif'>";
        $j.post("MonogramHandle.aspx", { ShowMonogram: "Form",ApID: id}, function(data){ document.getElementById("monogramLayer").innerHTML= data;} );
    }
   
   
   
     function showProduct_Ori()
   {
          var ProductCode = document.getElementById("Ori_ProductCode").value;
          updateProductHTML(ProductCode);
          display_balance(1);
          display_balance(2);
   }
   
    function openwin(htm){
        window.open(htm,"Detail_B2C", "width=680,height=600,resizable=1,scrollbars=1,status=no,toolbar=no,location=no,menu=no"); 
    }
  
   
     
    function changetoSuggest()
   {
        var obj = document.getElementById("suggest");
        var obj1 = document.getElementById("history");
        
        if(obj != null && obj1 != null)
        {
            obj.src = "../img/favorite/tj_03.gif";
            obj1.src = "../img/favorite/tj_04.gif";
            document.getElementById("suggestProduct").style.display="block";
            document.getElementById("historyProduct").style.display="none";
        }
   }
   
   function changetoHistory()
   {
        var obj = document.getElementById("suggest");
        var obj1 = document.getElementById("history");
        
        if(obj != null && obj1 != null)
        {
            obj.src = "../img/favorite/tj_01.gif";
            obj1.src = "../img/favorite/tj_02.gif";
            document.getElementById("suggestProduct").style.display="none";
            document.getElementById("historyProduct").style.display="block";
        }
   } 
