//----------------------------------- Setup -------------------------------------------
// Item list form name is "itemlist"
var checkboxname = "checkbox";
var OrderFormURL = "contactus.html";
//var OrderFormURL = this.document.location;


//--------------------------------Standard lib-----------------------------------------
//Sample --> getCookieVal (j)
function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);

   if ( endstr == -1 )
      endstr = document.cookie.length;
   return(unescape(document.cookie.substring(offset, endstr)));
}

//Sample --> NumberOrdered = GetCookie("NumberOrdered");
function GetCookie (name) {
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;

   while ( i < clen ) {
      var j = i + alen;
      if ( document.cookie.substring(i, j) == arg ) return(getCookieVal (j));
      i = document.cookie.indexOf(" ", i) + 1;
      if ( i == 0 ) break;
   }

   return(null);
}

//Sample --> SetCookie ("Orders", deletedbOrder, null, "/");
function SetCookie (name,value,expires,path,domain,secure) {
   document.cookie = name + "=" + escape (value) +
                     ((expires) ? "; expires=" + expires.toGMTString() : "") +
                     ((path) ? "; path=" + path : "") +
                     ((domain) ? "; domain=" + domain : "") +
                     ((secure) ? "; secure" : "");
}

//Sample --> DeleteCookie ("Orders", "/");
function DeleteCookie (name,path,domain) {
   if ( GetCookie(name) ) {
      document.cookie = name + "=" +
                        ((path) ? "; path=" + path : "") +
                        ((domain) ? "; domain=" + domain : "") +
                        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
   }
}

//Sample --> moneyFormat("11");
//return will be "11.00"
function moneyFormat(input) {
for(i=0;i<input.length;i++){
if( !(input.charCodeAt(i)>=48 && input.charCodeAt(i)<=57) ){return 0;}
}
   var dollars = Math.floor(input);
   var tmp = new String(input);

   for ( var decimalAt = 0; decimalAt < tmp.length; decimalAt++ ) {
      if ( tmp.charAt(decimalAt)=="." )
         break;
   }

   var cents  = "" + Math.round(input * 100);
   cents = cents.substring(cents.length-2, cents.length)
           dollars += ((tmp.charAt(decimalAt+2)=="9")&&(cents=="00"))? 1 : 0;

   if ( cents == "0" )
      cents = "00";

   return(dollars + "." + cents);
}

//--------------------------------Main Program-----------------------------------------
function removeorders()
{
SetCookie("Orders", "", null, "/");
top.mainFrame.document.location = ("contactus.html"); // Change this location only
}

function menuonload()
{
top.mainFrame.document.location.reload();
}

