var home = "/";
var app = navigator.appName.toLowerCase();
var netscape = app.indexOf("netscape");
var Open = ""
var Closed = ""
var app = navigator.appName.toLowerCase();
var netscape = app.indexOf("netscape");

var show = getCookie("show");
var ordwin = "";
var hwin = "";
var gid = "";

function spc_price() {
    u =  "spc_price.php";
    if( typeof(hwin.opener) != "object" ) {
        hwin = window.open( u ,"spc_price1","status=0,menubar=0,resizable=1,toolbar=0,scrollbars=1,left=0,top=0,width="+(screen.width-10)+",height="+(screen.height-60));
    } else {
        hwin.focus();
    }
   return;
}

function spc_price_h() {
    u =  "spc_price.php";
    if( typeof(hwin.opener) != "object" ) {
        hwin = window.open( u ,"spc_price1","status=0,menubar=0,resizable=1,toolbar=0,scrollbars=1,left=0,top=0,width="+(screen.width-10)+",height="+(screen.height-60));
    } else {
        hwin.focus();
    }
   return;
}

function wopen(doc) {
    window.open(doc,"_blank", " height=250,width=400");
}

function register(name,p) {
    setCookie(name, p);
}

function setCookie(name, value) {
    var today = new Date();
    var expire = new Date();
    expire.setTime(today.getTime() + 60*60*24*365);
    document.cookie = name + "=" + escape(value) + "; path=/; "+ ((expire == null) ? "" : ("; expires=" + expire.toGMTString())); 
}

function getCookie(Name) {
var search = Name + "=";
if (document.cookie.length > 0) { 
    offset = document.cookie.indexOf(search);
    if (offset != -1) { 
        offset += search.length;
        end = document.cookie.indexOf(";", offset);
        if (end == -1)  end = document.cookie.length;
        return unescape(document.cookie.substring(offset, end));
    }
}
return "";
}

function add(p) {
    var i,a;
    var t = getCookie("tmp_item");
    if( typeof(t) == "undefined" ) {
      setCookie("tmp_item", p+"||");
       sumitem();
       return;
    } else {
        a = t.split("||");
		if( a.length > 40 ) { alert( show=="t" ? "  เลือกได้ 40 รายการต่อ 1 ใบสั่งซื้อ. " : "  Limit 40 item / 1 Order. " );  return; }
        for(i=0; i<a.length; i++) { 
            if( a[i] == p && p != "" ) {
                return;
            }
        }
        if( p != "" ) t += p+"||"; 
        setCookie("tmp_item", t);
        sumitem();
        return;
    }
}

function sumitem() {
	var n = Math.max(0, getCookie("tmp_item").split("||").length -1);
	document.getElementById("card_item").innerHTML = n;
	document.getElementById("card_item2").innerHTML = "["+n+"]";
    return;
}

function spc_price( g ) {
	var u =  "spc_price_d.php?gid="+g;
    if( typeof(hwin.opener) != "object" ) {
        hwin = window.open( u ,"spc_price","status=0,menubar=0,resizable=1,toolbar=0,scrollbars=1,left=0,top=0,width="+(screen.width-10)+",height="+(screen.height-60));
    } else {
        if( gid != g ) hwin.location = u;
        hwin.focus();
    }
   gid = g;
   return;
}

	function page(n) {
		document.q.page.value=n;
		document.q.submit();
		return true;
	}

	function wopen(doc) {
		window.open(doc,"_blank", " height=250,width=400");
	}

	function picture(doc,x,y) {
             var addit = " left="+ (400-x /2) + ",top="+(300-y/2)+",height="+(+y+20)+",width="+(+x+20);
             if( y < 400 ) { 
	         window.open("/image1.php?src="+doc,"image", addit);
	     } else {    
	         window.open("/image2.php?src="+doc,"image", addit);
	     }
	}

	function check() {
             if( document.login.usercode.value == "" ) {
                 alert ("    Please enter User ID.    ");
                 document.login.usercode.setfocus();
                 return false;
             }
             if( document.login.passcode.value == "" ) {
                 alert ("    Please enter Password.    ");
                 document.login.passcode.setfocus();
                 return false;
             }
             document.login.submit();
             return true;
	}


