
/*
	these are the helper functions for the AJAX agent system
	
		getRefToDiv(string: div id, doc: document object);
		insertHTML(object: obj, string: HTML);
		String.prototype.unsecapeHTML() where String is an HTML entity encoded string
			the result is a normal string
*/
	
/*	try {
		include_once("/bgeJS/prototype.js");
	} catch (err) {
		//alert("include_once not defined");
	};
*/

	// get the DOM object called divID in oDoc
    function getRefToDiv(divID,oDoc) {
      if( document.getElementById ) {
      	// this brower responds to getElementByID so use it
        return document.getElementById(divID); 
	  }
      if( document.all ) {
      	// this browser has a single array of objects get the object form it
        return document.all[divID]; 
	  }
	  // this browser has layers search them
      if( !oDoc ) { 
	  	oDoc = document;
	  }
      if( document.layers ) {
        if( oDoc.layers[divID] ) {
        	// found the obj in a layer array
			 return oDoc.layers[divID]; 
		} else {
          // couldn't find the object, repeatedly run through all child layers.
          for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
            //on success, return that layer, else return nothing
            y = getRefToDiv(divID,oDoc.layers[x].document);  // recursive call with layers as documents
		  }
          return y; 
		}
	  }
      return false;
    }	
	
	
	// insert HTML into object pointed to by obj
	function insertHTML(obj,HTML) {
		// if this browser response to innerHTML useit
		if( typeof( obj.innerHTML ) != 'undefined' ) {
			obj.innerHTML =  HTML;
		// if it doesn't like innerHTML try insertAdjacentHTML
		  } else if( obj.insertAdjacentHTML ) {
		  	//alert('innerHTML');
		    obj.insertAdjacentHTML( 'beforeEnd', HTML );
		  } else {
		    alert('FAILURE, nothing works');
        } 
		
	}

	
	
	// this reverses the PHP call to htmlenities and reconsitutes &"<>
	// from &amp; &quot; &lt; &gt; 
	String.prototype.decodeHTML = function () {                                       
        return(                                                                 
            this.replace(/&amp;/g,'&').                                         
                replace(/&gt;/g,'>').                                           
                replace(/&lt;/g,'<').                                           
                replace(/&quot;/g,'"').
				replace(/&nbsp;/g,' ')                                         
        );                                                                     
    };
    
    String.prototype.encodeHTML = function () {
        return(                                                                 
            this.replace(/\&/g,'&amp;').                                         
                replace(/\>/g,'&gt;').                                           
                replace(/\</g,'&lt;').                                           
                replace(/\"/g,'&quot;').
				replace(/\ /g,'&nbsp;').
				replace(/\x27/g,'&')                                         
        );                                                                     
    };
    // replace \ (back slash) with / (forward slash)
    String.prototype.escapeSlashes = function () {
		return this.replace(/\\/g, '/');
	};
	
	String.prototype.encodeAmps = function() {
		return this.replace(/&/g,"#amp#");
	};
	
	String.prototype.decodeAmps = function() {
		return this.replace(/#amp#/g,'&');
	};

	
	String.prototype.handleTicks = function () {
		return (
			this.replace(/&#39;/g,"\'").
				replace(/&nbsp;/ig, ' ').
				replace(/&#34;/g, '\"')
			);
	};
	
	// Get the value of a TinyMCE editor
	
	function getTinyMCEValue(ed) {
		edname = "mce_editor_"+ed;
		value = $(edname).contentWindow.document.body.innerHTML;
		return value;
	}
	
	
	// make a JSON object from a pair of arrays
	function arraysToJSON (kArray,vArray) {
		len = kArray.length;
		aret = '';
		for (i = 0;i < len; i++) {
			aret += kArray[i]+' : "'+vArray[i]+'"';
			if(i != len-1) {
				aret += ', ';
			}
			//alert(aret);
		}
		return eval('({'+aret+'})'); 
	}
	
function getInputObject(form) {
	tmp = form.getElements();
	//alert(tmp.toString());
	str="";
	i = 0;
	j = 0;
	k = 0;
	nret = Array();
	dret = Array();
	while(i < tmp.length){
		//alert(tmp[i].type);
		switch (tmp[i].type) {
			case 'text': 
				nret[j] = tmp[i].name;
				dret[j] = $F(tmp[i]);
				i++;
				j++;
				break;
			case 'radio':
				if($F(tmp[i]) != null) {
					nret[j] = tmp[i].name;
					dret[j] = $F(tmp[i]);
					j++;
				}
				i++;
				break;
			case 'select-one':
			case 'select-multiple':
				nret[j] = tmp[i].name;
				dret[j] = $F(tmp[i]);
				i++;
				j++;
				break;
			case 'checkbox':
				if(k == 0) {
					res = Array();
					aname = tmp[i].name	
				}		
				if($F(tmp[i]) != null) {
					aname = tmp[i].name;
					res[k++] = ($F(tmp[i]));						
				}
				i++;
				if(tmp[i].name != aname) {
					nret[j] = aname;							
					dret[j] = res;
					k = 0;
				}
				break;
			case "textarea":
				nret[j] = tmp[i].name;
				tstr = $F(tmp[i++]);
				dret[j++] = escape(tstr);
				break;
				
			case 'hidden':
				nret[j] = tmp[i].name;
				dret[j++] = $F(tmp[i++]);
				break;
				
			case 'submit':
				i++;
				break;
			}
		}
		//alert('done while');
		obj = arraysToJSON(nret,dret);
		//alert(obj.toSource());
		return  obj;
	}
