var color_red='#D20000';
var color_white='';
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;
}

var xmlhttp = Inint_AJAX();
var xmlhttp2 = Inint_AJAX();
function loadcontent(url,loadto){
	var trv=document.getElementById(loadto)
	trv.innerHTML="<center><img src=\"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 loadcontent2(url,loadto){
	var trv=document.getElementById(loadto)
	trv.innerHTML="<center><img src=/img/loading.gif></center>";
	serverPage=url;	
	xmlhttp2.open("GET",serverPage,true);	
	xmlhttp2.onreadystatechange=function(){
		if(xmlhttp2.readyState==4 && xmlhttp2.status==200){
			trv.innerHTML="";
			trv.innerHTML=xmlhttp2.responseText;
		}	
	}
	xmlhttp2.send(null);	
}

function loadpictext(url,loadto){
	langconnect=url.substring(url.indexOf("?")+1);
	var trv=document.getElementById(loadto);
	//trv.innerHTML='<img src="images/wait2.gif" alt="loading" />';
	serverPage=url;	
	
	xmlhttp.open("POST",serverPage,true);	
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4 && xmlhttp.status==200){	
			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://canon-ecomm.icat.co.th/images/shared/Plus.gif' ) 
	{ 
	oIcon.src = 'http://canon-ecomm.icat.co.th/images/shared/Minus.gif';
	oList.style.display = 'block';
	oText.innerHTML = '<b>Hide Compatible products</b>';
	} 
else 
	{ 
	oIcon.src = 'http://canon-ecomm.icat.co.th/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 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("POST",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 == '' || document.getElementById('code').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 SearchProduct(obj){
	//var prefix=encodeURI(document.getElementById(obj).value);
	var prefix=document.getElementById(obj).value;
	var ulink='';
	if(prefix != ''){
		ulink='index.php?act=search&k='+prefix;	
	}
	if(ulink != ''){
		//window.location=ulink;	
	}
}
function CheckOutConfirm(){
	return confirm("Check Out Now?");	
}