function mo(e) {
var S = document.getElementById("m"+e);
    S.className="t";
}

function mu(e) {
var S = document.getElementById("m"+e);
    S.className="p";
}
function link(p) {
    window.location=home+p;
}

function mailwidow() {
		window.open("/mail/","mail");
}

document.write( ' \
<STYLE type=text/css> \
.button1 {cursor:'+ ((netscape<0) ? "hand" : "pointer") +';margin-bottom: 1px;background-color:#ECECFF;color:blue;width:168px;padding:1px;text-align:center;font-weight:normal;font-family:MS Sans Serif,Tahoma;font-size: 9pt;border:1px solid #000000;} \
.button2 {cursor:'+ ((netscape<0) ? "hand" : "pointer") +';margin-bottom: 1px;background-color:#ECECFF;color:red;width:168px;padding:1px;text-align:center;font-weight:normal;font-family:MS Sans Serif,Tahoma;font-size: 9pt;border:1px solid #000000;} \
.button3 {cursor:'+ ((netscape<0) ? "hand" : "pointer") +';margin-bottom: 1px;background-color:#ECECFF;color:#003333;padding:1px;text-align:center;font-weight:normal;font-family:MS Sans Serif,Tahoma;font-size: 9pt;border:1px solid #000000;} \
</STYLE> \
');

document.write( ' \
<style type="text/css"> \
.mnu {cursor:' + ((netscape<0) ? "hand" : "pointer") +'; font-Family:MS Sans Serif; color:#330066; text-decoration:none; font-weight:bold; padding-left: 5px;} \
</style> \
');

document.write( ' \
<STYLE type=text/css> \
.o td {white-space: nowrap; height: 23px; color:#330066;} \
.o a {color:#330066; text-decoration: none! important} \
.p {cursor: '+ ((netscape<0) ? "hand" : "pointer") +';padding-right: 4px; padding-left: 4px; color:#FF0000;} \
.t {border-right: #e4d9d2 1px solid; padding-right: 3px; border-top: #e4d9d2 1px solid; padding-left: 3px; border-left: #e4d9d2 1px solid; cursor: hand; border-bottom: #e4d9d2 1px solid; background-color:#f8f8f7;color:#FF0000;} \
.l {padding-left:1px} \
</STYLE> \
');


if(document.images){
Open = new Image(16,13) 
Closed = new Image(16,13)
Open.src = "/images/minus.gif"
Closed.src = "/images/plus.gif"
}

function showhide(what,what2,what3){
var el = document.getElementById(what);
if (el.style.display=="none"){
    el.style.display="";
    what2.src=Open.src
}
else{
    el.style.display="none"
    what2.src=Closed.src
}
setCookie(what3, el.style.display== "" ? 1 : 0)
}


var HttPRequest = false;
var homeurl="/";

