/*
var e_type = new Array("B$", "EURO$", "HK$", "S$", "US$", "Y$"); 
var e_value = new Array("1.000000", "0.020700", "0.225800", "0.042200", "0.029200", "2.846600");
var currency = 0;
var tmp = "";
var tmp_scr = "";
*/

function conv(s,n) {
    s =  parseFloat(0+s);
    s = s.toString();
    s = s.indexOf(".") < 0 ?  s+".000000" :  s.indexOf(".") == 0  ? "0"+s+"000000" : s+"000000";
    return s.substring(0,s.indexOf(".")+n+1);
// return s.toFixed(n);
}

function text_format(o,n) {
   o.value = conv(o.value,n);
}

function sum_line() {
var a, a2, s, o, total, g_total, i, price, qty, u_price, u_discount; 
o = document.tmp_ord;
a = tmp.split("||");
s = "";
g_total = 0;
    for(i=0; i<(o.elements['qty[]'].length)-1; i++) {
        a2 = a[i].split("|");
        qty = parseFloat(0+o.elements['qty[]'][i].value);
		qty = Math.max(1, qty); 
        price = a2[2] / (1 / e_value[currency]); 
        total = conv( price * qty,2);
        g_total += price * qty;
        u_price = conv(parseFloat(0+o.elements['u_price[]'][i].value),3);
        u_discount = conv(parseFloat(0+o.elements['u_discount[]'][i].value),2);
        s += a2[0] + "|" + a2[1] + "|" + a2[2] + "|" +qty+ "|" +u_price+ "|" +u_discount+ "|" +price+ "|" +total+"||";
        o.elements['qty[]'][i].value = qty;
        o.elements['u_price[]'][i].value = u_price;
        o.elements['u_discount[]'][i].value = u_discount;
        o.elements['price[]'][i].value = conv(price,3);
        o.elements['total[]'][i].value = total;    
    }
    o.elements['g_total'].value = conv(g_total,2);
    setCookie("tmp_item",s);
}

function curr(n) {
    currency = n;
    register("currency", n);
    register("currency_text", e_type[n]);
    sum_line();
}

function back_scr() {
	document.getElementById("query_text").innerHTML = tmp_scr;
	tmp_scr =  "";
	tmp = "";
}

function order() {
var a, a2, i, total, g_total, price, page_str, lng;
lng = document.getElementById("curr_lang").innerHTML;
tmp = ""+getCookie("tmp_item");
currency = (( getCookie("currency")=="") ? currency : getCookie("currency"));
if(isNaN(currency)) currency = 1;
tmp_scr = document.getElementById("query_text").innerHTML;
page_str = ' \
<form name="tmp_ord" method="post" action="'+home+'order2.php"> \
<div class=order_box><h1><table width=100% border=0><tr><td width=25%><a href="javascript:back_scr()"><div class=back title="Continue  Shopping"></div></a></td><td width=40%>' + ((lng != "TH") ? "SHOPPING CART" : "รายการสินค้าในรถเข็น") + '</td>\
<td width=30%>'+ ((lng != "TH") ? "Currency" : "สกุลเงิน") +' : \
<select name=exchange_type onchange="curr(this.value);" style="width:50px;color:red">\n ';
  for( i=0; i<e_type.length; i++ ) { 
    page_str += "<option value="+i+ ((i == currency) ? " selected>" : ">") + e_type[i]+"\n";
  }
page_str += '</select>\
</td></tr></table></h1>	';

page_str += ' \
<table width=100% border=0 cellspacing=1 cellpadding=0 bgcolor=#CCCCCC> \
    <tr valign=top align=center  bgcolor=#E9E9E9> \
        <td height=27 width=30>'+ ((lng !="TH") ? "No":"ลำดับ") +'</td> \
        <td width=80>'+ ((lng !="TH") ? "Part No.":"รหัสสินค้า") +'</td> \
        <td width=300>'+ ((lng !="TH") ? "Description":"รายการสินค้า") +'</td> \
        <td width=50>'+ ((lng !="TH") ? "Qty":"จำนวน") +'</td> \
        <td width=50>'+ ((lng !="TH") ? "Price":"ราคา") +'</td> \
        <td width=80>'+ ((lng !="TH") ? "Total":"รวม") +' \
		</td><td width=45>&nbsp;</td></tr>';
g_total = 0;
a = tmp.split("||");
for( i=0; i<a.length-1; i++ ) { 
    a2 = a[i].split("|");
    price = a2[2] * ( 1 * 1 / e_value[currency]); 
    price = a2[2] / (1 / e_value[currency]); 
    total = price * a2[3];
    g_total += total;
	page_str += ' \
    <tr align=center bgcolor=#ffffff><td height=24>'+ (i+1)+'</td> \
    <td>'+ a2[0] + '</td><td align=left>&nbsp;'+ a2[1] + ' </td> \
    <td><input type=text name=qty[] maxlength=4 value="'+ a2[3] + '" class="text_qty" onchange="sum_line()"></td> \
   	<input type=hidden name=u_price[] size=5 maxlength=6 value="0"> \
   	<input type=hidden name=u_discount[] size=5 maxlength=6 value="0"> \
   	<td><input type=text name=price[] size=5 maxlength=6 value="'+ conv(price,3) +'" class="text_total" onfocus="this.blur()"></td> \
    <td><input type=text name=total[] size=5 maxlength=8 value="'+ conv(total,2) +'" class="text_total2" onfocus="this.blur()"></td>\n \
    <td><a href="javascript:del_line(\''+ a2[0] + '\x27)"> \
    <font color=red>Delete</font></a></td></tr>\n' ;
}
while( i < 10) {
    page_str += '<tr bgcolor=#ffffff align=center><td height=24>&nbsp;</td><td>&nbsp;</td> \
    <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>';
    i++;
}
page_str += ' \
<tr valign=top bgcolor=#ffffff height=24><td colspan=4> \
<td style="padding-top:3px;" align=right>' + ((lng !="TH") ? "Total : " : "รวม : ") + '&nbsp;</td></td> \
<td align=center><input type=text name=g_total value="' + conv(g_total,2) + '" class="text_total2" onfocus="this.blur()"></td> \
<td>&nbsp;</td></tr> \
</table> <div align=center  style="font-family:Tahoma;font-size:12px; color:blue;">\
<input type=hidden name=qty[] value="">\
<input type=hidden name=u_price[] value="">\
<input type=hidden name=u_discount[] value="">\
<input type=hidden name=price[] value="">\
<input type=hidden name=total[] value="">\
<input type=hidden name="order_status" value="0"> \
' + ((lng !="TH") ? "Additional order ( description of unknown parts )" : "รายการที่ต้องการสั่งซื้อหรือบันทึกเพิ่มเติม") + '<br> \
<textarea name="remark" rows="5" cols="80" onchange="setCookie(\'remark\',this.value)">'+ getCookie("remark")+ '</textarea> \
</div><br><div align=center>\
<table align=center><tr align=cente> \
';

// if( getCookie("uid") == "" || getCookie("uid") =="deleted" ) {
if(	document.getElementById("user_logon").innerHTML == "" ) {
    page_str += ' \
        <td align=right>' + ((lng !="TH") ? "User ID :":" รหัสสมาชิก :") + '</td> \
        <td><input name=usercode size=6 maxlength=15></td> \
        <td align=right>&nbsp;&nbsp;' + ((lng !="TH") ? "Password :":"รหัสผ่าน :") +'</td> \
        <td><input name=passcode type=password size=6 maxlength=15>&nbsp;&nbsp;</td> \
    ';
} 

page_str += ' \
<td><input type=button name="bt1" value="'+ ((lng =="TH") ?  " ส่งรายการสั่งซื้อ " : "    Send Order    ") +'" onclick="chk_pwd()"></td> \
<td><input type=button name="bt3"  value="'+ ((lng =="TH")  ?   " ยกเลิกการสั่งซื้อ " : "   Cancel Order   ") +'" onclick="clr_tmp()"></td> \
</tr> \
</table> \
<a href="javascript:back_scr()"><div class=back title="Continue  Shopping"></div></a>\
</div>\
<br>&nbsp;<br>\
</form> \
</div>';

document.getElementById("query_text").innerHTML = page_str;
return;
}

