// JavaScript Document 
//////////// create Ajax Object //////////////
function createRequestObject() {
    var ro;
    browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}
var http = createRequestObject();
/////////////////////////////////////////////////////////
//send request for search bar and pageing search result//
/////////////////////////////////////////////////////////
function sndReqsearch(start) {
	
document.getElementById("extra").innerHTML="<div style='text-align:center;margin-top:8em'>syria-ads.com<br><img src='images/waiting.gif' vspace='3'></div>";
if(!s_info['sword']){s_info['sword']="";}
var str="";
 if(browser != "Microsoft Internet Explorer")
  str+="f=1&";
	str+="action[cat]="+s_info['catid']+"&action[type]="+s_info['parts']+"&action[cityid]="+s_info['city']+"&action[areaid]="+s_info['areaid']+"&action[advkind]="+s_info['kind']+"&action[sizemin]="+s_info['sizemin']+"&action[sizemax]="+s_info['sizemax']+"&action[pricemin]="+s_info['pricemin']+"&action[pricemax]="+s_info['pricemax']+"&action[salarymin]="+s_info['salarymin']+"&action[salarymax]="+s_info['salarymax']+"&action[sword]="+s_info['sword'];
	strp=str;
	curpage=0;
	str+="&start="+start;
	http.open('get', 'targettest.php?'+str,true);
    http.onreadystatechange = handleResponseSearch;
     http.setRequestHeader('X_REQUESTED_WITH','XMLHttpRequest');
    http.send(null);searchLocation();
   
}
 var recnum=0
 
function handleResponseSearch(){
	if(http.readyState == 4)
	{
		
        var response =http.responseText;
        document.getElementById("ajads").innerHTML=response;
		
     recnum=document.getElementById("recnum1").innerHTML;
	 
		}  paging();
		check_selected();
}
//////////////////////////////////////////////////////////////
// function to detected search location and alert settelment//
//////////////////////////////////////////////////////////////
var location1="";
function searchLocation(){
	
	location1="الفئة = ";
	location1+=catarr[cat]+" :: القسم = ";
	location1+=document.getElementById("parts").options[document.getElementById("parts").selectedIndex].text+" :: المحافظة = ";
	location1+=document.getElementById("city").options[document.getElementById("city").selectedIndex].text+" :: ";
	
	location1+="النوع ="+document.getElementById("kind").options[document.getElementById("kind").selectedIndex].text;
	document.getElementById("searchlocation").innerHTML="&nbsp&nbsp"+location1+"<input style='float:left;' type='button' onclick='ifalert()' value='حفظ كتنبيه'>";

}
function ifalert(){
if(document.getElementById("login"))
return document.getElementById("alertform").style.display="block";
else 
return alert("يجب أنت تكون عضو للاستفادة من خدمة التنبيهات ، للمساعدة اضغط على دليل المستخدم");
}
function sndreqalert(type,location1){
	ifalert();
var str="";
str+="action[cat]="+s_info['catid']+"&action[type]="+s_info['parts']+"&action[cityid]="+s_info['city']+"&action[areaid]="+s_info['areaid']+"&action[advkind]="+s_info['kind']+"&type="+type+"&loc="+location1;
	http.open('get', 'alert.php?'+str,true);
    http.onreadystatechange = handleResponseAlert;
    http.setRequestHeader('X_REQUESTED_WITH','XMLHttpRequest');
    http.send(null);
}
function handleResponseAlert(){
	if(http.readyState == 4)
	{
		var response =http.responseText;
		alert(response);
		document.getElementById("alertform").style.display="none";
	}
}

