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) { //v4.01
  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);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

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_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 itemSum(itemD, qty){
  var da = itemD.split(",")[1];
  return (da * qty);
}

function calcSum() {
  var sum1 = itemSum(document.form.D1.value, document.form.qty1.value);
  
  var d2 = document.form.D2.value
  var da2 = d2.split(",")[1]
  var q2 = document.form.qty2.value
  var sum2 = da2 * q2

  var d3 = document.form.D3.value
  var da3 = d3.split(",")[1]
  var q3 = document.form.qty3.value
  var sum3 = da3 * q3

  var d4 = document.form.D4.value
  var da4 = d4.split(",")[1]
  var q4 = document.form.qty4.value
  var sum4 = da4 * q4

  var d5 = document.form.D5.value
  var da5 = d5.split(",")[1]
  var q5 = document.form.qty5.value
  var sum5 = da5 * q5
  
  return (sum1+sum2+sum3+sum4+sum5);
}

function  doMath() {
  var sum = calcSum();
  
  this.document.form.pay1.value = "$" + sum*100/100;
  //this.document.form.pay2.value = "$" + sum*0/100;
}


//////////////////////////////////////////////////////////////////////////////
function getParams() {
  var idx = document.URL.indexOf('?');
  var params = new Array();
  if (idx != -1) {
    var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
    for (var i=0; i<pairs.length; i++) {
      nameVal = pairs[i].split('=');
      params[nameVal[0]] = nameVal[1];
    }
  }
  return params;
}

function readParams(fieldname, params) {
  var tmp = decodeURIComponent(params[fieldname]);
  //var tmp = params[fieldname];
  return unescape(tmp);
}

function writeParams(fieldname, params) {
  document.write(readParams(fieldname, params));
}

function encodevalue(myvalue){
  return encodeURIComponent(myvalue);
}

///////////////////////////////////////////////////////////////////////////////
function reverse(str) {
    if(!str) return str;
    return str.charAt(str.length-1) + reverse(str.substring(0,str.length-1));
}


function trimLeft(str) {
   for (var i=0; str.charAt(i) == ' '; i++);
   return str.substring(i, str.length);
}

function trimRight(str) {
   return reverse(trimLeft(reverse(str)));
}
 
function trimAll(str) {
   return trimRight(trimLeft(str));
}


function checkMail(myemail){
	var x = myemail;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	if (filter.test(x)) return true;
	else return false;
}

function checkPhone(myphone){
	var x = myphone;
	var filter  = /^([0-9\-])+[0-9]+$/;
	if (filter.test(x)) return true;
	else return false;
}

function checkEmptyStr(myStr){
	var x = myStr;
	x = trimAll(x);
    	if (x.length == 0) return false;
        else return true;
}

function checkInteger(myNum){
	var x = myNum;
	var filter = /^([0-9])+$/;
	if (filter.test(x)) return true;
	else return false;
}

function checkQty(myQty, myItem){
	var x = myQty;
  	var lItem = myItem.split(",")[1];
	if (lItem != '0'){
	  if (!checkInteger(myQty))  	{return false;}
	  if (myQty <= 0)  		{return false;}
	}

	return true;
}

function validateField(){
        if (!checkEmptyStr(this.document.form.name.value)){
           alert("Please input [Name]");
      	   this.document.form.name.focus();
           return false;
        }

        if (!checkMail(this.document.form.email.value)){
           alert("Please input a valid [Email]");
	   this.document.form.email.focus();
           return false;
        }
        
        if (!checkPhone(this.document.form.phone.value)){
           alert("Please input a valid [Contact Phone No.]");
  	   this.document.form.phone.focus();
           return false;
        }

	if (!checkEmptyStr(this.document.form.address1.value) &&
     	    !checkEmptyStr(this.document.form.address2.value) &&
	    !checkEmptyStr(this.document.form.address3.value)) {
	   alert("Please input a valid [Delivery Address]");
	   this.document.form.address1.focus();
	   return false;
	}
        
	if (!checkQty(this.document.form.qty1.value, this.document.form.D1.value)){
           alert("Please input [Qty], and the value should be greater than 0");
  	   this.document.form.qty1.focus();
           return false;
	}

	if (!checkQty(this.document.form.qty2.value, this.document.form.D2.value)){
           alert("Please input [Qty], and the value should be greater than 0");
  	   this.document.form.qty2.focus();
           return false;
	}

	if (!checkQty(this.document.form.qty3.value, this.document.form.D3.value)){
           alert("Please input [Qty], and the value should be greater than 0");
  	   this.document.form.qty3.focus();
           return false;
	}

	if (!checkQty(this.document.form.qty4.value, this.document.form.D4.value)){
           alert("Please input [Qty], and the value should be greater than 0");
  	   this.document.form.qty4.focus();
           return false;
	}

	if (!checkQty(this.document.form.qty5.value, this.document.form.D5.value)){
           alert("Please input [Qty], and the value should be greater than 0");
  	   this.document.form.qty5.focus();
           return false;
	}
  
	if (calcSum() <= 0){
           alert("Please select [Order Item]");
  	   this.document.form.D1.focus();
           return false;
	}
	
        return true;
}

//////////////////////////////////////////////////////////////////////

