/*
## Ahmad Ismail Abu Nadi .... ht4all@msn.com ##
* This file contain all the javasrcpits code which related with user functions
*/

var xmlHttp

/*//////////////////////////////////////////////////////////////////////////////////////////
* response for user login operation by using ajax
*//////////////////////////////////////////////////////////////////////////////////////////
function SendMsg(){
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null){
        alert ("Browser does not support HTTP Request");
        return;
	}	
	
	var name = document.getElementById('name').value; 
	var email = document.getElementById('email').value; 
	var subject = document.getElementById('subject').value; 
	var message = document.getElementById('message').value; 
	var url="includes/site.ajax.functions.php?do=SendMsg";
	var params = "name="+name+"&email="+email+"&subject="+subject+"&message="+message;
	xmlHttp.onreadystatechange=UserStateChanged;
	xmlHttp.open("POST", url, true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);		
}

function UserStateChanged(){
	
	var ErrorMsg = document.getElementById("error_msg");
	var ErrorTxt = document.getElementById("TxtMsg");
	var AjaxPic = document.getElementById("LoginAjax");
	var Loginbtn = document.getElementById("Loginbtn");
	var true_msg = document.getElementById("true_msg");
	
	if (xmlHttp.readyState==1){
		ErrorMsg.style.display = "none"; 
		Loginbtn.style.display = "none"; 
		AjaxPic.style.display = ""; 
		true_msg.style.display = "none"; 
		} 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		if(xmlHttp.responseText != "true"){
			AjaxPic.style.display = "none";
			ErrorMsg.style.display = ""; 
			Loginbtn.style.display = ""; 
			true_msg.style.display = "none"; 
			ErrorTxt.innerHTML=xmlHttp.responseText;
		}
		else{
			true_msg.style.display = ""; 
			AjaxPic.style.display = "none";
			ErrorMsg.style.display = "none"; 
			Loginbtn.style.display = "none"; 
		}
	}
}
/*//////////////////////////////////////////////////////////////////////////////////////////
* End
*//////////////////////////////////////////////////////////////////////////////////////////
/*//////////////////////////////////////////////////////////////////////////////////////////
* response for Get Main Page
*//////////////////////////////////////////////////////////////////////////////////////////
function AddToMailingList(){
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null){
        alert ("Browser does not support HTTP Request");
        return;
	}	
	
	var newemail = document.getElementById('newemail').value;
	
	if(newemail == "" || newemail == null){
		if (echeck(newemail)==false){
				return false
		}
	}
	
	var url="includes/site.ajax.functions.php?do=AddToMailingList&newemail="+newemail;
	xmlHttp.onreadystatechange=function(){
		var newemailinput = document.getElementById("newemailinput");
		var mailingimg = document.getElementById("newemailimg");

     if (xmlHttp.readyState==1){
		mailingimg.style.display = ""; 
		newemailinput.style.display = "none"; 
		} 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		mailingimg.style.display = "none";
		if(xmlHttp.responseText == '1'){
			alert("صيغة البريد الإلكتروني المدخل خاطئة ,, الرجاء المحاولة مرة أخرى.");
			newemailinput.style.display = ""; 
		}
		else{
			newemailinput.style.display = ""; 
			newemailinput.innerHTML= xmlHttp.responseText;
		}
	}};
     
	xmlHttp.open("GET", url, true);
	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){
		   alert("الرجاء إدخال بريدك الإلكتروني بالحقل.")
		   return false
		}

 		 return true					
	}
/*//////////////////////////////////////////////////////////////////////////////////////////
* response for Get Main Page
*//////////////////////////////////////////////////////////////////////////////////////////
function GetVoteResult(voteid){
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null){
        alert ("Browser does not support HTTP Request");
        return;
	}	
	
	var url="includes/site.ajax.functions.php?do=GetVoteResult&voteid="+voteid;
	xmlHttp.onreadystatechange=function(){
		var votesarea = document.getElementById("votesarea"+voteid);
		var votesimg = document.getElementById("votesimg"+voteid);

     if (xmlHttp.readyState==1){
		votesimg.style.display = ""; 
		votesarea.style.display = "none"; 
		} 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		votesimg.style.display = "none";
		votesarea.style.display = ""; 
		votesarea.innerHTML= xmlHttp.responseText;
		//$(xmlHttp.responseText).appendTo(votesarea);   
		
	}};
     
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);		
}


