// JavaScript Document

Function.prototype.bind = function(obj) {
  var method = this,
   temp = function() {
    return method.apply(obj, arguments);
   };
 
  return temp;
 } 
 
function dynRegion(url, target){
  function update(parameters) {
	if(typeof(parameters)=='undefined')
		parameters='';
    this.lastParams = parameters;
    this.http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
         this.http_request = new XMLHttpRequest();
         if (this.http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            this.http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
     if (!this.http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
     }
      
      this.http_request.onreadystatechange = alertContents.bind(this);
      this.http_request.open('POST', this.url, true);
      this.http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	  this.http_request.setRequestHeader("Content-length", parameters.length);
	  this.http_request.setRequestHeader("Connection", "close");
      this.http_request.send(parameters);
   }

   function alertContents() {
      if (this.http_request.readyState == 4) {
         if (this.http_request.status == 200) {
            //alert(http_request.responseText);
            result = this.http_request.responseText;
            document.getElementById(this.target).innerHTML = result;
			if(this.onloadevent!=""){
			  eval(this.onloadevent);
			}
         } else if(this.http_request.status) {
            alert('There was a problem with the request. Request Status:' + this.http_request.status);
         }
      }
   }
   
   function setOnLoad(onloadevent){
	   this.onloadevent=onloadevent;
   }
   
   function submit_form(formname){
	   params=this.lastParams;
	   targetElement = document.getElementById(this.target);
	   childForms = targetElement.getElementsByTagName("form")
		for (i=0; i<childForms.length; i++)
		if((childForms[i].name==formname) || (childForms[i].id==formname)){
	   		targetForm = childForms[i];
	   		for (j=0; j<targetForm.elements.length; j++){
				if(targetForm.elements[j].type == 'checkbox' || targetForm.elements[j].type == 'radio'){
					if(targetForm.elements[j].checked){
						params += "&" + targetForm.elements[j].name;
						params += "=" + escape(targetForm.elements[j].value);
					}
				}else{
					params += "&" + targetForm.elements[j].name;
					params += "=" + escape(targetForm.elements[j].value);
				}
			}
	   }
	   this.update(params);
   }
   	this.lastParams="";
	this.target=target;
	this.url = url;
	this.http_request=false;
    this.update=update;
    this.alertContents = alertContents;
	this.onloadevent="";
	this.setOnLoad=setOnLoad;
	this.submit_form = submit_form;
	
} 




function editRegion(name, editorType){
	var param = "action=edit&name=" + name;
	region = new dynRegion("/include/forms/editRegion.php", name);
	param += '&editorType=' + editorType;
	region.update(param);
}
function editPage(name, id){
	var param = "action=edit&name=" + name + "&pageID=" + id;
	region = new dynRegion("/include/forms/editPage.php", name);
	region.update(param);
}
function editSubpage(name){
	var param = "action=edit&name=" + name;
	region = new dynRegion("/include/forms/editSubpage.php", name);
	region.update(param);
}
function editnews(name, id){
	var param = "action=edit&name=" + name + "&newsID=" + id;
	region = new dynRegion("/include/forms/editNews.php", name);
	region.update(param);
}

function updateRegion(name, editorType, instanceName){
	var newVal = "";
	var param = "action=update&name=" + name;
	param += '&editorType=' + editorType;
	if( editorType != 0 ) FCKeditorAPI.GetInstance(instanceName).UpdateLinkedField();
	newVal = document.getElementById("fnField_" + name).value;
	param += "&value=" + escape(newVal);
	region = new dynRegion("/include/forms/editRegion.php", name);
	region.update(param);	
}
function updatePage(name, id){
	var newVal = "";
	var param = "action=update&name=" + name + "&pageID=" + id;
	newVal = document.getElementById("fnField_" + name).value;
	param += "&value=" + escape(newVal);
	region = new dynRegion("/include/forms/editPage.php", name);
	region.update(param);	
}
function updateSubpage(name){
	var newVal = "";
	var param = "action=update&name=" + name;
	newVal = document.getElementById("fnField_" + name).value;
	param += "&value=" + escape(newVal);
	region = new dynRegion("/include/forms/editSubpage.php", name);
	region.update(param);	
}
function updateNews(name, id){
	var newVal = "";
	var param = "action=update&name=" + name + "&newsID=" + id;
	newVal = document.getElementById("fnField_" + name).value;
	param += "&value=" + escape(newVal);
	region = new dynRegion("/include/forms/editNews.php", name);
	region.update(param);	
}

function restoreRegion(name, editorType){
	var param = "action=display&name=" + name;
	param += '&editorType=' + editorType;
	region = new dynRegion("/include/forms/editRegion.php", name);
	region.update(param);
}
function restorePage(name, id){
	var param = "action=display&name=" + name + "&pageID=" + id;
	region = new dynRegion("/include/forms/editPage.php", name);
	region.update(param);
}
function restoreSubpage(name){
	var param = "action=display&name=" + name;
	region = new dynRegion("/include/forms/editSubpage.php", name);
	region.update(param);
}
function restoreNews(name, id){
	var param = "action=display&name=" + name + "&newsID=" + id;
	region = new dynRegion("/include/forms/editNews.php", name);
	region.update(param);
}

function addFck(name,newtheme){
	switch(newtheme){
		case 'simple':
			settings = simpleSettings;
			break;
		case 'advanced':
			settings = advancedSettings;
	}
}
//
//	Fix this set in future to have image# in the originating call, rather than using wrapper
//
function updateImage(whichOne, url, info){
	if(url.substr(-3)=="swf"|| url.substr(-3)=="SWF"){
		alert("When updating a flash element, the flash movie will not be updated until changes are saved.");
	}else{
		img = document.getElementById(whichOne);
		img.src = url;
		img.style.visibility='visible';
		obj=document.getElementById(whichOne+'_flash');
		if(obj!=null && obj!="undefined"){
			obj.style.visibility='hidden';
			obj.style.display='none';
			obj.style.height='0px';
		}
	}
}
function updateImage1(url, info){
	updateImage( "FN_page_image1", url, info );
}
function updateImage2(url, info){
	updateImage( "FN_page_image2", url, info );
}
function updateImage3(url, info){
	updateImage( "FN_page_image3", url, info );
}
