
//returns the GET argument when passed the key, will return false if not found
function getARG(ARGkey) {
	var URLarray = location.href.split('?');
	var ARGarray = URLarray[1].split('&');
	
	for(i=0;i<ARGarray.length;i++) {
		KeyVal = ARGarray[i].split('=');
		if(KeyVal[0]==ARGkey) return KeyVal[1];
	}
	return false;
}

function setCookie(cookieName,cookieVal) {
	nextyear = new Date();
	nextyear.setFullYear(nextyear.getFullYear() + 1);
	document.cookie = cookieName+"="+cookieVal+"; expires="+nextyear.toGMTString()+"; path=/";
}

function getCookie(cookieName) {
	cookieArr = document.cookie.split(';');
	for(i=0;i<cookieArr.length;i++) {
		KeyVal = cookieArr[i].split('=');
		if((KeyVal[0]==cookieName)||(KeyVal[0]==" "+cookieName)) return KeyVal[1];
	}
	return false; //if not found
}


function checkText(theField,theForm) {
	var theString = document.forms[theForm].elements[theField].value;
	var theLength = document.forms[theForm].elements[theField].value.length;
	for (i = 0; i < theLength; i++) {
		s = theString.charCodeAt(i);
		if (((s >= 65) && (s <= 90)) || (s == 32) || ((s >= 97) && (s <= 122))) var isValid = 'yes';
		else {
			var isValid = 'no';			
			break;
			}		
		}
	if (isValid == 'no') {
		//alert('You have used numeric, special or accented characters.\nPlease re-enter value using alphabetic characters only.');
		document.forms[theForm].elements[theField].value = '';
		document.forms[theForm].elements[theField].focus();
		return false;
		}
	else return true;		
	}
	
// following function disallows accented or special entries into fields

function checkNumText(theField,theForm) {
	var theString = document.forms[theForm].elements[theField].value;
	var theLength = document.forms[theForm].elements[theField].value.length;
	for (i = 0; i < theLength; i++) {
		s = theString.charCodeAt(i);
		if (((s >= 65) && (s <= 90)) || ((s >= 48) && (s <= 57)) || (s == 32) || ((s >= 97) && (s <= 122))) var isValid = 'yes';
		else {
			var isValid = 'no';			
			break;
			}		
		}
	if (isValid == 'no') {
		//alert('You have used special or accented characters.\nPlease re-enter value using alphanumeric characters only.');
		document.forms[theForm].elements[theField].value = '';
		document.forms[theForm].elements[theField].focus();
		return false;
		}	
	else return true;	
	}		
		
// following function only allow numeric entry into certain fields

function checkNum(theField,theForm) {
	var s = document.forms[theForm].elements[theField].value;
	if(s=='.') ;
	else if (isNaN(s)) {
		document.forms[theForm].elements[theField].focus();
		if (isNaN(parseInt(s))) 
			document.forms[theForm].elements[theField].value = '';
		else	
			document.forms[theForm].elements[theField].value = parseInt(s);
		}
	}

//following function creates new popup window

function goPopupExt(myUrl,myWidth,myHeight, scrollbars) {
	//now = new Date();
	popup = window.open(myUrl,'thenewWin','top=50,left=50,toolbar=no,location=no,directories=no,status=no,resizable=yes,copyhistory=no,width=' + myWidth + ',height=' + myHeight + ',scrollbars=' + scrollbars);
	popup.focus();
	}

function goPopupExt(myUrl,myWidth,myHeight, scrollbars,winName) {
	//now = new Date();
	popup = window.open(myUrl,winName,'top=50,left=50,toolbar=no,location=no,directories=no,status=no,resizable=yes,copyhistory=no,width=' + myWidth + ',height=' + myHeight + ',scrollbars=' + scrollbars);
	popup.focus();
	}
	

function swapImages(imName,imSrc) {
	document.images[imName].src = imSrc;
}



//Ajax requesting
var http_request = false;

//retFunction is the function switch set prior to making an ajax request
var retFunction;

function makeRequest(url) {
  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;
  }

  //append rand so url doesn't cache
  if(url.search(/\?/)!=-1) var use_url = url + "&rand="+Math.random();
  else var use_url = url + "?rand="+Math.random();

  http_request.onreadystatechange = alertContents;
  http_request.open('GET', use_url, true);
  http_request.send(null);
}

function alertContents() {
  if (http_request.readyState == 4) {
     if (http_request.status == 200) {
        //alert(http_request.responseText);
        result = http_request.responseText;
        //processResults function is unique to page
        processResults(result);          
     } else {
        alert('There was a problem with the request.');
     }
  }
}

//dummy function, overwritten in page
function processResults(result) {
	
}


//DETERMINE X,Y COORDINATES of mouse

var xpos, ypos;


// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

jQuery(document).ready(function(){
   $(document).mousemove(function(e){
	   tempX = e.pageX;
	   tempY = e.pageY;
   }); 
})


//get scroll offset
var xOffset = 0;
var yOffset = 0;

function setScrollOffset() {
	if(navigator.userAgent.search(/MSIE/)==-1) {
		return;
	}
	if (self.pageYOffset) // all except Explorer
	{
		xOffset = self.pageXOffset;
		yOffset = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		xOffset = document.documentElement.scrollLeft;
		yOffset = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		xOffset = document.body.scrollLeft;
		yOffset = document.body.scrollTop;
	}
}



function copyToClipboard(s)
{
	if( window.clipboardData && clipboardData.setData )
	{
		clipboardData.setData("Text", s);
	}
	else
	{
		// You have to sign the code to enable this or allow the action in about:config by changing user_pref("signed.applets.codebase_principal_support", true);
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

		var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
	   if (!clip) return;
	   
	   // create a transferable
	   var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
	   if (!trans) return;
	   
	   // specify the data we wish to handle. Plaintext in this case.
	   trans.addDataFlavor('text/unicode');
	   
	   // To get the data from the transferable we need two new objects
	   var str = new Object();
	   var len = new Object();
	   
	   var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);
	   
	   var copytext=meintext;
	   
	   str.data=copytext;
	   
	   trans.setTransferData("text/unicode",str,copytext.length*[[[[2]]]]);
	   
	   var clipid=Components.interfaces.nsIClipboard;
	   
	   if (!clip) return false;
	   
	   clip.setData(trans,null,clipid.kGlobalClipboard);	   
	}
}


//menu navigation

var subnavitems = new Array();
var subnavitemcounter = 0;
var subnavTimer;

function ShowSubMenu() {
	if(arguments.length>0) clearTimeout(subnavTimer);
	for(i=0;i<subnavitems.length;i++) {
		visibility="hidden";
		$('#nav_'+subnavitems[i]).removeClass('hoverit');
		for(j=0;j<arguments.length;j++) {
			if(arguments[j]==subnavitems[i]) {
				visibility="visible";
				$('#nav_'+arguments[j]).addClass('hoverit');
			}
		}
		document.getElementById('submenu_'+subnavitems[i]).style.visibility=visibility;
	}	
}

function HideSubMenu() {
	subnavTimer = setTimeout("ShowSubMenu()",500);
}

/*jquery extensions*/

/**
 *	iFlip
 *	Author: keta
 *
 *	Simple plugin to switch images.
 *	@ a (str) - starting image
 *	@ b (str) - image to switch to on click.
 */

jQuery.fn.iflip = function( a, b ) {
	
	
		curr = $(this).attr('src');
	
		if( curr == a ) {
			$(this).attr('src', b);
		} else if( curr == b ) {
			$(this).attr('src', a);
		}	
}