/*//////////////////////////////////////////////////////////////////////////////////////////
* response for Get Main Page
*//////////////////////////////////////////////////////////////////////////////////////////
function Vote(voteid){
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null){
        alert ("Browser does not support HTTP Request");
        return;
	}	

	var res = getRadioValue('res'+voteid);
	if(res == null){
		alert("يجب عليك إختيار إجابة واحدة للتصويت ,, الرجاء المحاولة مرة أخرى.");
		return;
	}
	
	
	var url="includes/site.ajax.functions.php?do=Vote&voteid="+voteid+"&res="+res;
	xmlHttp.onreadystatechange=function(){
		var votesarea = document.getElementById("votesarea"+voteid);
		var votesimg = document.getElementById("votesimg"+voteid);
		var votesbtn = document.getElementById("votesbtn"+voteid);
		
     if (xmlHttp.readyState==1){
		votesimg.style.display = ""; 
		votesarea.style.display = "none"; 
		votesbtn.style.display = "none"; 
		} 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		votesimg.style.display = "none";
		votesarea.style.display = ""; 
		votesarea.innerHTML= xmlHttp.responseText;
		//$(xmlHttp.responseText).appendTo(votesarea);   
		
	}};
     
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);		
}

/*//////////////////////////////////////////////////////////////////////////////////////////
* response for Get Main Page
*//////////////////////////////////////////////////////////////////////////////////////////
function GetMainPage(){
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null){
        alert ("Browser does not support HTTP Request");
        return;
	}	

	var url="includes/site.ajax.functions.php?do=GetMainPage";
	xmlHttp.onreadystatechange=GeneralStateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);		
}

/*//////////////////////////////////////////////////////////////////////////////////////////
* response for Get ContactUs
*//////////////////////////////////////////////////////////////////////////////////////////
function GetContactUs(){
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null){
        alert ("Browser does not support HTTP Request");
        return;
	}	

	var url="includes/site.ajax.functions.php?do=GetContactUs";
	xmlHttp.onreadystatechange=GeneralStateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);		
}

/*//////////////////////////////////////////////////////////////////////////////////////////
* response for return news by id
*//////////////////////////////////////////////////////////////////////////////////////////
function GetNewsById(newsid){
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null){
        alert ("Browser does not support HTTP Request");
        return;
	}	

	var url="includes/site.ajax.functions.php?do=GetNewsById&newsid="+newsid;
	xmlHttp.onreadystatechange=GeneralStateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);		
}


/*//////////////////////////////////////////////////////////////////////////////////////////
* response for return the page by id
*//////////////////////////////////////////////////////////////////////////////////////////
function GetPage(pageid){
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null){
        alert ("Browser does not support HTTP Request");
        return;
	}	

	var url="includes/site.ajax.functions.php?do=GetPage&pageid="+pageid;
	xmlHttp.onreadystatechange=PageStateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);		
}


/*//////////////////////////////////////////////////////////////////////////////////////////
* End
*//////////////////////////////////////////////////////////////////////////////////////////

function PageStateChanged(){

	var pagecontent = document.getElementById("pagecontent");
	var ajaxloading = document.getElementById("ajaxloading");
	var page_title = document.getElementById("page_title");
	
	if (xmlHttp.readyState==1){
		ajaxloading.style.display = ""; 
		} 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		
		var result = xmlHttp.responseText.split('|||||||');
		
		ajaxloading.style.display = "none";
		pagecontent.innerHTML= "";
		page_title.innerHTML= result[0];
		$(result[1]).appendTo(pagecontent);   
		
	}
}

function GeneralStateChanged(){

	var pagecontent = document.getElementById("pagecontent");
	var ajaxloading = document.getElementById("ajaxloading");
	
	if (xmlHttp.readyState==1){
		ajaxloading.style.display = ""; 
		} 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		ajaxloading.style.display = "none";
		pagecontent.innerHTML= "";
		$(xmlHttp.responseText).appendTo(pagecontent);   
		
	}
}



function concat(array){
  var str = "";
  var val = "";
  for(i=0; i<array.length; i++){
  	val = array[i].value;
  	if(!val){
  		if(i != array.length - 1)
		  str += "0"+";;==;;";
		  else
		  str += "0";
  	}
  	else{
  		if(i != array.length - 1)
		  str += val+";;==;;";
		  else
		  str += val;
  	}
  }
  return str;
}


/*
* this funtion response for returning the value of radio button
*/
function getRadioValue(idOrName) {
        var value = null;
        var element = document.getElementById(idOrName);
        var radioGroupName = null;  
        
        // if null, then the id must be the radio group name
        if (element == null) {
                radioGroupName = idOrName;
        } else {
                radioGroupName = element.name;     
        }
        if (radioGroupName == null) {
                return null;
        }
        var radios = document.getElementsByTagName('input');
        for (var i=0; i<radios.length; i++) {
                var input = radios[ i ];    
                if (input.type == 'radio' && input.name == radioGroupName && input.checked) {                          
                        value = input.value;
                        break;
                }
        }
        return value;
}
/*
* define Xml Http Object.
*/
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

