// JavaScript Document
/*all browsers*/
/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    xmlHttp = false;
  }
}
@end @*/
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
}

var url="http://"+location.host+"/";


var tempPro = new Array();//Temporary products

var choose = false;

function XmlHttpAction(method,url,func,sender)
{
	xmlHttp.open(method,url,true);
	xmlHttp.onreadystatechange=func;
	if(method=="POST")
	{
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	}
	xmlHttp.send(sender);
}

function CheckUser(val){
	var t = true;
	//post
	XmlHttpAction(
		"GET",
		url+"checkup.asp?action=checkuser&username="+val+"&rnd="+Math.random(),
		function(){
			if(xmlHttp.readyState==4){
				var response=xmlHttp.responseText;
				if(response=="error"){
					t = false;
				}else{
					t = true;
				}
			}
		},
		"");
	//send
	return t;
}

function Login(thisform){
	var val = getFormElement(thisform);
	XmlHttpAction("POST",url+"checkup.asp?action=login&rnd="+Math.random(),
		function(){
			if(xmlHttp.readyState==4){
				var response=xmlHttp.responseText;
				if(response=="success"){
					UserState();
					tb_remove();
				}
				alert("Welcome to qiloo! you are logged now");
				document.location.reload();
			}
		},val.join("&"));	
}
//Login 
function CheckLogin(thisform){
	if(thisform.username.value==""){
		alert("Username is Empty");
		return false;
	}
	if(thisform.password.value==""){
		alert("Password is Empty");
		return false;
	}
	Login(thisform);
}

function Logout(){
	XmlHttpAction("GET",url+"checkup.asp?action=logout&rnd="+Math.random(),
		function(){
			if(xmlHttp.readyState==4){
				var response=xmlHttp.responseText;
				if(response=="success"){
					UserState();
					alert("You are logged out");
					document.location.reload();
				}else{
					alert(response);
				}
			}
		},"");	
}
//Jion Free

function CheckJoin(thisform){
	if(thisform.username.value==""){
		alert("Invalid! User name is Empty");
		return false;
	}
	if(thisform.username.value.length<4 || thisform.username.value.length>20){
		alert("Invalid! The length of user name must be between 4 character and 20 character");
		return false;
	}
	if(!CheckUser(thisform.username.value)){
		alert("Sorry! The user name is exist, choose the other one please");
		return false;
	}
	var re=/^[0-9a-zA-Z_]/;
	if(!re.test(thisform.username.value)){
		alert("Invalid! User name use only such as '0-9','a-z','A-Z' and '_'");
		return false;
	}
	if(thisform.password.value==""){
		alert("Invalid! Password is Empty");
		return false;
	}
	if(thisform.password.value.length<6 || thisform.password.value.length>30){
		alert("Invalid! The length of password must be between 4 character and 30 character");
		return false;
	}
	if(thisform.company.value=="" || thisform.company.value.length<4){
		alert("Invalid! Company name must be more than 4 character");
		return false;
	}
	if(thisform.firstname.value==""){
		alert("Invalid! Your first name is empty");
		return false;
	}
	if(thisform.lastname.value==""){
		alert("Invalid! Your last name is empty");
		return false;
	}
	if(thisform.province.value==""){
		alert("Invalid! Province/State is empty");
		return false;
	}
	if(thisform.address.value==""){
		alert("Invalid! Street address is empty");
		return false;
	}
	re = /^[0-9a-zA-Z\-]/;
	if(!re.test(thisform.address.value) || thisform.address.value.length<4){
		alert("Invalid! Plese enter valid address");
		return false;
	}
	if(thisform.zip.value==""){
		alert("Invalid! Zip code is empty");
		return false;
	}
	if(!re.test(thisform.zip.value)){
		alert("Invalid! Please enter valid zip code");
		return false;
	}
	if(thisform.email.value==""){
		alert("Invalid! E-mail is empty");
		return false;
	}
	re = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	if(!re.test(thisform.email.value)){
		alert("Invalid! Please enter valid E-Mail");
		return false;
	}
	if(thisform.country.value==""){
		alert("Invalid! Country code is empty");
		return false;
	}
	if(thisform.telephone.value==""){
		alert("Invalid! Telephone number is empty");
		return false;
	}
	AddJoin(thisform);
}

