// JavaScript Document
var xmlHttp;
function createXMLHttpRequest(){
  if(window.ActiveXObject){
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if(window.XMLHttpRequest){
    xmlHttp = new XMLHttpRequest();
  }
}

function refreshproductList(id){

  var countryID = document.getElementById(id).value;
  if(countryID ==""){
   clearBindSelect();
    return;
  }

  var url = "DoubListBoxZoneData.jsp?ID="+countryID;
  //document.frmEdit.tableName.value=tableName;
  createXMLHttpRequest();

  xmlHttp.onreadystatechange = handleStateChange;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function handleStateChange(){
  if(xmlHttp.readyState==4){
    if(xmlHttp.status==200){
      updateproductList();
  } else {
      alert("พบข้อผิดพลาด: " + xmlHttp.statusText);
    }
}
}

function updateproductList(){
 // var tableName = document.frmEdit.tableName.value;
  var filedName = "zoneID";
 /* switch(tableName){
    case "countries":
    filedName = "countryID";
    break;
     case "cities":
    filedName = "cityID";
    break;
  }*/
  clearproductList();
  var xfiled = document.getElementById(filedName);

  var result = xmlHttp.responseText;
  var option = null;

  p = result.split(",");

  for(var i=0;i<p.length-1;i++){
 	 x = p[i].split("@");
    option = document.createElement("option");
    option.appendChild(document.createTextNode(x[1]));
	option.setAttribute("value",x[0]);
    xfiled.appendChild(option);
  }
}

function clearBindSelect(){
 clearproductList();
	 var xfiled = document.getElementById("countryID");
	  var option = null;
	   option = document.createElement("option");
  	   option.appendChild(document.createTextNode("เลือกประเทศ"));
	   option.setAttribute("value","");
       xfiled.appendChild(option);

	   var xfiled1 = document.getElementById("zoneID");
	  var option1 = null;
	   option1 = document.createElement("option");
  	   option1.appendChild(document.createTextNode("ทุกเมือง"));
	   option1.setAttribute("value","");
       xfiled1.appendChild(option1);
}

function clearproductList(){
  // var tableName = document.frmEdit.tableName.value;
   var filedName = "zoneID";
  /* switch(tableName){
    case "countries":
    filedName = "countryID";
    break;
     case "cities":
    filedName = "cityID";
    break;
  }*/
   var xfiled = document.getElementById(filedName);

    while(xfiled.childNodes.length>0){
      xfiled.removeChild(xfiled.childNodes[0]);
    }

}
function  validFrm(){
var firstName = document.frmEdit.firstName.value;
var lastName = document.frmEdit.lastName.value;
var birthDate = document.frmEdit.birthDate.value;
var streetAddress = document.frmEdit.streetAddress.value;
var city = document.frmEdit.city.value;
var zipcode = document.frmEdit.zipcode.value;
var phone = document.frmEdit.phone.value;
var countryID = document.frmEdit.countryID.value;
var zoneID =document.frmEdit.zoneID.value;
var email = document.frmEdit.email.value;
var password = document.frmEdit.password.value;
var confirmPassword = document.frmEdit.confirmPassword.value;
if(firstName==""){
alert("กรุณากรอกชื่อด้วยนะค่ะ!!!");
 document.frmEdit.firstName.focus();
return false;
}else if(lastName==""){
alert("กรุณากรอกนามสกุลด้วยนะค่ะ!!!");
 document.frmEdit.lastName.focus();
return false;
}else if(birthDate==""){
alert("กรุณากรอกวันเกิดด้วยนะค่ะ!!!");
 document.frmEdit.birthDate.focus();
return false;
}else if(email==""){
alert("กรุณากรอกอีเมล์ด้วยนะค่ะ!!!");
 document.frmEdit.email.focus();
return false;
}else if(streetAddress==""){
alert("กรุณากรอกที่อยู่ด้วยนะค่ะ!!!");
 document.frmEdit.streetAddress.focus();
return false;
}else if(city==""){
alert("กรุณากรอกชื่อเมืองด้วยนะค่ะ!!!");
 document.frmEdit.city.focus();
return false;
}else if(zipcode==""){
alert("กรุณากรอกรหัสไปรษณีย์ด้วยนะค่ะ!!!");
 document.frmEdit.zipcode.focus();
return false;
}else if(countryID==""||countryID==0){
alert("กรุณาระบุประเทศด้วยนะค่ะ!!!");
 document.frmEdit.countryID.focus();
return false;
}
else if(zoneID==""||zoneID==0){
alert("กรุณาระบุเมืองด้วยนะค่ะ!!!");
 document.frmEdit.zoneID.focus();
return false;
}else if(phone==""){
alert("กรุณากรอกเบอร์โทรศัพท์ด้วยนะค่ะ!!!");
 document.frmEdit.phone.focus();
return false;
}else if(password==""){
alert("กรุณากรอกรหัสผ่านด้วยนะค่ะ!!!");
 document.frmEdit.password.focus();
return false;
}else if(confirmPassword==""){
alert("กรุณายืนยันรหัสผ่านด้วยนะค่ะ!!!");
 document.frmEdit.confirmPassword.focus();
return false;
}
else{
filter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				/*if(Object_email.value.length == 0 || Object_email.value == ""){
						alert("เพื่อสิทธิประโยชน์ของท่าน กรุณาใส่ E-Mail ของท่านด้วยครับ");
						Object_email.focus();
						return false;
				}*/
				if(!filter.test(email)){
						alert("เพื่อสิทธิประโยชน์ของท่าน กรุณาใส่ E-Mail ของท่านให้ถูกต้องด้วยนะค่ะ!!!");
						document.frmEdit.email.focus();
						return false;
				}else if (password != confirmPassword) {
						alert("กรุณายืนยัน รหัสผ่าน ให้ตรงกันด้วยนะค่ะ!!!");
						document.frmEdit.confirmPassword.focus();
						return false;
				}else{
						return true;
				}

}
}
function CkStringLowerEN(){
if(((event.keyCode >= 65) && (event.keyCode <= 90)) && event.keyCode != 13){
alert("ข้อมูลส่วนนี้ ต้องเป็นตัวอักษรภาษาอังกฤษตัวเล็ก( a-z ) เท่านั้น");
event.returnValue = false;
}else{
event.returnValue = true;
}
}

function CkNumberEN(){
if(!((event.keyCode >= 48) && (event.keyCode <= 57)) && event.keyCode != 13){
alert("ข้อมูลส่วนนี้ ต้องเป็นตัวเลขอารบิก( 1-9 ) เท่านั้น");
event.returnValue = false;
}else{
event.returnValue = true;
}
}
function add()
{
var id = "<%=orderProductID%>";
var qty = document.frmShop.qty.value;
var finalPrice = document.frmShop.finalPrice.value;
window.location.href = "shopping_cart.jsp?orderProductID="+id+"&txtCommand=add&finalPrice="+finalPrice+"&nQuantity="+qty;
}
function modifyx(object,id){
 // var id = "";
 // var qty = document.frmShop.qty.value;
 if(validqtyModify(object)){
   window.location.href = "shopping_cart.jsp?orderProductID="+id+"&txtCommand=modify&nQuantity="+object.value;
 }
}
function removeQty(id){
   window.location.href = "shopping_cart.jsp?orderProductID="+id+"&txtCommand=remove";
}

function CkNumberEN(){
if(!((event.keyCode >= 48) && (event.keyCode <= 57)) && event.keyCode != 13){
alert("ข้อมูลส่วนนี้ ต้องเป็นตัวเลขอารบิก( 1-9 ) เท่านั้น");
event.returnValue = false;
}else{
event.returnValue = true;
}
}
function validQtyAdd(){
  if(validqty()){
    add();
  }
}
/*function validQtyModify(){
if(validqty()){
  modifyx();
}
}*/
function validqty(){
var qty = document.frmShop.qty.value;
if((qty==0)){
 alert("ข้อมูลส่วนนี้ ต้องมีค่าตั้งแต่1ขึ้นไป");
return false;
}else{
return true;
}
}
function validqtyModify(object){
var qty = object.value;
if((qty==0)){
 alert("ข้อมูลส่วนนี้ ต้องมีค่าตั้งแต่1ขึ้นไป");
return false;
}else{
return true;
}
}

function validateEnew(obj)
{
	if (IsEmpty(obj.ti_search.value) ||  obj.ti_name.value=='Keyword to Find a Product'){
			Warning(obj.ti_name,"กรุณาระบุคำที่ต้องการค้นหา");
			return false;
	}
}

