// JavaScript Document

function setPic(id,idPic,val)
{
		if(document.getElementById(id)!=null)
		  document.getElementById(id).value=val;
		if(document.getElementById(idPic)!=null){			 
		  document.getElementById(idPic).src=val;
		 if(val.length==0){
		  document.getElementById(idPic).style.display="none";
		 }else {
		  document.getElementById(idPic).style.display="";
		 }
		}
}

function setDeleteContactus(id)
{
	if(confirm("ยืนยันการลบ?"))
	{
		document.getElementById("contract_id_").value=id;
	    setClick("deleteBtn");
	}
}

function setDeleteCustomer(id)
{
	if(confirm("ยืนยันการลบ?"))
	{
		document.getElementById("customer_id_").value=id;
	    setClick("deleteBtn");
	}
}

function setDeleteOrder(id)
{
	if(confirm("ยืนยันการลบ?"))
	{
		document.getElementById("bill_id").value=id;
	    setClick("deleteBtn");
	}
}

function setViewPO(id)
{
	window.location="outstanding-view.php?p_id="+id;
}

function setViewP(id)
{
	window.location="products-view.php?p_id="+id;
}

function setViewBill(id)
{
	document.getElementById("bill_id").value=id;
	submitForm("billListForm");
}

function setViewBillExcel(id)
{
	document.getElementById("bill_id").value=id;
	document.getElementById("bill_create_sts").value="1";
	submitForm("billListForm");
}

function printOrder(id)
{
	window.open("print_order.php?bid="+id,"","");	
}

function confirmFinishBilling()
{
   if(confirm(_MSG_WARNING_CONFIRM_BILLING))
   {
		document.getElementById("confirm_id").value=1;
		setClick('billingBtn');
   }
}

function confirmDelOrder(id)
{
	if(confirm(_MSG_WARNING_CONFIRM_DEL_ORDER))
	{
		document.getElementById("p_id").value=id;
		setClick('delBtn');
	}
}

function confirmCheckSignin()
{
	//alert(_MSG_WARNING_CHECKSIGNIN);	
	//document.getElementById("email_").focus();
	window.location="register.php";
}

function confirmOrder()
{
	if(confirm(_MSG_WARNING_CONFIRM_ORDER))
	{
		document.getElementById("confirm_id").value=1;
		setClick('orderBtn');
	}
}

function setSumAmounts()
{
	var p_id_list=document.getElementById("p_id_list").value;
	var sum_amounts=0;
	var temp="";
	for(i=0;i<p_id_list.length;i++)
	{
		if(p_id_list.charAt(i)==",")
		{
			 pid=temp;
			 if(document.getElementById("amounts_"+pid)!=null)
			{
				amounts_=parseFloat(document.getElementById("amounts_"+pid).value);	
				sum_amounts+=amounts_;
			}			
			 temp=""; 
		}else{
		     temp+=p_id_list.charAt(i); 	
		}
		
		if(i+1==p_id_list.length)
		{
			 pid=temp;
			 if(document.getElementById("amounts_"+pid)!=null)
			{
				amounts_=parseFloat(document.getElementById("amounts_"+pid).value);	
				sum_amounts+=amounts_;
			}	
		}
		
	}
	document.getElementById("sum_amounts").innerHTML="$"+sum_amounts;
	
}

function setChangeItemsOrder(pid)
{
	if(document.getElementById("price_"+pid)!=null&&
		document.getElementById("amounts_"+pid)!=null&&
		document.getElementById("items_"+pid)!=null&&
		document.getElementById("amounts_"+pid+"_html")!=null)
	{
		price_=parseFloat(document.getElementById("price_"+pid).value);
		items_=document.getElementById("items_"+pid).value==""?0:parseInt(document.getElementById("items_"+pid).value);
		amounts_=price_*items_;
		document.getElementById("amounts_"+pid).value=amounts_;
		document.getElementById("amounts_"+pid+"_html").innerHTML=amounts_;
		setSumAmounts();
	}
}

function setFocus(id)
{
   if(document.getElementById(id)!=null)	
	document.getElementById(id).focus();
}

function getValue(id)
{
  	if(document.getElementById(id)!=null)	
	  document.getElementById(id).value;
	 else
	  return null;
}

function setClick(id)
{
  	if(document.getElementById(id)!=null)	
	  document.getElementById(id).click();
	 else
	  return null;
}

