url = document.location.href;
xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);

function doPostAjax(sUrl,sValue){
alert("doPostAjax")  
//  $.post('modules/preschool/ajax/increment_counter.php',{pkv_value:sValue});
  $.post(sUrl,{pkv_value:sValue});
//  alert(sFormname);
}

function ajax_do(url) {
	// Does URL begin with http?
	if (url.substring(0, 4) != 'http') {
		url = base_url + url;
	}
//alert(url);
	
	// Create new JS element
	var jsel = document.createElement('SCRIPT');
	jsel.type = 'text/javascript';
	jsel.src = url;
	// Append JS element (therefore executing the 'AJAX' call)
	document.body.appendChild(jsel);

//alert(jsel);
	
} 

function ajax_get(url, el) {
	// Has element been passed as object or id-string?
	if (typeof(el) == 'string') {
		el = document.getElementById(el);
	}
	// Valid el?
	if (el == null) { return false; }
	// Does URL begin with http?
	if (url.substring(0, 4) != 'http') {
		url = base_url + url;
	}
	// Create getfile URL
	getfile_url = base_url + 'getfile.php?url=' + escape(url) + '&el=' + escape(el.id);
	// Do Ajax
	ajax_do(getfile_url);

	return true;
}

//"ajax_do_select('includes/GappEdit/ajax/GappAjax_select.php?formname=".$this->formname."&fieldname=".$this->fieldnameform."&addempty=$select_prop_addempty&emptydisplayval=$select_prop_empty_display_val&sql=$select_prop_sql', '$sourcefieldval')";
/**
*params String pFormname - The formname to which the select belongs to
*        String pFieldname - The select fieldname or id which must be populated
*        Boolean pAddEmpty - Does the select has an empty option
*        String pEmpty DisplayVal - If the above param is true(1) then this value will be displayed as the empty value
*        String pSql - The sql to populate the select
*        String/element el - The input's value to be used for populating the select
*
* eg."onchange" => "ajax_pop_select('entryForm','suburb___','1','Select Suburb','select id, suburb from nuke_sa_suburbs where city_id in ([param])', 'city___')",
*/
function ajax_pop_select(pFormname,pFieldname,pAddEmpty,pEmptyDisplayVal,pSql, el) {
//alert("ajax_do_select")  ;

  var sWhereVal = "";
  var url = "includes/GappEdit/ajax/GappAjax_select.php?";
  var sFormname = "formname="+pFormname;
  var sFieldname = "&fieldname="+pFieldname;
  var sAddEmpty = "&addempty="+pAddEmpty;
  var sEmptyDisplayVal = "&emptydisplayval="+pEmptyDisplayVal;
  var sSql = "&sql="+pSql;
  
  url += sFormname+sFieldname+sAddEmpty+sEmptyDisplayVal+sSql;
  
	// Has element been passed as object or id-string?
	if (typeof(el) == 'string') {
		el = document.getElementById(el);
	}
	// Valid el?
	if (el == null) { 
		return false; 
	}else{
    for (i=0; i<el.options.length; i++) {
      if (el.options[i].selected) {
        sWhereVal += el.options[i].value+",";
      }
    }
		//sWhereVal = el.value;
	}
	//remove last ,
  sWhereVal=sWhereVal.replace(/,$/,"");
	
	// Does URL begin with http?
	if (url.substring(0, 4) != 'http') {
		url = base_url + url;
	}
	// Create getfile URL
	//getfile_url = base_url + 'getfile.php?url=' + escape(url) + '&el=' + escape(el.id);
	
	//selectfile_url = escape(url) + '&whereval=' + escape(swhereval);
	selectfile_url = url + '&whereval=' + sWhereVal;
	//selectfile_url = url + swhereval;
//alert('AAAA='+selectfile_url)	;
	
	// Do Ajax
	ajax_do(selectfile_url);

	return true;
}

function ajax_create_session(pVarName, pValue, pDelay) {
//alert("ajax_create_session")  ;
//alert(pVarName+" -- "+pValue);
  var millis = pDelay;
  var url = "includes/GappEdit/ajax/GappAjax_create_session.php?";
  var sVarName = "varname="+pVarName;
  var sValue = "&value="+pValue;

  url += sVarName+sValue;
  
//alert(url);
	// Do Ajax
	ajax_do(url);
	
  //pause for 1 second
  var date = new Date();
  var curDate = null;
  //var millis = 1000;
  do { curDate = new Date(); }
  while(curDate-date < millis);
 	
	return true;
}

  /*function ajax_do_select(url, el) {
//alert("ajax_do_select")  ;
	// Has element been passed as object or id-string?
	if (typeof(el) == 'string') {
		el = document.getElementById(el);
	}
	// Valid el?
	if (el == null) { 
		return false; 
	}else{
		swhereval = el.value;
	}
	
	// Does URL begin with http?
	if (url.substring(0, 4) != 'http') {
		url = base_url + url;
	}
	// Create getfile URL
	//getfile_url = base_url + 'getfile.php?url=' + escape(url) + '&el=' + escape(el.id);
	
	//selectfile_url = escape(url) + '&whereval=' + escape(swhereval);
	selectfile_url = url + '&whereval=' + swhereval;
	//selectfile_url = url + swhereval;
//alert('AAAA='+selectfile_url)	;
	
	// Do Ajax
	ajax_do(selectfile_url);

	return true;
}*/

/*var http = createRequestObject();
var objectId = '';

function createRequestObject(htmlObjectId){
    var obj;
    var browser = navigator.appName;
    
    objectId = htmlObjectId;
    
    if(browser == "Microsoft Internet Explorer"){
        obj = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
        obj = new XMLHttpRequest();
    }
    return obj;    
}

function sendReq(serverFileName, variableNames, variableValues) {
	var paramString = '';
	
	variableNames = variableNames.split(',');
	variableValues = variableValues.split(',');
	
	for(i=0; i<variableNames.length; i++) {
		paramString += variableNames[i]+'='+variableValues[i]+'&';
	}
	paramString = paramString.substring(0, (paramString.length-1));
			
	if (paramString.length == 0) {
	   	http.open('get', serverFileName);
	}
	else {
		http.open('get', serverFileName+'?'+paramString);
	}
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
	
	if(http.readyState == 4){
		responseText = http.responseText;
		document.getElementById(objectId).innerHTML = responseText;
    }
}*/