//join
function AddJoin(thisform){
	var val = getFormElement(thisform);
	tb_remove();
	$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
	$('#TB_load').show();//show loader
	XmlHttpAction("POST",url+"checkup.asp?action=saveuser&rnd="+Math.random(),
		function(){
			if(xmlHttp.readyState==4){
				tb_remove();
				var response=xmlHttp.responseText;				
				if(response=="success"){
					tb_show('','#TB_inline?height=60&width=300&inlineId=complete',true);
				}else{
					alert(response);
				}
			}
		},val.join("&"));
}

function showEdit(){
	XmlHttpAction("GET",url+"checkup.asp?action=getuser&username="+getCookie("UserName")+"&rnd="+Math.random(),
		function(){
			if(xmlHttp.readyState==4){
				var xmldoc = xmlHttp.responseXML;
				if(xmldoc!=null){
					var root = xmldoc.documentElement;					
					var thisform = document.getElementById("myform").elements;
					
					for(var i=0; i<thisform.length; i++){
						thisform[i].value = root.childNodes[i].text;
					}
				}
			}
		},"");
}
//check edit
function checkEdit(thisform){
	if(thisform.company.value=="" || thisform.company.value.length<4){
		alert("Company name must be more than 4 character");
		return false;
	}
	if(thisform.contact.value==""){
		alert("Contact is empty");
		return false;
	}
	if(thisform.address.value==""){
		alert("Address is empty");
		return false;
	}
	re = /^[0-9a-zA-Z\-]/;
	if(!re.test(thisform.address.value) || thisform.address.value.length<10){
		alert("Plese enter valid address");
		return false;
	}
	if(thisform.email.value==""){
		alert("Email is empty");
		return false;
	}
	re = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	if(!re.test(thisform.email.value)){
		alert("Please enter valid e-mail");
		return false;
	}
	EditSave(thisform);
}

//check is login
function isLogin(){
	XmlHttpAction("GET",url+"checkup.asp?action=getcookie&rnd="+Math.random(),
		function(){
			if(xmlHttp.readyState==4){
				var response=xmlHttp.responseText;
				var disLogin=document.getElementById("disLogin");
				var logined=document.getElementById("Logined");
				var usercookie=document.getElementById("usercookie");
				if(response!=""){
					disLogin.style.display="none";
					logined.style.display="";
					usercookie.innerHTML = response;
					getBasket();
				}else{
					disLogin.style.display="";
					logined.style.display="none";
				}
			}
		},"");
	
}

function EditSave(thisform){
	var val = getFormElement(thisform);
	XmlHttpAction("POST",url+"checkup.asp?action=editsave&username="+getCookie("username")+"&rnd="+Math.random(),
		function(){
			if(xmlHttp.readyState==4){
				var response=xmlHttp.responseText;
				if(response=="success"){
					alert(response);
					tb_remove();
				}else{
					alert(response);
				}
			}
		},val.join("&"));
}

function addBasket(val){
	//val is product item
	//if product item is exist, then exit
	//else add 1
	if(val=="")return;
	if(choose){
		alert("You are chose!");
		return;
	}
	XmlHttpAction("GET",url+"checkup.asp?action=add&product="+val+"&rnd="+Math.random(),
		function(){
			if(xmlHttp.readyState==4){
				var response=xmlHttp.responseText;
				if(/d+/.test(response)){
					alert(response);
					return;
				}
				tb_show('','viewpro.html?height=300&amp;width=550',false);
				UserState();
				choose=true;
			}
		},"");
}

function checkInquiry(thisform){
	if(thisform.company.value=="" || thisform.company.value.length<4){
		alert("Company name must be more than 4 character");
		return false;
	}
	if(thisform.contact.value==""){
		alert("Contact is empty");
		return false;
	}
	if(thisform.address.value==""){
		alert("Address is empty");
		return false;
	}
	var re = /^[0-9a-zA-Z\-]/;
	if(!re.test(thisform.address.value) || thisform.address.value.length<10){
		alert("Plese enter valid address");
		return false;
	}
	if(thisform.email.value==""){
		alert("Email is empty");
		return false;
	}
	re = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	if(!re.test(thisform.email.value)){
		alert("Please enter valid e-mail");
		return false;
	}
	re = /^[0-9\- ]/;
	if(!re.test(thisform.telephone.value)){
		alert("Please enter valid telephone");
		return false;
	}
	if(thisform.products.value==""){
		alert("Please enter your inquiryed products");
		return false;
	}
	if(thisform.Description.value==""){
		alert("Please enter the description of product");
		return false;
	}
	sendInquiry(thisform);
}

