/* AJAX ENABLER */
function ajaxFunction(theTarget, theScript, theMethod, theFormID, theType, includeRte, rteFieldName){
	var theType = (theType == null) ? "HTML" : theType;
	var ajaxRequest;
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// No support found
				alert("Ajax not supported!");
				return false;
			}
		}
	}
	// Function for recieving data from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById(theTarget);
			//alert (theType + ' ~ ' + ajaxRequest.responseText);
			switch (theType){
				case 'HTML':
					var response = ajaxRequest.responseText;
					var script = '';
					var postScript = '';
					var ar_script1 = null;
					var ar_script3 = null;
					var myString = '';
					var n=0;
					var posA = 0;
					var posB = 0;

					//alert(response);

					// PRE SCRIPT Look for any embedded javascript and execute it 
					// - then remove it from the Response HTML
					if(response.search(/<prescript>/) != -1) {
						ar_script1=response.split("<prescript>");
						while(ar_script1.length > 0) {
							if(n!=0) {
								preScript = ar_script1.shift();
								if(preScript.search(/<\/prescript>/) != -1) {
									var ar_script2 = preScript.split("</prescript>");
									preScript = ar_script2.shift();
									//alert(n+'[2]'+script);
									eval(preScript);
								}
							}
							n++;
						}
						// Now remove all evidence of the pre-script
						for(var preCount = 0; preCount < ar_script1.length; ++preCount) {
							posA = response.indexOf("<prescript>");
							posB = response.indexOf("</prescript>");
							myString = response.substring(0, posA);
							myString += response.substring(posB + 12);
							reponse = myString;
						}
					}

					// POST SCRIPT Look for any embedded javascript and STORE it 
					//for post execution - then remove it from the Response HTML
					if(response.search(/<postscript>/) != -1) {
						ar_script3=response.split("<postcript>");
						var postScriptCount = ar_script3.length;
						while(ar_script3.length > 0) {
							if(n!=0) {
								postScriptTmp = ar_script3.shift();
								if(postScriptTmp.search(/<\/postcript>/) != -1) {
									var ar_script4 = postScriptTmp.split("</postcript>");
									postScript += ar_script4.shift();
								}
							}
							++n;
						}
						// Now remove all evidence of the post-script
						for(var postCount = 0; postCount < postScriptCount; ++postCount) {
							posA = response.indexOf("<postscript>");
							posB = response.indexOf("</postscript>");
							myString = response.substring(0, posA);
							myString += response.substring(posB + 12);
							reponse = myString;
						}
					}

					// HTML:  update the target tag's inner HTML
					ajaxDisplay.innerHTML = response;

					// EXECUTE STANDARD JAVASCRIPT (still included):
					if(response.search(/<script>/) != -1) {
						ar_script1=response.split("<script>");
						n=0;
						while(ar_script1.length > 0) {
							if(n!=0) {
								script = ar_script1.shift();
								if(script.search(/<\/script>/) != -1) {
									var ar_script2 = script.split("</script>");
									script = ar_script2.shift();
									//alert(n+'[2]'+script);
									eval(script);
								}
							}
							++n;
						}
					}

					// Once all sels is done, EXECUTE ANY POSTSCRIPT:
					if(postScript.length > 0) {
						eval(postScript);
					}

				break;

				case 'JS':
					// Javascript:  eval() the returned string
					eval(ajaxRequest.responseText);
					break;
			}
			if (includeRte == 1 && rteFieldName != '') {
  			//eval('make_wyzz(\'' + rteFieldName + '\');');	
				eval('WYSIWYG.generate(\'' + rteFieldName + '\', 550, 200);');
			}
		}
	} 
	
	// Call appropriate server-side script
	if (theMethod == 'GET') {
			ajaxRequest.open("GET", theScript + "?sid=%sid%&lang=%lang%&" + theFormID, true);
			ajaxRequest.send(null); 
	}
	else if (theMethod == 'POST') {
		// For the POST method theQuery submit all input-type fields in the given form
			var params = '';
			for (var i=0; i < document.forms[theFormID].elements.length; i++) {
				if( document.forms[theFormID].elements[i].type == 'text' 
					|| document.forms[theFormID].elements[i].type == 'textarea' 
					|| document.forms[theFormID].elements[i].type == 'select-one' 
					|| document.forms[theFormID].elements[i].type == 'radio' 
					|| document.forms[theFormID].elements[i].type == 'hidden' ) {
					if(encodeURIComponent(document.forms[theFormID].elements[i].value) != '') {
						params +=  '&'+document.forms[theFormID].elements[i].id+'='+encodeURIComponent(document.forms[theFormID].elements[i].value);
						//alert(document.forms[theFormID].elements[i].id+': '+document.forms[theFormID].elements[i].value);
					}
				}
				
				if (document.forms[theFormID].elements[i].type == 'select-multiple' && document.forms[theFormID].elements[i].length > 0) {
					var selectedValues = [];
					var theList = document.forms[theFormID].elements[i];
					for(var n=0; n<theList.options.length; n++) {
						if(theList.options[n].selected) {
							selectedValues.push(encodeURIComponent(document.forms[theFormID].elements[i].options[n].value));
							//alert(theList.id+": "+theList.options[n].value);
							//alert(theList.id);
						}
					}
					if (selectedValues != '') {
						params +=  '&'+document.forms[theFormID].elements[i].id+'='+selectedValues;
						//alert(document.forms[theFormID].elements[i].id+' = '+selectedValues);
					}
				}
				
				if (document.forms[theFormID].elements[i].type == 'checkbox' && document.forms[theFormID].elements[i].checked == true) {
					if(encodeURIComponent(document.forms[theFormID].elements[i].value) != '') {
						params +=  '&'+document.forms[theFormID].elements[i].id+'='+encodeURIComponent(document.forms[theFormID].elements[i].value);
						//alert (document.forms[theFormID].elements[i].id+' - '+document.forms[theFormID].elements[i].value);
					}
				}
			}
			ajaxRequest.open("POST", theScript, true);
			ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			ajaxRequest.setRequestHeader("Content-length", params.length);
			ajaxRequest.setRequestHeader("Connection", "close");

			//alert('PARAMS: '+params);
			ajaxRequest.send(params);
	}
}

