// JavaScript Document:  Library (JS functions common to all apps)

/*Global vars*/
var ajaxaction= "";
var menustate= 'on';
var openlist= '';
var keepfading= 'yes';

//RGB Hex values denoting fully visible text, fully invisible text, and current text visibility
var invisible_r= 255;
var invisible_g= 255;
var invisible_b= 204;

var visible_r= 102;
var visible_g= 102;
var visible_b= 102;

var current_r= 102;
var current_g= 102;
var current_b= 102;
 
function elementFadeIn(eid) {
	
	if (keepfading== 'yes')
		{
			if (current_r > visible_r || current_g > visible_g || current_b > visible_b) 
				{ 
					
					if (current_r > visible_r)
						{
							current_r -= 11; // increase darkness
						}
					if (current_g > visible_g)
						{
							current_g -= 11;
						}
					if (current_b > visible_b)
						{
							current_b -= 11;
						}
						
					//alert("eid is " + eid);
					document.getElementById(eid).style.color= "rgb("+current_r+","+current_g+","+current_b+")";
					setTimeout("elementFadeIn('" + eid + "')",20); 
				 }
			else
				{
					setTimeout("elementFadeOut('" + eid + "')",300);
				}
		}
	else
		{
			//Reset keep fading
			keepfading= 'yes';
		}
} //end of function


function elementFadeOut(eid) {
	
	if (keepfading== 'yes')
		{			
			if (current_r < invisible_r || current_g < invisible_g || current_b < invisible_b) 
				{ 
					if (current_r < invisible_r)
						{
							current_r += 11; // increase darkness
						}
					if (current_g < invisible_g)
						{
							current_g += 11;
						}
					if (current_b < invisible_b)
						{
							current_b += 11;
						}
					
					//alert("eid is " + eid);
					document.getElementById(eid).style.color= "rgb("+current_r+","+current_g+","+current_b+")";
					setTimeout("elementFadeOut('" + eid +"')",20);
				 }
			else
				{
					setTimeout("elementFadeIn('" + eid + "')",300);
				}
		}
	else
		{
			//Reset keep fading
			keepfading= 'yes';
		}
} //end of function



function jsChangeHref(id,href) {
	/*Action usage:  To change the href value of a hyperlink
	id:  the id of the hyperlink
	href:  the new href value */
	//Get object identified by id
	target_object= document.getElementById(id);
	
	target_object.href= href;
	
} //end of function


function jsChangeNodeClass(id,newclass) {
	//Action usage:  Any listener.
	//id:  the id of the page element
	//newclass:  the name of the new class.  Must be in the form of "CSSattribute_value" (examples:  "color_red", "font-weight_bold")
	
	//Get object identified by id
	target_object= document.getElementById(id);
	//Get object's current classes
	old_value= target_object.className;
	
	//Define variable to denote if newclass is replacing an existing class or is being added
	var replacing= 'no';
	
	//split newclass into style and value
	newclass_style= newclass.split("_");
	
	//Split old_value by spaces
	class_array= old_value.split(" ");
	for (var i= 0; i < class_array.length; i++)
		{
			class_data= class_array[i].split("_");
			if (class_data[0]== newclass_style[0])
				{
					replacing= 'yes';
					class_data[1]= newclass_style[1];
					class_array[i]= class_data[0] + "_" + class_data[1];
				}
		}
	
	if (replacing== 'no')
		{
			new_value= old_value + " " + newclass;
		}
	else
		{
			replace_value= '';
			for (var i= 0; i < class_array.length; i++)
				{
					replace_value= replace_value + class_array[i] + " ";
				}
			new_value= replace_value;
		}
		
	//This works in IE and Firefox:  thisitem.className= 'color_green';
	target_object.className= new_value;
	
} //end of function



function jsTextAreaCharCount(textarea,count_element,count_element_nodetype) {
	//Action usage:  onKeyDown action of a textarea.
	//textarea:  the object reference to the textarea you want to perform the character count on
	//count_element:  id of the page element that will receive the character count
	//count_element_nodetype:  whether the count will be written to the textnode of the target element ('textnode') or directly to the element's value ('node')
	
	var count= textarea.value.length;
	
	
	if (count_element_nodetype== 'node')
		{
			target_element= document.getElementById(count_element);
			target_element.value= count;
		}
	else if (count_element_nodetype== 'textnode')
		{
			jsChangeNodeText(count_element,count);	
		}

} //end of function

function jsChangeNodeText(id,text) {

	//Get element
	//alert("in jsChangeNodeText");
	node_element= document.getElementById(id);
	
	//Update the text node within the element, forking depending on browser
	if (typeof node_element.innerText != "undefined")
		{
			//IE browser, so use innerText to update
			node_element.innerText= text;
		}
	else
		{
			//Use childNodes reference
			node_element.childNodes[0].nodeValue= text;
		}
	
}  //end of function

function jsREValidation(thevalue,validation) {
	//Action usage:  as part of validation script.
	//thevalue:  the value of the object/item
	//validation:  the regular expression evaluation you want to use
	
	//alert("in jsREValidation");
	
	switch (validation) {
		case "positive_integer":
			if (/\d+/.test(thevalue)== false)
				{
					return false;
				}
			break;
		
		case "integer":
			if (/(^-?\d\d*$)/.test(thevalue)==false)
				{
					return false;
				}
			break;
	} //end of switch
	
} //end of function