function assignValue() {
  params = getParams();
  this.document.form.name.value = readParams('name', params);
  this.document.form.email.value = readParams('email', params);
  this.document.form.phone.value = readParams('phone', params);
  this.document.form.address1.value = readParams('address1', params);
  this.document.form.address2.value = readParams('address2', params);
  this.document.form.address3.value = readParams('address3', params);
  this.document.form.D1.value = readParams('D1', params);
  this.document.form.qty1.value = readParams('qty1', params);
  this.document.form.remark1.value = readParams('remark1', params);
  this.document.form.D2.value = readParams('D2', params);
  this.document.form.qty2.value = readParams('qty2', params);
  this.document.form.remark2.value = readParams('remark2', params);
  this.document.form.D3.value = readParams('D3', params);
  this.document.form.qty3.value = readParams('qty3', params);
  this.document.form.remark3.value = readParams('remark3', params);
  this.document.form.D4.value = readParams('D4', params);
  this.document.form.qty4.value = readParams('qty4', params);
  this.document.form.remark4.value = readParams('remark4', params);
  this.document.form.D5.value = readParams('D5', params);
  this.document.form.qty5.value = readParams('qty5', params);
  this.document.form.remark5.value = readParams('remark5', params);
}

function docWriteItem(itemNum, itemD, qty, remark){
   document.write("<tr><td width=7%>" + itemNum + 
    	          "</td><td width=40%>" + itemD.split(",")[0] +
		  "</td><td width=10%>x " + qty + "" +
		  "</td><td width=16%>$" + itemSum(itemD, qty));
   remark = trimAll(remark);
   if (remark.length != 0){
	document.write("</td><td width=27%>Remarks:" + remark + "</td></tr>");
   }
   else {
	document.write("</td><td width=27%>&nbsp;</td></tr>");
   }
	  
}

function genOrderItems() {
  var itemNum = 0;
  document.write("<table width=95%>");
  if (document.form.D1.value.split(",")[1] != '0'){
   	itemNum += 1;
	docWriteItem(itemNum, document.form.D1.value, document.form.qty1.value, document.form.remark1.value);
  }
  if (document.form.D2.value.split(",")[1] != '0'){
   	itemNum += 1;
	docWriteItem(itemNum, document.form.D2.value, document.form.qty2.value, document.form.remark2.value);
  }
  if (document.form.D3.value.split(",")[1] != '0'){
   	itemNum += 1;
	docWriteItem(itemNum, document.form.D3.value, document.form.qty3.value, document.form.remark3.value);
  }
  if (document.form.D4.value.split(",")[1] != '0'){
   	itemNum += 1;
	docWriteItem(itemNum, document.form.D4.value, document.form.qty4.value, document.form.remark4.value);
  }
  if (document.form.D5.value.split(",")[1] != '0'){
   	itemNum += 1;
	docWriteItem(itemNum, document.form.D5.value, document.form.qty5.value, document.form.remark5.value);
  }
  document.write("</table>");
}

function fromOrderPage1(filePath){
    if (validateField()){
       goNextPage(filePath);
    }
}

function fromOrderPage2(filePath){
    if (!this.document.form.TandC.checked){
           alert("如閣下同意以上買賣條款, 請點選[本人同意以上的條款], 再按[繼續]到下一貢.");
    }
    else {
       goNextPage(filePath);
    }
}

function goNextPage(filePath) { 
    var pageStr = filePath + "?name=" + encodevalue(this.document.form.name.value)
        + "&email=" + encodevalue(this.document.form.email.value) 
        + "&phone=" + encodevalue(this.document.form.phone.value) 
        + "&address1=" + encodevalue(this.document.form.address1.value)
        + "&address2=" + encodevalue(this.document.form.address2.value)
        + "&address3=" + encodevalue(this.document.form.address3.value)
        + "&D1=" + encodevalue(this.document.form.D1.value) 
        + "&qty1=" + encodevalue(this.document.form.qty1.value)
        + "&remark1=" + encodevalue(this.document.form.remark1.value)
        + "&D2=" + encodevalue(this.document.form.D2.value)
        + "&qty2=" + encodevalue(this.document.form.qty2.value)
        + "&remark2=" + encodevalue(this.document.form.remark2.value)
        + "&D3=" + encodevalue(this.document.form.D3.value)
        + "&qty3=" + encodevalue(this.document.form.qty3.value)
        + "&remark3=" + encodevalue(this.document.form.remark3.value)
        + "&D4=" + encodevalue(this.document.form.D4.value)
        + "&qty4=" + encodevalue(this.document.form.qty4.value)
        + "&remark4=" + encodevalue(this.document.form.remark4.value)
        + "&D5=" + encodevalue(this.document.form.D5.value)
        + "&qty5=" + encodevalue(this.document.form.qty5.value)
        + "&remark5=" + encodevalue(this.document.form.remark5.value);
     window.location.href = pageStr; 
} 

function launchProductWindow(filename, winname, height, width, productID)
{
  var pageStr = filename + "?productid=" + encodevalue(productID);
  launchWindow(pageStr, winname, height, width);
}


/////////////////////////////////////////////////////////////////////////////
function launchWindow(filename, winname, height, width) 
{ 
   leftPos = (screen.width-width)/2;
   topPos =  (screen.height-height-50)/2; 
   newwin = window.open(filename,winname,'height='+height+',width='+width+',left='+leftPos+',top='+topPos+',directories=no,location=no,menubar=no,resizable=no,status=no,titlebar=no,toolbar=no,scrollbars=yes'); 
   newwin.focus();
} 

/////////////////////////////////////////////////////////////////////////////

function JumpTo(page) {
   self.location.replace(page.menu.options[page.menu.selectedIndex].value);
}
