/* Simple AJAX Code-Kit (SACK) v1.6.1 */
/* ©2005 Gregory Wild-Smith */
/* www.twilightuniverse.com */
/* Software licenced under a modified X11 licence,
   see documentation or authors website for more details */

function sack(file) {
	this.xmlhttp = null;

	this.resetData = function() {
		this.method = "POST";
  		this.queryStringSeparator = "?";
		this.argumentSeparator = "&";
		this.URLString = "";
		this.encodeURIString = true;
  		this.execute = false;
  		this.element = null;
		this.elementObj = null;
		this.requestFile = file;
		this.vars = new Object();
		this.responseStatus = new Array(2);
  	};

	this.resetFunctions = function() {
  		this.onLoading = function() { };
  		this.onLoaded = function() { };
  		this.onInteractive = function() { };
  		this.onCompletion = function() { };
  		this.onError = function() { };
		this.onFail = function() { };
	};

	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};

	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}

		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.failed = true;
			}
		}
	};

	this.setVar = function(name, value){
		this.vars[name] = Array(value, false);
	};

	this.encVar = function(name, value, returnvars) {
		if (true == returnvars) {
			return Array(encodeURIComponent(name), encodeURIComponent(value));
		} else {
			this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
		}
	}

	this.processURLString = function(string, encode) {
		encoded = encodeURIComponent(this.argumentSeparator);
		regexp = new RegExp(this.argumentSeparator + "|" + encoded);
		varArray = string.split(regexp);
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split("=");
			if (true == encode){
				this.encVar(urlVars[0], urlVars[1]);
			} else {
				this.setVar(urlVars[0], urlVars[1]);
			}
		}
	}

	this.createURLString = function(urlstring) {
		if (this.encodeURIString && this.URLString.length) {
			this.processURLString(this.URLString, true);
		}

		if (urlstring) {
			if (this.URLString.length) {
				this.URLString += this.argumentSeparator + urlstring;
			} else {
				this.URLString = urlstring;
			}
		}

		// prevents caching of URLString
		this.setVar("rndval", new Date().getTime());

		urlstringtemp = new Array();
		for (key in this.vars) {
			if (false == this.vars[key][1] && true == this.encodeURIString) {
				encoded = this.encVar(key, this.vars[key][0], true);
				delete this.vars[key];
				this.vars[encoded[0]] = Array(encoded[1], true);
				key = encoded[0];
			}

			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
		}
		if (urlstring){
			this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
		} else {
			this.URLString += urlstringtemp.join(this.argumentSeparator);
		}
	}

	this.runResponse = function() {
		eval(this.response);
	}

	this.runAJAX = function(urlstring) {
		if (this.failed) {
			this.onFail();
		} else {
			this.createURLString(urlstring);
			if (this.element) {
				this.elementObj = document.getElementById(this.element);
			}
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
					totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					this.xmlhttp.open(this.method, this.requestFile, true);
					try {
						this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
					} catch (e) { }
				}

				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState) {
						case 1:
							self.onLoading();
							break;
						case 2:
							self.onLoaded();
							break;

						case 3:
							self.onInteractive();
							break;
						case 4:
							self.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;

							if (self.execute) {
								self.runResponse();
							}

							if (self.elementObj) {
								elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input"
								|| elemNodeName == "select"
								|| elemNodeName == "option"
								|| elemNodeName == "textarea") {
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							if (self.responseStatus[0] == "200") {
								self.onCompletion();
							} else {
								self.onError();
							}

							self.URLString = "";
							/* These lines were added by Alf Magne Kalleland ref. info on the sack home page. It prevents memory leakage in IE */

							delete self.xmlhttp['onreadystatechange'];
							self.xmlhttp=null;
							self.responseStatus=null;
							self.response=null;
							self.responseXML=null;
														
							break;
					}
				};

				this.xmlhttp.send(this.URLString);
			}
		}
	};

	this.reset();
	this.createAJAX();
}

// here we define global variable
var ajaxdestination="";