function GetClientInfo()
{
var strOutput = "";
strOutput += "<TABLE WIDTH=\"591\" BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\" >\n";
strOutput += "<TR><TD HEIGHT=\"30\" WIDTH=\"128\" >公司名稱</TD>\n";
strOutput += "<TD HEIGHT=\"30\" WIDTH=\"11\" >:</TD><TD HEIGHT=\"30\" >\n";
strOutput += "<INPUT TYPE=\"text\" NAME=\"companyName\" VALUE=\"\" ></TD><TD HEIGHT=\"30\" WIDTH=\"298\" ></TD></TR>\n";
strOutput += "<TR><TD HEIGHT=\"30\" WIDTH=\"128\" >姓名 **</TD>\n";
strOutput += "<TD HEIGHT=\"30\" WIDTH=\"11\" >:</TD><TD HEIGHT=\"30\" >\n";
strOutput += "<INPUT TYPE=\"text\" NAME=\"name\" VALUE=\"\" ></TD><TD HEIGHT=\"30\" WIDTH=\"298\" ></TD></TR>\n";
strOutput += "<TR><TD HEIGHT=\"30\" WIDTH=\"128\" >電話號碼 **</TD>\n";
strOutput += "<TD HEIGHT=\"30\" WIDTH=\"11\" >:</TD><TD HEIGHT=\"30\" >\n";
strOutput += "<INPUT TYPE=\"text\" NAME=\"tel\" VALUE=\"\" ></TD><TD HEIGHT=\"30\" WIDTH=\"298\" ></TD></TR>\n";
strOutput += "<TR><TD HEIGHT=\"30\" WIDTH=\"128\" >傳真號碼</TD>\n";
strOutput += "<TD HEIGHT=\"30\" WIDTH=\"11\" >:</TD><TD HEIGHT=\"30\" >\n";
strOutput += "<INPUT TYPE=\"text\" NAME=\"fax\" VALUE=\"\" ></TD><TD HEIGHT=\"30\" WIDTH=\"298\" ></TD></TR>\n";
strOutput += "<TR><TD HEIGHT=\"30\" WIDTH=\"128\" >電子郵件地址</TD>\n";
strOutput += "<TD HEIGHT=\"30\" WIDTH=\"11\" >:</TD><TD HEIGHT=\"30\" >\n";
strOutput += "<INPUT TYPE=\"text\" NAME=\"email\" VALUE=\"\" ></TD><TD HEIGHT=\"30\" WIDTH=\"298\" ></TD></TR>\n";
strOutput += "<TR><TD HEIGHT=\"23\" WIDTH=\"128\" >郵寄地址 **</TD>\n";
strOutput += "<TD HEIGHT=\"23\" WIDTH=\"11\" >:</TD><TD HEIGHT=\"23\" COLSPAN=\"2\" >\n";
strOutput += "<TEXTAREA NAME=\"address\" COLS=\"50\" ></TEXTAREA><BR></TD></TR>\n";
strOutput += "<TR><TD HEIGHT=\"23\" COLSPAN=\"4\" >\n";
strOutput += "<TABLE WIDTH=\"500\" BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\" >\n";
strOutput += "<TR>";
strOutput += "<TD WIDTH=\"120\" >請選擇付款方式</TD>\n";
strOutput += "<TD WIDTH=\"110\" ><INPUT TYPE=\"radio\" NAME=\"payment\" VALUE=\"visa\" checked >Visa / Master</TD>\n";
strOutput += "<TD WIDTH=\"110\" ><INPUT TYPE=\"radio\" NAME=\"payment\" VALUE=\"bank\" >入數</TD>\n";
strOutput += "<TD WIDTH=\"160\" ><INPUT TYPE=\"radio\" NAME=\"payment\" VALUE=\"elect\" >電匯</TD></TR>\n";
strOutput += "<TR>";
strOutput += "<TD WIDTH=\"120\" ></TD>\n";
strOutput += "<TD WIDTH=\"\" COLSPAN=\"3\" >入數及電匯請注意</TD></TR>\n";
strOutput += "<TR>";
strOutput += "<TD WIDTH=\"120\" >請選擇付運方式</TD>\n";
strOutput += "<TD WIDTH=\"110\" ><INPUT TYPE=\"radio\" NAME=\"sendby\" VALUE=\"byself\" checked >自取</TD>\n";
strOutput += "<TD WIDTH=\"110\" ><INPUT TYPE=\"radio\" NAME=\"sendby\" VALUE=\"bymail\" >郵寄</TD>\n";
strOutput += "<TD WIDTH=\"160\" ><INPUT TYPE=\"TEXT\" NAME=\"byselflocation\" READONLY ></TD></TR>\n";
strOutput += "<TR>";
strOutput += "<TD WIDTH=\"120\" ></TD>\n";
strOutput += "<TD WIDTH=\"\" COLSPAN=\"3\" >自取請選擇地點</TD></TR>\n";
strOutput += "<TR>";
strOutput += "<TD WIDTH=\"120\" >請選擇地區</TD>\n";
strOutput += "<TD WIDTH=\"110\" ><INPUT TYPE=\"radio\" NAME=\"area\" VALUE=\"HK\" checked >香港</TD>\n";
strOutput += "<TD WIDTH=\"\" COLSPAN=\"2\" ><INPUT TYPE=\"radio\" NAME=\"area\" VALUE=\"Oversea\" >海外 (加$50.00作為運費)</TD>\n";
strOutput += "</TR></TABLE></TD></TR>\n";
strOutput += "<TR><TD COLSPAN=\"4\" >\n";
strOutput += "</TD></TR>\n";
strOutput += "</TABLE>\n";
document.write(strOutput);
document.close();
}

// Addtobasket
function nochecked()
{
if ( this.document.forms.length != 0 ){
for( k = 1 ; this.document.forms.itemlist[checkboxname+k] ; k++ )
{
if ( this.document.forms.itemlist[checkboxname+k].checked == true )
{
this.document.forms.itemlist[checkboxname+k].checked = false;
} // End of //if ( this.document.forms.form1["checkbox"+k].checked == true )
} // End of //for( k = 0 ; this.document.forms.form1["checkbox"+k] ; k++ )
} // End of //if ( this.document.forms != 0 ){
} // End of //function nochecked()