function getHttPRequest() {
	HttPRequest = false;
	if (window.XMLHttpRequest) {
	  // Mozilla, Safari,...
		 HttPRequest = new XMLHttpRequest();
		 if (HttPRequest.overrideMimeType) {
			HttPRequest.overrideMimeType('text/html');
		 }
	} else if (window.ActiveXObject) { // IE
		 try {
			HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
		 } catch (e) {
			try {
				   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	 } 
	 if (!HttPRequest) {
		alert('Cannot create XMLHTTP instance');
		return false;
	 }
	 return true;
}

function sendRequest( url, pmeters, elename ) {
	HttPRequest.open('POST',url,true);
	HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	HttPRequest.setRequestHeader("Content-length", pmeters.length);
	HttPRequest.setRequestHeader("Connection", "close");
	HttPRequest.send(pmeters);

	HttPRequest.onreadystatechange = function() 
	{
		if(HttPRequest.readyState == 3) document.getElementById(elename).innerHTML = "<center> Loading...</center>";
		if(HttPRequest.readyState == 4) document.getElementById(elename).innerHTML = HttPRequest.responseText;
	}
}

function user_logon() {
	if( !getHttPRequest() ) return;
	var o = document.logon;
	if( o.userid.value == "" ) { alert( " Enter User ID " ); o.userid.focus(); return false; }
	if( o.passwd.value == "" ) { alert( " Enter Password " ); o.paswd.focus(); return false; }

	var p = "userid=" + encodeURI( document.getElementById("userid").value);
			p+= "&passwd=" + encodeURI( document.getElementById("passwd").value );
			p+= "&num_item=" + encodeURI( document.getElementById("card_item").innerHTML );

	sendRequest( homeurl+"user_logon.php", p, "logon_text" );
}

function wt_users( id ) {
	var 	p = "userid=" + encodeURI( id );
	if( !getHttPRequest() ) return;
	sendRequest( homeurl+"users.php", p, "query_text" );
}

function myaccount() {
	var p = "user_id=" + encodeURI( document.getElementById("user_logon").innerHTML);
	if( !getHttPRequest() ) return;
	sendRequest( homeurl+"users_profile.php", p, "query_text" );
}

function check_register() {
var o = document.form1; 
	if (o.usercode.value=="") {
		alert("Please Enter Login ID");
		o.usercode.focus(); return false;
	}
	if (o.passcode.value == "") {
		alert("Please choose a valid Password");
		o.password.focus(); return false;
	}
	if (o.passcode.value  != o.confirmpasscode.value ) {
		alert("Please choose a valid Password or confirm Password");
		o.confirmpasscode.focus(); return false;
	}
	if (o.mname.value == "" ) {
		alert("Please Enter Name");
		o.mname.focus(); return false;
	}
	if ( o.address1.value == "") {
		alert("Please Enter Address");
		o.address1.focus(); return false;
	}
	if ( o.email.value == "") {
		alert("Please Enter  E-Maill");
		o.email.focus(); return false;
	}
	q = "usercode=" + encodeURI( o.usercode.value );
	q += "&passcode=" + encodeURI( o.passcode.value );
	q += "&mname=" + encodeURI( o.mname.value );
	q += "&custname=" + encodeURI( o.custname.value );
	q += "&address1=" + encodeURI( o.address1.value );
	q += "&country=" + encodeURI( o.country[o.country.selectedIndex].value );
	q += "&phone=" + encodeURI( o.phone.value );
	q += "&fax=" + encodeURI( o.fax.value );
	q += "&email=" + encodeURI( o.email.value );

	if( !getHttPRequest() ) return;
	sendRequest( homeurl+"users.php", q, "query_text" );
	return true;
}

function check_users_profile() {
var o = document.form1; 
	if (o.passcode.value == "") {
		alert("Please choose a valid Password");
		o.password.focus(); return false;
	}
	if (o.passcode.value  != o.confirmpasscode.value ) {
		alert("Please choose a valid Password or confirm Password");
		o.confirmpasscode.focus(); return false;
	}
	if (o.mname.value == "" ) {
		alert("Please Enter Name");
		o.mname.focus(); return false;
	}
	if ( o.address1.value == "") {
		alert("Please Enter Address");
		o.address1.focus(); return false;
	}
	if ( o.email.value == "") {
		alert("Please Enter  E-Maill");
		o.email.focus(); return false;
	}
	q = "user_id=" + encodeURI( o.user_id.value );
	q += "&status=" + encodeURI( o.status.value );
	q += "&passcode=" + encodeURI( o.passcode.value );
	q += "&mname=" + encodeURI( o.mname.value );
	q += "&custname=" + encodeURI( o.custname.value );
	q += "&address1=" + encodeURI( o.address1.value );
	q += "&country=" + encodeURI( o.country[o.country.selectedIndex].value );
	q += "&phone=" + encodeURI( o.phone.value );
	q += "&fax=" + encodeURI( o.fax.value );
	q += "&email=" + encodeURI( o.email.value );

	if( !getHttPRequest() ) return;
	sendRequest( homeurl+"users_profile.php", q, "query_text" );
	return true;
}

function wt_search() {
	var	o = document.s01;
	if( !getHttPRequest() ) return;
	if( o.s_type.checked  && o.s_by[0].checked ) {
		p = "name=" + encodeURI( o.name.value );
		p += "&sort=" + encodeURI( o.sort.value );
		p += "&case=" + encodeURI( o.prod_case.value );
		p += "&brand=" + encodeURI( o.brand.value );
		p += "&code=" + encodeURI( o.code.value );
		p += "&type=" + encodeURI( o.type.value );
		p += "&volt=" + encodeURI( o.volt.value );
		p += "&amp=" + encodeURI( o.amp.value );
		p += "&s_type=" + encodeURI("1");
		p += "&s_by=" + encodeURI("1");

		sendRequest( homeurl+"query.php", p, "query_text" );
		return;
	}
	if( o.s_by[0].checked ) {
		if( o.s_text1.value == "") return;
		p = "name="+ encodeURI( o.s_text1.value ) + "&s_by=1";
		sendRequest( homeurl+"query.php", p, "query_text" );
		return;
	}
	if( o.s_by[1].checked ) {
		if( o.s_text2.value == "") return;
		oo = document.gform;
		oo.q.value = 'site:www.wt.co.th ' + o.s_text2.value;
		oo.submit();
		return;
	}
}

function chk_element(){
	var o = document.s01;
	if( o.s_by[0].checked ) {
		document.getElementById("s01_1").style.display = "";
		document.getElementById("s_text2").style.display = "none";
		if( o.s_type.checked) {
			document.getElementById("s01_2").style.display = "";
			document.getElementById("s_text1").style.display = "none";
		} else {
			document.getElementById("s01_2").style.display = "none";
			document.getElementById("s_text1").style.display = "";
		}
	}
	if( o.s_by[1].checked ) {
		document.getElementById("s01_1").style.display = "none";
		document.getElementById("s_text1").style.display = "none";
		document.getElementById("s01_2").style.display = "none";
		document.getElementById("s_text2").style.display = "";
	}
}

function quotation() {
	if( !getHttPRequest() ) return;
	sendRequest( homeurl+"users_quotation.php", '', "query_text" );
}

function quotation_cancel( id ) {
	if (!confirm( '"'+id+'"\r\n'+ (show =="t" ? "ยกเลิกรายการนี้ใช่หรือไม่ ?    " : "Cancel Order\r\nAre you sure ?   ") )) return;
		if( !getHttPRequest() ) return;
		sendRequest( homeurl+"users_quotation.php", "doc_no="+id, "query_text" );
}

function quotation_form( id ) {
	if( !getHttPRequest() ) return;
 	tmp_scr = document.getElementById("query_text").innerHTML;
	sendRequest( homeurl+"users_quotation_form.php", "doc_no="+id, "query_text" );
}

function quotation_back() {
 	document.getElementById("query_text").innerHTML = tmp_scr;
	tmp_scr = "";
}

function cat( p ) {
	if( !getHttPRequest() ) return;
	p = "type=" + encodeURI( p );
	sendRequest( homeurl+"query.php", p, "query_text" );
}

function q01(qstr) {
	if( !getHttPRequest() ) return;
	sendRequest( homeurl+"query.php", qstr, "query_text" );
}

function cat2( f ) {
	if( !getHttPRequest() ) return;
	qstr = "cat_id=" + encodeURI( f );
	sendRequest( homeurl+"query2.php", qstr, "query_text" );
}

function p2( f, p ) {
	if( !getHttPRequest() ) return;
	var qstr = "cat_id=" + encodeURI( f ) + "&page="+encodeURI( p );
	sendRequest( homeurl+"query2.php", qstr, "query_text" );
}


