function createXmlHttpRequest()
{
	var $http_request = null;
	if(window.XMLHttpRequest) { 
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) { 
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) { 
		return false;
	}
	return http_request;
}
function loadProductLists(ctg_id){
	var keyword = document.getElementById("keyword").value;
//	if(document.getElementById("show_content")==undefined){
//		window.location = "product_list.php?keyword=" + keyword + "&ctg_id=" + ctg_id;
//	}
	var url = "show_product_lists.php?ctg_id=" + ctg_id;
	var page = document.getElementById("page").value
	document.getElementById("ctg_id").value = ctg_id;
	if(page>0){ url += "&page="+page;}
	if(keyword != ""){ url += "&keyword="+keyword;}
	url += "&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
			//	ChatClose();
				document.getElementById("show_content").innerHTML = xmlHttp.responseText;
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function loadProductDetails(cp_id){
	document.getElementById("cp_id").value = cp_id;
	var url = "show_product_details.php?cp_id=" + cp_id;
	url += "&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				document.getElementById("show_content").innerHTML = xmlHttp.responseText;
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}
function loadSubMenu(smn_id){
	var url = "show_sub_menus.php?smn_id=" + smn_id;
	url += "&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				document.getElementById("sub_menu").innerHTML = xmlHttp.responseText;
				document.getElementById("sub_menu").style.display = "block";
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);		
}
function loadStaticContent(smn_id){
	var url = "show_static_content.php?smn_id=" + smn_id;
	url += "&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				ChatClose();
				document.getElementById("show_content").innerHTML = xmlHttp.responseText;
				//if(document.getElementById("show_content").clientHeight<=545){
				//	document.getElementById("main_left").style.height = 380;
				//	document.getElementById("category").style.height = 380;
				//	document.getElementById("show_content").style.height = 545;
				//}
				if(document.getElementById("show_content").clientHeight>600){
					document.getElementById("main_left").style.height = document.getElementById("show_content").clientHeight;
				}
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}
function loadProductByCtgId(ctg_id){
	document.getElementById("page").value = 1;
	document.getElementById("keyword").value = "";
	//document.getElementById("main_left").style.height = 600;
	//document.getElementById("category").style.height = 432;
	loadProductLists(ctg_id);
}

function gotopage(page){
	document.getElementById("page").value = page;
	var ctg_id = document.getElementById("ctg_id").value;
	loadProductLists(ctg_id);
}
function gotonextpage(page){
	var temp_page = document.getElementById("page").value;
	if(temp_page == "")temp_page = 1;else temp_page =  parseInt(temp_page) + page;
	if(temp_page == 0)temp_page = 1;
	document.getElementById("page").value = temp_page;
	var ctg_id = document.getElementById("ctg_id").value;
	loadProductLists(ctg_id);
}
function searchProducts(){
	var keyword = document.getElementById("keyword").value
	loadProductLists('');
}

function changeCtg() {
	if(document.getElementById('ctg').style.display=='none') {
		document.getElementById('ctg').style.display='block';
		document.getElementById('ctg_img').src='images/template_50.gif'
	} else {
		document.getElementById('ctg').style.display='none';
		document.getElementById('ctg_img').src='images/template_50.gif'
	}
}
function changeCtgCtg() {
	if(document.getElementById('ctg_ctg').style.display=='none') {
		document.getElementById('ctg_ctg').style.display='block';
		document.getElementById('ctg_ctg_img').src='images/template_50.gif'
	} else {
		document.getElementById('ctg_ctg').style.display='none';
		document.getElementById('ctg_ctg_img').src='images/template_50.gif'
	}
}
function changeAddImage(url){
	document.getElementById('product_image').innerHTML = '<img src="./addtional/b_' + url + '">';
}
function showsub_a(id){
	for (i=0;i<4;i++){
		document.getElementById("tab_a"+i).className = "tab_link_center";
		document.getElementById("tab_left_a"+i).className = "tab_link_left";
		document.getElementById("tab_right_a"+i).className = "tab_link_right";
	}
	document.getElementById("tab_a"+id).className = "tab_active_center";
	document.getElementById("tab_left_a"+id).className = "tab_active_left";
	document.getElementById("tab_right_a"+id).className = "tab_active_right";
	document.getElementById("current_tab").value = id;
	document.getElementById("page").value = 0;
	loadProductLists(id);
}