function submitForm(id)
{
   if(document.getElementById(id)!=null)	
	document.getElementById(id).submit();
}

function checkValidLength(id)
{
	if(document.getElementById(id)!=null)
		return 	document.getElementById(id).value.length==0;
	else{
		alert("Not Found "+id);
		return false;
	}
}

function copyIdValue(target,src)
{
	document.getElementById(target).value=document.getElementById(src).value;
}

function copy_ship_info()
{
	   copyIdValue("s_fullname","b_fullname");
	   copyIdValue("s_company","b_company");
	   copyIdValue("s_address","b_address");
	   copyIdValue("s_city","b_city");
	   copyIdValue("s_state","b_state");
	   copyIdValue("s_country","b_country");
	   copyIdValue("s_zip","b_zip");
	   copyIdValue("s_phone","b_phone");
}

function mergeDate(day,month,year,id)
{
   if(document.getElementById("b").value!="1")
   {
	d=parseInt(document.getElementById(day).value);
	if(d<10)
	  d="0"+d;
	document.getElementById(id).value=document.getElementById(year).value+"-"+document.getElementById(month).value+"-"+d;
   }
}

function mergeEmail(email1,email2,email)
{
	document.getElementById(email).value=document.getElementById(email1).value+"@"+document.getElementById(email2).value;
}

function doCreateRegister()
{
	valid=true; 
	idList=new Array(
					 "fname","lname","email1","email2","pwd1","pwd2","f_answer","birth_day","zip",
					 "b_fullname","b_address","b_city","b_zip","b_country","b_email1","b_email2",
					 "s_fullname","s_address","s_city","s_zip","s_country");
	for(i=0;i<idList.length;i++)
		if(checkValidLength(idList[i]))
		{
			alert(_MSG_WARNING_REGISTER);
			//alert(idList[i]);
			setFocus(idList[i]);	
			valid=false;
			return ;
		}
		
	if(document.getElementById("pwd1").value!=document.getElementById("pwd2").value)	
	{
		
			alert(_MSG_WARNING_REGISTER_PWD);
			setFocus("pwd2");
			valid=false;
			return ;
	}
	
	if(valid){
		mergeEmail("email1","email2","email"); 
		mergeEmail("f_email1","f_email2","f_email"); 
		mergeEmail("b_email1","b_email2","b_email"); 
		mergeDate("birth_day","birth_month","birth_year","birthdate");
		submitForm("registerForm");		
	}
}

function doSignin()
{
   if(checkValidLength("email_")||
	  checkValidLength("pwd_"))
   {
	    alert(_MSG_WARNING_LOGIN);
   }else{
	    submitForm("LoginForm");
   }
}

function doSignout()
{
	document.getElementById("check_signout").value="1";
	submitForm("LoginForm");

}

function doSearch()
{
   if(checkValidLength("s"))
   {
	    alert(_MSG_WARNING_SEARCH);
		setFocus("s");
   }else{
		submitForm("SearchForm");
   }
}

function doPostContract()
{
	if(checkValidLength("name")||checkValidLength("message"))//||checkValidLength("email__")||checkValidLength("address"))
	{
		 alert(_MSG_WARNING_CONTRACT);
	}else{
		submitForm("contractForm");
   }
}

function checkMail(id)
{
	var x = document.getElementById(id).value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)){
	  return true;
	}else{  
	  return false;
	}
}


function checkValidPricelist(checkFileUpload)
{
	 if(checkFileUpload){
		if(checkValidLength("file_nm_upload")){
			alert("กรุณาระบุ File");
			return false;
		}
	 }
	 if(checkValidLength("price_list_nm")){ 
	 	alert('ต้องกรอกชื่อหัวข้อ Price List ก่อน'); 
		setFocus("price_list_nm");
		return false;
	 }	else {		 
	 	return confirm('ยืนยันการทำรายการ?');	
	 }
   
}

function checkValidUploadPricelist()
{
    if(checkValidPricelist(true))
	{
		submitForm("pricelistForm");
	}
}

function checkValidInfo()
{
	 if(checkValidLength("info_menu_nm")){ 
	 	alert('ต้องกรอกชื่อหัวข้อ'); 
		setFocus("info_menu_nm");
		return false;	 	
	 }else { 
	    if(confirm('ยืนยันการทำรายการ?'))
		{
			submitForm("infoForm");
			return true;
		}else
			return false;
	 }	 
}