function getdata(what,where) { // get data from source (what)
 try {
   xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
  		new ActiveXObject("Microsoft.XMLHTTP");
 }
 catch (e) { /* do nothing */ }

 document.getElementById(where).innerHTML ="<center><img src='loading.gif'></center>";
// we are defining the destination DIV id, must be stored in global variable (ajaxdestination)
 ajaxdestination=where;
 xmlhttp.onreadystatechange = triggered; // when request finished, call the function to put result to destination DIV
 xmlhttp.open("GET", what);
 xmlhttp.send(null);
  return false;
}

function triggered() { // put data returned by requested URL to selected DIV
  if (xmlhttp.readyState == 4) if (xmlhttp.status == 200) 
    document.getElementById(ajaxdestination).innerHTML =xmlhttp.responseText;
}



function grayOut(vis, options) {

  // Pass true to gray out screen, false to ungray

  // options are optional.  This is a JSON object with the following (optional) properties

  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 

  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out

  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code

  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});

  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear

  // in any order.  Pass only the properties you need to set.

  var options = options || {}; 

  var zindex = options.zindex || 40;

  var opacity = options.opacity || 80;

  var opaque = (opacity / 100);

  var bgcolor = options.bgcolor || '#000000';

  var dark=document.getElementById('darkenScreenObject');

  if (!dark) {

    // The dark layer doesn't exist, it's never been created.  So we'll

    // create it here and apply some basic styles.

    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917

    var tbody = document.getElementsByTagName("body")[0];

    var tnode = document.createElement('div');           // Create the layer.

        tnode.style.position='absolute';                 // Position absolutely

        tnode.style.top='0px';                           // In the top

        tnode.style.left='0px';                          // Left corner of the page

        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            

        tnode.style.display='none';                      // Start out Hidden

        tnode.id='darkenScreenObject';                   // Name it so we can find it later

    tbody.appendChild(tnode);                            // Add it to the web page

    dark=document.getElementById('darkenScreenObject');  // Get the object.

  }

  if (vis) {

    // Calculate the page width and height 

    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {

        var pageWidth = document.body.scrollWidth+'px';

        var pageHeight = document.body.scrollHeight+200+'px';

    } else if( document.body.offsetWidth ) {

      var pageWidth = document.body.offsetWidth+'px';

      var pageHeight = document.body.offsetHeight+'px';

    } else {

       var pageWidth='100%';

       var pageHeight='100%';

    }   

    //set the shader to cover the entire page and make it visible.

    dark.style.opacity=opaque;                      

    dark.style.MozOpacity=opaque;                   

    dark.style.filter='alpha(opacity='+opacity+')'; 

    dark.style.zIndex=zindex;        

    dark.style.backgroundColor=bgcolor;  

    dark.style.width= pageWidth;

    dark.style.height= pageHeight;

    dark.style.display='block';                          

  } else {

     dark.style.display='none';

  }

}


   var http_request = false;
   function makeRequest(url, parameters, mode) {
      http_request = false;


      if (window.XMLHttpRequest) { // Mozilla, Safari,...

         http_request = new XMLHttpRequest();

         if (http_request.overrideMimeType) {

         	// set type accordingly to anticipated content type

            //http_request.overrideMimeType('text/xml');

            http_request.overrideMimeType('text/html');

         }

      } else if (window.ActiveXObject) { // IE

         try {

            http_request = new ActiveXObject("Msxml2.XMLHTTP");

         } catch (e) {

            try {

               http_request = new ActiveXObject("Microsoft.XMLHTTP");

            } catch (e) {}

         }

      }

      if (!http_request) {

         alert('Cannot create XMLHTTP instance');

         return false;

      }

	  

	  if(http_request.readyState == 3) {

		  document.write('loading');

	  }

	  

	  http_request.onreadystatechange = function(){ alertContents(mode) }; 

      //http_request.onreadystatechange = alertContents;

      http_request.open('GET', url + parameters, true);

      http_request.send(null);

   }



   function alertContents(mode) { 

	  

	  if (http_request.readyState == 4) {

         if (http_request.status == 200) {

			 //alert(http_request.responseText);

			result = http_request.responseText;

            document.getElementById(mode).innerHTML = result;

         } else {

            alert(mode);

         }

      }

   }  

   

      function get(obj, mode) {

	  

	   var getstr = "?";

	   getstr += "comment=" + encodeURI( document.getElementById("comment").value  + "&");

	   

      for (i=0; i<obj.childNodes.length; i++) {

         if (obj.childNodes[i].tagName == "INPUT") {

            if (obj.childNodes[i].type == "text") {

               getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&;";

            }

			if (obj.childNodes[i].type == "hidden") {

               getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";

            }

			if (obj.childNodes[i].type == "image") {

               getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";

			   var mode = obj.childNodes[i].value;

            }

			

            if (obj.childNodes[i].type == "checkbox") {

               if (obj.childNodes[i].checked) {

                  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";

               } else {

                  getstr += obj.childNodes[i].name + "=&";

               }

            }

            if (obj.childNodes[i].type == "radio") {

               if (obj.childNodes[i].checked) {

                  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";

               }

            }

			

         }   

         if (obj.childNodes[i].tagName == "SELECT") {

            var sel = obj.childNodes[i];

            getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";

         }

		

         

      }



      makeRequest('./ajax_scripts/' + mode + '2.php', getstr, mode);

   }