function showProductDetails(id){
	if(!id)return false;
	var url = "show_product_details.php?id=" + id;
	url += "&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				var p = getElementPos("product_pos");
				var product_details_obj = document.getElementById("product_details");
				product_details_obj.innerHTML = xmlHttp.responseText;
				document.getElementById("mainframe").style.left = p['x'] + 5;
				document.getElementById("mainframe").style.display = "block";
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}
function showInquiryInfo(step){
	var url = "show_inquiry_info.php?step=" + step;
	url += "&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				ChatClose();
				document.getElementById("show_content").innerHTML = xmlHttp.responseText;
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}
function addInquiry(id,action){
	if(action == "remove"){
		x = confirm("Are you really want to remove this product?");
		if(x == false) {
			return;
		}
	}
	var url = "show_inquiry.php?action="+ action +"&id=" + id;
	url += "&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				document.getElementById("inquiry_id").innerHTML = xmlHttp.responseText;
				if(xmlHttp.responseText){
					//document.getElementById("main_table").style.width = 950;
					//var p = getElementPos("main_table");
				//	document.getElementById("floater").style.left = p['x'] + 800;
					document.getElementById("floater").style.display = "block";
					showProductDetails(id);
				}else{
				//	document.getElementById("main_table").style.width = 800;
					document.getElementById("floater").style.display = "none";
				}
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}
function login(){
	var password = document.getElementById("password").value;
	var email = document.getElementById("email").value;
	if(email == ""){
		alert("email cannot be empty!");
		document.getElementById("email").focus();
		return;
	}
	if(password == ""){
		alert("password cannot be empty!");
		document.getElementById("password").focus();
		return;
	}
	var url = "login.php?step=login&email=" + email + "&password=" + password;
	url += "&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				if(xmlHttp.responseText == "error"){
					alert("username or password error!");
				}else{
					showInquiryInfo(3);
				}
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}
function logout(){
	var url = "login.php?step=logout";
	url += "&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			showInquiryInfo(1);
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}
function register(){
	var email = document.getElementById("email").value;
	var password = document.getElementById("password").value;
	var password2 = document.getElementById("password2").value;
	var gender = document.getElementById("gender").value;
	var firstname = document.getElementById("firstname").value;
	var lastname = document.getElementById("lastname").value;
	var company = document.getElementById("company").value;
	var address = document.getElementById("address").value;
	var tel = document.getElementById("tel").value;
	var fax = document.getElementById("fax").value;
	var zip = document.getElementById("zip").value;
	var city = document.getElementById("city").value;
	var country = document.getElementById("country").value;
	if(email == ""){
		alert("email cannot be empty!");
		document.getElementById("email").focus();
		return;
	}
	if(password == ""){
		alert("password cannot be empty!");
		document.getElementById("password").focus();
		return;
	}
	if(password != password2){
		alert("password and password confirmation!");
		document.getElementById("password").focus();
		return;
	}
	var url = "register.php?email=" + email + "&password=" + password + "&gender=" + gender + "&firstname=" + firstname + "&lastname=" + lastname + "&company=" + company + "&address=" + address + "&tel=" + tel + "&fax=" + fax + "&zip=" + zip + "&city=" + city + "&country=" + country;
	url += "&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				if(xmlHttp.responseText == "error"){
					alert("username is aler");
				}else if(xmlHttp.responseText == "success"){
					alert("register success");
					showInquiryInfo(3);
				}else{
					alert(xmlHttp.responseText);
				}
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function order(){
	var email = document.getElementById("email").value;
	var gender = document.getElementById("gender").value;
	var firstname = document.getElementById("firstname").value;
	var lastname = document.getElementById("lastname").value;
	var company = document.getElementById("company").value;
	var address = document.getElementById("address").value;
	var tel = document.getElementById("tel").value;
	var fax = document.getElementById("fax").value;
	var zip = document.getElementById("zip").value;
	var city = document.getElementById("city").value;
	var country = document.getElementById("country").value;
	if(email == ""){
		alert("email cannot be empty!");
		document.getElementById("email").focus();
		return;
	}
	var url = "order.php?email=" + email + "&gender=" + gender + "&firstname=" + firstname + "&lastname=" + lastname + "&company=" + company + "&address=" + address + "&tel=" + tel + "&fax=" + fax + "&zip=" + zip + "&city=" + city + "&country=" + country;
	url += "&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				if(xmlHttp.responseText == "success"){
					window.top.location="index.php";
					alert("Send inquiry success. Thanks for your inquiry, we will contact with you as soon as possible!");
				}else{
					alert(xmlHttp.responseText);
				}
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}