//////////////////////////////////////
//paging function//////////
/////////////////////////////////
var strp="";
function sndReqsearchp(start) {
	stra=strp+"&start="+start;
	document.getElementById("ajads").innerHTML="<div style='text-align:center;margin-top:8em'>syria-ads.com<br><img src='images/waiting.gif' vspace='3'></div>";
	http.open('get', 'targettest.php?'+stra,true);
	// same handleResponse for sndReqsearch()
    http.onreadystatechange = handleResponseSearch;
    http.setRequestHeader('X_REQUESTED_WITH','XMLHttpRequest');
    http.send(null);
}
var curpage=0;
function paging(){
	
	if(recnum==0 || recnum<15) return;
strlink="أنت تشاهد من "+parseInt(curpage+1)+" إلى "+parseInt(curpage+15)+" يوجد "+recnum+" إعلان &nbsp;&nbsp;&nbsp;&nbsp;";
	if(curpage==0){	
	strlink+="<a style='color:#cccccc'>[الصفحة السابقة]</a> | <a onclick='javascript:sndReqsearchp("+parseInt(curpage+15)+");curpage=curpage+15'>[الصفحة التالية]</a>";
	}
	if(curpage>0 && curpage+15< recnum){
		strlink+="<a onclick='javascript:sndReqsearchp("+parseInt(curpage-15)+");curpage=curpage-15;'>[الصفحة السابقة]</a> |  ";
		strlink+="<a onclick='sndReqsearchp("+parseInt(curpage+15)+");curpage=curpage+15;'>[الصفحة التالية]</a>";
	}
	if((curpage+15)>=recnum){
		strlink+="<a onclick='javascript:sndReqsearchp("+parseInt(curpage-15)+");curpage=curpage-15'>[الصفحة السابقة]</a> | <a style='color:#cccccc'>[الصفحة السابقة]</a>";
	}
document.getElementById("recnum1").innerHTML=strlink;
document.getElementById("recnum2").innerHTML=strlink;	
}
function check_all(flage)
{ 
	for(var i=0;i<15;i++)
	{   var e=document.getElementById("c"+i);
		if(flage==1)e.checked=true;
		else e.checked=false;
	}
}
//////////////////////////////////////////////////////
// send rquest for featch area select in search form//
//////////////////////////////////////////////////////
function sndReqarea(areaid){
    multi_area('no');
	s_info['areaid']=0;
	document.getElementById("areatd").innerHTML="<select><option>تحميل ...</option></select>";
	http.open('get','processarea.php?areaid='+areaid,true);
    http.onreadystatechange = handleResponsearea;
	http.setRequestHeader('X_REQUESTED_WITH','XMLHttpRequest');
    http.send(null);
}
function handleResponsearea(){
	if(http.readyState == 4){
        var response = http.responseText;
		sss="if(this.value=='more')multi_area('yes');else{multi_area('no');s_info[this.id]=this.value;}";
		document.getElementById("areatd").innerHTML='<select id="areaid" onchange="'+sss+'"><option value="0">الكل</option><optgroup label=" مناطق"><option value="more" >أكثر من منطقة</option></optgroup><optgroup label=" منطقة">'+response+'</optgroup></select>';
		}
}
//////////////////////////////////
//light box function (sndReq)/////
//////////////////////////////////
function sndReq(cat,advid) {
	document.getElementById("lightbox").innerHTML="<div style='text-align:center;margin-top:8em'>syria-ads.com<br><img src='images/waiting.gif' vspace='3'></div>";
	http.open('get', 'showadvertising.php?cat='+cat+'&advid='+advid,true);
    http.onreadystatechange = handleResponse;
	http.setRequestHeader('X_REQUESTED_WITH','XMLHttpRequest');
    http.send(null);
}
function handleResponse()
{
	if(http.readyState == 4)
	{
        lightboxresponse = http.responseText;
		//repeated code if the respose late (; 
		var str=navigator.appVersion;
	    var arr=str.split("; ");
	   if(navigator.appName=="Microsoft Internet Explorer" && arr[1]=="MSIE 6.0" )
	  {
	   body = '<body onblur="this.foucs()"><center>'+lightboxresponse+'<br><p><form><input type="button" value="   إغلاق   " onClick="self.close()"></form></body></html>';
  msgWindow.document.write(head + body);
		  }
	   else	{
	//hide flash object for IE
	document.getElementById("lightbox").innerHTML=lightboxresponse+"<div id='cdiv'><input type='button' onclick='hidelightbox()' value='إلغاء' /></div>";
		}
    }
}
function showlightbox(cat,advid){
	var str=navigator.appVersion;
	var arr=str.split("; ");
	   if(navigator.appName=="Microsoft Internet Explorer" && arr[1]=="MSIE 6.0" )
	   {
		myOpen();
	   }
	   else	{
	//hide flash object for IE
	document.getElementById("obj1").className="ohide";
	//document.getElementById("obj2").className="ohide";
	document.getElementById("overlay").style.display="block";
	document.getElementById("lightbox").style.display="block";
				}
	sndReq(cat,advid);	
}
/////////////////////////////////
////////////hidden lightbox//////
/////////////////////////////////
function hidelightbox(){
	document.getElementById("overlay").style.display="none";
	document.getElementById("lightbox").style.display="none";
	document.getElementById("obj1").className="oshow";
	//document.getElementById("obj2").className="oshow";
}
//////////////////////////////////////////
////////////// myOpen for lightbox	//////
//////////////////////////////////////////
function myOpen()
{
 var width="500", height="300";
 var left = (screen.width/2) - width/2;
 var top = (screen.height/2) - height/2;
 var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
 msgWindow = window.open("","msgWindow", styleStr);
 head = '<html><head><title>إعلان مبوب</title><link rel="stylesheet" type="text/css" href="style.css" /></head>';
 //body = '<center>'+lightboxresponse+'<br><p><form><input type="button" value="   إغلاق   " onClick="self.close()"></form>';
  //msgWindow.document.write(head + body);
}
////////////////////////////////////
////////////showparts///////////////
////////////////////////////////////
function showparts(index)
{
	document.getElementById("parts").options.length = 0;
	document.getElementById("parts").options[0] = new Option("الكل","0");
	for(j in partarr[index])
	{
		document.getElementById("parts").options[j] = new Option(partarr[index][j],j);
	}
}
//=====================================
function showkind()
	{
		document.getElementById('kind').options.length = 0;

		document.getElementById('kind').options[0] = new Option("الكل","0");

		document.getElementById('kind').options[1] = new Option("بيع","1");

		document.getElementById('kind').options[2] = new Option("شراء","2");

		document.getElementById('kind').options[3] = new Option("أجار","3");

		document.getElementById('kind').options[4] = new Option("مقايضة","4");

		document.getElementById('kind').options[5] = new Option("طلب أجار","5");

		document.getElementById('kind').options[6] = new Option("طلب مقايضة","6");

	}