/*

Block multiple form submission script- By JavaScriptKit.com

Visit http://www.javascriptkit.com for this script and more

This notice must stay intact for use

*/



//Enter error message to display if submit button has been pressed multiple times below.

//Delete below line if you don't want a message displayed:



var formerrormsg="You\'ve attempted to submit the form multiple times.\n Please reload page if you need to resubmit form."



function checksubmit(submitbtn){

submitbtn.form.submit()

checksubmit=blocksubmit

return false

}



function blocksubmit(){

if (typeof formerrormsg!="undefined")

// alert(formerrormsg)

return false

}


    
        

/*AjaxCall is an object that will be used to handle AJAX requests*/
function AjaxCall(){
	this.url = "";//url to call
	this.formMethod = "POST";//use the POST or GET method, default to post.
	this.postString = "";//variables to send by the POST method.

	//create the request object that is appropriate for the user's browser		
	if (window.XMLHttpRequest) {
		this.req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		this.req = new ActiveXObject("Microsoft.XMLHTTP");
	}


	this.resultFunc = null;//the function to call with the resultant information
	this.loadUrl = loadUrl;//the function that makes the call
}

/*used with AjaxCall*/
function loadUrl(){
	if(window.XMLHttpRequest){
		this.req.resultFunc = this.resultFunc;//this must be passed along to ajPrc like this.
		this.req.onreadystatechange = ajPrc;
		this.req.open(this.formMethod, this.url, true);
		this.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

		//if there are POST items send them, otherwise send nothing.
		if(this.postString == "") this.req.send(null);
		else this.req.send(this.postString);
	} else if (window.ActiveXObject) {
		if (this.req) {
			this.req.resultFunc = this.resultFunc;//this must be passed along to ajPrc like this.
			this.req.onreadystatechange = ajPrc;
			this.req.open(this.formMethod, this.url, true);
			this.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			//if there are POST items send them, otherwise send nothing.
			if(this.postString == "") this.req.send(null);
			else this.req.send(this.postString);
		}
	}
}


/*used with AjaxCall*/
function ajPrc(){

	//handle the request, note the use of 'this', because of how it is called by loadUrl's onreadystatechange it is a child of this.req

	if(this.readyState == 4){//4 is OK status
		if(this.status == 200){
			this.resultFunc();//upon success call the function specified.
		}else if (this.status != 0){
			alert(this.status);//this could stand to be a more detailed error.
		}
	}
}

/*called by ajPrc by AjaxCall instances*/
function closeMovie(){
	document.getElementById('displaymovie').innerHTML = this.responseText; 
}

closeswf = new AjaxCall();
closeswf.url = "display/fullscreen_open.php";
closeswf.resultFunc = closeMovie;


function ShowPopup(hoveritem)
{
	hp = document.getElementById("displayfs");
	// Set position of hover-over popup
	hp.style.top = hoveritem.offsetTop + 18;
	hp.style.left = hoveritem.offsetLeft + 20;

	// Set popup to visible
	hp.style.visibility = "Visible";
}

function HidePopup()
{

	hp = document.getElementById("displaymovie");
	hp.style.visibility = "Hidden";
}
