/*******************************
     COMMON JAVASCRIPT FUNCTIONS
     DON'T PUT ANYTHING IN HERE THAT ISN'T GENERIC FOR ALL SITES

ToDo: Clean up non-generic functions

*********************************/

/*******************************
     AJAX Initialization
*********************************/

/* Create a new XMLHttpRequest object to talk to the Web server */
var send = false;
var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  if (http_request.overrideMimeType) {
       	// set type accordingly to anticipated content type
        //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
  }

} catch (e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    xmlHttp = false;
  }
}
@end @*/

if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
}

/*******************************
     AJAX Functions
*********************************/
function callServer(page, number, table) {

	 var url = escape(page) + ".php";
	 if (number) { url += "?id=" + number; }
	 if (table) { url += "&table=" + table; }
	 	
	  // Open a connection to the server
	  xmlHttp.open("GET", url, true);
	
	  // Setup a function for the server to run when it's done
	  xmlHttp.onreadystatechange = updatePage;
	  
	  if (document.getElementById("serverstatus")){
		  document.getElementById("serverstatus").innerHTML = 'status: loading..';
	  }
	  // Send the request
	 xmlHttp.send(null);
}

function updatePage() {
	
  	if (xmlHttp.readyState == 4) { //if state of request is completed
		if (xmlHttp.status == 200) { //if status is ok
			 if (document.getElementById("serverstatus")){
				 document.getElementById("serverstatus").innerHTML = 'status: done';
			 }
			var response = xmlHttp.responseText;
			//alert("response received" + response);
			if (response == "authenticated"){ //do something else for authentication callback
				document.getElementById("loginbox").innerHTML = "<span id=\"success\">logged in</span>";	
			}else if (response == "notauthenticated"){ //do something else for authentication callback
				document.getElementById("serverstatus").innerHTML = "login incorrect";	
			} else {
				document.getElementById("main").innerHTML = response;
			}
		} else {
			//alert("status is " + xmlHttp.status);
		}
  	}
}

function updateSubmit(url, formname, user) {

    var fields = new Array();

	if(formname){
      var form = formname;
    }
	if(user){
      var username = user;
    }
	
	if (username == "guest") {
		document.getElementById("main").innerHTML = "Sorry, but the Guest account is not authorized to update or add records"; 
		exit;
	}
	
	//loop through form elements and retrieve field NAMEs and Values
	for (var x = 0; x < eval("document."+form+".elements.length"); x++){
		// join them into a string.
		eval("fields.push(document."+form+
		".elements[x].name+'='+document."
		+form+".elements[x].value)");
	}

	var post = fields.join('&'); //create string from elements in fields array

	// Open a connection to the server
	xmlHttp.open("POST", url, true);
	 if (document.getElementById("serverstatus")){
		 document.getElementById("serverstatus").innerHTML = 'status: saving..';
	 }
	
	// Setup a function for the server to run when it's done
	xmlHttp.onreadystatechange = updatePage;
	
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	// Send the request
	xmlHttp.send(post);
	
	send = true;
}

function jumpTo(formname, menuname, urlstring){

	if (formname && menuname) {
		var form = formname;
		var menu = menuname;
		var selected = eval("document." + form + "." + menu + ".selectedIndex");
		var value = eval("document." + form + "." + menu + "." + "options[selected].value");
	}
	
	if (urlstring) {var url = "" + urlstring + value;}
	//alert(url);
		  // Open a connection to the server
	   if (document.getElementById("serverstatus")){
		   document.getElementById("serverstatus").innerHTML = 'status: loading..';
	   }
	  xmlHttp.open("GET", url, true);
	
	  // Setup a function for the server to run when it's done
	  xmlHttp.onreadystatechange = updatePage;
	  
	  // Send the request
	 xmlHttp.send(null);

}

function changeHidden(id, url, tablename){
	if (id && url && tablename) {
		var url = url;
		var url = url + "?id=" + id + "&table=" +tablename+ "&field=hidden";
	}
	
	 // Open a connection to the server
	  if (document.getElementById("serverstatus")){
		  document.getElementById("serverstatus").innerHTML = 'status: saving..';
	  }
	  xmlHttp.open("GET", url, true);
	
	  // Setup a function for the server to run when it's done
	  xmlHttp.onreadystatechange = updatePage;
	  
	  // Send the request
	 xmlHttp.send(null);
}

