/****************************************************************************
* | Important Note:
* | This source code belongs to Active Ice CC and forms part of 
* | the Retained Copyright governed by the terms and conditions 
* | of service located at http://activeice.co.za/terms/
* | Reuse of this entire file and/or it's scripts without 
* | permission from Active Ice CC is prohibited
* |
* | ai_lib.js		general functions
* | created: 		20 Mar 2007
* | updated: 		25 Mar 2007
*/

/**
* Get elements value
* @param id 	: object id
* @return string
*/
function gv(id)
{
	return ( o = get_obj(id) ) ? o.value : '';
}

/**
* Return element as Object
* @param id 	: object id
* @return object
*/
function get_obj(id)
{
	if ( document.getElementById(id) )
	{
		return document.getElementById(id);
	}
	alert('Element "' + id + '" not found');
	return false;
}

/**
* Get CSS for element
* @param id 	: object id
* @return object
*/
function get_css(id)
{
	if ( o = get_obj(id) ) return o.style;
}

/**
* Replace HTML within element
* @param id 	: object id
* @param html 	: string
* @return void
*/
function add_html(id,html)
{
	if ( o = get_obj(id) ) o.innerHTML = html;
}

/**
* Rollover function
* @param name 	: object id
* @param img 	: string 
* Usage			: swap_img(this,'/images/newimage.jpg')
* @return void
*/
function swap_img(id,img)
{
	if ( o = get_obj(id) ) o.src = img;
}

/**
* Rollover function which switches to 'on' state
* Dependant on correct naming convention
* @param e 	: element
* Usage		: do_over('imageelementname')
* @return void
*/
function do_over(e)
{
	if ( document.images ) document.images[e].src = eval(e + 'on.src');
}

/**
* Rollover function which switches to 'off' state
* Dependant on correct naming convention
* @param e 	: element
* Usage		: do_out('imageelementname')
* @return void
*/
function do_out(e)
{
	if ( document.images ) document.images[e].src = eval(e + 'off.src');
}

/**
* Removes leading zero
* @param s 		: string
* @return string
*/
function strip_zero(s)
{
	return ( s.indexOf('0') == 0 ) ? s.replace('0','') : s;
}

/**
* Validate email address using regular expression
* @param addr 	: string
* @return bool
*/
function valid_email(addr)
{
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	//var regex = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
	if ( !regex.test(addr) )
	{
		alert("Invalid email address");
		return false;
	}
	return true;
}

/**
* Show/Hide a single element
* @param name 	: object id
* @return bool
*/
function show_hide(id)
{
	if ( o = get_obj(id) )
		o.style.display = ( o.style.display == '' || o.style.display == 'block' ) ? 'none' : 'block';
}

/**
* Change background colour of element
* @param name 	: object id
* @return void
*/
function row_over(o,hex)
{
	o.style.background="#" + hex;
}

/**
* Change background colour of element
* @param name 	: object id
* @return void
*/
function row_out(o,hex)
{
	o.style.background="#" + hex;
}

/**
* Redirect to url
* @param url	: string
* @return void
*/
function go_url(url)
{
	if ( url.length > 0 ) window.location.href = url;
}

/**
* Redirect to url based on dropdown value
* @param o		: object [select box]
* @return void
*/
function dd_go_url(o)
{
	if ( o ) go_url(o.options[o.selectedIndex].value);
}

/**
* Replace large image with new path
* @param large	: element name
* @param path	: string
* @return void
*/
function load_img(large,path)
{
	eval('document.' + large + '.src = (document.' + large + ') ? path : alert("Large image not found");');
}

/**
* Y2K bug correction
* @param year	: integer
* @return integer
*/
function y2k(year)
{ 
	return (year < 1000) ? year + 1900 : year; 
}

/**
* Checks if a date is valid
* @param d, m, y: integer
* @return bool
*/
function is_date(d,m,y)
{
    var today = new Date();
    y = ( ( !y ) ? y2k( today.getYear() ) : y );
    m = ( ( !m ) ? today.getMonth() : m-1 );
    if ( !d ) return false;
    
	var test = new Date(y,m,d);
	if ( ( y2k( test.getYear() ) == y ) && ( m == test.getMonth() ) && ( d == test.getDate() ) )
		return true;

	return false;
}

/**
* Returns days in current month
* @param year, month	: integer
* @return integer
*/
function days_in_month(year, month)
{
     return 32 - new Date(year, month, 32).getDate();
}

