/****************************************************************************
**	
**	Filename:	wyndhamvrap.js
**	Date:		8/03/2007
**	Author:		Troy Lutton & Naomi Ngawaka
**	Purpose:	To provide generic functions for the WyndhamVRAP website
**
*****************************************************************************/

// redirect
function redirectPage(url)
{
	window.location.href=url;
}

// On Body Load: show the wyndham menu items (open and closed)
function showLeftNavItem(id) 
{
	// check to see if this is the same menu
	var currentItem = (cookieSet('WyndhamNavLeft'))?getCookie('WyndhamNavLeft'):"none";	

	// see if there is an open item
	if (currentItem != "none")
	{
		// if yes then open it up
		openLayer(currentItem);	
	}	
}

// Onclick: show menu item and close any open ones
function showMenu (id)
{
	try
	{	
		// get the object selected
		var submenu = document.getElementById(id);

		// make sure that this menu item exists	
		if (submenu) 
		{	
			// see if the cookie is set
			var currentItem = (cookieSet('WyndhamNavLeft'))?getCookie('WyndhamNavLeft'):"none";						
			
			// IF not the same menu item
			if (currentItem != id)
			{	
				// if the first time through in IE it will choke without a cookie
				if (currentItem != "none"){closeLayer(currentItem);}				

				// open the selected object
				openLayer(id);				

				// set the memory for next time
				clearCookie('WyndhamNavLeft');
				setCookie('WyndhamNavLeft', id, 1);
				
				// make sure there is a cookie for IE otherwise it will choke
				if (cookieSet('WyndhamNavLeft')){clearCookie('WyndhamNavLeftStatus');}
				
				// set the current status of the node
				setCookie('WyndhamNavLeftStatus', "open", 1);
			
			} // if its the same node and its already open then close it
			else if (currentItem != "none" && getCookie('WyndhamNavLeftStatus') == "open")
			{
				closeLayer(currentItem);
				clearCookie('WyndhamNavLeftStatus');
				setCookie('WyndhamNavLeftStatus', "closed", 1);

			} // if its the same node and its already closed then open it
			else if (currentItem != "none" && getCookie('WyndhamNavLeftStatus') == "closed")
			{
				openLayer(currentItem);
				clearCookie('WyndhamNavLeftStatus');
				setCookie('WyndhamNavLeftStatus', "open", 1);
			}			
		}
	}
	catch (e)
	{
		//window.alert(e.message);
	}

	return false;
}

// IE fix to add events to suckerfish menu
startList = function() {
	if (document.all&&document.getElementById && document.getElementById("menu")) {
		navRoot = document.getElementById("menu");
		
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="DT") {
				
				node.onmouseover=function() {
					var hoverClass = this.className.indexOf("has_sub")!= -1 ? " over has_sub_over" : " over"
					this.className += hoverClass;
				}

				node.onmouseout=function() {

					var hoverClass = this.className.indexOf("has_sub_over")!= -1 ? " over has_sub_over" : " over"
					this.className = this.className.replace(hoverClass, "");
				}
			}
		}
	}
}

// COOKIE FUNCTIONS 

// see if cookie exists
function cookieSet(c_name)
{	
	// see if a cookie exists
	if ((document.cookie.indexOf(escape(c_name) + '=')) == -1)
	{
		return false;
	}
	else
	{
		return true;
	}
}

// clear the cookie
function clearCookie(name)
{
	// clear it
	setCookie(name,"",-1);
}

// set a cookie
function setCookie(name, value, days)
{
	// make the date
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));

	// set the expiry
	var expires = "; expires="+date.toGMTString();

	// set cookie
	document.cookie = name+"="+value+expires+"; path=/";	
}

// get the cookies value
function getCookie(name)
{
	var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {		
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

// LAYER FUNCTIONS

// Void: set the div as open
function openLayer(layer_name)
{
	//alert("Layer: " + layer_name)
	// open the layer
	if (document.getElementById(layer_name)) {
		document.getElementById(layer_name).style.visibility='visible';
		document.getElementById(layer_name).style.display='block';
	}
}

// Void: set the div as open
function closeLayer(layer_name)
{
	if (document.getElementById(layer_name)) {
		// open the layer
		document.getElementById(layer_name).style.visibility='hidden';
		document.getElementById(layer_name).style.display='none';
	}
}

// Boolean: is the layer open?
function layerOpen(layer_name)
{
	return (document.getElementById(layer_name).style.visibility == 'visible');
}

// GOOGLE MAPS

// load google map
function loadMap(lat, lng, description, address) {
  if (GBrowserIsCompatible()) {
	var point = new GLatLng(lat,lng);

	var map = new GMap2(document.getElementById("map"));
	map.setCenter(point, 17);
	map.addControl(new GSmallMapControl());

	var icon = new GIcon();
	icon.image = "http://images.trendwest.com.au/resorts/wyndham-icon.gif";
	icon.iconSize = new GSize(32, 27);
	icon.iconAnchor = new GPoint(16, 13);
	icon.infoWindowAnchor = new GPoint(15, 12);

	var marker = new GMarker(point, icon);
	map.addOverlay(marker);

	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml('<div style="display:inline; float:left;"><strong>' + description + '</strong><br />' + address + '</div>');
	});

	return marker;
  }
}

function validateEducationForm(form) {

	var msg = '';
	var owner_number_regex = /[0-9]{11}/;
	var email_regex = /[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/;
	var value = '';
	
	value = form.elements['first_name'].value;
	if(value == '') {
		msg += '\n- your first name must be entered';
	}

	value = form.elements['last_name'].value;
	if(value == '') {
		msg += '\n- your last name must be entered';
	}

	value = form.elements['owner_number'].value;
	if(value == '') {
		msg += '\n- your owner number must be entered';
	} else if(!owner_number_regex.test(value)) {
		msg += '\n- your owner number must be 11 numbers';
	}

	value = form.elements['email'].value;
	if(value == '') {
		msg += '\n- your email address must be entered';
	} else if(!email_regex.test(value)) {
		msg += '\n- the email address entered is not valid';
	}


	value = form.elements['phone'].value;
	if (value == '') {
		msg += '\n- your phone number must be entered';
	}
	
	value = form.elements['number_attending'].value;
	if (value == '') {
		msg += '\n- the number of people attending must be entered';
	} else if (isNaN(value)) {
		msg += '\n- the number of people attending must be a number';
	}

	if (msg != '') {
		alert('Invalid information has been entered.\nPlease correct these fields.' + msg);
		return false;
	} else {
		return true;
	}
}

function isUndefined(v) {
    var undef;
    return v===undef;
}

var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,width=380,height=200,resizable=yes';

function raw_popup(url, target, features) {

    // pops up a window containing url optionally named target, optionally having features
    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target)) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();

    return theWindow;
}

function link_popup(src, features) {
    // to be used in an html event handler as in: <a href="..." onclick="link_popup(this,...)" ...
    // pops up a window grabbing the url from the event source's href
    return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

function link_opener(src) {	

	opener.location.href = src.getAttribute('href');
	window.close();
}

function setNavMenu () {
	
	// opens menu according to sub-section of page
}
