// JavaScript Document
// REQUIRES Scripts/validator.js and Scripts/ajaxbridge.js
	
	function URLEncode (clearString) {
	  var output = '';
	  var x = 0;
	  clearString = clearString.toString();
	  var regex = /(^[a-zA-Z0-9_.]*)/;
	  while (x < clearString.length) {
		var match = regex.exec(clearString.substr(x));
		if (match != null && match.length > 1 && match[1] != '') {
			output += match[1];
		  x += match[1].length;
		} else {
		  if (clearString[x] == ' ')
			output += '+';
		  else {
			var charCode = clearString.charCodeAt(x);
			var hexVal = charCode.toString(16);
			output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
		  }
		  x++;
		}
	  }
	  return output;
	}


	// -------------------------------------------------------------------
	// hasOptions(obj)
	//  Utility function to determine if a select object has an options array
	// -------------------------------------------------------------------
	function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}


    // -------------------------------------------------------------------
	// removeAllOptions(select_object)
	//  Remove all options from a list
	// -------------------------------------------------------------------
	function removeAllOptions(from) { 
	if (!hasOptions(from)) { return; }
	for (var i=(from.options.length-1); i>=0; i--) { 
		from.options[i] = null; 
		} 
	from.selectedIndex = -1; 
	} 

	// -------------------------------------------------------------------
	// addOption(select_object,display_text,value,selected)
	//  Add an option to a list
	// -------------------------------------------------------------------
	function addOption(obj,text,value,selected) {
	if (obj!=null && obj.options!=null) {
		obj.options[obj.options.length] = new Option(text, value, false, selected);
		}
	}


   function  fireEvent(element,event){
		if (document.createEventObject){
			// dispatch for IE
			var evt = document.createEventObject();
			return element.fireEvent('on'+event,evt)
		}
		else{
			// dispatch for firefox + others
			var evt = document.createEvent("HTMLEvents");
			evt.initEvent(event, true, true ); // event type,bubbling,cancelable
			return !element.dispatchEvent(evt);
		}
	}


   function linkComponents(masterId,childId, enc_query) {
	       /* 
		      enc_query syntax:  valueField~CaptionField~currValue~Query
		      Query must have @value@ where the master value will be substituted 
		   */
		   
	       var masterObj = document.getElementById(masterId);
		   var childObj = document.getElementById(childId);
		   
		   masterObj.setAttribute('autocomplete', 'off');
		   childObj.setAttribute('autocomplete', 'off');
		   
		   addListener(masterObj, "change", function(e) {	
				
				if (masterObj.value != "") {				
					var response = getResult("Scripts/rel_db_combo_populate.php?master_value=" + URLEncode(masterObj.value) + "&" + enc_query);
					childObj.innerHTML = response;
					
					var temp = document.createElement('DIV');
					temp.innerHTML = "<select id='tmp_sel'>" + response + "</select>";
					var sel = temp.childNodes[0];
					
					
					if (childObj.innerHTML != response) {
						removeAllOptions(childObj);
					
						for (i=0; i < sel.options.length; i++) {
							childObj.options[childObj.options.length] = new Option(sel.options[i].innerHTML.replace("&amp;","&"), sel.options[i].value, false, sel.options[i].selected);
						}
						
						fireEvent(childObj,'change');
						
					}
					
					
				}
				else {
					removeAllOptions(childObj);
					fireEvent(childObj,'change');
				}
				
		   });   
		   
		   //set clock
		   var lastValue = masterObj.value;
		   
		   
		   // display first values
		   if (masterObj.value != "") {
			   
			   var response = getResult("Scripts/rel_db_combo_populate.php?master_value=" + URLEncode(masterObj.value) + "&" + enc_query);
				childObj.innerHTML = response;
				
				var temp = document.createElement('DIV');
				temp.innerHTML = "<select id='tmp_sel'>" + response + "</select>";
				var sel = temp.childNodes[0];
				
				removeAllOptions(childObj);
				
				for (i=0; i < sel.options.length; i++) {
					childObj.options[childObj.options.length] = new Option(sel.options[i].innerHTML.replace("&amp;","&"), sel.options[i].value, false, sel.options[i].selected);
				}
			   
		   }
   }
   
   function linkDynText(masterId,childId, enc_query, callback) {
	       // enc_query syntax: field~sqlsentence  must have @value@
	   
	       var masterObj = document.getElementById(masterId);
		   var childObj = document.getElementById(childId);
		   
		   addListener(masterObj, "change", function(e) {
				if (masterObj.value != "") {									 
					childObj.innerHTML = getResult("Scripts/rel_dyn_text.php?master_value=" + URLEncode(masterObj.value) + "&" + enc_query);								 
				}
				else {
					childObj.innerHTML = getResult("Scripts/rel_dyn_text.php?master_value=0&" + enc_query);	
				}
			    
				if (callback) {
					eval(callback);
			    }
		   });   
		   
		   //set clock
		   var lastValue = masterObj.value;

		   
		   // display first values
		   if (masterObj.value != "") {
		   			childObj.innerHTML = getResult("Scripts/rel_dyn_text.php?master_value=" + URLEncode(masterObj.value) + "&" + enc_query);
		   }
		   
		   if (callback) {
		   		eval(callback);
		   }
   
   }
   
   function linkHideAreaFunction( expressionStr , childId ) {
	       var lastValue = eval(expressionStr);
		   var childObj = document.getElementById(childId);

		   
   		   setInterval(function () {
			   var res = eval(expressionStr);
			   if (lastValue != res) {
			        lastValue = res;
				    var visible = res;								 
					if (visible == 0) {
					   childObj.style.display = "none";
					}
					else {
					   childObj.style.display = "";
					}
					
			   }

		   },1000);
		   
		   var visible = lastValue;								 
		   if (visible == 0) {
			   childObj.style.display = "none";
		   }
		   else {
			   childObj.style.display = "";
		   }
		   
   }
   
   
   function linkHideArea(masterId,childId, enc_query) {
	       /* 
		      searches for number of rows in the specified query if num_rows = 0 the "style.display=none" will be applied to childId, else the "style.diplay.block" will be applied.
		      enc_query syntax:  SQLQuery
		      Query must have @value@ where the master value will be substituted 
		   */
		   
	       var masterObj = document.getElementById(masterId);
		   var childObj = document.getElementById(childId);
		   
		   addListener(masterObj, "change", function(e) {
													 
				if (masterObj.value != "") {		 							 
					var visible = getResult("Scripts/rel_hide_area.php?master_value=" + URLEncode(masterObj.value) + "&" + enc_query);	
				}
				else {
					visible = "0";
				}
				
				//alert(visible);
				if (visible == "0") {
				   childObj.style.display = "none";
				}
				else {
				   childObj.style.display = "";
				}
				
		   });   
		   
		   //set clock
		   var lastValue = masterObj.value;
		  
		   
		   // display first values
		   if (masterObj.value != "") {
		   			var visible = getResult("Scripts/rel_hide_area.php?master_value=" + URLEncode(masterObj.value) + "&" + enc_query);								 
				
					if (visible == "0") {
					   childObj.style.display = "none";
					}
					else {
					   childObj.style.display = "";
					}
		   }
		   else {
			   childObj.style.display = "none";
		   }
   }
   

