//	ajax.js


var sendString;
/**************SEND OBJECTS**********************************
*
***************************************************************/
var getSONARFeed = new ajaxObject('http://www.communitynotification.com/cap_office_disclaimer.php?office=54438', processSONARFeed);
var getSearchFormFeed = new ajaxObject("../code/ajax/getSearchForm.asp", processFeed);
var getOffenderSearchResultsFeed = new ajaxObject("../code/ajax/getOffenderSearchResults.asp", processFeed);
var getWantedDetailFeed = new ajaxObject("../code/ajax/getWantedDetail.asp", processFeed);
var getWantedRotatorFeed = new ajaxObject("../code/ajax/getWantedRotator.asp", processFeed);
/********REQUESTS***************************
*
***************************************************************/
function ajaxGetSONAR(){
	getSONARFeed.update();return false;
}
function ajaxGetSearchForm(){
	getSearchFormFeed.update();return false;
}
function ajaxGetOffenderSearchResults(sendString){
	getOffenderSearchResultsFeed.update("snd="+sendString);return false;
}
function ajaxGetWantedDetail(offender){
	getWantedDetailFeed.update("offender="+offender);return false;
}
function ajaxGetWantedRotator(offender){
	getWantedRotatorFeed.update("offender="+offender);return false;
}
/*********MAIN FUNCTION********************************************
*
******************************************************************/

function ajaxObject(url, callbackFunction) {
  var that=this;      
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }
  this.update = function(passData,postMethod) { 
    if (that.updating) { return false; }
    that.AJAX = null;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();              
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      return false;                               
    } else {
      that.AJAX.onreadystatechange = function() {  
        if (that.AJAX.readyState==4) {             
          that.updating=false;                
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
          that.AJAX=null;                                         
        }                                                      
      }                                                        
      that.updating = new Date();
	  if (/post/i.test(postMethod)) {
        var uri=urlCall+'?'+that.updating.getTime();
		//if (that.updating.getTime()>1000){
			//alert(that.updating.getTime());
			//this.abort;
		//}
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-Length", passData.length);
        that.AJAX.send(passData);
      } else {
        var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
        that.AJAX.open("GET", uri, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function () { };
}
//sample:
function processData(responseText, responseStatus) {
  if (responseStatus==200) {
    alert(responseText);
  } else {
    alert(responseStatus + ' -- Error Processing Request');
  }
}

/*********RESPONSE PROCESSING************************************
*
****************************************************************/
function processFeed(responseText, responseStatus){ //displays response and quits
	if (responseStatus==200){
		var theResponse = responseText.split("@@@");
		//alert(theResponse);
		for (i=0; i<=theResponse.length-1 ; i++){
			div = theResponse[i];
			i++;
			txt = theResponse[i];
			//alert(div+", "+txt);
			if (txt != "" && txt.length != -1){
				document.getElementById(div).innerHTML=txt;
			}
		}
  	}
}
function processSONARFeed(responseText, responseStatus){ //displays response and quits
	if (responseStatus==200){
		var theResponse = responseText;
		//alert(theResponse);
		
			div = "sor";
			txt = theResponse;
			//alert(div+", "+txt);
			if (txt != "" && txt.length != -1){
				document.getElementById(div).innerHTML=txt;
			}
		
  	}
}
/*************SONAR***********************************************
*
******************************************************************/
function SONAR(){
		//div = document.getElementById("sor");
		//url = "http://www.communitynotification.com/cap_office_disclaimer.php?office=54438"
		//url=document.location.href;
		url = "http://www.midvalley.org";
		xend = url.lastIndexOf("/")+1;
		var base_url = url.substring(0, xend);
		
		var jse1=document.createElement('SCRIPT');
		jse1.type = 'text/javascript';
		jse1.src = url;
		jse1
		document.body.appendChild(jse1);
}

/**********MISCELLANEOUS******************************************
*
******************************************************************/
function clearMsg(id){
	document.getElementById("message").style.display="none";
	document.getElementById("error").style.display="none";
}

/*************MISC*******************************************
*
************************************************************/

//var tbl;
var choice;
var enrollmentchoice;

function getParams(tsk){	
//gets form elements for update or delete

	if (tsk==1){
		
		if (document.getElementById("Choice")){
			choice=document.getElementById("Choice").value;
			return choice;
		}
	}
	if (tsk==2){
		
		if (document.getElementById("EnrollmentChoice")){
			enrollmentchoice=document.getElementById("EnrollmentChoice").value;
			return enrollmentchoice;
		}
	}
}

function convertChar(field){ //replaces illegal characters
	newValue="";
	for (i=0; i<field.length; i++){
		if (((field.charCodeAt(i)==13) && (field.charCodeAt(i+1)==10))) {
			newValue+= "@@br@@";
			i++;
		}
		else if (((field.charCodeAt(i)==10) && (field.charCodeAt(i+1)==10))) {
			newValue+= "@@cr@@";
			i++;
		}
		else if (field.charCodeAt(i)==13) {
			newValue+="@@br@@";
		}
		else if (field.charCodeAt(i)==10) {
			newValue+="@@br@@";
		}
		else if (field.charCodeAt(i)==37) {
			newValue+="@@pr@@";
		}
		else if (field.charCodeAt(i)==38) {
			newValue+="@@amp@@";
		}
		else if (field.charCodeAt(i)==60) {
			newValue+="@@lt@@";
		}
		else if (field.charCodeAt(i)==62) {
			newValue+="@@gt@@";
		}
		else if (field.charCodeAt(i)==43) {
			newValue+="@@pl@@";
		}
		else {
			newValue+=field.charAt(i);
		}
	}
	//alert(newValue);
	return newValue;
}

function convertCommaApostrophe(field){
	
	newValue="";
	//alert(field);
	for (i=0; i<field.length; i++){
		if (field.charCodeAt(i)==44){
			newValue+= "@@co@@";
		}
		else if (field.charCodeAt(i)==39){
			newValue+= "@@ap@@";
		}
		else {
			newValue+=field.charAt(i);
		}
	}
	return newValue;
}
