// JavaScript Document
// page contains
// request_search
// jajax
// pageNumberResults
function request_search(data_id, args) {
	var params = '';
	var theform = document.frm_search;
	var url = window.location.pathname;
	url = url.substring(url.lastIndexOf('/')+1);
	for(i = 0; i < theform.length; i++) {
		if(theform[i].type != 'submit' && theform[i].type != 'button') {
			params += theform[i].name+'='+theform[i].value+'&';
		}
	}
	if(args == null && args == '') {
		args = '';
	}
	if(params != '') {
		params += 'method=ajax&action=search_records&'+args;
	}
	url = url+'?'+params;
	//alert(url);
	var sch = new jajax(url, 'record_list');
	sch.get();
}

function jajax(url, status_id, data_id) {
	var HTTP;
	HTTP = GetXmlHttpObject();
	if(HTTP == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	this.status_start = 'Processing...';
	this.status_end = 'Action Successfull...';
	this.url = url;
	this.status_id = status_id;
	this.data_id = data_id;
	this.show_status = true;
	this.show_data = true;
	this.script	 = false;
	this.params = null;
	this.success = success;
	this.post = post;
	this.get = get;
	this.process = process;
	this.send = send;
	this.status_message = status_message;
		
	if(this.status_id == null) {
		if(this.data_id != null && this.show_status) {
			this.status_id = this.data_id;
		}
	}
	if(this.data_id == null) {
		if(this.status_id != null && this.show_data) {
			this.data_id = this.status_id;
		}
	}
	
	if(this.url == null) {
		alert('URL is null');
		return false;
	}
		
	function post(params) {
		this.params = params;
		if(this.params == null) {
			alert('params is NULL');
		}
		HTTP.open("POST",this.url,true);
		HTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		HTTP.setRequestHeader("Content-length", this.params.length);
		HTTP.setRequestHeader("Connection", "close");
		this.send();
	}
	
	function onInit() {
		return true;
	}
	
	function onSent() {
		return true;	
	}
	
	function success(response) {
		return true;
	}
	
	function onSuccess(response) {
		return true;
	}
	
	function get() {
		HTTP.open("GET",this.url,true);
		this.send();
	}
	
	function status_message(message) {
		if(this.show_status) {
			if(message == null) {
				alert('Status message is NULL');
				return false;
			}
			if(document.getElementById(this.status_id)) {
				document.getElementById(this.status_id).innerHTML = '<span style="width:200px; padding:3px;" class="test2">'+message+'</span>';
			} else {
				alert('Status ID is not available');
				alert(message);
				return false;
			}
		}
	}
	
	function process() {
		var parent = this;
		HTTP.onreadystatechange = function() {
			if(HTTP.readyState == 1) {				
				if(parent.show_status) {
					parent.status_message('<img src="'+GLOBALS['IMG']+'busy.gif" /> Loading...');					
				} else {
					parent.onInit();
					
				}
			}
			if(HTTP.readyState == 2) {
				parent.onSent();
				
			}
			if(HTTP.readyState == 4) {				
				parent.status_message(parent.status_end);
				//if(HTTP.responseText != '') {
				if(parent.script) {
					eval(HTTP.responseText);
				} else if(parent.show_data) {
					if(document.getElementById(parent.data_id)) {						
						document.getElementById(parent.data_id).innerHTML = HTTP.responseText;
						parent.onSuccess(HTTP.responseText);
					} else {
						alert('Data ID not available');
						alert(HTTP.responseText);
					}
				} else {
					parent.onSuccess(HTTP.responseText);
				}
				
				if(jui) {
					jui.removeLoading();
				}
			}
		}
	}
	
	function send() {
		this.process();
		HTTP.send(this.params);
	}
	
	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;
	}	
}


// ----------------- Ajax Page number ser ------------------
function pageNumberResults(urlPageNum, element, obj) {
	if(!document.getElementById(element)) {
		alert('page results element is not available');	
	}
	if(urlPageNum == null) {
		alert('Url cannot be null');	
	}
		
	if(element == null) {
		alert('Element cannot be null');	
	}
	
	var e = document.getElementById(element);
	e.style.position = 'relative';
	var newElement = document.createElement('div');
	newElement.style.position = 'absolute';
	newElement.style.top = '50%';
	newElement.style.left = '40%';
	newElement.style.backgroundColor = '#FFF';
	newElement.style.padding = '10px'
	newElement.style.border = '2px solid #FF0000';
	newElement.innerHTML = '<img src="'+GLOBALS['IMG']+'busy.gif" /> Loading...';
	e.appendChild(newElement);
	
	var a = new jajax(urlPageNum, element);	
	a.show_status = false;
	a.get();
}
// ----------------- End Ajax Page number set------------------


// --------------------- AJAX Star rating system ---------------- //

function starRating(url, obj) {
	var query;
	if(url != '') {
		if(url.indexOf('?') != -1) {
			url = url+'&';
		} else {
			url = url+'?';
		}
	}
	
	
	url = url+'method=ajax&req=rating&val='+obj.innerHTML;
	var a = new jajax(url);
	a.show_status = false;
	a.show_data = false;

	a.onSuccess = function(response) {
//		alert(obj.parentNode.parentNode.nextSibling.tagName);
		var str = 'var r = '+response;
		eval(str);
		
		if(r.width != null && r.width != '') {
			obj.parentNode.parentNode.getElementsByTagName('li')[0].style.width = r.width+'px';
		}
		obj.parentNode.parentNode.nextSibling.innerHTML = r.error;
	}
	
	a.get();
}




