/*

	url 

	id

	callbackMethod  ->  must = AjaxStates.Response

	selValue		->  the value you want to select

	xmlTagName      ->  the name of the tag name in xml page

	selectId        ->  the name of the select menu you want to update it's options

	loadDiv         ->  the id of the loading div

*/





var AjaxStates = new Object();



AjaxStates.Request = function(url, id, callbackMethod, selValue, xmlTagName, selectId, loadDiv, functionToDo, otherView)

{

	AjaxStates.selValue    	= selValue;

	AjaxStates.xmlTagName  	= xmlTagName;

	AjaxStates.selectId    	= selectId;

	AjaxStates.loadDiv    	= loadDiv;

	AjaxStates.functionToDo   = functionToDo;
	
	AjaxStates.otherView		= otherView;

	

	if ( id == '' )

	{	
		$(AjaxStates.selectId).length = 0;
		
		$(AjaxStates.selectId).options[0] = new Option("إختر الدولة",'');
		
		return;
	}



	AjaxStates.request = AjaxStates.createRequestObject();

	AjaxStates.request.onreadystatechange = callbackMethod;

	AjaxStates.request.open("POST", url+id, true);

	AjaxStates.request.send(url);

}



AjaxStates.Response = function () {

	

	if(AjaxStates.CheckReadyState(AjaxStates.request)){	

		$(AjaxStates.selectId).length = 0;

		

		var	response = AjaxStates.request.responseXML.documentElement;

		var _data = response.getElementsByTagName(AjaxStates.xmlTagName);



		$(AjaxStates.selectId).options[0] = new Option(' الكل ', '0');
		if(AjaxStates.selValue == 0)	$(AjaxStates.selectId).options[0].selected = true;
			
		var i = 0;
		
		if(_data.length > 0){

			for( i = 0 ; i < _data.length ; i++ ){
	
				var ID   = response.getElementsByTagName('id')[i].firstChild.data;
	
				var NAME = response.getElementsByTagName('name')[i].firstChild.data;
	
				
	
				$(AjaxStates.selectId).options[i+1] = new Option(NAME, ID);
	
				
	
				if (AjaxStates.selValue != null) {
	
					if (is_array(AjaxStates.selValue)) {   // if the selected values is array
	
						if(in_array(ID, AjaxStates.selValue)) {
	
							$(AjaxStates.selectId).options[i+1].selected = true;
	
						}
	
					} else {    //  if the selected value is not an array
	
						if(AjaxStates.selValue == ID) {
	
							$(AjaxStates.selectId).options[i+1].selected = true;
	
						}	
	
					}
	
				}	
	
			} // for
		}
		
		
		if (AjaxStates.otherView == 1){
			
			$(AjaxStates.selectId).options[i+1] = new Option("أخرى", "other");
			
			if (is_array(AjaxStates.selValue)) {   // if the selected values is array

					if(in_array("other", AjaxStates.selValue))		$(AjaxStates.selectId).options[i+1].selected = true;

			} else {    //  if the selected value is not an array

					if(AjaxStates.selValue == "other")		$(AjaxStates.selectId).options[i+1].selected = true;
			}
			
		}

	}

}



AjaxStates.createRequestObject = function(){

	var obj;

	if(window.XMLHttpRequest){

		obj = new XMLHttpRequest();

	}

	else if(window.ActiveXObject){

		obj = new ActiveXObject("MSXML2.XMLHTTP");

	}

	return obj;

}



AjaxStates.CheckReadyState = function(obj){

	if(obj.readyState < 4) {

		$(AjaxStates.loadDiv).innerHTML = $('loading_clown').innerHTML;

	}

	

	if(obj.readyState == 4){

		if(obj.status == 200){

			$(AjaxStates.loadDiv).innerHTML = "";
			
			eval(AjaxStates.functionToDo);

			return true;

		}

		else

		{

			

		}

	}

}