//=====================================
function changkind()
	{
		document.getElementById('kind').options.length = 0;
		document.getElementById('kind').options[0] = new Option("الكل","0");
		document.getElementById('kind').options[1] = new Option("عرض","1");
		document.getElementById('kind').options[2] = new Option("طلب","2");
	}
//=======================reset select list================
function resetselect()
{
	document.getElementById("parts").options[0].selected=true;
	document.getElementById("city").options[0].selected=true;
	document.getElementById("kind").options[0].selected=true;
}
//================================
//function for search bar
//================================
function zeroarr()
	{
		s_info["catid"] = 0;
		s_info["parts"] = 0;
		s_info["city"] = 0;
		s_info["areaid"] = 0;
		s_info["kind"] = 0;
		s_info["sizemin"] = 0;
		s_info["sizemax"] = 0;
		s_info["pricemin"] = 0;
		s_info["pricemax"] = 0;
		s_info["salarymin"] = 0;
		s_info["salarymax"] = 0;
	    //s_info["sword"] ="";
	}
function dochange(s_cat)
{
		document.getElementById('link0').className='browse';
		document.getElementById('link1').className='browse';
		document.getElementById('link2').className='browse';
		document.getElementById('link3').className='browse';
		document.getElementById('link4').className='browse';
		document.getElementById('link'+s_cat).className='browseactive';
		
		
		zeroarr();
	    multi_area('no');
		s_info['catid']=s_cat;
		cat=s_cat;
		curpage=0;
		document.getElementById("city").options[0].selected=true;
		showparts(s_cat);
		if(s_cat==2) changkind(); else showkind();
		if(s_cat!=0)
		document.getElementById("areatd").innerHTML="<select disabled='disabled'><option value='no'>غير متاحة</option></select>";
		else document.getElementById("areatd").innerHTML=areatd;
		changelab(s_cat);
	}

