var color_red='#D20000';
var color_white='';
function Inint_AJAX() {
	try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
	try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE	
	alert("XMLHttpRequest not supported");
	return null;
}

var xmlhttp = Inint_AJAX();
var xmlhttp2 = Inint_AJAX();
function parseScript(_source){
	var source = _source;
	var scripts = new Array();
	
	// Strip out tags
	while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
		var s = source.indexOf("<script");
		var s_e = source.indexOf(">", s);
		var e = source.indexOf("</script", s);
		var e_e = source.indexOf(">", e);
		
		// Add to scripts array
		scripts.push(source.substring(s_e+1, e));
		// Strip from source
		source = source.substring(0, s) + source.substring(e_e+1);
	}
	
	// Loop through every script collected and eval it
	for(var i=0; i<scripts.length; i++) {
		try {
			eval(scripts[i]);
		}
		catch(ex) {
			// do what you want here when a script fails
		}
	}
	
	// Return the cleaned source
	return source;
}
function randoms(){
	return cur=Math.random();
}
function loadcontent(url,loadto){	
	var trv=document.getElementById(loadto);	
	trv.innerHTML='';
	trv.innerHTML="<center><img src=img/loading.gif></center>";
	if(url.lastIndexOf('time')==-1){
		if(url.lastIndexOf("?")!=-1){
			url+='&time='+randoms();
		}else{
			url+='?time='+randoms();	
		}
	}else{
		time_arr=url.split('time');
		if(time_arr[1].lastIndexOf("?")!=-1){
			url=time_arr[0]+'time='+randoms();
		}else{
			url=time_arr[0]+'time='+randoms();	
		}
	}	
	xmlhttp.open("GET",url,true);	
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4 && xmlhttp.status==200){
			trv.innerHTML="";
			trv.innerHTML=parseScript(xmlhttp.responseText);						
		}
	}
	xmlhttp.send(null);	
}
function loadcontent2(url,loadto){	
	var trv=document.getElementById(loadto);	
	trv.innerHTML='';
	trv.innerHTML="<center><img src=img/loading.gif></center>";
	if(url.lastIndexOf('time')==-1){
		if(url.lastIndexOf("?")!=-1){
			url+='&time='+randoms();
		}else{
			url+='?time='+randoms();	
		}
	}else{
		time_arr=url.split('time');
		if(time_arr[1].lastIndexOf("?")!=-1){
			url=time_arr[0]+'time='+randoms();
		}else{
			url=time_arr[0]+'time='+randoms();	
		}
	}	
	xmlhttp2.open("GET",url,true);	
	xmlhttp2.onreadystatechange=function(){
		if(xmlhttp2.readyState==4 && xmlhttp2.status==200){
			trv.innerHTML="";
			trv.innerHTML=parseScript(xmlhttp2.responseText);						
		}
	}
	xmlhttp2.send(null);	
}
function loadcontent3(url,loadto,pro){
	var promotion = "";
	if(pro=="1"){
		var promotion_type_id = document.form_pro.promotion_type_id;
		if(promotion_type_id.length){
			for(var i=0; i < promotion_type_id.length; i++){
				if(promotion_type_id[i].checked){
					promotion = promotion_type_id[i].value;
				}
			}
		}else{
			promotion = promotion_type_id.value;
		}
	}
	//alert(promotion);
	var currentTime = new Date();
	var Hours = currentTime.getHours();
	var Minutes = currentTime.getMinutes();
	var Seconds = currentTime.getSeconds();
	var time = Hours+Minutes+Seconds;

	var trv=document.getElementById(loadto)
	trv.innerHTML="<center><img src=img/loading.gif></center>";
	serverPage=url+'?promotion='+promotion+"&t="+time;	
	xmlhttp.open("GET",serverPage,true);	
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4 && xmlhttp.status==200){
			trv.innerHTML="";
			trv.innerHTML=xmlhttp.responseText;
		}	
	}
	xmlhttp.send(null);	
}
function makePOSTRequest(url,loadto,parameters){	
	var trv=document.getElementById(loadto)
	trv.innerHTML="<center><img src=img/loading.gif></center>";
	serverPage=url;	
	xmlhttp.open("POST",serverPage,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  	xmlhttp.setRequestHeader("Content-length", parameters.length);
 	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4 && xmlhttp.status==200){
			trv.innerHTML="";
			trv.innerHTML=xmlhttp.responseText;
			
		}	
	}
	xmlhttp.send(parameters);	
}
function loadsupport(obj,loadto,url){
	var prefix=document.getElementById(obj).options[document.getElementById(obj).selectedIndex].value;
	loadcontent(url+prefix,loadto);
}
function loadsubcontent(obj,obj2,loadto,url){
	var prefix=document.getElementById(obj).options[document.getElementById(obj).selectedIndex].value;
	document.getElementById(obj2).options[document.getElementById(obj).selectedIndex].selected=true;
	loadcontent(url+prefix,loadto);
}
function ShowHide(product_code) {

var oIcon = document.getElementById('open_close_'+product_code);
var oList = document.getElementById(product_code);
var oText = document.getElementById('text_'+product_code);

if ( oIcon.src == 'http://www.e-suppliescanon-th.com/images/shared/Plus.gif' ) 
	{ 
	oIcon.src = 'http://www.e-suppliescanon-th.com/images/shared/Minus.gif';
	oList.style.display = 'block';
	oText.innerHTML = '<b>Hide Compatible products</b>';
	} 
else 
	{ 
	oIcon.src = 'http://www.e-suppliescanon-th.com/images/shared/Plus.gif';
	oList.style.display = 'none';
	oText.innerHTML = '<b>Show Compatible products</b>';
	}
}
function emailCheck (obj,emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|co.th)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		//alert("Email address seems incorrect (check @ and .'s)");
		//document.getElementById("email").focus();
		document.getElementById(obj).style.backgroundColor = color_red;
		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			//alert("Ths username contains invalid characters.");
			//document.getElementById("email").focus();
			ddocument.getElementById(obj).style.backgroundColor = color_red;
			alert("Ths username contains invalid characters.");
			return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			//alert("Ths domain name contains invalid characters.");
			//document.getElementById("email").focus();
			document.getElementById(obj).style.backgroundColor = color_red;
			alert("Ths domain name contains invalid characters.");
			return false;
   		}
	}
	if (user.match(userPat)==null) {
		//alert("The username doesn't seem to be valid.");
		//document.getElementById("email").focus();
		document.getElementById(obj).style.backgroundColor = color_red;
		alert("The username doesn't seem to be valid.");
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				//alert("Destination IP address is invalid!");
				//document.getElementById("email").focus();
				document.getElementById(obj).style.backgroundColor = color_red;
				alert("Destination IP address is invalid!");
				return false;
		   }
		}
		//return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			//alert("The domain name does not seem to be valid.");
			//document.getElementById("email").focus();
			document.getElementById(obj).style.backgroundColor = color_red;
			alert("The domain name does not seem to be valid.");
			return false;
	   }
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		//alert("The address must end in a well-known domain or two letter " + "country.");
		//document.getElementById("email").focus();
		document.getElementById(obj).style.backgroundColor = color_red;
		alert("The address must end in a well-known domain or two letter \" + \"country.");
		return false;
	}

	if (len<2) {
		//alert("This address is missing a hostname!");
		//document.getElementById("email").focus();
		document.getElementById(obj).style.backgroundColor = color_red;
		alert("This address is missing a hostname!");
		return false;
	}
	//return true;	
	document.getElementById(obj).style.backgroundColor = color_white;
}
function loadhtml(text,loadto){
	document.getElementById(loadto).innerHTML=text;
}
function Popup(url,popupname,width,height){
	window.open(''+url,''+popupname,'scrollbars=no,width='+width+',height='+height+',resizable=no');
}
function Popup2(url,popupname,width,height){
	window.open(''+url,''+popupname,'scrollbars=yes,width='+width+',height='+height+',resizable=yes');
}
function isExisting(obj){
  if (document.getElementById(obj)) { return true; } else { return false; }
}
function chkkey(obj){
	if(document.getElementById(obj).value != '') document.getElementById(obj).style.backgroundColor=color_white;
	else document.getElementById(obj).style.backgroundColor=color_red;
}
function ChkRegister(){
	var check_enum = new Array();	
	var check_enum2=1;
	var i;		
	loadhtml('','exists_email');
	loadhtml('','exists_user');
	for(i=0;i<document.forms['frm_register'].elements.length;i++){
		box=document.forms['frm_register'].elements[i];
		if(box != null && box.name != ""){
			if(box.name=="email" && box.value != ''){
				if(emailCheck(box.name,box.value) == false){
					box.style.backgroundColor = color_red;
					check_enum[i]=0;
				} else {
					box.style.backgroundColor = color_white;					
					check_enum[i]=1;					
				}
			}else if(box.name=="company_name" || box.name=="job" || box.name=="position" || box.name=="fax_number"){
				box.style.backgroundColor = color_white;					
				check_enum[i]=1;				
			}else if(box.name=="U_Password" || box.name=="U_Password"){
				box.style.backgroundColor = color_white;					
				check_enum[i]=1;				
			}else{
				if(!box.value){
					box.style.backgroundColor = color_red;			
					check_enum[i]=0;
				}else{
					box.style.backgroundColor = color_white;	
					check_enum[i]=1;
				}
			}			
		}
	}
	if(document.getElementById("member_pass1").value.length < 4 && document.getElementById("member_pass2").value!=''){
		alert("Password too short!");
		check_enum[i+1]=0;
	}else{
		if(document.getElementById("member_pass1").value != '' && document.getElementById("member_pass2").value != ''){
			if(document.getElementById("member_pass1").value != document.getElementById("member_pass2").value){
				alert("Password not match!");
				document.getElementById("member_pass1").style.backgroundColor = color_red;
				document.getElementById("member_pass2").style.backgroundColor = color_red;
				check_enum[i+1]=0;
			}
		}
	}
	for(i=0;i<check_enum.length;i++){
		if(check_enum[i] == 0) {
			check_enum2=0;
		}
	}
	
	if(check_enum2 == 0) {		
		return false;	
	}else{
		if(document.getElementById('agreement').checked == false){
			alert("I have read this agreement and agree to all of the provisions contained above.");
			return false;
		}
	}
}
function ChkProfile(){
	var check_enum = new Array();	
	var check_enum2=1;
	var i;		
	loadhtml('','wrong_pass');
	for(i=0;i<document.forms['frm_profile'].elements.length;i++){
		box=document.forms['frm_profile'].elements[i];
		if(box != null && box.name != ""){
			if(box.name=="email" && box.value != ''){
				if(emailCheck(box.name,box.value) == false){
					box.style.backgroundColor = color_red;
					check_enum[i]=0;
				} else {
					box.style.backgroundColor = color_white;					
					check_enum[i]=1;					
				}
			}else if(box.name=="company_name" || box.name=="job" || box.name=="position" || box.name=="fax_number"){
				box.style.backgroundColor = color_white;					
				check_enum[i]=1;				
			}else if(box.name=="member_pass1" || box.name=="member_pass2"){
				box.style.backgroundColor = color_white;					
				check_enum[i]=1;				
			}else{
				if(!box.value){
					box.style.backgroundColor = color_red;			
					check_enum[i]=0;
				}else{
					box.style.backgroundColor = color_white;	
					check_enum[i]=1;
				}
			}			
		}
	}
	if(document.getElementById("member_pass1").value != '' && document.getElementById("member_pass2").value != ''){
		if(document.getElementById("member_pass1").value.length < 4 && document.getElementById("member_pass2").value != ''){
			alert("Password too short!");
			check_enum[i+1]=0;
		}else{
			if(document.getElementById("member_pass1").value != document.getElementById("member_pass2").value){
				alert("Password not match!");
				document.getElementById("member_pass1").style.backgroundColor = color_red;
				document.getElementById("member_pass2").style.backgroundColor = color_red;
				check_enum[i+1]=0;
			}
		}
	}
	for(i=0;i<check_enum.length;i++){
		if(check_enum[i] == 0) {
			check_enum2=0;
		}
	}
	
	if(check_enum2 == 0) {		
		return false;
	}
}
function selectbox(obj){	
	document.getElementById(obj).checked = true;
}
function disablebox(obj,value){	
	document.getElementById(obj).disable = value;
}
function delcontent(url,loadto,msg){
	if(confirm(msg)== true){
		var trv=document.getElementById(loadto)
		trv.innerHTML="<center><img src=img/loading.gif></center>";
		serverPage=url;	
		xmlhttp.open("GET",serverPage,true);	
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4 && xmlhttp.status==200){
				trv.innerHTML="";
				trv.innerHTML=xmlhttp.responseText;
			}	
		}
		xmlhttp.send(null);	
	}
}
function pos(obj){
	var a=document.getElementById(obj).value;
	a++;
	document.getElementById(obj).value=a;
	}