function Addcbtobasket()
{
if ( this.document.forms.length != 0 ){
for( k = 1 ; this.document.forms.itemlist[checkboxname+k] ; k++ )
{
if ( this.document.forms.itemlist[checkboxname+k].checked == true )
{
Addtobasket(this.document.forms.itemlist[checkboxname+k].value);
} // End of //if ( this.document.forms.form1["checkbox"+k].checked == true )
} // End of //for( k = 0 ; this.document.forms.form1["checkbox"+k] ; k++ )
} // End of //if ( this.document.forms != 0 ){
} // End of //function Addcbtobasket()

function Addtobasket(kAddPartNo,ProductNo,Name,NonMemberPrice,MemberPrice)
{
var descdatas = "";
var arrayofOrder;
var OrderAdded = false;
var thistimedbUpdatedOrder = "";
if(thistimedbUpdatedOrder = GetCookie("Orders"))
{
arrayofOrder = thistimedbUpdatedOrder.split("^:^");
if(arrayofOrder.length > 39)
{
alert("too much!!!");
return;
}
for(var i = 0 ; i < arrayofOrder.length ; i++ )
{
arrayofthis = arrayofOrder[i].split(":");
if(arrayofthis[0] == kAddPartNo){OrderAdded = true;}
}
descdatas = thistimedbUpdatedOrder;
if(!OrderAdded){
descdatas += "^:^" + kAddPartNo + ":" + ProductNo + ":" + Name + ":" + NonMemberPrice + ":" + MemberPrice + ":1";
}else{cartcheckout();return;}
//}else{alert(Name + " : Already added!!");return;}
}else{
descdatas = kAddPartNo + ":" + ProductNo + ":" + Name + ":" + NonMemberPrice + ":" + MemberPrice + ":1";
}
SetCookie("Orders", descdatas, null, "/");
this.document.location = ("contactus.html"); // Change this location only
}

function Removefrombasket(kRemovePartNo)
{
var descdatas = "";
var arrayofOrder = "";
var thistimedbUpdatedOrder = "";
thistimedbUpdatedOrder = GetCookie("Orders");
arrayofOrder = thistimedbUpdatedOrder.split("^:^");
for(var i = 0 ; i < arrayofOrder.length ; i++ )
{
arrayofthis = arrayofOrder[i].split(":");
if(arrayofthis[0] == kRemovePartNo){
}else{
descdatas += "^:^" + arrayofOrder[i];
}
}
descdatas = descdatas.substring(3);
SetCookie("Orders", descdatas, null, "/");
this.document.location = ("contactus.html"); // Change this location only
}

function cartcontinue()
{
this.document.location = ("/cgi-bin/product.cgi?action=product&"); // Change this location only
}

function cartcheckout()
{
this.document.location = ("contactus.html"); // Change this location only
}

function cartpay()
{
top.AFrame.ShopTopFrame.ShopFrameTop.document.location = ("http://www..com/shoptop.html");
top.AFrame.ShopTopFrame.ShopFrameBottom.document.location = ("http://www..com/shopbottomb.html");
}

function GetFromBasket()
{
var strOutput = "";
var formOutput = "";
var arrayofOrder = "";
var thistimedbUpdatedOrder = "";
if(thistimedbUpdatedOrder = GetCookie("Orders"))
{
arrayofOrder = thistimedbUpdatedOrder.split("^:^");
for(var i = 0 ; i < arrayofOrder.length ; i++ )
{
arrayofthis = arrayofOrder[i].split(":");
strOutput += "<TR><TD WIDTH=\"100\" ><INPUT TYPE=\"HIDDEN\" NAME=\"intno" + i + "\" VALUE=\"" + arrayofthis[0] + "\" ><INPUT TYPE=\"HIDDEN\" NAME=\"itemno" + i + "\" VALUE=\"" + arrayofthis[1] + "\" >" + arrayofthis[1] + "</TD>";
strOutput += "<TD ><INPUT TYPE=\"HIDDEN\" NAME=\"itemname" + i + "\" VALUE=\"" + arrayofthis[2] + "\" >" + arrayofthis[2] + "</TD>";
//strOutput += "<TD WIDTH=\"72\" ><INPUT TYPE=\"HIDDEN\" NAME=\"price" + i + "\" VALUE=\"" + arrayofthis[3] + "\" ><SMALL>HKD</SMALL>$ " + arrayofthis[3] + "</TD>";
//strOutput += "<TD WIDTH=\"55\" ALIGN=\"CENTER\" ><INPUT TYPE=\"TEXT\" NAME=\"qty" + i + "\" VALUE=\"" + arrayofthis[5] + "\" SIZE=\"4\" onchange=\'Javascript:orderformonchange(this,\"" + i + "\")\' style=\'text-align: center;\' ></TD>";
//strOutput += "<TD WIDTH=\"73\" ALIGN=\"CENTER\" ><INPUT TYPE=\"TEXT\" NAME=\"amount" + i + "\" VALUE=\"\" SIZE=\"6\" readonly style=\'background-color: #FFFFFF;BORDER-style : none;font-family: \"Arial\", \"Helvetica\", \"sans-serif\"; color: #000000; font-size: 10pt;text-align: center;\' ></TD>";
//strOutput += "<TD WIDTH=\"78\" >&nbsp;</TD>";
strOutput += "<TD WIDTH=\"80\" ><A HREF=\"#\" onClick=\'Removefrombasket(\"" + arrayofthis[0] + "\");\' >&nbsp;<IMG SRC=\"gra/remove.gif\" BORDER=\"0\" ALIGN=\"CENTER\" VALIGN=\"MIDDLE\" >&nbsp;Remove</A></TD></TR>\n"
//formOutput += "<INPUT TYPE=\"HIDDEN\" NAME=\"Item" + i + "\" VALUE=\"" + arrayofOrder[i] + "\" >";
}
if(!strOutput){strOutput = "<TR><TD>None</TD><TD></TD></TR>\n";}

document.write(strOutput);
document.write(formOutput);
//document.close();
}
}

