//Basic prototype extentions
//zachCode: this was inspired by prototype, it just symplfies id retrieval
function $v(elementID){return $(elementID).value;}
function $i(elementID){if (MathExt.IsNumeric($v(elementID))== false) return 0; return parseInt($(elementID).value);}
function $f(elementID){if (MathExt.IsNumeric($v(elementID))== false) return 0; return parseFloat($(elementID).value);}

/*-----------\
| Prototypes |
\-----------*/
//array find (since there is no array.find we ahve to make one)
Array.prototype.find = function(searchStr) {
  for (var index = 0; index < this.length; index++){
	if (this[index] == searchStr) return index;
  }
  return -1;
}

 /*-----------------+                                                   
 |MOUSE LOCATION X,Y|                                                   
 +------------------+--------------------------------------------------+
    | This code gets the current x,y location of the mouse on the page.|
    | It stores these values in mouse_x, mouse_y and are stored        |
    | globally for other functions to use.                             |
    +-----------------------------------------------------------------*/

var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMouseXY;
mouse_x = 0; mouse_y = 0;// init x and y
function getMouseXY(e) {
	if (IE) { // grab the x-y pos.s if browser is IE
		mouse_x = event.clientX + document.body.scrollLeft;
		mouse_y = event.clientY + document.body.scrollTop;
	}
	else {  // grab the x-y pos.s if browser is NS
		mouse_x = e.pageX;
		mouse_y = e.pageY;
	}  
	if (mouse_x < 0){mouse_x = 0;}
	if (mouse_y < 0){mouse_y = 0;}
}
openImage = new Image();
openImage.src = 'Images/open.gif';
closeImage = new Image();
closeImage.src = 'Images/close.gif';