function chk_pwd() {
    var a = getCookie("tmp_item").split("||");
    if( a.length < 2 ) {
        alert(document.getElementById("curr_lang").innerHTML=="TH" ? "ไม่สินค้าในรถเข็น ! " : " None Product in Basket.");
        return false;
    }    
    // if( getCookie("uid") != "" ) {
	if(	document.getElementById("user_logon").innerHTML != "" ) {
        if ( !confirm( document.getElementById("curr_lang").innerHTML=="TH" ? "ยืนยันส่งรายการสั่งซื้อ ?     " : " Confirm Send Order ?.   ")) return false;
        document.tmp_ord.submit();
        return true;
    }

    if( document.tmp_ord.usercode.value == "" ) {
        alert( document.getElementById("curr_lang").innerHTML=="TH" ? " กรุณาใส่รหัสสมาชิก      " : "Please enter User ID.");
        document.tmp_ord.usercode.focus();
        return false;
    }
    if( document.tmp_ord.passcode.value == "" ) {
        alert( document.getElementById("curr_lang").innerHTML=="TH" ? " กรุณาใส่รหัสผ่าน      " : "Please enter Password.");
        document.tmp_ord.passcode.focus();
        return false;
    }
    document.tmp_ord.submit();
    return true;    
}

function add_items() {
    if( typeof(top.opener) == "object" ) {
        top.opener.focus();
    } else {
        window.open("./","");
        this.close();
    }
}

function clr_tmp() {
a = getCookie("tmp_item").split("||");
if( a.length < 2 ) {
    alert(document.getElementById("curr_lang").innerHTML=="TH" ? "ไม่สินค้าในรถเข็น ! " : " None product in basket.");
    return false;
}    
if (!confirm(document.getElementById("curr_lang").innerHTML=="TH" ? "ยกเลิกรายการสั่งซื้อใช่หรือไม่ ?    " : "Cancel Order\r\nAre you sure ?   ")) return false;
    setCookie("tmp_item","");
    setCookie("remark", "");
    setCookie( "currency", "");
    setCookie("pid", "");
    window.location= home+"set_pid.php";
}

function del_line(p) {
var i,s = "",a = getCookie("tmp_item").split("||");
var scr = tmp_scr;
    for( i=0;i<a.length-1; i++) {
        if(a[i].indexOf(p) == -1) s += a[i] + "||";
    } 
    setCookie("tmp_item",s);
	order();
    sumitem();
	tmp_scr = scr;
}