/**
* Ensures arrival date is before departure
* a_ = arrival, d_ = departure
* @param a_d, a_m, a_y, d_d, d_m, d_y	: integer 
* @return bool
*/
function valid_daterange(a_d,a_m,a_y,d_d,d_m,d_y)
{
	a_m = a_m - 1;
	d_m = d_m - 1;

	var sys_date = new Date();
	var today = new Date(y2k(sys_date.getYear()),sys_date.getMonth(),sys_date.getDate());
	var arrival_date = new Date(a_y,a_m,a_d);
	var departure_date = new Date(d_y,d_m,d_d);
	
	if ( arrival_date < today )
	{
		alert("Your arrival date is before today");
		return false;
	}
	
	if ( departure_date < today )
	{
		alert("Your departure is before today");
		return false;
	}
	
	if ( departure_date < arrival_date )
	{
		alert("Your departure date is before arrival date");
		return false;
	}
	
	if ( arrival_date + '' == departure_date + '' )
	{
		alert("Arrival and departure dates are the same");
		return false;
	}
	
	return true;
}


/**
* Bookmarks URL with Title
* @param title, url	: string
* @return void
*/
function bookmark(title,url)
{
	if ( window.sidebar )
	{
		window.sidebar.addPanel( title, url, '' );
	}
	else if ( document.all )
	{
		window.external.AddFavorite( url, title );
	}
	else if ( window.opera && window.print )
	{
		return true;
	}
}

/**
* Launch popup window
* @param url	: string
* @param w, h	: integer
* @return void
*/
function pop(url,w,h)
{
	var popwin = window.open(url,'','width='+w+',height='+h+',location=no,menubar=no,scrollbars=no,toolbar=no,resizable=no');
	//popwin.focus();
}

/**
* Prints the current page
* @param none
* @return void
*/
function print_page()
{
   var bv = parseInt( navigator.appVersion );
   if ( bv >= 4 ) window.print();
}

/**
* Returns GET variable in url
* @param k		: string [var name in querystring .html?[name]=123]
* @return string
*/
function querystring(k)
{
	var page = new page_query( window.location.search ); 
	return unescape(page.get_value(k)); 
}

/**
* Helper function for querystring(k) above
* @param q		: string [eg. ?q=devmo&m=news&id=3]
* @return string
*/
function page_query(q)
{
	
	if ( q.length > 1 )
		this.q = q.substring(1, q.length);
	else
		this.q = null;
	
	this.key_value_pairs = new Array();
	
	if (q)
	{
		for ( var i=0; i < this.q.split("&").length; i++ )
		{
			this.key_value_pairs[i] = this.q.split("&")[i];
		}
	}
	
	this.get_key_value_pairs = function()
	{ 
		return this.key_value_pairs;
	}
	
	this.get_value = function(s)
	{
		for( var j = 0; j < this.key_value_pairs.length; j++) {
			if ( this.key_value_pairs[j].split("=")[0] == s )
				return this.key_value_pairs[j].split("=")[1];
		}
		return false;
	}
	
	this.get_parameters = function()
	{
		var a = new Array( this.get_length() );
		for ( var j = 0; j < this.key_value_pairs.length; j++ )
		{
			a[j] = this.key_value_pairs[j].split("=")[0];
		}
		return a;
	}
	
	this.get_length = function()
	{
		return this.key_value_pairs.length;
	}

}



function doProfile(id,selected) {

	if (document.getElementById) {
	// sort out the right thing to display
		for ( var i = 0; i < 4; i++ ) {
			// hide all
			document.getElementById('content_' + i).style.display = 'none';
		}
		// show one with ID
		document.getElementById('content_' + id).style.display = 'block';
		// get all links inside the right column
		var nav_box = document.getElementById("map-navigation");
		var links = nav_box.getElementsByTagName("a");	
		// set all the links classes to nothing
		for(var i=0; i<links.length; i++)
		links[i].className = "";
		// set selected link as selected
		selected.className = "selected";
		} else
		alert('Please update your browser');

}

function showHideSub(id) {
	var e = document.getElementById( "sub" + id );
	if ( e )
		e.style.display = ( e.style.display == 'block' ) ? 'none' : 'block';
	else
		alert("Subnav element was not found");
}

function setconn(b)
{
	if ( b == 'bb' ) mode = 'Full';
	if ( b == 'du' ) mode = 'Lite';
	
	if ( window.confirm('Switch to ' + mode + ' version?') )
	{
		document.connection.new_conn_type.value=b;
		document.connection.submit();
	}
	return false;
}