function checkSimpleInquiry(thisform){
	if(thisform.products.value==""){
		alert("Please enter your inquiryed products");
		return false;
	}
	if(thisform.Description.value==""){
		alert("Please enter the description of product");
		return false;
	}
	sendInquiry(thisform);
}

function sendInquiry(thisform){
	//save inquiry form
	var val = getFormElement(thisform);
	XmlHttpAction("POST",url+"checkup.asp?action=send&rnd="+Math.random(),
		function(){
			if(xmlHttp.readyState==4){
				var response=xmlHttp.responseText;
				if(response=="success"){
					//var proNum = document.getElementById("proNum");
					//proNum.innerHTML = "(<span style=\"color:red\">" + getCookie("quantity") + "</span>)";
					tb_show("","complete.html?height=160&amp;width=550",false);
				}else{
					alert(response);
				}
			}
		},val.join("&"));
}

function showInquiry(thisform){
	//show inquiry form
	//send selected productName
	//if tempPro.length>0 then  which chose?
	if(typeof(thisform.productName)!="object"){
		alert("Nothing you checked!");
		return;
	}
	tempPro.length = 0;
	if(typeof(thisform.productName.length)=="undefined"){ //one product
		if(thisform.productName.value=="")return false;
		if(thisform.productName.checked)
			tempPro.push(thisform.productName.value);
	}else{ //more than one
		for(var i = 0; i<thisform.productName.length; i++){
			if(thisform.productName[i].checked)
				tempPro.push(thisform.productName[i].value);
		}
	}
	if(getCookie("UserName")!=""){
		tb_show("","send.html?height=240&amp;width=550",false);
	}else{
		tb_show("","details.html?height=350&amp;width=550",false);
	}
}

function LoadProduct(){
	var val = tempPro.join(",");
	XmlHttpAction("GET",url+"checkup.asp?action=getuser&username="+getCookie("username")+"&rnd="+Math.random(),
		function(){
			if(xmlHttp.readyState==4){
				var xml = xmlHttp.responseXML;
				if(xml!=null){
					//order by this form elements
					var thisform = document.getElementById("myform");

					thisform.products.value = val;
				}
			}
		},"");	
}

function showProducts(){
	if(getCookie("products") == "")return;
	var products = getCookie("products");
	var productList = document.getElementById("productList");
	var stringbuffer = Array();
	products = products.substring(1,products.length-1);
	if(products.indexOf("|")){
		products = products.split("|");
		for(var i=0; i<products.length;i++){
			stringbuffer.push("<input type=\"checkbox\" name=\"productName\" value=\""+products[i]+"\" checked=\"checked\" /> "+products[i]);
		}
	}else{
		stringbuffer.push("<input type=\"checkbox\" name=\"productName\" value=\""+products+"\" checked=\"checked\" /> "+products);
	}
	productList.innerHTML = stringbuffer.join(" ");
}

//form elements
function getFormElement(thisform){
	var el = thisform.elements;
	var except = "submit,reset,button,image"; //except these controls
	var params = Array();
	for(var i=0; i<el.length; i++){
		if(except.indexOf(el[i].type)<0){
			params.push(el[i].name+"="+escape(el[i].value));
		}
	}
	return params;
}


function SelectImage(obj){
	if(typeof(obj)!="object")return;
	var photo = document.getElementById("photo");
	photo.innerHTML="";
	var img = new Image();
	img.src=obj.rel;
	photo.innerHTML="<img src='"+obj.rel+"' alt='"+obj.title+"' />";
//	function complete(){
//		if(img.complete){
//			photo.innerHTML="<img src='"+obj.rel+"' alt='"+obj.title+"' />";
//			window.clearTimeout(complete);
//		}else{
//			photo.style.lineHeight="304px";
//			photo.style.fontSize="12px";
//			photo.style.fontWeight="bold";
//			photo.innerHTML="Loading......";
//		}
//	}
//	window.setTimeout(complete,1);
}