var xmlHttp;
var pages = 1;
function change(){
	if(xmlHttp.readyState==4&&xmlHttp.status==200){
		var opts = document.getElementById("city");
		for(var j=opts.length-1;j>=0;j--){
			opts.remove(j);
		}
		str=xmlHttp.responseText;
		strs=str.split("|||");
		for(var i=0;i<strs.length;i++){
			s=strs[i].split("@@@");
			el = document.createElement("option");
			el.value=s[0];
			el.text=s[1];
			opts.add(el);
		}
	}
}
function RelatedProd(v,pid){
	document.getElementById("relpro").innerHTML = "<div style='padding-top:30px;text-align:center;'><img src='../templates/huali/images/loading.gif' /> Loading...</div>";
	if(v == 'prov' && pages >1){pages -= 1;}
	if(v == 'next'){pages += 1;}
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = relatedretrun;
	xmlHttp.open("GET",'../relatedprod.php?v=' + v + '&pid=' + pid + '&page=' + pages,true);
	xmlHttp.send(null);
}
function relatedretrun(){
	if(xmlHttp.readyState==4&&xmlHttp.status==200){
		if(xmlHttp.responseText.indexOf(">") >=0){
			document.getElementById("relpro").innerHTML = xmlHttp.responseText;
		}else{
			alert('This is the last page');	
			pages -= 1;
			RelatedProd('',xmlHttp.responseText)
		}
	}
}
function favorites(){
	if(xmlHttp.readyState==4&&xmlHttp.status==200){
		var str=xmlHttp.responseText;
		if(str == 'nologin'){
			if(confirm('您还没有登陆，请先登陆')){window.location='../login.php';}
		}else{
			alert(str);
		}
	}
}
function createXMLHttpRequest(){
	if(window.ActiveXObject){
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if(window.XMLHttpRequest){
		xmlHttp = new XMLHttpRequest();
	}
}
function region(url,obj){
	createXMLHttpRequest();
	var nowtime	= new Date().getTime();
	url	+= (url.indexOf("?") >= 0) ? "&nowtime=" + nowtime : "?nowtime=" + nowtime;
	xmlHttp.onreadystatechange = obj;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function chkcarta(){
	var che = false;
	var frm = document.forms['formcart'].paymentr;
	for (var i=0;i<frm.length;i++){
        if(frm[i].checked){
            che = true;
			break;
        }
	}
	if(!che){alert('Please select the preferred payment method to use on this order');return false;}
	
}


//------------------------ajaxform start---------------
var serverAddress = "ajaxform.php";
var cache = new Array();

function getHttpObject() {
    var objType = false;
    try {
         objType = new ActiveXObject('Msxml2.XMLHTTP');
     } catch(e) {
        try {
             objType = new ActiveXObject('Microsoft.XMLHTTP');
         } catch(e) {
             objType = new XMLHttpRequest();
         }
     }
    return objType;
}

function getAJAX(postBody,messageID){
   var theHttpRequest = getHttpObject();
   theHttpRequest.onreadystatechange = function() {processAJAX(messageID);};
   theHttpRequest.open("POST", serverAddress, true);
   theHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   theHttpRequest.send(postBody);

    function processAJAX(messageID){
       if (theHttpRequest.readyState == 4) {
         if (theHttpRequest.status == 200) {
            document.getElementById(messageID).innerHTML = theHttpRequest.responseText;
            setTimeout("validate();", 500);
          }
        }
     }
}

function validate(inputValue, fieldID, messageID){
  if (fieldID){
     inputValue = encodeURIComponent(inputValue);
     fieldID = encodeURIComponent(fieldID);
     cache.push("inputvalue=" + inputValue + "&inputid=" + fieldID);
     }
  if (cache.length > 0){
    var cacheEntry = cache.shift();
     getAJAX(cacheEntry,messageID);
   }
}

//----------------------ajaxform end--------------


var agt = navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
function getObj(id){
	return document.getElementById(id);
}
function ietruebody(){	
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}
function getObjX(obj){
	var curleft = 0;
	if(obj.offsetParent){
		while(obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if(obj.x){
		curleft += obj.x;
	}
	return curleft - ietruebody().scrollLeft;
}
function getObjY(obj){
	var curtop = 0;
	if(obj.offsetParent){
		while(obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if(obj.y){
		curtop += obj.y;
	}
	return curtop - ietruebody().scrollTop;
}
