﻿function strTrim(str){
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

function setXY_Top(objId){
	var obj = document.getElementById(objId);

	if(obj){		
	    obj.style.top = document.documentElement.scrollTop+100+"px";
		obj.style.left = document.documentElement.scrollLeft+(document.documentElement.clientWidth-obj.clientWidth)/2+"px";			
	}
}

//設定物件在視窗中置中位置
function setXY(objId){
	var obj = document.getElementById(objId);

	if(obj){		
	    obj.style.top = document.documentElement.scrollTop+(document.documentElement.clientHeight-obj.clientHeight)/2+"px";
		obj.style.left = document.documentElement.scrollLeft+(document.documentElement.clientWidth-obj.clientWidth)/2+"px";			
	}
}

//移除 浮動視窗物件
function removeObj(objId){
  var obj = document.getElementById(objId); 
  if(obj) obj.parentNode.removeChild(obj);

}


function changeAction(actionFlag){
	var form1 = document.getElementById("form1");
	var doAction = document.getElementById("doAction");
	

	if(actionFlag=="delete"){
		var objChecked = false
		var cks = document.getElementsByName("cks");
		var doAction = document.getElementById("doAction");

		if(cks && cks.length>0){
			for(var i=0;i<cks.length;i++){
				if(cks[i].checked){
					objChecked = true;
					break;
				}
			}

			if(!objChecked){
				alert("至少要選擇一個刪除項目");
			}else if(confirm("確定要刪除嗎？")){	
				doAction.value = actionFlag;
				form1.submit();
			}
		}else{
				alert("目前沒有資料可以刪除");
		}
	}else{
		doAction.value = actionFlag;
		form1.submit();
	}
	
	
}


function changeParent(parentKey){
	if(parentKey!=""){
		var form1 = document.getElementById("form1");
		var objChecked = false
		var cks = document.getElementsByName("cks");
		var doAction = document.getElementById("doAction");
		var newParent = document.getElementById("newParent");
		newParent.value= parentKey;
		
		if(cks && cks.length>0){
			for(var i=0;i<cks.length;i++){
				if(cks[i].checked){
					objChecked = true;
					break;
				}
			}

			if(!objChecked){
				alert("至少要選擇一個項目");
			}else if(confirm("確定要變更上層分類嗎？")){	
				doAction.value = "newParent";
				form1.submit();
			}
		}else{
				alert("目前沒有資料可以變更");
		}	
	}
}


function attachEventListener(target, eventType, functionRef, capture){
  if (typeof target.addEventListener != "undefined")
  {
    target.addEventListener(eventType, functionRef, capture);
  }
  else if (typeof target.attachEvent != "undefined")
  {
    target.attachEvent("on" + eventType, functionRef);
  }
  else
  {
    eventType = "on" + eventType;

    if (typeof target[eventType] == "function")
    {
      var oldListener = target[eventType];

      target[eventType] = function()
      {
        oldListener();

        return functionRef();
      }
    }
    else
    {
      target[eventType] = functionRef;
    }
  }

  return true;
}

function getEventTarget(event){
  var targetElement = null;

  if (typeof event.target != "undefined"){
    targetElement = event.target;
  }else{
    targetElement = event.srcElement;
  }

  while (targetElement.nodeType == 3 && targetElement.parentNode != null){
    targetElement = targetElement.parentNode;
  }

  return targetElement;
}




function guestbookInputCheck(flag){
	var form1 = document.getElementById("form1");
	var cks = document.getElementsByName("cks");
	var doAction = document.getElementById("doAction")
	
	var hasCkecked = false;
	
	for(var i=0 ;i<cks.length ; i++){
		if([i]&&cks[i].checked){
			hasCkecked = true;
			break;			
		}				
	}
	
	if(!hasCkecked){
		alert("請勾選留言");		
	}else{
		doAction.value = flag;
		form1.submit();
	}	
}




function questbookCommentCheck(){
	var actionMail = document.getElementById("actionMail");

	var fromail = document.getElementById("fromail");
	var sendmail = document.getElementById("sendmail");
	var subject = document.getElementById("subject");
	var content = document.getElementById("content");		
	
	var msg = "";
	if(actionMail.checked){
		if(fromail.value=="" || sendmail.value==""　|| subject.value=="" || content.value==""){
			msg +="資料不足";		
		}		
	}
	
	if(msg!=""){
		alert("資料不足");
		return false;
	}else{
		
		return true;	
	}		
}

function showSendMailArea(){
	var actionMail = document.getElementById("actionMail");
	var sendMailArea = document.getElementById("sendMailArea");
	
	if(actionMail.checked){
		sendMailArea.style.display="block";				
	}else{
		sendMailArea.style.display="none";
	}
}


//刪除留言回應
function deleteGuestRplay(gid) { 	
	  $.ajax({
	    url: 'ajax_guestbook.jsp',
	    data: {doAction: 'delete', gid: gid},	    	    
	    error: function(xhr) {
	      alert('Ajax request 發生錯誤');
	     
	    },
	    success: function(response) {
	    	if(response.indexOf("actionResult:down")!=-1){	    			    		
  	      $('#item_'+gid).fadeOut();
  	      
  	      $('#msg').html("刪除成功");
  	      $('#msg').fadeIn();
  	      setTimeout(function(){
  	        $('#msg').fadeOut();    	        
  	      }, 5000);
	    		
	    	}else{
	    		alert("刪除失敗");
	    	}

	    }
	  });
}	

function guestRplay(){
	var form1 = document.getElementById("form1");
	ajax("post","ajax_guestbook.jsp",form1,afterAjax_guestRplay);
}

function afterAjax_guestRplay(req){
	var responseText = req&&req.responseText?req.responseText:""; 
	var itemDiv = document.getElementById("item_"+deleteItem);

	if (responseText.indexOf("<script>")!=-1){
		eval(responseText.substring(responseText.indexOf("<script>")+8,responseText.indexOf("&lt;/script>")));			
	}

	if(responseText.indexOf("actionResult:down")!=-1){
	//	itemDiv.innerHTML="";
		alert("執行成功");
	}else{
		alert("執行失敗");
	}
}


function clickAll(checked){
	var cks = document.getElementsByName("cks");

	for(var i=0;i<cks.length;i++){
		cks[i].checked = checked;			
	}
	
}


function showUploadFile(){
	var body = document.getElementsByTagName('body')[0];	
	var div = document.createElement("div");
		div.setAttribute("id","ajaxDiv");
		div.style.height=body.clientHeight+40+"px";
		body.appendChild(div);
	
		var fileDiv = $('#fileDiv');
		$('#fileDiv').css('top','230').css('display','');		
		setXY('fileDiv');	

		
		$('#uploadImg').css('display','');
		$('#uploadMsg').html('');	
}

function closeFileUploadWin(){
	$('#fileDiv').css('display','none');
	removeObj('ajaxDiv');	
}

function closeImageWin(){
	$('#imageDiv').css('display','none');
	removeObj('ajaxImageDiv');	
}

function closeImageWin2(){
	$('#imageDiv2').css('display','none');
	removeObj('ajaxImageDiv2');	
}


function closeFileWin(){
	$('#fileDiv').css('display','none');
	removeObj('ajaxImageDiv');	
}

function closeImgDetailWin(){
	$('#imgDetailDiv').css('display','none');
	removeObj('ajaxImgDetailDiv');	
}

function isEmail(email){
	if (email=="") 
		return true;
	reEmail=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/
	return reEmail.test(email);
}

function isTel(tel){
	var regu = /^[A0-9#-() -]{7,20}$/
	var re = new RegExp(regu); 
	var re = new RegExp(regu); 
	return re.test(tel);
}



//widget 增加圖片
function widget_addImageLinkHtml(){
	var body = document.getElementsByTagName('body')[0];	
	var div = document.createElement("div");
		div.setAttribute("id","ajaxImageDiv");
		div.style.height=screen.availHeight+"px";
		body.appendChild(div);

		var imageDiv = $('#imageDiv');
		$('#imageDiv').css('top','10').css('display','');		
		setXY('imageDiv');	
		$('#imagList').html('<img src="/images/web/ajax-loader.gif" style="display:none;">');   

		widget_showImageList();
}

function widget_showImageList(){
	 $.ajax({
		  	type: "post",
		    url: 'ajax_imageFileList.jsp',
		    data: {tag: $('#fileTag').val()},		  	    
		    error: function(xhr) {
		      //alert('Ajax request 發生錯誤');
		    },
		    success: function(response) {		      			   		  
		    	$('#imagList').html(response);		      			    
		    }
	});			
}


function showImageList(){
	 $.ajax({
			type: "post",
			url: 'ajax_imageFileList.jsp',
			data: {tag: $('#fileTag').val()},		  	    
			error: function(xhr) {
			  //alert('Ajax request 發生錯誤');
			},
			success: function(response) {		      			   
			  $('#imagList').html(response);			    
			}
	});			
}

function showImageListForLink(){
	 $.ajax({
			type: "post",
			url: 'ajax_imageFileListForLink.jsp',
			data: {tag: $('#fileTag2').val()},		  	    
			error: function(xhr) {
			  //alert('Ajax request 發生錯誤');
			},
			success: function(response) {		      			   
			  $('#imagList2').html(response);			    
			}
	});			
}



function addImageHTML(imgId){
	var imgUrl = '/img/'+imgId+"_m.jpg";
	arr['rte1'].set_content("<img src='"+imgUrl+"'><br/>"+arr['rte1'].get_content())
}

function addImageLink(imgId){
	var imgUrl = '/img/'+imgId+'.jpg';
	 imgUrl = imgId.indexOf('product')!=-1 ?  '/img/'+imgId+'_p_.jpg' : imgUrl;				 
	 $('#imageKey').val(imgUrl);
	 $('#imageDiv2').css('display','none');
	
}


//account會員專區 夾帶檔案
function account_addFile(){
	var body = document.getElementsByTagName('body')[0];	
	var div = document.createElement("div");
		div.setAttribute("id","ajaxImageDiv");
		div.style.height=screen.availHeight+"px";
		body.appendChild(div);

		var imageDiv = $('#imageDiv');
		$('#fileDiv').css('top','10').css('display','');		
		setXY('fileDiv');	
		//$('#imagList').html('<img src="/images/web/ajax-loader.gif" style="display:none;">');   
}


function checkImageFile(){
	if($('#file').val()==""){
		$('#uploadMsg').html('請選擇檔案....');
	}else{
		$('#uploadMsg').html('');
	}
}

function checkImageFile2(){
	if($('#file2').val()==""){
		$('#uploadMsg2').html('請選擇檔案....');
	}else{
		$('#uploadMsg2').html('');
	}
}



function checkFile(){
	if($('#uploadFile').val()==""){
		$('#uploadMsg').html('請選擇檔案....');
	}else{
		$('#uploadMsg').html('');
	}
}

//在編輯內容時,要新增圖片
function uploadImageFile(){		
	$('#uploadForm').ajaxSubmit({ 
		url: "ajax_imageUploadWhenEditContent.jsp",
		beforeSubmit: function() {
			var canUpload = true;

			 $('#tag').val($('#fileTag').val());

			if($('#tag').val()==""){
				$('#uploadMsg').html('請選擇分類');
				canUpload = false;
			}else{
				$('#uploadMsg').html('');				
			}
	
			if($('#file').val()==""){
				canUpload = false;
		    	$('#uploadMsg').html('請選擇檔案....');		
			}

			if(canUpload){
				$('#uploadLink').css('display','none');
				$('#uploadImg').html('上傳中....<img src=\"/images/web/ajax-loader_y.gif\"/>');   
				$('#uploadMsg').html('');	
			}
			 return canUpload ;
		} , 
		success: function(response) { 
			$('#uploadLink').css('display','');
			$('#uploadImg').css('display','none');
			
			if(response.indexOf("imageFile")!=-1){

				addImageHTML(response);
				
				$('#uploadMsg').html("上傳檔案成功");
				showImageList();
			}else{		
				$('#uploadMsg').html(response);		
			}
		}
	
	 });
}



//在編輯menu link時,要新增圖片
function uploadImageFile2(){		
	$('#uploadForm2').ajaxSubmit({ 
		url: "ajax_imageUploadForMenuLink.jsp",
		beforeSubmit: function() {
			var canUpload = true;

			 $('#tag2').val($('#fileTag2').val());

			if($('#tag2').val()==""){
				$('#uploadMsg2').html('請選擇分類');
				canUpload = false;
			}else{
				$('#uploadMsg2').html('');				
			}
	
			if($('#file2').val()==""){
				canUpload = false;
		    	$('#uploadMsg2').html('請選擇檔案....');		
			}

			if(canUpload){
				$('#uploadLink2').css('display','none');
				$('#uploadImg2').html('上傳中....<img src=\"/images/web/ajax-loader_y.gif\"/>');   
				$('#uploadMsg2').html('');	
			}
			 return canUpload ;
		} , 
		success: function(response) { 
			$('#file2').val('');
			$('#uploadLink2').css('display','');
			$('#uploadImg2').css('display','none');
			
			if(response.indexOf("imageFile")!=-1){
				$('#uploadMsg2').html("上傳檔案成功");
				showImageListForLink();
			}else{		
				$('#uploadMsg2').html(response);		
			}
		}
	
	 });
}



//在編輯會員專區時,要新增附加檔案
function uploadAccountFile(){		
	$('#uploadFileForm').ajaxSubmit({ 
		url: "ajax_accountFileUpload.jsp",
		beforeSubmit: function() {
			var canUpload = true;

			if($('#uploadFile').val()==""){
				canUpload = false;
		    	$('#uploadMsg').html('請選擇檔案....');		
			}else{
				$('#uploadLink').css('display','none');
				$('#uploadImg').html('上傳中....<img src=\"/images/web/ajax-loader_y.gif\"/>');   
				$('#uploadMsg').html('');				
			}

			 return canUpload ;
		} , 
		success: function(response) { 
			$('#uploadLink').css('display','');
			$('#uploadImg').css('display','none');
			
			if(response.indexOf("imageFile")!=-1){
				
				$('#uploadMsg').html("上傳檔案成功");
			
			}else{		
				$('#uploadMsg').html(response);		
			}
		}
	
	 });

}

//商品介紹 詳細頁面,用於tab切換
function showTab(tabId){
	var itemDetail = document.getElementById('itemDetail');
	var sizeTab = document.getElementById('sizeTab');
	var link_itemDetail = document.getElementById('link_itemDetail');
	var link_sizeTab = document.getElementById('link_sizeTab');

	if(tabId=="itemDetail"){
		itemDetail.style.display="";
		sizeTab.style.display="none";		
		link_itemDetail.style.background="#000";
		link_sizeTab.style.background="#fff";	
		link_sizeTab.style.color="#000";			
	}else{
		itemDetail.style.display="none";
		sizeTab.style.display="";
		link_itemDetail.style.background="#fff";
		link_itemDetail.style.color="#000";			
		link_sizeTab.style.background="#000";		
	}	
}


/*slide*/
function slideSwitch(a,d,t) {
	$('#'+a).cycle({ 
		fx:    d, 
		sync:   1, 
		andom:  1,
		speed: 2000,
		timeout: t
	});
}


function initNews(){
    $('#news').cycle({
		fx: 'scrollUp',
		timeout: 3000,
		delay: -3000,
		pause: true

	});
}