/* SLEEP FUNCTION */
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
} 

/* WINDOW RE-LOADER FOR NAVIGATOR */
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);


/* NEW BROWSER WINDOW */
function newWindow(newLink, winName, h, w, scrollbars, giveFocus)
{
	var Focus = (giveFocus == null) ? true : giveFocus;
	var sbars = (scrollbars == null) ? "yes" : scrollbars;
  var left = (screen.width/2) - w/2;
  var top = (screen.height/2) - h/2;
  var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+sbars+',resizable=yes,copyhistory=no,width='+w+',height='+h+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
  var w = window.open(newLink, winName, styleStr);
	if (Focus) w.focus();
	return false;
}


/* SHOW / HIDE SCRIPT */
function DispDispHide ( disp1, disp2, hide )
{
    if (hide) hide.style.display = 'none';
    if (disp1) disp1.style.display = 'inline';
    if (disp2) disp2.style.display = 'inline';
}

function DispHideHide ( disp, hide1, hide2 )
{
    if (hide1) hide1.style.display = 'none';
    if (hide2) hide2.style.display = 'none';
    if (disp) disp.style.display = 'inline';
}

/* FIND OBJECT */
function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

/* IMAGE SWAP */
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

/* PRE-LOAD IMAGES */
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

/* GET THE INDEX OF ANY FORM BY NAME */
function formIndex(formName) {
	for(i=0;i<document.forms.length;i++) { 
		if (document.forms[i].name==formName) 
			return i;
	} 
}

/* STRING TRIMMER */
function strTrim (str) {
	var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
	for (var i = 0; i < str.length; i++) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(i);
			break;
		}
	}
	for (i = str.length - 1; i >= 0; i--) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

