// **************************************
// * js_lib.js		
// * Version 3.0			
// * a library of usefull functions 	
// * for writting javascript		
// *					
// * Emil Diego				
// * ediego@miami.edu
// **************************************


//*
//* PRELOAD MENU BUTTONS
//* 
menu1_on = new Image();
menu1_on.src = "images2/main-nav-1-over.gif";
menu2_on = new Image();
menu2_on.src = "images2/main-nav-2-over.gif";
menu3_on = new Image();
menu3_on.src = "images2/main-nav-3-over.gif";
menu4_on = new Image();
menu4_on.src = "images2/main-nav-4-over.gif";
menu5_on = new Image();
menu5_on.src = "images2/main-nav-5-over.gif";
menu6_on = new Image();
menu6_on.src = "images2/main-nav-6-over.gif";
menu7_on = new Image();
menu7_on.src = "images2/main-nav-7-over.gif";


menu1_off = new Image();
menu1_off.src = "images2/main-nav-1.gif";
menu2_off = new Image();
menu2_off.src = "images2/main-nav-2.gif";
menu3_off = new Image();
menu3_off.src = "images2/main-nav-3.gif";
menu4_off = new Image();
menu4_off.src = "images2/main-nav-4.gif";
menu5_off = new Image();
menu5_off.src = "images2/main-nav-5.gif";
menu6_off = new Image();
menu6_off.src = "images2/main-nav-6.gif";
menu7_off = new Image();
menu7_off.src = "images2/main-nav-7.gif";


var isNav, isIE;
var isMac, isWin;

isNav = false;
isIE  = false;
isMac = false;
isWin = false;

// Determine which browser we are using
if (parseInt(navigator.appVersion) >= 4) {
	if (navigator.appName == "Netscape") {
		isNav = true;
	} else {
		isIE = true;
	}
}

// Determine which os the browser is running on
if ((navigator.platform).substr(0, 3) == "Mac") {
	isMac = true;
} else {
	isWin = true;
}


//*******************************
//* Image Manipulation
//*******************************
//*
//* rdFindImage
//* Find an image given the name and return it.
//* If we are running in netscape we have to check all the layers, since the image arrays are seperated for each layer
function rdFindImage(sName) {
	var index;
	var temp_image = null;
	var num_layers = 0;
	
	
	
	if (isNav) {
		var	i = 0;
		if (document.layers == null)
			num_layer = 0;
		else
			num_layers = document.layers.length;
		if (num_layers > 0) {
			// search the main document first.  then search all layers
			for (index = 0; index < document.images.length; index++) {
				if (document.images[index].name == sName) {
					return document.images[index];
				}
			}
			
			// we need to search all layers for the images.
			for (index = 0; index < num_layers; index++) {
				for (i = 0; i < document.layers[index].document.images.length; i++) {	
					if (document.layers[index].document.images[i].name == sName) {
						return document.layers[index].document.images[i];
					}
				}
			}
			
		} else {
			// just search the document images collection.
			for (index = 0; index < document.images.length; index++) {
				if (document.images[index].name == sName) {
					return document.images[index];
				}
			}
		}
		
	} else {
		for (index = 0; index < document.images.length; index++) {
			if (document.images[index].name == sName) {
				return document.images[index];
			}	
		}
	}
	return null;
}

//*
//* rdButtonOn
//* Used in mouse over events to highlight a selected image button.
//* this function assumes that an images exists called <imgName>_on.
function ButtonOn(imgName) {
	imgOn = eval(imgName + '_on.src');
	
	if (isNav) {
		var temp_obj = rdFindImage(imgName);
		temp_obj.src = imgOn;

	} else {
		document.images [imgName].src = imgOn;
	}
}

//*
//* rdButtonOff
//* Used in mouse over events to turn off a selected image button.
//* this function assumes that an images exists called <imgName>_off.
function ButtonOff(imgName) {
	
	imgOff = eval(imgName + '_off.src');
	
	if (isNav) {
		var temp_obj = rdFindImage(imgName);
		temp_obj.src = imgOff;
	} else {
		document [imgName].src = imgOff;
	}
}





//*******************************
//* Style Sheet Manipulation	*
//*******************************
//*
//* rdFindLayer
//* find a layer or style given the name and return it
function rdFindLayer(sName) {
	var index;
	var tmp_obj = null;
	
	if (isNav) {
		var num_layers = 0;
		if (document.layers == null) {
			//* Must be netscape 6 or greater.  Doesn't support layers anymore
			tmp_obj = document.getElementById(sName);
  			if (tmp_obj != null) 
  				return tmp_obj;
		} else {
			//* Netscape version less than 6
			num_layers = document.layers.length;
			for (index = 0; index < num_layers; index++) {
				if (document.layers[index].name == sName)
					return document.layers[index];	
				
			}
			
		}
		
		return null;
	} else {
		
		tmp_obj = document.all[sName];
		if (tmp_obj == null) {
			//* Not valid
			return null;
		} else {
			//* valid
			return tmp_obj;
		}
		
		/*
		for (index = 0; index < document.all.length; index++) {
			if (document.all[index].id == sName) {
				return document.all[index];
			}		
		}	
		return null;
		*/
	}
}