function jsAJAXStart(action,element) {
	//The action is recorded to the global variable so jsAJAXEnd knows what to do with the AJAX_response XML
	ajaxaction= action;
	
	//Determine if element is a string (the value itself) or an object you need to retrieve the value from
	if (typeof(element)== 'object')
		{
			var url_parameter= element.value;
		}	
	else
		{
			var url_parameter= element;
		}
	
	//Action also determines the URL the request is geared towards
	/*switch (action)
		{
			case "dupname":
				jsAJAXLoad('https://dev.webservices.umd.edu/lcpas/ajax/ajax_dupname.cfm?uname=' + url_parameter);
				break;
				
			case "dupname_admin":
				//Also need user_id
				uid_obj= document.getElementById('uid');
				jsAJAXLoad('https://dev.webservices.umd.edu/lcpas/ajax/ajax_dupname_edit.cfm?uname=' + url_parameter + '&uid=' + uid_obj.value);
				break;
				
			case "mstate":
				 jsAJAXLoad('https://dev.webservices.umd.edu/lcpas/ajax/ajax_menu.cfm?mstate=' + url_parameter);
				break;
				
		} //end of switch
	*/
} //end of function


function jsAJAXLoad(url) {
	// branch for native XMLHttpRequest object
	if ( window.XMLHttpRequest ) 
		{
			req = new XMLHttpRequest();
			//the call to jsAJAXEnd CANNOT have parans ()
			req.onreadystatechange = jsAJAXEnd;
			req.open("GET", url, true);
			req.send(null);
		// branch for IE/Windows ActiveX version
		} 
	else if ( window.ActiveXObject ) 
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
			if ( req ) 
				{
					//the call to jsAJAXEnd CANNOT have parans ()
					req.onreadystatechange = jsAJAXEnd;
					req.open("GET", url, true);
					req.send();
				}
		}
} //end of function


function jsAJAXEnd() {
		
	if (req.readyState == 4 ) 
		{		
			if (req.status == 200) 
				{		
					//Get XML-formatted response from the server
					var response = req.responseXML;
					//alert("status 200");
					//alert("ajaxaction is " + ajaxaction);
					//Use ajaxaction value to determine where to send the data
					
					/*switch (ajaxaction)
						{
								
						} //end of switch
					*/
				}  //end of req.status= 200
			
		}  //end of req.readyState= 4
	
} //end of function


function jsOpenNewWindow(url,title) {
	var newwin= window.open(url,'title','width=765,height=411,left=100,top=100,scrollbars="yes"');
} //end of function

function jsValidateEmail(mail) {
	
	var oneAt = 0;
	var onePeriod= 0;
	var str= mail;
	for (var i=0; i< str.length; i++)
		{
			oneChar = str.charCodeAt(i);
			if (oneChar== 64)
				{
					oneAt= oneAt+1;
				}
			if (oneChar== 46)
				{
					onePeriod= onePeriod+1;
				}
		}
	if (oneAt != 1 || onePeriod== 0)
		{
			return false;
		}
	else
		{
			return true;
		}

} //end of function


//From www.quirksmode.org

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
} //end of function


function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
} //end of function

function eraseCookie(name)
{
	createCookie(name,"",-1);
} //end of function

/*End of QuirksMode scripts */

function toggleItemDropCookie(id) {

	//alert("toggleItemDropCookie called");
	list_id= id + '_list';
	
	//Determine if item is in the openlist
	var in_list= 'no';
	
	list_array= openlist.split("|");
	for (var j= 0; j <  list_array.length; j++)
		{
			if (list_id== list_array[j])
				{
					in_list= 'yes';
					//Remove from list right now
					list_array[j]= '';
				}
		}
	
	if (in_list== 'yes')
		{
			jsChangeNodeClass(list_id,'list_hidden');
			//Build new openlist
			new_openlist= '';
			for (var k= 0; k < list_array.length; k++)
				{
					 if (list_array[k] != '')
					 	{
							new_openlist= new_openlist + list_array[k] + "|";
						}
				}
			openlist= new_openlist;
		}
	else
		{
			//Add to list
			if (openlist== '')
				{
					openlist= list_id;
				}
			else
				{
					openlist= openlist + "|" + list_id;
				}
			//Change class
			jsChangeNodeClass(list_id,'list_show');
		}
			
	//alert("openlist at end of toggledrop is "  + openlist);		
	/*document.cookie="mvisible=" + openlist;*/
	//9/29/06: Added check for empty string in addition to null
	if (readCookie('mvisible')== null || readCookie('mvisible')== '') 
		{
			
			eraseCookie('mvisible');
			//alert("eraseCookie actived");
			createCookie('mvisible',openlist,0);
		}
	else
		{
			//alert("cookies number " + document.cookie.length);
			//11/4/06:  Instead of writing just mvisible:openlist pair, use CreateCookie again
			//document.cookie= 'mvisible=' + openlist;
			createCookie('mvisible',openlist,0);
		}
} //end of function


function resetMenuCookie(savedlist) {
	if (savedlist != ' ' && savedlist != null)
		{
			//Handle menu items first
			saved_list= savedlist.split("|");
			openlist= '';
			for (m= 0; m < saved_list.length; m++)
				{
						//9/29/06 Only record if saved_list[m] is not empty ('')	
						if (saved_list[m] != '')
							{
						
								if (openlist== '')
									{
										openlist= saved_list[m];
									}
								else
									{
										openlist= openlist + "|" + saved_list[m];
									}
							//Change class
							jsChangeNodeClass(saved_list[m],'list_show');
						}
				}
			
		}
	
} //End of function


