function getXMLHTTP() { //fuction to return the xml http object
		var xmlhttp=false;	
		try{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)	{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1){
					xmlhttp=false;
				}
			}
		}
		 	
		return xmlhttp;
	}
	//QuynhTTN 2009.07.30 Start
	//su dung hien thi danh sach OPTION DANH MUC cua nguoi dang tin chuyen nghiep
	function ShowGetCity(strURL) {
		var req = getXMLHTTP();
		
		if (req) {			
			req.onreadystatechange = function() {
				//alert(req.responseText);
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('modeldiv').innerHTML=req.responseText;						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}
	}
	//Ham lay ra gia tri ngau nhien cua so n
	function GetRandom(n)
	{
	  return (Math.floor(Math.random()*n));
	}
	//Ham lay gia tri Radio
	function getRadioCheckedValue(radio_name)
	{
		var oRadio = document.forms[0].elements[radio_name];
		//alert(oRadio.length);
		for(var i = 0; i < oRadio.length; i++) { 
		
			if(oRadio[i].checked) {
				return oRadio[i].value;
			}		
		}
		
		return 0;
	} 
	//Ham lay gia tri Combobox
	function getSelectedValue(selected_name)
	{
		var selected_index = document.forms[0].elements[selected_name].selectedIndex;
		if(selected_index > 0) { 
			return  document.forms[0].elements[selected_name].options[selected_index].value;
		}else{
			return 0;
		}

	}
	//Ham lay gia tri Checkbox
	function GetCheckboxValue(oCheckbox) {
		//oCheckBox1 = oForm.elements["email_alerts"]
		var checkbox_val = oCheckbox.value;
		
		if(oCheckbox.checked == true) {
			return checkbox_val;
		} else {		
			//alert("Checkbox with name = " + oCheckbox.name + " and value =" + checkbox_val + " is not checked");
			return 0;
		}
		
	} 
	//Dang ky mot tin moi
	function CreateNews()
	{
		
		if(document.getElementById("title").value.replace(" ","") == "")
		{
			alert("Bạn hãy nhập tiêu đề tin đăng!");
			return;
		}else if(document.getElementById("content").value.replace(" ","") == ""){
			alert("Bạn hãy nhập nội dung tin đăng!");
			return;
		}else{
			var req = getXMLHTTP();
			if (req == null)
			{
				alert ("Trình duyệt của bạn không hỗ trợ AJAX!");
				return;
			}
			//Ten file xuly PHP Insert noi dung tin vao Database
			var url = "CreateNews.php";
			//Lay ra ten file anh
			var fileName;// = document.getElementById("file").value;
			//var randFileImages='randomimages.jpg';
			//fileName = fileName.substr(fileName.lastIndexOf("\\")+1, fileName.length);
			//KHi khong chon mot file nao cu the thi chuong trinh mac dinh lay ngau nhien mot anh nao day de hien thi len
			
			var intRandom=GetRandom(5);
			if(intRandom==1)
				fileName = "fileRandomImages1.jpg";
			else if(intRandom==2)
				fileName = "fileRandomImages2.jpg";
			else if(intRandom==3)
				fileName = "fileRandomImages3.jpg";
			else if(intRandom==4)
				fileName = "fileRandomImages4.jpg";
			else
				fileName = "fileRandomImages5.jpg";
				/*alert('Quynh Test nhe'+GetRandom(5)+fileName);return;/*else if(fileName!=''){
				var dotpos;
				var ext;
				var fname;
				fileName= fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length);
				dotpos 	= fileName.lastIndexOf('.');
				ext 	= fileName.substr(dotpos + 1, 3);
				ext 	= ext.toLowerCase();
				fname=fileName.substr(0, dotpos);
				if (ext!=""){
					if ((ext!="gif") && (ext!="jpg") && (ext!="png")  && (ext!="bmp")){
						alert("Chỉ chọn file ảnh có dạng: GIF, JPG, PNG. Hãy chọn lại.");
						return false;
					}
				}
			}*/
			
			var title = document.forms[0].elements["title"].value;
			var categories = getRadioCheckedValue("categories");
			
			var needs = getRadioCheckedValue("needs");
			var type =  getRadioCheckedValue("type");
			var typeobjnews = getRadioCheckedValue("typeobjnews");
			var picture = fileName;
			var content = document.forms[0].elements["content"].value;
			var place = getSelectedValue("place");
			var ochkChinhChu = document.forms[0].elements["chinh_chu"];
			var chinh_chu = GetCheckboxValue(ochkChinhChu);
			
			url = url + "?title=" +title + "&categories=" + categories + "&needs=" + needs+ "&type=" + type+ "&typeobjnews=" + typeobjnews + "&picture="+ picture +  "&content=" + content+ "&place=" + place + "&chinh_chu="+chinh_chu;
			alert(url);
			//req.onreadystatechange = UpdateStateChangedNews;
			if (req) {			
				req.onreadystatechange = function() {
					//alert(req.responseText);
					if (req.readyState == 4) {
						// only if "OK"
						if (req.status == 200) {						
							//alert(req.responseText);
							document.getElementById('divNews').innerHTML=req.responseText;						
						} else {
							alert("There was a problem while using XMLHTTP:\n" + req.statusText);
						}
					}				
				}			
				req.open("GET", url, true);
				req.send(null);
			}
			
		}
		
		
		
	}
	function UpdateStateChangedNews()
	{
		if (req.readyState == 4 || req.readyState=="complete") {
			// only if "OK"
			if (req.status == 200) {						
				document.getElementById('divNews').innerHTML=req.responseText;						
			} else {
				alert("There was a problem while using XMLHTTP:\n" + req.statusText);
			}
		}	
	}
	//QuynhTTN 2009.07.30 End
	function getCity(strURL) {		
		
		var req = getXMLHTTP();
		
		if (req) {			
			req.onreadystatechange = function() {
				//alert(req.responseText);
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {				
						document.getElementById('modeldiv').innerHTML=req.responseText;						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}
				
	}
	
	function getAlbum(strURL) {		
		
		var req = getXMLHTTP();
		
		if (req) {			
			req.onreadystatechange = function() {
				//alert(req.responseText);
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {				
						document.getElementById('albumdiv').innerHTML=req.responseText;						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}
				
	}
	
	//QuynhTTN Update 2009.07.22 Start
	function getSelf(strURL) {		
		//alert(strURL);
		var req = getXMLHTTP();
		
		if (req) {			
			req.onreadystatechange = function() {
				//alert(req.responseText);
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('selfdiv').innerHTML=req.responseText;
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}				
	}
	//QuynhTTN Update 2009.07.22 End
	
	
	function getCategory(strURL) {		
		
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('modeldiv1').innerHTML=req.responseText;						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}
				
	}
	
function isBlank(val) {
	if (val == null) {
		return true;
	}
	for (var i = 0; i < val.length; i++) {
		if ((val.charAt(i) != ' ') && (val.charAt(i) != "\t") && (val.charAt(i) != "\n") && (val.charAt(i) != "\r")) {
			return false;
		}
	}
	return true;
}
function isZero(val) {
	if(val == null){
		return true;
	}
	if (val != 0) {
		return false;
	}
	return true;
}
function ShowDialogMap(domain,lat,lng){
	w = 650;
 	h = 490;
 	var left = (screen.availWidth/2) - (w/2);
	var top = (screen.availHeight/2) - (h/2);
    window.open(domain+'/viewMapProject.php?lat='+lat+'&lng='+lng,'mywindow','toolbar=no,directories=no,menubar=no,scrollbars=1,width='+w+', height='+h+',left='+left+',top='+top);
	return true;    
}
function ShowAlbum(domain,id_album){
	w = 650;
 	h = 590;
 	var left = (screen.availWidth/2) - (w/2);
	var top = (screen.availHeight/2) - (h/2);
    window.open(domain+'/viewPhoto.php?id_album='+id_album,'mywindow','toolbar=no,directories=no,,menubar=no,scrollbars=1,width='+w+', height='+h+',left='+left+',top='+top);
	return true;    
}