//*
//* rdShowLayer
//* find a layer specified by the name and make it visible
function rdShowLayer(sName) {
	var	temp_obj = null;
	
	temp_obj = rdFindLayer(sName);	
	//alert(temp_obj);
	if (temp_obj != null) {
		// we fo und it
		if (isNav) {			
			temp_obj.style.visibility = "visible";
			
		} else {
			//alert(temp_obj.style.visibility);
			temp_obj.style.visibility = "visible";
		}
	} else {
		// we didn't find it
		return false;
	}
	return true;	
}

//*
//* rdHideLayer
//* find a layer specified by the name and make it invisible
function rdHideLayer(sName) {
	var	temp_obj = null;
	
	temp_obj = rdFindLayer(sName);
	if (temp_obj != null) {
		// we fo und it

		if (isNav) {
			temp_obj.visibility = "hide";
			temp_obj.style.visibility = "hidden";
		} else {
			temp_obj.style.visibility = "hidden";
		}
		
		return true;
		
	} else {
		// we didn't find it
		return false;
	}

	return true;	
}



		

//*******************************
//* ANCHORS			*
//*******************************
//*
//* rdGetAnchorElement
//* Returns an anchor with the specified name
function rdGetAnchorElement(sName) {
	var xElem = null;	

	if (isIE) {
		xElem = document.all [sName];
		
		return xElem;
		
	} else {
		var	i = 0;
		var 	index = 0;
		for (index = 0; index < document.layers.length; index++) {
			for (i = 0; i < document.layers[index].document.anchors.length; i++) {
				if (document.layers[index].document.anchors[i].name == sName) {
					return document.layers[index].document.anchors[i];
				}
			}
		}
	}
}

//*
//* rdGetAnchorCount
//* Return the number of anchors in the page
function rdGetAnchorCount() {
	var count = 0;
	if (isIE) {
		count = document.all.length;
	} else {
		count = document.anchors.length;
	}
	
	return count;	
}


//*******************************
//* Misc			*
//*******************************
//*
//* rdSetWindowStatus
//* set the window status bar message.
function rdSetWindowStatus(xWindow, sMsg) {
	
	xWindow.status = sMsg;
	
}

//*
//* rdEmptyLink
//* Used in netscape in the HREF to allow the event handlers.
function rdEmptyLink() {
	
	return false;
}



//*******************************
//* Form Validaters
//*******************************
//*
//* rdVerifyDelete
//*
//* prompt the user to confoirm the delete 
//* before we submit the form.
//*
function rdVerifyDelete( sFrmName )
{
	var xFrmxField	
	alert('Form Name = ' + sFrmName);
	
	//* Get the Form
	xFrm = document.forms[sFrmName];
	if (xFrm == null)
	{
		alert('Unable to access form: ' + sFrmName);
		return;
	}
	
	var answer=confirm("Are you sure you wish to delete?");
	if(answer)
	{
		xFrm.submit();
	}
	return;
}


function confirmDeleteOperation( $sFormName, $sOperation )
	{
		//alert('sFrmName=' + $sFormName + ', sOperation=' + $sOperation);
		
		var	$xFrm = null;
		var $sMessage = "There was an error in the form.\n\n";
		var $bErrors = false;
		
		//* Let's find the form
		$xFrm = document.forms[$sFormName];
		if ($xFrm == null)
		{
			//* There is an error and we can't find the form
			alert('Cant find form ' + $sFormName);
			return;
		}
		
		//* Now lets check to see if we are performing a delete operation
		if ( $sOperation.toUpperCase() == "DELETE" )
		{
			//* We need to promot
			$sMsg = "Are you sure you wish to delete the entry?"
			$bRet = confirm($sMsg);
			if ($bRet != true)
			{
				//* We dont want to delete it
				return;
			}
			
		} 

		//* Submit the form
		$xFrm.submit();		
	}