// 说明：用 Javascript 获取指定页面元素的位置
// 整理：http://www.codebit.cn 
// 来源：YUI DOM  
function getElementPos(elementId) {      
	var ua = navigator.userAgent.toLowerCase();     
	var isOpera = (ua.indexOf('opera') != -1);     
	var isIE = (ua.indexOf('msie') != -1 && !isOpera); 
	// not opera spoof      
	var el = document.getElementById(elementId);      
	if(el.parentNode === null || el.style.display == 'none')      {         
		return false;     
	}      
	var parent = null;     
	var pos = [];     
	var box;      
	if(el.getBoundingClientRect)    //IE     
	{        
		box = el.getBoundingClientRect();         
		var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);         
		var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);         
		return {x:box.left + scrollLeft, y:box.top + scrollTop};     
	}     
	else if(document.getBoxObjectFor)    // gecko    
	{        
		box = document.getBoxObjectFor(el);                     
		var borderLeft = (el.style.borderLeftWidth)?parseInt(el.style.borderLeftWidth):0;         
		var borderTop = (el.style.borderTopWidth)?parseInt(el.style.borderTopWidth):0;          
		pos = [box.x - borderLeft, box.y - borderTop];     
	}    
	else    // safari & opera     
	{         
		pos = [el.offsetLeft, el.offsetTop];         
		parent = el.offsetParent;         
		if (parent != el) {            
			while (parent) {                 
				pos[0] += parent.offsetLeft;                 
				pos[1] += parent.offsetTop;                 
				parent = parent.offsetParent;             
			}         
		}         
		if (ua.indexOf('opera') != -1  || ( ua.indexOf('safari') != -1 && el.style.position == 'absolute' ))          
		{                
			pos[0] -= document.body.offsetLeft;                 
			pos[1] -= document.body.offsetTop;         
		}      
	}             
	if (el.parentNode) { parent = el.parentNode; }     else { parent = null; }        
	while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML')      { // account for any scrolled ancestors         
		pos[0] -= parent.scrollLeft;        
		pos[1] -= parent.scrollTop;            
		if (parent.parentNode) { parent = parent.parentNode; }          else { parent = null; }     
	}     
	return {x:pos[0], y:pos[1]}; 
} 
var review_form = null;
function writeReview(id){
	str="./write_review.php?id=" + id;
	review_form = window.open(str,"review","height=500,width=600,left=120,top=150,scrollbars=yes");
}
function ontop(){
	if (review_form && !review_form.closed) {
		review_form.focus()
	}
}
function copyToClipboard(id){
	if(id == "")return false;
	 window.clipboardData.setData("Text",  "http://www.torintools.com/?product="+id);
}