// For the Topics Menu
function cr_load() {
	sfHover = function() {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
	if (window.attachEvent) window.attachEvent("onload", sfHover);
}

// Updates the Source Selection Settings

function updateSources(sourceName, displaySearchAgain) {

	var sourceBox = document.getElementById( sourceName );
	if( sourceBox.checked )
		setCookie( sourceName, "true" );
	else
		setCookie( sourceName, "false" );
	
	if ( displaySearchAgain ) {
		
		var newSpan = document.createElement( "span" );
		var newText = document.createTextNode( "SEARCH AGAIN >>" );	
		newSpan.appendChild(newText);

		var spanElm = document.getElementById( "searchAgain" );
		var link = document.getElementById( "searchAgainLink" );
		var replaced = link.replaceChild( newSpan, spanElm );
		displaySearchAgain = false;
	}
}

// Hides the emailAddress

function printEmail( title )
{
    enc1= ":G&ntc`;!kgojri<bgpobFetsouohatcuistecu(eik!8";
    enc2 = ":)g8";
	for(i=0;i<enc1.length;++i)
	{
		document.write(String.fromCharCode(6^enc1.charCodeAt(i)));
	}
	document.write(title);
	for(i=0;i<enc2.length;++i)
	{
		document.write(String.fromCharCode(6^enc2.charCodeAt(i)));
	}
}

// General Cookie Setting Funciton
function setCookie(name, value)
{
	var newCookie
	newCookie = name + "=" + escape(value) + "; expires=";
	var d = new Date();
	d.setFullYear( d.getFullYear() + 3 );
	newCookie += d.toGMTString();
	document.cookie = newCookie;
}

function getCookie(name)
{
	var result = null;
	var myCookie = " " + document.cookie + ";";
	var searchName = " " + name + "=";
	var startOfCookie = myCookie.indexOf(searchName);
	var endOfCookie;
	if(startOfCookie != -1)
	{
		startOfCookie += searchName.length;
		endOfCookie = myCookie.indexOf(";", startOfCookie);
		result = unescape(myCookie.substring(startOfCookie, endOfCookie));
	}
	return result;
}

// Add the indicated listing to My Resources
// and adjust the image on the page to reflect this.
function clickMyResources( id ) {
	
	var checkBox = document.getElementById( id );
	var imagePathArray = checkBox.src.split(/\//);
	//alert( "Clicked on check box. ID: " + id + " checked: " + imagePathArray[imagePathArray.length - 1] );

	// If it was checked then uncheck it and remove the resource
	if( imagePathArray[imagePathArray.length - 1] == "myresources.gif" ) {
		removeFromMyResources( id );
		checkBox.src = checkBox.src.replace( "myresources",
						     "addmyresources" );
		checkBox.alt = "Add this to MyResources";
		checkBox.title = "Add this to MyResources";
	}
	else {
		addToMyResources( id );
		checkBox.src = checkBox.src.replace( "addmyresources",
						     "myresources");
		checkBox.alt = "Remove this from MyResources";
		checkBox.title = "Remove this from MyResources";
	}
}

function addToMyResources(id) {

	if( getCookie("cr_myResources") == null ) {
		setCookie("cr_myResources", id + ",");
		alert( "Congratulations!!!\n" +
		       "You have added your first item to the MyResources Page." );
	}
	else {
		var cookieString = "";
		cookieString = getCookie("cr_myResources");
		
		var myResourcesArray = cookieString.split(/,+/);
		var newCookieString = "";
		for( var i = 0; i < myResourcesArray.length; i++ ) {
	     
			if( myResourcesArray[i] != id && myResourcesArray[i] != "undefined")
			    newCookieString += myResourcesArray[i] + ",";
		}
		newCookieString += id + ",";
		setCookie("cr_myResources", newCookieString);
	}
}

function removeFromMyResources(id) {

	var cookieString = "";
	cookieString = getCookie("cr_myResources");

	var myResourcesArray = cookieString.split(/,+/);
	var newCookieString = "";
	for( var i = 0; i < myResourcesArray.length; i++ ) {
	     
	     if( myResourcesArray[i] != id && myResourcesArray[i] != "undefined")
		 	newCookieString += myResourcesArray[i] + ",";
	}
	setCookie("cr_myResources", newCookieString);
}

// Add the indicated listing to My Boats
// and adjust the image on the page to reflect this.
function clickMyBoats( id ) {
	
	var checkBox = document.getElementById( id );
	var imagePathArray = checkBox.src.split(/\//);
	//alert( "Clicked on check box. ID: " + id + " checked: " + imagePathArray[imagePathArray.length - 1] );

	// If it was checked then uncheck it and remove the resource
	if( imagePathArray[imagePathArray.length - 1] == "myBoats.png" ) {
		removeFromMyBoats( id );
		checkBox.src = checkBox.src.replace( "myBoats",
						     "addmyboats" );
		checkBox.alt = "Add this to MyBoats";
		checkBox.title = "Add this to MyBoats";
	}
	else {
		addToMyBoats( id );
		checkBox.src = checkBox.src.replace( "addmyboats",
						     "myBoats");
		checkBox.alt = "Remove this from MyBoats";
		checkBox.title = "Remove this from MyBoats";
	}
}

function addToMyBoats(id) {

	if( getCookie("cr_myBoats") == null ) {
		setCookie("cr_myBoats", id + ",");
		alert( "Congratulations!!!\n" +
		       "You have added your first item to the MyBoats Page." );
	}
	else {
		var cookieString = "";
		cookieString = getCookie("cr_myBoats");
		
		var myResourcesArray = cookieString.split(/,+/);
		var newCookieString = "";
		for( var i = 0; i < myResourcesArray.length; i++ ) {
	     
			if( myResourcesArray[i] != id && myResourcesArray[i] != "undefined")
			    newCookieString += myResourcesArray[i] + ",";
		}
		newCookieString += id + ",";
		setCookie("cr_myBoats", newCookieString);
	}
}

function removeFromMyBoats(id) {

	var cookieString = "";
	cookieString = getCookie("cr_myBoats");

	var myResourcesArray = cookieString.split(/,+/);
	var newCookieString = "";
	for( var i = 0; i < myResourcesArray.length; i++ ) {
	     
	     if( myResourcesArray[i] != id && myResourcesArray[i] != "undefined")
		 	newCookieString += myResourcesArray[i] + ",";
	}
	setCookie("cr_myBoats", newCookieString);
}

//
// Check that the submit fields aren't empty
//
function checkSubmit( formName ) {
	
	// if the submittal occured do to the user clicking enter
	return inputCheck( formName.ss.value );
		
}	

function inputCheck( value ) {

	//alert( "Checking: " + value );

	if( value != "" ) {
		if(  value.indexOf("?") == -1 && value.indexOf("\"") == -1 &&
		     //value.indexOf("\'") == -1 &&
		     value.indexOf("\*") == -1 &&
		     value.indexOf("\%") == -1 && value.indexOf("\&") == -1 &&
		     value.indexOf(".") == -1 && value.indexOf(",") == -1 &&
		     value.indexOf(";") == -1 )
			return true;
		else
		{
			alert("Please check your search text for unsearchable characters.");
			return false;
		}
	}
	else
		return false;
}

// Search Within Search uses this function to populate the searchString field
function searchWithinSearch( currentSearchString ) {
	if( checkSubmit( document.searchWithin ) ) {
		document.searchWithin.ss.value = currentSearchString + " " + document.searchWithin.ss.value;
		document.searchWithin.submit();
	}
}

// Function to show the specification info
function showSpecInfo( specName, specDesc, rely ) {
	var div = document.getElementById( 'specNameDiv' );
	div.style.display = "block";
	var name = document.getElementById( 'specName' );
	name.innerHTML = specName;
	div = document.getElementById( 'specDescDiv' );
	div.style.display = "block";
	var desc = document.getElementById( 'specDesc' );
	if( rely != "" )
		desc.innerHTML = specDesc + "<br/><br/><a href='" + rely + "'>Reliability</a>\n";
	else
		desc.innerHTML = specDesc;
}

// Hides the info for a boat spec
function hideSpecInfo() {
	var name = document.getElementById( 'specNameDiv' );
	name.style.display = "none";
	desc = document.getElementById( 'specDescDiv' );
	desc.style.display = "none";
}

// Javascript functions to change the Status Bar
// on external links
function mOver(w){window.status=w;return true;}
function mOut(){window.status='';}

/**	The new Link Handler tracking system
*/
function check_link(link, listing, topic) {
	var url = link.href;
	url = url.replace( "&", "%26");
	url = url.replace( "#", "%23");
	var trImg = new Image();
	var path = "cr_linkHandler2.php?url="+url+"&listingID="+listing+"&topic="+topic;
// 	document.location = path;
	trImg.src = path;
}


/** Caption the images on the home page */
function caption( id ) {
	var id_name = id.id;
	var p1 = document.getElementById( id_name + "_p1" );
	var p2 = document.getElementById( id_name + "_p2" );
	var v = document.getElementById( "v_outer_" + id_name );
	p1.style.display = "none";
	p2.style.display = "block";
	v.className = "caption";
	
}

/** Caption the images on the home page */
function uncaption( id ) {
	var id_name = id.id;
	var p1 = document.getElementById( id_name + "_p1" );
	var p2 = document.getElementById( id_name + "_p2" );
	var v = document.getElementById( "v_outer_" + id_name );
	p1.style.display = "block";
	p2.style.display = "none";
	v.className = "";
}