function changelab(lab)
{
	switch(lab)

		{

		case 0: document.getElementById("lab1").innerHTML="المساحة";
					document.getElementById("inlab1").innerHTML='<input id="sizemin" maxlength="5" size="5" value="Min" onfocus="this.select()" onblur="s_info[this.id]=this.value;" />|<input id="sizemax" maxlength="5" size="5" value="Max" onfocus="this.select()" onblur="s_info[this.id]=this.value;"  />م<sup>2</sup>';
					document.getElementById("lab2").innerHTML="السعر";
					document.getElementById("inlab2").innerHTML='<input id="pricemin" maxlength="8" size="5" value="Min" onfocus="this.select()" onblur="s_info[this.id]=this.value;" />|<input id="pricemax" maxlength="8" size="5" value="Max" onfocus="this.select()" onblur="s_info[this.id]=this.value;" />ل.س';
					break;
		case 1:document.getElementById("lab1").innerHTML="<br />";
					document.getElementById("inlab1").innerHTML='<br />';
					document.getElementById("lab2").innerHTML="السعر";
					document.getElementById("inlab2").innerHTML='<input id="pricemin" maxlength="8" size="5" value="Min" onfocus="this.select()" onblur="s_info[this.id]=this.value;" />|<input id="pricemax" maxlength="8" size="5" value="Max" onfocus="this.select()" onblur="s_info[this.id]=this.value;" />ل.س';
					break;
		case 2:
					document.getElementById("lab1").innerHTML="<br />";
					document.getElementById("inlab1").innerHTML='<br />';
					document.getElementById("lab2").innerHTML="الراتب";
					document.getElementById("inlab2").innerHTML='<input id="salarymin" maxlength="8" size="5" value="Min" onfocus="this.select()" onblur="s_info[this.id]=this.value;" />|<input id="salarymax" maxlength="8" size="5" value="Max" onfocus="this.select()" onblur="s_info[this.id]=this.value;" />ل.س';
					break;
		case 3:document.getElementById("lab1").innerHTML="<br>";
					document.getElementById("inlab1").innerHTML='<br>';
					document.getElementById("lab2").innerHTML="السعر";
					document.getElementById("inlab2").innerHTML='<input id="pricemin" maxlength="8" size="5" value="Min" onfocus="this.select()" onblur="s_info[this.id]=this.value;" />|<input id="pricemax" maxlength="8" size="5" value="Max" onfocus="this.select()" onblur="s_info[this.id]=this.value;" />ل.س';
					break;
		case 4:document.getElementById("lab1").innerHTML="<br>";
					document.getElementById("inlab1").innerHTML='<br>';
					document.getElementById("lab2").innerHTML="السعر";
					document.getElementById("inlab2").innerHTML='<input id="pricemin" maxlength="8" size="5" value="Min" onfocus="this.select()" onblur="s_info[this.id]=this.value;" />|<input id="pricemax" maxlength="8" size="5" value="Max" onfocus="this.select()" onblur="s_info[this.id]=this.value;" />ل.س';
					}

}
/////////////////////////////////////
//multi area////////////////////////
////////////////////////////////////
function sndReqmarea(areaid){
	document.getElementById("mareatd").innerHTML="المناطق<br /><select size='4'><option>تحميل ...</option></select>";
	http.open('get','processarea.php?areaid='+areaid,true);
    http.onreadystatechange = handleResponsemarea;
	 http.setRequestHeader('X_REQUESTED_WITH','XMLHttpRequest');
    http.send(null);
}
function handleResponsemarea() {
	if(http.readyState == 4){
        var response = http.responseText;
		document.getElementById("mareatd").innerHTML='المناطق<br /><select id="mareaid"  size="4">'+response+'</select>';
}
}
function multi_area(flage){
	if(flage=="yes"){
	document.getElementById("mtable").style.display="block";
	sndReqmarea(document.getElementById("city").value);
	}
	else{ 
	document.getElementById("mtable").style.display="none";
	document.getElementById("mareaid2").options.length=0;
	}
}
function sa2sb(){
v=document.getElementById("mareaid").value;
t=document.getElementById("mareaid").options[document.getElementById("mareaid").selectedIndex].text;
document.getElementById("mareaid2").options[document.getElementById("mareaid2").options.length]= new Option(t,v);
document.getElementById("mareaid").remove(document.getElementById("mareaid").selectedIndex);
document.getElementById("mareaid2").options[document.getElementById("mareaid2").options.length-1].selected="true";
}
function sb2sa(){
v=document.getElementById("mareaid2").value;
t=document.getElementById("mareaid2").options[document.getElementById("mareaid2").selectedIndex].text;
document.getElementById("mareaid").options[document.getElementById("mareaid").options.length]= new Option(t,v);
document.getElementById("mareaid2").remove(document.getElementById("mareaid2").selectedIndex);
document.getElementById("mareaid").options[document.getElementById("mareaid").options.length-1].selected='selected';
}
function mareafun(ml1){
	if(ml1>0){
	var i;
	var str="'";
	for(i=0;i<ml1;i++)
	str+=document.getElementById("mareaid2").options[i].value+"','";
	s_info['areaid']=str.substring(0,str.length-2);
	}
}
///////////////////////////////////////////
///////////////print select functions
///////////////////////////////////////////
//=============================
function sndReq_for_print(printstr) {
	printWindow.document.write('<a href=\'javascript:window.print()\'>طباعة</a>');
	http.open('get', 'printselected.php?'+printstr);
	//alert(printstr);
    http.onreadystatechange = printhandleResponse;
	http.setRequestHeader('X_REQUESTED_WITH','XMLHttpRequest');
    http.send(null);
}
function printhandleResponse(){
	if(http.readyState == 4){
     printpage = http.responseText;
	 printWindow.document.write(printpage);
	 printWindow.document.write("<script>window.print();document.write('<a href=\"javascript:window.print()\">طباعة</a>')</script>");
	 //for speed the print method in browser
	  browser = navigator.appName;
		if(browser == "Microsoft Internet Explorer"){
			printWindow.document.write('<script>document.execCommand("Stop");</script>');
		}
		else{
		   printWindow.document.write('<script>window.stop()</script>');
		}
	}
}
//serach in array
function arr_index_of(x,v){
    for(var i = x.length; i-- && x[i] !== v;);
    return i;
  }