function userAuthenticate() {

	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
 
  	// Build the URL to connect to
  	url = "../common/shared/authenticate.php";  
  
	var post = "password=" + escape(password) + "&username=" + username;
	//create string from elements in fields array
	
	// Open a connection to the server
	xmlHttp.open("POST", url, true);
	 if (document.getElementById("serverstatus")){
		 document.getElementById("serverstatus").innerHTML = 'verifying password..';
	 }
	
	// Setup a function for the server to run when it's done
	xmlHttp.onreadystatechange = updatePage;
	
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	// Send the request
	xmlHttp.send(post);
	
	send=true;
}
/*******************************
     BESPOKE Functions
*********************************/

function fixFlash() {
		// script to counter IE update april 2006
		// thanks to microsoft for making this possible!
		/*
		activateActiveX
		---------------
		Purpose:  Dynamically replace any elements that will be affected by the new security feature in IE6/IE7 that requires a user to click certain types of elements to activate them before use.
		
		Usage:  Include this file in the <head></head> section of your html document using the following...
			<script language="JScript" type="text/jscript" src="activateActiveX_onload.js"></script>
		
		
		Since this script is in response to a software patent lawsuit, I feel it necessary to state the following...	
		
		License:
		activateActiveX is Copyright (C) 2006 Jason Baker (therippa AT gmail.com). It is available as open source code from:
		http://therippa.blogspot.com
		
		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 http://www.gnu.org/licenses/gpl.html
		*/
		//Determine browser, we only need this for Internet Explorer
	if (navigator.appName == "Microsoft Internet Explorer") {
		
		//Array of elements to be replaced
		var arrElements = new Array(3);
		arrElements[0] = "object";
		arrElements[1] = "embed";
		arrElements[2] = "applet";
	
		
		//Loop over element types
		for (n = 0; n < arrElements.length; n++) {
		
			//set object for brevity
			replaceObj = document.getElementsByTagName(arrElements[n]);
			
			//loop over element objects returned
			for (i = 0; i < replaceObj.length; i++ ) {
			
				//set parent object for brevity
				parentObj = replaceObj[i].parentNode;
				
				//grab the html inside of the element before removing it from the DOM
				newHTML = parentObj.innerHTML;
				
				//remove element from the DOM
				parentObj.removeChild(replaceObj[i]);
				
				//stick the element right back in, but as a new object
				parentObj.innerHTML = newHTML;
			
				}
			}
		}	
}

function confirmation(url) {
	var answer = confirm("Are you sure you wish to log off?")
	if (answer){
		//alert("url=" + url);
		window.location = url;
		exit;
	}
	else{
		return;
	}
}


function openPage(name)

	{
	//receives a string which is used as first part of url

		var url = "xml/travelogue.php?id="+name;

		window.open(url, name ,"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=640");
	}
	
function windowOpen(page)
//receives a string which is used as first part of url
//open window with specified name (page)
	{

	//create the url
	url = '' + page + ""; 

	//open new window with parameters
	window.open(url,"top","toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=680,height=480");

	}


//function close()
//	{ 
//		this.document.write('<a href="#" onClick="parent.window.close()" title="close window">close</a>');
//	};

function createWindowClose()
	{
	this.document.write('<div id="closebutton"><a href="javascript:window.close()">[close window]</a></div>');
	}


function lastmodified()
	{
		document.write(' ' +document.lastModified+ ' ');
	}



function functionShow (jpglocation)
{
	var img;
	var imgsrc = '' + jpglocation + '';
	
	if (document.getElementById)
	  {
	    	img = document.getElementById('photoimage');
	  }
	
	else if (document.all)
	
	  {
	  	img =  document.all('photoimage');
	  }
	
	img.src = jpglocation;
	
}

function fixCSS(){
	
	if(document.all){
		elements = document.getElementsByTagName("*")
		
		for (i = 0; i < elements.length; i++ ) {
			
			if(elements[i].style.opacity != undefined){
				//alert(elements[i].style.opacity*100);
				elements[i].style.filter = 'alpha(opacity='+(elements[i].style.opacity*100)+')';
				//this.style.filter = 'alpha(opacity=70)'
			}
		}
	}
}

function createExternalLinks(){
	//function to allow target="_blank" emulation in xhtml strict documents
	arrTags = document.getElementsByTagName('a');
	
	for (var i=0; i<arrTags.length; i++) {
		if (arrTags[i].getAttribute("href") && arrTags[i].getAttribute("rel") == "external") {
			arrTags[i].onclick = function(){
				window.open(this.href);
				return false;
			}
		}
	}

}