function nnn(obj){
	var a=document.getElementById(obj).value;
	if(a>1){
		a--;
		document.getElementById(obj).value=a;
	}
}
function SearchSupplies(obj,obj2){
	var prefix=document.getElementById(obj).options[document.getElementById(obj).selectedIndex].value;
	var prefix2=document.getElementById(obj2).options[document.getElementById(obj2).selectedIndex].value;
	var ulink='';
	if(prefix != ''){
		if(prefix == 'ink_model'){
			ulink='index.php?act=view&category_id=1&category=ink_model';
		}else if(prefix == 'toner_model'){
			ulink='index.php?act=view&category_id=2&category=toner_model';
		}else{
			ulink='index.php';	
		}
	}
	if(prefix2 != ''){
		ulink+='&model='+prefix2;		
	}	
	if(ulink != ''){
		window.location=ulink;	
	}
}
function CheckLogin(){
	if(document.getElementById('U_Username').value == '' || document.getElementById('U_Password').value == ''){
		/*var U_Username = hex_sha1(document.getElementById('U_Username').value);
		var U_Password = hex_sha1(document.getElementById('U_Password').value);
		var parameters="Login=yes";
		parameters+="&U_Username="+U_Username;
		parameters+="&U_Password="+U_Password;
		makePOSTRequest('member_action.php','login_area',parameters);*/
		return false;
	}
}
function ChkReset(){
	disablebox('Reset_Pass',true);
	if(document.getElementById('email').value == '' && document.getElementById('forgot_secure').value == ''){
		disablebox('Reset_Pass',false);
		return false;	
	}
}
function LoadSecureIMG(obj){
	document.getElementById(obj).src = 'securimage_show.php?sid=' + Math.random();
}
function LTrim(str){
    if (str==null){return null;}
    for(var i=0;str.charAt(i)==" ";i++);
    return str.substring(i,str.length);
}
function RTrim(str){
    if (str==null){return null;}
    for(var i=str.length-1;str.charAt(i)==" ";i--);
    return str.substring(0,i+1);
}
function Trim(str){
    return LTrim(RTrim(str));
}
function SearchProduct(obj){
	//var prefix=encodeURI(document.getElementById(obj).value);
	var prefix=Trim(document.getElementById(obj).value);
	var ulink='';
	if(prefix != ''){
		ulink="index.php?act=search&k="+prefix;	
	}
	if(ulink != ''){
		window.location.href=ulink;
		return true;
	}
	return false;
}
function CheckOutConfirm(){
	return confirm("Check Out Now?");	
}
function roll_over(img_name, img_src){
   document.getElementById(img_name).src = img_src;
}

function Inint_AJAX() {
   try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
   try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
   alert("XMLHttpRequest not supported");
   return null;
}

function check_coupon(coupon_code,loadto){
	var trv=document.getElementById(loadto);
	if(coupon_code.length != 0){
			  var req = Inint_AJAX(); 
			  req.onreadystatechange = function() {
				if (req.readyState==4) {
				  if (req.status==200) { 
						trv.innerHTML=req.responseText;
				  }
				}
			  }
			  req.open('POST', 'check_coupon.php', true);
			  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
			  req.send("Cupon_Code=" + coupon_code);
	}	
}