//remove form array 
function remove_items(x,val){
var index_value = arr_index_of(x,val);
var range = 1;
x.splice(index_value,range);
}
//add to array
function pusharr(e)
{ var str='';
if (e.checked==false)
remove_items(forprint[cat],e.value);
else forprint[cat].push(e.value); 
}
//check the ads was selected in other page or categories
function check_selected()
{
	for(var i=0;i<15;i++)
	{ var e=document.getElementById("c"+i);
		for(var j in forprint[cat])
		if(forprint[cat][j]==e.value)e.checked=true;
	}
}
//=========================
function myOpentoprint(printstr)
{
 var width="650", height="800";
 var left = 0;
 var top = 0;
 var styleStr = 'toolbar=no,location=no,scrollbars=yes,directories=no,menubar=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
 printWindow = window.open("","printWindow", styleStr);
 sndReq_for_print(printstr);
}
//============================
function print_selected_ads()
{  //alert('قيد التجربة');
printstr="";
	for(var i in forprint)
	{
	if(forprint[i].length!=0)printstr+="selected_cat"+(parseInt(i)+1)+"=";
	for(var j in forprint[i])
	printstr+=forprint[i][j]+",";
	if(forprint[i].length!=0)printstr=printstr.substring(0,printstr.length-1);
	if(forprint[i].length!=0)printstr+="&";
	}
	printstr=printstr.substring(0,printstr.length-1);
	//alert(printstr);
	myOpentoprint(printstr);
}

//////////////////////////
// other
//////////////////////////
function myopen(path){
window.open(path,"_blank","");
}




function showRandomads(){

images = new Array(3);



images[0] = "<a href='./search.php'><img border='0' src='images/ad1.jpg' alt=''/></a>";



images[1] = "<a href='./alarm.php'><img border='0' src='images/ad2.gif'  alt=''/></a>";



images[2] = "<a href='./search.php'><img border='0' src='images/ad3.gif'  alt=''/></a>";



images[3] = "<a href='http://www.syria-ads.com/html_ads_cast.php'><img border='0' src='images/ad4.gif'  alt=''/></a>";





index = Math.floor(Math.random() * images.length);



document.write(images[index]);

}

  function checkEmail(form) 
			  {	
			  	if (isBlank(form.email.value) || !isEmailValid(form.email.value) )
				   {
				   	alert("الرجاء إدخال صيغة الإيميل الصحيحة");		
					form.email.focus();		
					return false;	
				   }return true;
			  }
			  function isBlank(fieldValue) 
			  {	var blankSpaces = / /g;	
			        fieldValue = fieldValue.replace(blankSpaces, "");	
					return (fieldValue == "") ? true : false;
			  }
			  function isEmailValid(fieldValue) 
			  {	
			  	var emailFilter = /^.+@.+\..{2,4}$/;
				var atSignFound = 0;
				for (var i = 0; i <= fieldValue.length; i++)
				if ( fieldValue.charAt(i) == "@" )			
				atSignFound++;	
				if ( atSignFound > 1 )		
				return false;	
				else		
				return ( emailFilter.test(fieldValue) && !doesEmailHaveInvalidChar(fieldValue) ) ? true : false;
				}