var xmlHttp = false;
try {
	xmlHttp = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (othermicrosoft) {
				try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
			xmlHttp = false;
		}
	}
}
if (!xmlHttp){
	alert("Can't create XMLHttpRequest object!");
}

function $(id){
	return document.getElementById(id);
}

function getInfo(cat) {
	var cat_id = cat.options[cat.selectedIndex].value;//获得菜单的数据
	var url = "ajax.php?cat_id="+ escape(cat_id);
	xmlHttp.open("GET", url, true);//这里的true代表是异步请求
	xmlHttp.onreadystatechange = updatePage;
	xmlHttp.send(null);
}
 
function updatePage(){
	if (xmlHttp.readyState == 4) {
		var response = xmlHttp.responseText;
		//document.write(response);
		var arr = eval("("+response+")");
		//document.write(arr.toJSONString());
		var text = '<select name="subcat" id="subcat"><option selected="selected">Sub Category(s)</option>';
		for (var i=0;i<arr.length;i++){
			text += '<option value="'+arr[i]["subcat_id"]+'">'+arr[i]["subcat_name"]+'</option>';
		}
		text += '</select>';
		$("subcat_select").innerHTML=text;
	}
}

/*function upit(id) {
	var url = "/ajax/upit.php?id="+ escape(id);
	xmlHttp.open("GET", url, true);//这里的true代表是异步请求
	xmlHttp.onreadystatechange = function(){update(id)};
	xmlHttp.send(null);
}*/
function upit(id) {
	var upid = Get_Cookie("up"+id);
	if(upid == null){
		var url = "/ajax/upit.php?id="+ escape(id);
		xmlHttp.open("GET", url, true);//这里的true代表是异步请求
		xmlHttp.onreadystatechange = function(){update(id)};
		xmlHttp.send(null);
		Set_Cookie("up"+id,id,1,"/");
	}
}
 
function update(id){
	if (xmlHttp.readyState == 4) {
		var response = xmlHttp.responseText;
		if (response != '' ) {
			document.getElementById("up"+id).innerHTML = response;
		}
	}
}


//no ajax
function Get_Cookie(name){
	var $m=document.cookie.indexOf(name+"=");
	var $n=$m+name.length+1;
	if((!$m) && (name!=document.cookie.substring(0,name.length)))return null;
	if($m==-1)return null;
	var end=document.cookie.indexOf(";",$n);
	if(end==-1)end=document.cookie.length;
	return unescape(document.cookie.substring($n,end));
}
function Set_Cookie(name,$o,$p,$q,$r,$s){
	$p=$p*60*60*24*1000;
	var $t=new Date();
	var $u=new Date($t.getTime()+($p));
	var $v=name+"="+escape($o)+(($p)?";expires="+$u.toGMTString():"")+(($q)?";path="+$q:"")+(($r)?";domain="+$r:"")+(($s)?";secure":"");
	document.cookie=$v;
}

function checkinput(input){
	if (input.value == "mm/dd/yyyy"){
		input.value = "";
	} else if (input.value == "") {
		input.value = "mm/dd/yyyy";
	}
}

function is_email( str ){
	p = /^([\w\.-]+)@([a-zA-Z0-9-]+)(\.[a-zA-Z\.]+)$/;
	if(str.search(p) == -1){
		return false;
	}else{
		return true;
	}
}
function special_char( str ){
	p = /^([\w-]+)$/i;
	if(str.search(p) == -1){
		return true;
	}else{
		return false;
	}
}
function changecode(){
	var pic = document.getElementById('imagecode');
	var n = Math.random();
	pic.src = '/include/imagecode.php?t=' + n * 1000;
}
function show_num(c,id,num){
	num -= c.value.length;
	$(id).innerHTML = num + ' characters left';
	if(num<0){
		$(id).style.color="#F00";
	} else {
		$(id).style.color="#060";
	}
}

function clearall(){
	$('countries').style.display='none';
	$('categories').style.display='none';
}
function changec(c){
	clearall();
	$(c).style.display='';
}