function orderformonload()
{
for( k = 0 ; this.document.forms.payForm["price"+k] ; k++ )
{
orderformcal(k);
}
totalcal();
}

function orderformonchange(kInput,kNum)
{
checkkey(kInput);
orderformcal(kNum);
totalcal();
// Start of Update cookies
intno = this.document.forms.payForm["intno"+kNum].value;
itemno = this.document.forms.payForm["itemno"+kNum].value;
itemname = this.document.forms.payForm["itemname"+kNum].value;
price = this.document.forms.payForm["price"+kNum].value;
qty = this.document.forms.payForm["qty"+kNum].value;

var descdatas = "";
var arrayofOrder;
var OrderAdded = false;
var thistimedbUpdatedOrder = "";
if(thistimedbUpdatedOrder = GetCookie("Orders"))
{
arrayofOrder = thistimedbUpdatedOrder.split("^:^");
for(var i = 0 ; i < arrayofOrder.length ; i++ )
{
arrayofthis = arrayofOrder[i].split(":");
if(arrayofthis[0] == intno){
if(i != 0)
{
descdatas += "^:^" + intno + ":" + itemno + ":" + itemname + ":" + arrayofthis[3] + ":" + arrayofthis[4] + ":" + qty;
}else{
descdatas = intno + ":" + itemno + ":" + itemname + ":" + arrayofthis[3] + ":" + arrayofthis[4] + ":" + qty;
}
}else{
if(i != 0)
{
descdatas += "^:^" + arrayofOrder[i];
}else{
descdatas = arrayofOrder[i];
}
}
}
}
//descdatas = thistimedbUpdatedOrder;
SetCookie("Orders", descdatas, null, "/");
// End of Update cookies
}

function orderformcal(kNum)
{
amount = (this.document.forms.payForm["price"+kNum].value * this.document.forms.payForm["qty"+kNum].value);
this.document.forms.payForm["amount"+kNum].value = moneyFormat(parseFloat(amount));
}

function totalcal()
{
var amount = 0;
var totalamount = 0;
for( i = 0 ; this.document.forms.payForm["amount"+i] ; i++ ){
amount = this.document.forms.payForm["amount"+i].value;
totalamount += parseFloat(amount);
}
//if(this.document.forms.payForm["FastExpress"].checked){totalamount += 156;}
this.document.forms.payForm["amount"].value = moneyFormat(totalamount);
}

function checkkey(kNum)
{
input = kNum.value;
output = "";
for(i=0; i<input.length; i++){
if(input.charCodeAt(i)>=48 && input.charCodeAt(i)<=57){
output += input.substring(i,i+1);
}
}
if(!output){output = 0;}
kNum.value = output;
}

function RandomOrderRef(){
var NewRef = "";
while(NewRef.length<20)
{
NewRefParts = Math.floor(Math.random()*130);
if( (NewRefParts >= 65 && NewRefParts <= 90) || (NewRefParts >= 97 && NewRefParts <= 122) || (NewRefParts >= 48 && NewRefParts <= 57) )
{
NewRef += String.fromCharCode(NewRefParts);
}
}
this.document.forms.payForm["orderRef"].value = NewRef.substring(0,8);
}
