prefix="./";
function myXMLHttpRequest() {
  var xmlhttplocal;
  try {
    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
    xmlhttplocal=false;
  }
 }

if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
 try {
  var xmlhttplocal = new XMLHttpRequest();
 } catch (e) {
  var xmlhttplocal=false;
  alert('couldn\'t create xmlhttp object');
 }
}
return(xmlhttplocal);
}

function subscribe() {
	var email=document.getElementById('email');
	if(email.value==""){
		document.getElementById('empty_msg').style.display="block";
		return;
	}else{
		document.getElementById('empty_msg').style.display="none";
		if(!echeck(email.value)){
			document.getElementById('invalid_msg').style.display="block";
			return false;
		}else{
			document.getElementById('invalid_msg').style.display="none";
			document.getElementById('status_msg').style.display="block";
		  url=prefix+"internal_request.php?action=subscribe&email="+email.value;
		 var xmlhttp=myXMLHttpRequest();
		 xmlhttp.open("GET",url,true);
		  xmlhttp.onreadystatechange=function() {
		 //alert('url');
			if (xmlhttp.readyState==4) {
			 //document.getElementById("sub_category").innerHTML=xmlhttp.responseText;
			 document.getElementById('status_msg').style.display="none";
			 document.getElementById('subscribe_div').style.display="none";
			 document.getElementById('subscribe_success').style.display="block";
			}
		  }
		  xmlhttp.send(null);
			
		}
	}
}

function rate(id) {
		document.getElementById('rate_'+id).style.display="none";
		document.getElementById('rate_'+id+'_1').style.display="none";
		//document.getElementById('rate_'+id+'_2').style.display="block";
		var rating=document.getElementById('rating_'+id).value;
	  url=prefix+"internal_request.php?action=rate&id="+id+'&rating='+rating;
	  //alert(url);
	 var xmlhttp=myXMLHttpRequest();
	 xmlhttp.open("GET",url,true);
	  xmlhttp.onreadystatechange=function() {
	 //alert('url');
		if (xmlhttp.readyState==4) {
		 //document.getElementById("sub_category").innerHTML=xmlhttp.responseText;
		 //document.getElementById('rate_'+id+'_2').style.display="none";
		 document.getElementById('rate_'+id).innerHTML=xmlhttp.responseText;
		 document.getElementById('rate_'+id).style.display="block";
		}
	  }
	  xmlhttp.send(null);
}

function report(id) {
		document.getElementById('report_'+id).style.display="none";
	  url=prefix+"internal_request.php?action=report&id="+id;
	 var xmlhttp=myXMLHttpRequest();
	 xmlhttp.open("GET",url,true);
	  xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
		 document.getElementById('report_'+id).innerHTML=xmlhttp.responseText;
		 document.getElementById('report_'+id).style.display="block";
		}
	  }
	  xmlhttp.send(null);
}

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }
	 if (str.indexOf(" ")!=-1){
		return false
	 }
	 return true					
}

function OpenWindow(URL, width, height){
	window.open(URL,'newwindow','scrollbars=no,status=no,resizable=yes,width='+width+',height='+height+',top=100,left=200');			
}

function displaySubCategories(s_id) {
	document.getElementById('cat_msg').innerHTML="fetching data from server...";
	var c_id=document.getElementById('cat_id').value;
  url="internal_request.php?action=scat&c_id="+c_id+"&s_id="+s_id;
 var xmlhttp=myXMLHttpRequest();
 xmlhttp.open("GET",url,true);
  xmlhttp.onreadystatechange=function() {
 //alert('url');
    if (xmlhttp.readyState==4) {
     document.getElementById("sub_category").innerHTML=xmlhttp.responseText;
	 document.getElementById('cat_msg').innerHTML="";
    }
  }
  xmlhttp.send(null);
}

function displaySubCategories2(s_id) {
	document.getElementById('cat_msg').innerHTML="fetching data from server...";
	var c_id=document.getElementById('cat_id').value;
  url="internal_request.php?action=scat2&c_id="+c_id+"&s_id="+s_id;
 var xmlhttp=myXMLHttpRequest();
 xmlhttp.open("GET",url,true);
  xmlhttp.onreadystatechange=function() {
 //alert('url');
    if (xmlhttp.readyState==4) {
     document.getElementById("sub_category").innerHTML=xmlhttp.responseText;
	 document.getElementById('cat_msg').innerHTML="";
    }
  }
  xmlhttp.send(null);
}

function validateAddLink(form){
	var ch=document.getElementById("name");
	if(ch.value==""){
		alert("إسمك");
		ch.focus();
		return false;
	}
	ch=document.getElementById("email");
	if(ch.value==""){
		alert("بريدك الإلكتروني");
		ch.focus();
		return false;
	}	
	ch=document.getElementById("cat_id");
	if(ch.value==""){
		alert("تصنيف");
		ch.focus();
		return false;
	}	
	ch=document.getElementById("scat");
	if(ch.value==""){
		alert("تصنيف");
		ch.focus();
		return false;
	}	
	ch1=document.getElementById("subject");
	if(ch1.value==""){
		alert("عنوان");
		ch1.focus();
		return false;
	}	
	ch1=document.getElementById("url");
	if(ch1.value==""){
		alert("رابـط");
		ch1.focus();
		return false;
	}	
	ch=form.description;
	if(ch.value==""){
		alert("وصف");
		ch.focus();
		return false;
	}
}