function validateSearch()
	{
		var	$xFrm = null;
		var $sMessage = "There was an error in the form.\n\n";
		var $bErrors = false;
		
		//* Let's find the form
		$xFrm = document.forms['frmSearch'];
		if ($xFrm == null)
		{
			//* There is an error and we can't find the form
			alert('Cant find form frmSearch');
			return;
		}
		
		//* Now let's check for the fields.
		if ($xFrm.tbKeywords.value == "")
		{
			$sMessage = $sMessage + "\tThe Keywords fields can not be blank.\n";
			$bErrors = true;
		}
		
		//* Now if there are no errors we can submit the form
		//if ($bErrors == true)
		//{
		//	alert($sMessage);
		//	return;
		//}

		//* Submit the form
		$xFrm.submit();		
	}
	
	function validateNewsletter()
	{
		var	$xFrm = null;
		var $sMessage = "There was an error in the form.\n\n";
		var $bErrors = false;
		
		//* Let's find the form
		$xFrm = document.forms['frmNewsletterSignup'];
		if ($xFrm == null)
		{
			//* There is an error and we can't find the form
			return;
		}
		
		//* Now let's check for the email address.
		if ($xFrm.tbEmailAddress.value == "")
		{
			$sMessage = $sMessage + "\tThe Email Address can not be blank.\n";
			$bErrors = true;
		}
		
		//* Now let's check for the state.
		if ($xFrm.cbState.value == "")
		{
			$sMessage = $sMessage + "\tThe State can not be blank.\n";
			$bErrors = true;
		}
		
		//* Now if there are no errors we can submit the form
		if ($bErrors == true)
		{
			alert($sMessage);
			return;
		}

		//* Submit the form
		$xFrm.submit();
	}
	
	//*
	//* validateUserEditCustomerInfo
	//* 
	//* Validate the data on the user | Edit Customer Data form
	//*
	function validateUserEditCustomerInfo()
	{
		var	$xFrm = null;
		var $sMessage = "There was an error in the form.\n\n";
		var $bErrors = false;
		
		//* Let's find the form
		$xFrm = document.forms['frmEditCustomerInfo'];
		if ($xFrm == null)
		{
			//* There is an error and we can't find the form
			return;
		}
		
		//* Now let's check for the email address.
		if ($xFrm.first_name.value == "")
		{
			$sMessage = $sMessage + "\tThe First Name can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.last_name.value == "")
		{
			$sMessage = $sMessage + "\tThe Last Name can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.address1.value == "")
		{
			$sMessage = $sMessage + "\tThe Address can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.city.value == "")
		{
			$sMessage = $sMessage + "\tThe City can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.state.value == "")
		{
			$sMessage = $sMessage + "\tThe State can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.phone1.value == "")
		{
			$sMessage = $sMessage + "\tThe Day Phone can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.email_address.value == "")
		{
			$sMessage = $sMessage + "\tThe Email Address can not be blank.\n";
			$bErrors = true;
		}
		
		
		//* Now if there are no errors we can submit the form
		if ($bErrors == true)
		{
			alert($sMessage);
			return;
		}

		//* Submit the form
		$xFrm.submit();
	}
	
	//*
	//* validateUserEditProfile
	//* 
	//* Validate the data on the user | Edit Profile form
	//*
	function validateUserEditProfile()
	{
		var	$xFrm = null;
		var $sMessage = "There was an error in the form.\n\n";
		var $bErrors = false;
		
		//* Let's find the form
		$xFrm = document.forms['frmEditProfile'];
		if ($xFrm == null)
		{
			//* There is an error and we can't find the form
			return;
		}
		
		//* Now let's check the fields.
		if ($xFrm.profile_name.value == "")
		{
			$sMessage = $sMessage + "\tThe Profile Name can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.description.value == "")
		{
			$sMessage = $sMessage + "\tThe Description can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.category.value == "")
		{
			$sMessage = $sMessage + "\tThe Category can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.address1.value == "")
		{
			$sMessage = $sMessage + "\tThe Address can not be blank.\n";
			$bErrors = true;
		}
	
		if ($xFrm.city.value == "")
		{
			$sMessage = $sMessage + "\tThe City can not be blank.\n";
			$bErrors = true;
		}
			
		if ($xFrm.state.value == "")
		{
			$sMessage = $sMessage + "\tThe State can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.postalcode.value == "")
		{
			$sMessage = $sMessage + "\tThe Postal Code can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.phone1.value == "")
		{
			$sMessage = $sMessage + "\tThe Day Phone can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.email_address.value == "")
		{
			$sMessage = $sMessage + "\tThe Email Address can not be blank.\n";
			$bErrors = true;
		}
		
		
		//* Now if there are no errors we can submit the form
		if ($bErrors == true)
		{
			alert($sMessage);
			return;
		}

		//* Submit the form
		$xFrm.submit();
	}
	
	
	//*
	//* validateUserNewProfileStep1
	//* 
	//* Validate the data on the user | New Profile Step 1 form
	//*
	function validateUserNewProfileStep1()
	{
		var	$xFrm = null;
		var $sMessage = "There was an error in the form.\n\n";
		var $bErrors = false;
		
		//* Let's find the form
		$xFrm = document.forms['frmEditProfile'];
		if ($xFrm == null)
		{
			//* There is an error and we can't find the form
			alert("Unable to find form: frmEditProfile");
			return;
		}
		
		//* Now let's check the fields.
		if ($xFrm.profile_name.value == "")
		{
			$sMessage = $sMessage + "\tThe Profile Name can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.description.value == "")
		{
			$sMessage = $sMessage + "\tThe Description can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.category.value == "")
		{
			$sMessage = $sMessage + "\tThe Category can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.address1.value == "")
		{
			$sMessage = $sMessage + "\tThe Address can not be blank.\n";
			$bErrors = true;
		}
	
		if ($xFrm.city.value == "")
		{
			$sMessage = $sMessage + "\tThe City can not be blank.\n";
			$bErrors = true;
		}
			
		if ($xFrm.state.value == "")
		{
			$sMessage = $sMessage + "\tThe State can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.postalcode.value == "")
		{
			$sMessage = $sMessage + "\tThe Postal Code can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.phone1.value == "")
		{
			$sMessage = $sMessage + "\tThe Day Phone can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.email_address.value == "")
		{
			$sMessage = $sMessage + "\tThe Email Address can not be blank.\n";
			$bErrors = true;
		}
		
		
		//* Now if there are no errors we can submit the form
		if ($bErrors == true)
		{
			alert($sMessage);
			return;
		}

		//* Submit the form
		$xFrm.submit();
	}
	
	
		//*
	//* validateNewRegistration
	//* 
	//* Validate the data on the user | New Registration Form Step 1
	//*
	function validateNewRegistration()
	{
		var	$xFrm = null;
		var $sMessage = "There was an error in the form.\n\n";
		var $bErrors = false;
		
		//* Let's find the form
		$xFrm = document.forms['frmNewRegistration'];
		if ($xFrm == null)
		{
			//* There is an error and we can't find the form
			alert("Unable to find form: frmNewRegistration");
			return;
		}
		
		//* Now let's check the fields.
		if ($xFrm.first_name.value == "")
		{
			$sMessage = $sMessage + "\tThe First Name can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.last_name.value == "")
		{
			$sMessage = $sMessage + "\tThe last_name Name can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.address1.value == "")
		{
			$sMessage = $sMessage + "\tThe Address can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.city.value == "")
		{
			$sMessage = $sMessage + "\tThe City can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.state.value == "")
		{
			$sMessage = $sMessage + "\tThe state can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.postalcode.value == "")
		{
			$sMessage = $sMessage + "\tThe Postal Code can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.phone1.value == "")
		{
			$sMessage = $sMessage + "\tThe Day Phone can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.email_address.value == "")
		{
			$sMessage = $sMessage + "\tThe Email Address can not be blank.\n";
			$bErrors = true;
		}
		
		//* Now if there are no errors we can submit the form
		if ($bErrors == true)
		{
			alert($sMessage);
			return;
		}

		//* Submit the form
		$xFrm.submit();
	}
	
	
	
	//*
	//* frmNewAd
	//* 
	//* Validate the input form for creating a new Ad entry
	//*
	function validatefrmNewAd()
	{
		var	$xFrm = null;
		var $sMessage = "There was an error in the form.\n\n";
		var $bErrors = false;
		
		//* Let's find the form
		$xFrm = document.forms['frmNewAd'];
		if ($xFrm == null)
		{
			//* There is an error and we can't find the form
			alert("Unable to find form: frmNewAd");
			return;
		}
		
		//* Now let's check the fields.
		if ($xFrm.name.value == "")
		{
			$sMessage = $sMessage + "\tThe Ad Name can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.account.value == "")
		{
			$sMessage = $sMessage + "\tThe Account Name can not be blank.\n";
			$bErrors = true;
		}
		
		
		if ($xFrm.url.value == "")
		{
			$sMessage = $sMessage + "\tThe Ad Location can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.destination.value == "")
		{
			$sMessage = $sMessage + "\tThe Ad Destination can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.type.value == "")
		{
			$sMessage = $sMessage + "\tThe Ad Type can not be blank.\n";
			$bErrors = true;
		}
		
		if ($xFrm.dim.value == "")
		{
			$sMessage = $sMessage + "\tThe Ad Dimensions can not be blank.\n";
			$bErrors = true;
		}
		
		//* Now if there are no errors we can submit the form
		if ($bErrors == true)
		{
			alert($sMessage);
			return;
		}
		

		
		//* Now check the category and profile fields
		if ($xFrm.cat_id.value == "" && $xFrm.profile_id.value == "")
		{
			$sMessage = "The category and profile fields can not both be blank\n";
			alert($sMessage);
			return;
		}
		
		if ($xFrm.cat_id.value != "" && $xFrm.profile_id.value != "")
		{
			$sMessage = "The category and profile fields can not be used together.  Please select one or the other.\n";
			alert($sMessage);
			return;
		}

		//* Submit the form
		$xFrm.submit();
	}
	
	
