/******************************************************
 * DHTML Popups
 *****************************************************/
var crrt = null;
function show(name)
	{
	if (name != crrt) hide(crrt);
	var div = document.getElementById(name);
	if (div)
		{
		div.style.visibility = 'visible';
		crrt = name;
		}
	}

function hide(name)
	{
	var div = document.getElementById(name);
	if (div) 
		{
		div.style.visibility = 'hidden';
		crrt = null;
		}
	}
function showSub(idx,height)
	{
	if (eval("!sub"+idx+"open"))
		{
		var outer = document.getElementById("outer"+idx);
		var inner = document.getElementById("inner"+idx);
		outer.style.height = height+"px";
		inner.style.visibility = 'visible';
		eval("sub"+idx+"open=true;");
		}
	else hideSub(idx);
	}
function hideSub(idx)
	{
	var outer = document.getElementById("outer"+idx);
	var inner = document.getElementById("inner"+idx);
	outer.style.height = "0px";
	inner.style.visibility = 'hidden';
	eval("sub"+idx+"open=false;");
	}

/******************************************************
 * Image Swap Functions
 *****************************************************/
function findObj(n, d)
	{
	var p,i,x;
	if (!d) d=document;
	if ((p=n.indexOf("?"))>0&&parent.frames.length)
		{
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
		}
	if (!(x=d[n])&&d.all) x=d.all[n];
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for (i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
	if (!x && d.getElementById) x=d.getElementById(n);
	return x;
	}

function imageSwap()
	{
	var i,j=0,x,a=imageSwap.arguments;
	document.sr=new Array;
	for (i=0;i<(a.length-2);i+=3)
		{
		if ((x=findObj(a[i]))!=null)
			{
			document.sr[j++]=x;
			if (!x.oSrc) x.oSrc=x.src; x.src=a[i+2];
			}
		}
	}

function imageRestore()
	{
	var i,x,a=document.sr;
	for (i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
	}
	
/******************************************************
 * Validation Constants
 *****************************************************/
// validation constants
var TEXT = 1;
var CHECKBOX = 2;
var RADIO = 3;

/******************************************************
 * Form Validation
 *****************************************************/
function validateForm(params,formname) {
	// true if no items passed
	if (params == null || params.length == 0) return true;
	if (formname == null) formname = "form";
	var form = eval("document."+formname);
	// doesn't validate if it can't find the form
	if (!form) return true;
	var invalidFields = "";
	for (var i = 0; i < params.length; i++) {
		var param = params[i];
		var p_name = param[0];
		var p_type = param[1];
		var p_req = param[2];
		
		// validate fields by field type
		if (p_type == TEXT && p_req) invalidFields += checkText(p_name);
		else if (p_type == CHECKBOX && p_req) invalidFields += checkCheckbox(p_name);
		else if (p_type == RADIO && p_req) invalidFields += checkRadio(p_name);
	}
	
	// return error message
	if (invalidFields.length > 0) {
		alert("The following fields are required:\n"+invalidFields);
		return false;
	} else return true;
	
	// text field validation
	function checkText(n) {
		var field = form[n];
		if (!field) return "";	// valid if field doesn't exist
		if (field.disabled) return "";	// valid if disabled
		if (field.value.length == 0) return "\n - "+field.title;
		return "";
	}
	// checkbox validation
	function checkCheckbox(n) {
		var check = form[n];
		if (!check || !check[0]) return "";		// valid if check doesn't exist
		var hasCheck = false;
		var isDisabled = true;
		for (var j = 0; j < check.length; j++) {
			var chk = check[j];
			if (!chk.disabled) isDisabled = false;
			if (chk.checked) {
				hasCheck = true;
				break;
			}
		}
		if (!isDisabled && !hasCheck) return "\n - "+check[0].title;
		return "";
	}
	// radio button validation
	function checkRadio(n) {
		var radio = form[n];
		if (!radio || !radio[0]) return "";		// valid if radio doesn't exist
		var hasCheck = false;
		var isDisabled = true;
		for (var j = 0; j < radio.length; j++) {
			var rdo = radio[j];
			if (!rdo.disabled) isDisabled = false;
			if (rdo.checked) {
				hasCheck = true;
				break;
			}
		}
		if (!isDisabled && !hasCheck) return "\n - "+radio[0].title;
		return "";
	}
}

/******************************************************
 * Email Validation
 *****************************************************/
var invalidFlds = "";
function chkEmail(obj1, divID)
	{
	var field_value = obj1.value.toLowerCase();
	var invalid = isEmailIllegal(field_value);

	if (invalid) { obj1.style.background = '#E6B8AD'; } //'#fecdcd'; }
	else obj1.style.background = '#FDFDAA';
	
	// style div object
	if (divID != null)
		{
		var div = document.getElementById(divID);
		if (invalid) div.style.visibility = 'visible';
		else div.style.visibility = 'hidden';
		}
	}
function isEmailIllegal(field_value)
	{
	if (field_value.length == 0) return false;
	if (field_value.length < 5) return true;
		
	// reject illegal chars	
	var nmbs = "abcdefghijklmnopqrstuvwxyz0123456789._@-+";	
	var letters = field_value.split("");
	for (var n = 0; n < field_value.length; n++)
		{
		if (nmbs.indexOf(letters[n]) < 0) return true;
		}
	
	// verify 1 and only 1 '@' and that strings on boths sides of it have length > 0
	var parts = field_value.split("@");
	if (parts.length != 2) return true;
	if (parts[0].length == 0 || parts[1].length < 3) return true;
	
	// verify that domain part has at least one '.' and that all substrings around '.' have a length > 0
	var domainParts = parts[1].split(".");
	if (domainParts.length < 2) return true;	
	for (var n = 0; n < domainParts.length; n++)
		{
		if (domainParts[n].length < 1) return true;
		}	
		
	return false;
	}

/******************************************************
 * Confirm Deletion
 *****************************************************/
function confirmDelete()
	{
	return confirm("Are you sure you want to permanently delete this item?");
	}

/******************************************************
 * Window Opener
 *
 * This method opens a new method with the specified
 * size and url.
 *****************************************************/
function openWin(URL, width, height, name)
	{
	var settings = "toolbar=0,location=0,statusbar=0,menubar=0,resizable=1";
	var windowname = (name != null) ? name : 'openwin';
	if (name == null)
		{
		if (URL.indexOf(".aspx") > -1) windowname = URL.substring(0,URL.indexOf(".aspx"));
		while (windowname.indexOf("/") > -1)
			{ windowname = windowname.substring(windowname.indexOf("/")+1); }
		if (windowname.length > 20) windowname = windowname.substring(0,20);
		}

	/** scrollbars */
	settings += ",scrollbars=1";

	/** dimensions for specific pages */
	if (width == null) settings += ",width=450,height=350";
	else settings += ",width="+width+",height="+height;

	window.child = window.open(URL, windowname, settings);
	if (window.opener == null) window.opener = self;
	child.focus();
	}

/******************************************************
 * AJAX functions
 *****************************************************/

/* The following function creates an XMLHttpRequest object... */
function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

/* You can get more specific with version information by using 
	parseInt(navigator.appVersion)
	Which will extract an integer value containing the version 
	of the browser being used.
*/

/* The variable http will hold our new XMLHttpRequest object. */
var http = createRequestObject(); 

/* Function called to get the data list */
function getData(page){
	/* Create the request. The first argument to the open function is the method (POST/GET),
		and the second argument is the url... 
		document contains references to all items on the page
		We can reference document.form_category_select.select_category_select and we will
		be referencing the dropdown list. The selectedIndex property will give us the 
		index of the selected item. 
	*/
	http.open('get', page);
	/* Define a function to call once a response has been received. This will be our
		handleData function that we define below. */
	http.onreadystatechange = handleData; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}

/* Function called to handle the list that was returned from the internal_request.html file.. */
function handleData(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the data <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		document.getElementById('div_data').innerHTML = response;
	}
}

function getDataNow(page)
	{
	var cont = document.getElementById('container');
	cont.style.background='url(images/gray.png)';
	cont.style.width = '100%';
	cont.style.height = '100%';
	cont.style.overflow = 'auto';
	var dat = document.getElementById('div_data');
	dat.style.visibility = 'visible';
	dat.style.height = '425px';
	dat.style.width = '375px';
	getData(page);
	}
function closeData()
	{
	var cont = document.getElementById('container');
	cont.style.background='';
	cont.style.width = '0';
	cont.style.height = '0';
	cont.style.overflow = 'hidden';
	var dat = document.getElementById('div_data');
	dat.style.visibility = 'hidden';
	dat.style.height = '0px';
	dat.style.width = '0px';
	dat.innerHTML = 'Connecting...';
	}
