//General Purpose image-rollover Function
function rollOver(image_name, image_location)
{
        if (document.images)
        {
                document [image_name].src = image_location;
        }
}



//General Purpose popUp Function
//usage - call from the onClick: return (#, #, 'name', 'url', 1/0, 1/0) where the last 2 parameters must be a 1 or 0.
function popUpWindow (window_width, window_height, window_name, window_url, _resizeable, _scrollbars )
{
	var options="resizable="+_resizeable+",scrollbars="+_scrollbars+",width="+window_width+",height="+window_height+"";
	popupWin=window.open(window_url, window_name, options);
	return false;
}



//Full Window (all the browser bells and whistles) popUp Function
//usage - call from the onClick: return (#, #, 'name', 'url', 1/0, 1/0) where the last 2 parameters must be a 1 or 0.
function popUpFullWindow (window_width, window_height, window_name, window_url)
{
	var options="resizable=1,scrollbars=1,toolbar=1,location=1,menubar=1,status=1,directories=1,width="+window_width+",height="+window_height+"";
	popupWin=window.open(window_url, window_name, options);
	return false;
}


//Function to popup the little scriptable calendar
function popUpCalendar(field_name)
{
	var calendar_url = "/calendar_popup.aspx?field_name=" + field_name;
	var random_variable = popUpWindow(220, 200, "calendar_popup", calendar_url, 0, 0);
	return false;
}


//Handy email validation function
function checkEmail(checkString)
{  
    var newstr = ""; 
    var at = false;
    var dot = false;

    if (checkString.indexOf("@") != -1)
    {
      at = true;
    } 
    else if (checkString.indexOf(".") != -1)
    {
      dot = true;
    }
    for (var i = 0; i < checkString.length; i++)
    {
        ch = checkString.substring(i, i + 1)
        if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || (ch == "@") || (ch == ".") || (ch == "_") || (ch == "-") || (ch >= "0" && ch <= "9"))
        {
            newstr += ch;
                if (ch == "@")
                {
                    at=true;
                }
                if (ch == ".")
                {
                    dot=true;
                }
        }
    }
    if ((at == true) && (dot == true))
    {
        return true;
    }
    else
    {
        //alert ("Sorry, the email address you\nentered is not in the correct\nformat.");
        return false;
    }
}


//This works sort of like isNaN but test for letters and numbers - no weird characters or symbols
function isNumersAndLetters(test_string)
{
	var return_boolean;
	var n;
	
	return_boolean = true;
	
	test_string = test_string.toLowerCase();
	for(n = 0; n < test_string.length; n++)
	{
		if(test_string.charCodeAt(n) > 96 && test_string.charCodeAt(n) < 123)
		{
			//its a-z so we are good
			//alert("its a letter");
		}
		else if(test_string.charCodeAt(n) > 47 && test_string.charCodeAt(n) < 58)
		{
			//its 0-9 so we are good
			//alert("its a number");
		}
		else
		{
			//its something other than a letter or a number
			return_boolean = false;
			break;
		}
	}

	return return_boolean
}


//Function to update any class on any stylesheet
function modifyCSSClass(class_name, css_element, css_value)
{
	var rule_descriptor;
	//Insert cheap ie detector here :()
	if(document.all)
	{
		rule_descriptor = "rules";
	}
	else
	{
		rule_descriptor = "cssRules";
	}
	
	for(var n=0; n<document.styleSheets.length; n++)
	{
		for(var p=0; p<document.styleSheets[n][rule_descriptor].length; p++)
		{
			if(document.styleSheets[n][rule_descriptor][p].selectorText.toLowerCase() == class_name.toLowerCase())
			{
				document.styleSheets[n][rule_descriptor][p].style[css_element] = css_value;
			}
		}
	}
}


//To help hide email addresses from scavengers
function writeEmailAddress(user_id, domain_name)
{
	document.write("<a href=\"mailto:"+user_id+"@"+domain_name+"\">"+user_id+"@"+domain_name+"</a>")
}


//AJAX function that turns a string into an xml parseable document
function getXMLDoc(xml_data)
{
	if(window.ActiveXObject)
	{
		//for ie
		var xml_doc=new ActiveXObject("Microsoft.XMLDOM");
		xml_doc.async="false";
		xml_doc.loadXML(xml_data);
	}
	else
	{
		//for Mozilla, Firefox, Opera, etc.
		var parser=new DOMParser();
		var xml_doc=parser.parseFromString(xml_data,"text/xml");
	}
	
	//documentElement always represents the root node so just return the root object
	var return_object = xml_doc.documentElement;
	
	return return_object;
}


//AJAX function that actually retrieves the xml from a specified url
function getXMLHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch(e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				return null;
			}
		}
	}
	return xmlHttp;
}



	//This function uses prebuilt javascript arrays to fill in 2 select boxes (assigned/unassigned)
	//this only works because arrays are passed by refrence in javascript
	function setSuperBoxes(value_array, text_array, assigned_array, assigned_select_id, unassigned_select_id)
	{
		var n;
		var number_assigned;
		
		document.getElementById(assigned_select_id).options.length = 0;
		document.getElementById(unassigned_select_id).options.length = 0;
		
		for(n = 0; n < value_array.length; n++)
		{
			if(assigned_array[n] == true)
			{
				//the user is assigned to this task
				document.getElementById(assigned_select_id).options.length++;
				number_assigned = document.getElementById(assigned_select_id).options.length;
				
				document.getElementById(assigned_select_id).options[number_assigned - 1].value = value_array[n];
				document.getElementById(assigned_select_id).options[number_assigned - 1].text = text_array[n];
			}
			else
			{
				//the user is not assigned to this task
				document.getElementById(unassigned_select_id).options.length++;
				number_assigned = document.getElementById(unassigned_select_id).options.length;
				
				document.getElementById(unassigned_select_id).options[number_assigned - 1].value = value_array[n];
				document.getElementById(unassigned_select_id).options[number_assigned - 1].text = text_array[n];
			}
		}
	}

	//This function just changes the assignment flag on the selected values in the assigned_array
	//it then calls the setSuperBoxes function to update the screen display
	function add2SuperBox(value_array, text_array, assigned_array, assigned_select_id, unassigned_select_id)
	{
		var n = 0;
		var p = 0;
		
		//never traverse the select box always travers via the arrays otherwise you won't know which array element to update
		//(without searching anyway :)
		for (n = 0; n < value_array.length; n++)
		{
			if(assigned_array[n] == false)
			{
				//this user is an entry in the unassigned box so test if for selection
				if(document.getElementById(unassigned_select_id).options[p].selected)
				{
					assigned_array[n] = true;
				}
				p = p + 1;
			}
		}
		setSuperBoxes(value_array, text_array, assigned_array, assigned_select_id, unassigned_select_id);
		return false;
	}

	//This function just changes the assignment flag on the selected values in the assigned_array
	//it then calls the setSuperBoxes function to update the screen display
	function removeFromSuperBox(value_array, text_array, assigned_array, assigned_select_id, unassigned_select_id)
	{
		var n = 0;
		var p = 0;
		
		//never traverse the select box always travers via the arrays otherwise you won't know which array element to update
		//(without searching anyway :)
		for (n = 0; n < value_array.length; n++)
		{
			if(assigned_array[n])
			{
				//this user is an entry in the assigned box so test if for selection
				if(document.getElementById(assigned_select_id).options[p].selected)
				{
					assigned_array[n] = false;
				}
				p = p + 1;
			}
		}
		setSuperBoxes(value_array, text_array, assigned_array, assigned_select_id, unassigned_select_id);
		return false;
	}