/* 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)
{
  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=yes,resizable=yes,copyhistory=no,width='+w+',height='+h+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
  window.open(newLink, winName, styleStr);
}


/* 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];}}
}



/* NIFTY CORNERS CODE */

/* Nifty Corners Cube - rounded corners with CSS and Javascript
Copyright 2006 Alessandro Fulciniti (a.fulciniti@html.it)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

var niftyOk=(document.getElementById && document.createElement && Array.prototype.push);
var niftyCss=false;

String.prototype.find=function(what){
return(this.indexOf(what)>=0 ? true : false);
}

var oldonload=window.onload;
if(typeof(NiftyLoad)!='function') NiftyLoad=function(){};
if(typeof(oldonload)=='function')
    window.onload=function(){oldonload();AddCss();NiftyLoad()};
else window.onload=function(){AddCss();NiftyLoad()};

function AddCss(){
niftyCss=true;
var l=CreateEl("link");
l.setAttribute("type","text/css");
l.setAttribute("rel","stylesheet");
l.setAttribute("href","stylesheets/niftyCorners.css");
l.setAttribute("media","screen");
document.getElementsByTagName("head")[0].appendChild(l);
}

function Nifty(selector,options){
if(niftyOk==false) return;
if(niftyCss==false) AddCss();
var i,v=selector.split(","),h=0;
if(options==null) options="";
if(options.find("fixed-height"))
    h=getElementsBySelector(v[0])[0].offsetHeight;
for(i=0;i<v.length;i++)
    Rounded(v[i],options);
if(options.find("height")) SameHeight(selector,h);
}

function Rounded(selector,options){
var i,top="",bottom="",v=new Array();
if(options!=""){
    options=options.replace("left","tl bl");
    options=options.replace("right","tr br");
    options=options.replace("top","tr tl");
    options=options.replace("bottom","br bl");
    options=options.replace("transparent","alias");
    if(options.find("tl")){
        top="both";
        if(!options.find("tr")) top="left";
        }
    else if(options.find("tr")) top="right";
    if(options.find("bl")){
        bottom="both";
        if(!options.find("br")) bottom="left";
        }
    else if(options.find("br")) bottom="right";
    }
if(top=="" && bottom=="" && !options.find("none")){top="both";bottom="both";}
v=getElementsBySelector(selector);
for(i=0;i<v.length;i++){
    FixIE(v[i]);
    if(top!="") AddTop(v[i],top,options);
    if(bottom!="") AddBottom(v[i],bottom,options);
    }
}

function AddTop(el,side,options){
var d=CreateEl("b"),lim=4,border="",p,i,btype="r",bk,color;
d.style.marginLeft="-"+getPadding(el,"Left")+"px";
d.style.marginRight="-"+getPadding(el,"Right")+"px";
if(options.find("alias") || (color=getBk(el))=="transparent"){
    color="transparent";bk="transparent"; border=getParentBk(el);btype="t";
    }
else{
    bk=getParentBk(el); border=Mix(color,bk);
    }
d.style.background=bk;
d.className="niftycorners";
p=getPadding(el,"Top");
if(options.find("small")){
    d.style.marginBottom=(p-2)+"px";
    btype+="s"; lim=2;
    }
else if(options.find("big")){
    d.style.marginBottom=(p-10)+"px";
    btype+="b"; lim=8;
    }
else d.style.marginBottom=(p-5)+"px";
for(i=1;i<=lim;i++)
    d.appendChild(CreateStrip(i,side,color,border,btype));
el.style.paddingTop="0";
el.insertBefore(d,el.firstChild);
}

function AddBottom(el,side,options){
var d=CreateEl("b"),lim=4,border="",p,i,btype="r",bk,color;
d.style.marginLeft="-"+getPadding(el,"Left")+"px";
d.style.marginRight="-"+getPadding(el,"Right")+"px";
if(options.find("alias") || (color=getBk(el))=="transparent"){
    color="transparent";bk="transparent"; border=getParentBk(el);btype="t";
    }
else{
    bk=getParentBk(el); border=Mix(color,bk);
    }
d.style.background=bk;
d.className="niftycorners";
p=getPadding(el,"Bottom");
if(options.find("small")){
    d.style.marginTop=(p-2)+"px";
    btype+="s"; lim=2;
    }
else if(options.find("big")){
    d.style.marginTop=(p-10)+"px";
    btype+="b"; lim=8;
    }
else d.style.marginTop=(p-5)+"px";
for(i=lim;i>0;i--)
    d.appendChild(CreateStrip(i,side,color,border,btype));
el.style.paddingBottom=0;
el.appendChild(d);
}

function CreateStrip(index,side,color,border,btype){
var x=CreateEl("b");
x.className=btype+index;
x.style.backgroundColor=color;
x.style.borderColor=border;
if(side=="left"){
    x.style.borderRightWidth="0";
    x.style.marginRight="0";
    }
else if(side=="right"){
    x.style.borderLeftWidth="0";
    x.style.marginLeft="0";
    }
return(x);
}

function CreateEl(x){
return(document.createElement(x));
}

function FixIE(el){
if(el.currentStyle!=null && el.currentStyle.hasLayout!=null && el.currentStyle.hasLayout==false)
    el.style.display="inline-block";
}

function SameHeight(selector,maxh){
var i,v=selector.split(","),t,j,els=[],gap;
for(i=0;i<v.length;i++){
    t=getElementsBySelector(v[i]);
    els=els.concat(t);
    }
for(i=0;i<els.length;i++){
    if(els[i].offsetHeight>maxh) maxh=els[i].offsetHeight;
    els[i].style.height="auto";
    }
for(i=0;i<els.length;i++){
    gap=maxh-els[i].offsetHeight;
    if(gap>0){
        t=CreateEl("b");t.className="niftyfill";t.style.height=gap+"px";
        nc=els[i].lastChild;
        if(nc.className=="niftycorners")
            els[i].insertBefore(t,nc);
        else els[i].appendChild(t);
        }
    }
}

function getElementsBySelector(selector){
var i,j,selid="",selclass="",tag=selector,tag2="",v2,k,f,a,s=[],objlist=[],c;
if(selector.find("#")){ //id selector like "tag#id"
    if(selector.find(" ")){  //descendant selector like "tag#id tag"
        s=selector.split(" ");
        var fs=s[0].split("#");
        if(fs.length==1) return(objlist);
        f=document.getElementById(fs[1]);
        if(f){
            v=f.getElementsByTagName(s[1]);
            for(i=0;i<v.length;i++) objlist.push(v[i]);
            }
        return(objlist);
        }
    else{
        s=selector.split("#");
        tag=s[0];
        selid=s[1];
        if(selid!=""){
            f=document.getElementById(selid);
            if(f) objlist.push(f);
            return(objlist);
            }
        }
    }
if(selector.find(".")){      //class selector like "tag.class"
    s=selector.split(".");
    tag=s[0];
    selclass=s[1];
    if(selclass.find(" ")){   //descendant selector like tag1.classname tag2
        s=selclass.split(" ");
        selclass=s[0];
        tag2=s[1];
        }
    }
var v=document.getElementsByTagName(tag);  // tag selector like "tag"
if(selclass==""){
    for(i=0;i<v.length;i++) objlist.push(v[i]);
    return(objlist);
    }
for(i=0;i<v.length;i++){
    c=v[i].className.split(" ");
    for(j=0;j<c.length;j++){
        if(c[j]==selclass){
            if(tag2=="") objlist.push(v[i]);
            else{
                v2=v[i].getElementsByTagName(tag2);
                for(k=0;k<v2.length;k++) objlist.push(v2[k]);
                }
            }
        }
    }
return(objlist);
}

function getParentBk(x){
var el=x.parentNode,c;
while(el.tagName.toUpperCase()!="HTML" && (c=getBk(el))=="transparent")
    el=el.parentNode;
if(c=="transparent") c="#FFFFFF";
return(c);
}

function getBk(x){
var c=getStyleProp(x,"backgroundColor");
if(c==null || c=="transparent" || c.find("rgba(0, 0, 0, 0)"))
    return("transparent");
if(c.find("rgb")) c=rgb2hex(c);
return(c);
}

function getPadding(x,side){
var p=getStyleProp(x,"padding"+side);
if(p==null || !p.find("px")) return(0);
return(parseInt(p));
}

function getStyleProp(x,prop){
if(x.currentStyle)
    return(x.currentStyle[prop]);
if(document.defaultView.getComputedStyle)
    return(document.defaultView.getComputedStyle(x,'')[prop]);
return(null);
}

function rgb2hex(value){
var hex="",v,h,i;
var regexp=/([0-9]+)[, ]+([0-9]+)[, ]+([0-9]+)/;
var h=regexp.exec(value);
for(i=1;i<4;i++){
    v=parseInt(h[i]).toString(16);
    if(v.length==1) hex+="0"+v;
    else hex+=v;
    }
return("#"+hex);
}

function Mix(c1,c2){
var i,step1,step2,x,y,r=new Array(3);
if(c1.length==4)step1=1;
else step1=2;
if(c2.length==4) step2=1;
else step2=2;
for(i=0;i<3;i++){
    x=parseInt(c1.substr(1+step1*i,step1),16);
    if(step1==1) x=16*x+x;
    y=parseInt(c2.substr(1+step2*i,step2),16);
    if(step2==1) y=16*y+y;
    r[i]=Math.floor((x*50+y*50)/100);
    r[i]=r[i].toString(16);
    if(r[i].length==1) r[i]="0"+r[i];
    }
return("#"+r[0]+r[1]+r[2]);
}