var Request = new Object();
Request.Params = Request.QueryString = function(strParamName){
	var strReturn = "";
	var strHref = window.location.href;
	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			if (
				aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return unescape(strReturn);
}


var URL = new Object();
URL.by_id = function(element_id){
	return "&"+element_id+"="+$(element_id).value;
}
URL.by_object_and_tagname = function(tag_object,tag_name){
	url_object_string = "";
	tag_object_list = tag_object.getElementsByTagName(tag_name);
	for(object_each_iter = 0; object_each_iter < tag_object_list.length; object_each_iter++){
		each_tag_object = tag_object_list[object_each_iter];
		if (each_tag_object.id){
			url_object_string += "&"+each_tag_object.id+"="+each_tag_object.value
		}
	}
	return url_object_string;
}

var MathExt = new Object();

// Is numeric this function basically test to see if a given string is a number or not.
MathExt.IsNumeric =  function(sText,isInt)
{
	var ValidChars = (isInt == true)? "0123456789.":"0123456789.";
	var IsNumber=true;
	var Char;
	if (sText == "") return false;
	for (i = 0; i < sText.length && IsNumber == true; i++) 
		{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
			{
			IsNumber = false;
			}
		}
	return IsNumber;
}



var DOM = new Object();
/*[WIDTH HEIGHT FUNCTIONS]{
	These functions return the available height and width of the browser cross browser compatible
}*/ 
DOM.width = function(){
	return (document.all)? document.body.clientWidth: innerWidth;
}
DOM.height = function(){
	return (document.all)? document.body.clientHeight: innerHeight;
}
 /*-------------------\                                           
 | Split and Populate |                                           
 \--------------------/-----------------------------------------\
  |This is a simple function that parses through a repsonse text|
  |and splits it according to ids and adds the specific text    |
  |to that id.  Essentially allowing for multiple updates with  |
  \------------------------------------------------------------*/
DOM.split_and_populate = function(array_of_ids, response){
	for (var each_id = 0; each_id < array_of_ids.length; each_id++){
		text_to_populate  =  response.split("<!-- "+array_of_ids[each_id]+" -->");
		//test for the actual existence updated data it exists then update the corrisponding div
		if(text_to_populate.length != 1) $(array_of_ids[each_id]).innerHTML = text_to_populate[1];
		//otherwise call the on_no_update_data()
		else DOM.on_no_update_data(array_of_ids[each_id]);
	}
}
//Split and Populate related functions {
	//this is the default response to no data.
	DOM.defalut_no_update_data_response = function(element_id){
		$(element_id).innerHTML = "<span style='color:red; font-weight:bold;'>No Data</span>";
	}
	//this function is ment to overridden, but is set up to the defalut response
	DOM.on_no_update_data = function(element_id){
		DOM.defalut_no_update_data_response(element_id);
	}
//}
var initialize;
DOM.ajax_request = function(ajax_url,onsuccess_func){
	Effect.Appear('ajax_load');//turn on the ajax image.
	new Ajax.Request(ajax_url,//make ajax request
	{
		method:'get',
		onSuccess: function(transport){//if successful then call success function
			var response = transport.responseText || "no response text";
			
			onsuccess_func(response);
			//this line reinitializes the lightbox.
				//the lightbox is the code that allows for the darking of 
				// the screen and displaying of another page.
					initialize();
				
			
			Effect.Fade("ajax_load");//turn off ajax image
		},
		onFailure: function(){DOM.msgbox("Your session has expired.  Please <a href='Default.aspx'>start over again</a>.",'Session'); Effect.Fade("ajax_load");}
		
	});
}		

DOM.show_hide_table_column = function(id) {
	var i = 0;
	while(document.getElementById(id+i) != null){
		obj = document.getElementById(id+i);
		if(obj.style.display=="block" || obj.style.display=="") {
			obj.style.display="none";
		}
		else {
			obj.style.display="";
		}
		i++;
	}
	if (DOM.on_show_hide_table_column != null) DOM.on_show_hide_table_column(id);
}

DOM.preload_images = function(image_paths){
	for (var each = 0; each < image_paths.length; each++){
		new Image().src = image_paths[each];
	}
}

DOM.msgbox = function(message, title){
	//$('msgbox').style.top = window.screen.height/2 - 75;
	//$('msgbox').style.left = window.screen.width/2 - 75;
	$('msgbox_title').innerHTML = (title != null)? title: "Alert";
	$('msgbox_message').innerHTML = message;
	$('msgbox').style.display = "";
}


//INFO PANEL SETTINGS
var seconds_to_show_helpmsg = 0;
var seconds_to_delay_before_showing_helpmsg_on_mouse_over = 0;
/*[DOM INFO PANEL]{
	This function will show the info panel with some message
}*/

DOM.helpmsg = function(message){
	//set message\
	$('helpmsg_message').innerHTML = message;
	//move info panel to mouse x,y
	$('helpmsg').style.left = (mouse_x+10)+"px"; //move 10 over form current mouse postion to keep from mouseout flickering
	$('helpmsg').style.top = ((mouse_y < DOM.height()-250)?mouse_y:DOM.height()-225)+"px";
	$('helpmsg').style.display = "";//show the info panel
}
DOM.helpmsg_clear = function(){
	$('helpmsg').style.display = "none"; 
	clearInterval(DOM.helpmsg_interval);
	DOM.helpmsg_interval = null;
}
DOM.helpmsg_onmouseout = function(){
	if (DOM.helpmsg_interval) return;
	DOM.helpmsg_interval = window.setInterval(DOM.helpmsg_clear,.5);
}

DOM.add_helpmsg_to_imgs = function(){
	var all_images = document.getElementsByTagName('img');
	for (var i=0; i < all_images.length; i++){
		img = all_images[i];
		// if image is a help image then
		if (img.alt == "help") {
			img.name = img.title;
			img.title = "";
			img.onmouseover = function(){DOM.helpmsg(this.name);};
			//img.onclick = function(){DOM.helpmsg(this.title)};
			img.onmouseout = DOM.helpmsg_clear;
		}
	}	
}
/*|JS_SESSION|----------------------------------\
| These next set of functions will essentially	|
| take care of sesssion updating will the proper|
| session setting backend for asp. It will use	|
| the url as a transport method and willl look	|
| like &session_<session_var_name>=<value> and	|
| the asp backend will detect these session		|
| vars and set the session when the url is sent	|
| via ajax.	For ease of session updaing via JS	|
\----------------------------------------------*/


var Session = new Object(); //this set of functions trys to mimic ASP.net closly for ease of use.

/*|Session.to_url|------------------------------\
| This essentially will dump session vars to a 	|
| url encoded string for transport to the		|
| backend.										|
\----------------------------------------------*/
Session.to_url = function(){
	var return_string = "&session=true";
	for (each in Session){
		if (each != "to_url"){ // these will what not include for example Session.to_url will be skipped
			// it is assumed that any session variable is in correct url format meaning no special chars or spaces
			return_string += "&"+__session__each+"="+escape(Session[each]); // escape coverts the value to url safe string
		}
	}
	return return_string;
}



/*-----------------------\ 
|/----------------------\|
||ASP Specific Overrides||
|\----------------------/|
\-----------------------*/


function toggleBox(element){
	// this is the content of the box
	content = element.parentNode.getElementsByTagName('div')[1];
	// this is the image up/down image in the box header
	image = element.getElementsByTagName('img')[0];
	input = element.getElementsByTagName('input')[0];
	if (content.style.display == "none") {
		input.value = '1';
		Effect.BlindDown(content); // this is the code that makes the the windows glide closed and open
		image.src = "\\Images\\close.gif";// this changes the image down image to the up image
		//this is a function that sets status of a box so it can not be closed until it is all the way open
		content.interval = window.setInterval(function(){content.style.display="block";clearInterval(content.interval);},1000);}
	//only close if open
	else if (content.style.display == "block") {
		input.value = '0';
		content.style.display = "";
		Effect.BlindUp(content);
		image.src = "\\Images\\open.gif";

	}
}
/*[DSS NAV]{
	This function essentially allows changes between different
	pages and will be responsable for any validation or changes
	prior to to page switches.
}*/
function dss_nav(page_to_navigate_to){
	switch(page_to_navigate_to){
		case 1: parent.location = "InputAjax.aspx?guid="+Request.Params("guid");
	
	}
	
}