// sunrype 2005
// james at tribalddb

// trigger hover on another link
function hover( id, over ) {
	if ( over )
		document.getElementById( id ).className = 'hover';
	else
		document.getElementById( id ).className = '';
}

// image swapping
function swap( id, img ) {
	document.getElementById( id ).src = img.src;
}

// colour swapping
function colour( c ) {
	// current url
	uri = document.location.href;
	// get rid of any anchor
	uri = uri.replace( /\#.*/g, "" );
	// remove existing colour parameter from query string
	uri = uri.replace( /\?c=[^&]*&?/g, "?" );	// found in beginning of query string
	uri = uri.replace( /&c=[^&]*&/g, "&" );		// found in middle of query string
	uri = uri.replace( /&c=[^&]*$/g, "" );		// found in end of query string
	// add new colour parameter to query string
	if ( uri.match(/\?$/) )
		uri = uri + "c=" + c;
	else if ( !uri.match(/\?/) )
		uri = uri + "?c=" + c;
	else
		uri = uri + "&c=" + c;
	// go to new url
	document.location.href = uri;
}

// part of your day tabs
function tab( id, on ) {
	if ( on ) {
		swap( "tab_" + id, eval( "yourday_tab_" + id + "_on" ) );
		document.getElementById( "block_" + id ).style.display = "block";
	}
	else {
		swap( "tab_" + id, eval( "yourday_tab_" + id + "_off" ) );
		document.getElementById( "block_" + id ).style.display = "none";
	}
	return false;
}

// part of your day tips
function tip( id, on ) {
	if ( on ) {
		document.getElementById( "tip_" + id ).style.display = "block";
	}
	else {
		document.getElementById( "tip_" + id ).style.display = "none";
	}
	return false;
}

// part of your day scroller
var scroller;

function up( obj ) {
	element = document.getElementById( obj );
	if ( parseInt( element.style.top ) < 0 ) {
		element.style.top = ( parseInt( element.style.top ) + 15 ) + "px";
	}
	scroller = setTimeout( "up('" + obj + "')", 20 );
}

function down( obj ) {
	element = document.getElementById( obj );
	if ( ( parseInt( element.style.top ) * -1 ) < ( element.offsetHeight - parseInt( element.parentNode.offsetHeight ) ) ) {
		element.style.top = ( parseInt( element.style.top ) - 15 ) + "px";
	}
	scroller = setTimeout( "down('" + obj + "')", 20 );
}

function stop( ) {
	clearTimeout( scroller );
}

// career listings
var curCareer = 0;
function showCareer( id ) {
	if ( curCareer ) {
		document.getElementById("career" + curCareer).style.display = 'none';
	}
	if ( id == curCareer ) {
		curCareer = 0;
	}
	else if ( id ) {
		Ajax2Page( "GET", "careers_view.php", "id=" + id, "", "" );
		document.getElementById("career" + id).style.display = 'block';
		curCareer = id;
	}
}

// email address validation
function isEmail( string ) {
	if ( string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1 ) {
		return true;
	} else {
		return false;
	}
}

function isPostalCode( pc ) {
	// canadian postal codes (6 or 7 characters)
	if( ( pc.match(/^[A-Za-z]\d[A-Za-z]\d[A-Za-z]\d$/) ) || ( pc.match(/^[A-Za-z]\d[A-Za-z] \d[A-Za-z]\d$/) ) )
		return true;
	return false;
}

function isPhone( phone ) {
	// 3+3+4 digit
	if( phone.match(/^\d\d\d\-\d\d\d\-\d\d\d\d$/) )
		return true;
	return false;
}

function fixPhone( phone ) {
	var num = phone.value;
	var newnum = "";
	var output = "";
	// strip out non-numbers
	for ( var i = 0; i < num.length; i++ )
		if ( num.charAt( i ).match(/\d/) )
			newnum += num.charAt( i );
	if ( newnum ) {
		// rebuild number with hyphen
		for ( var i = 0; i < 10; i++ ) {
			output += newnum.charAt( i )
			if ( i == 2 || i == 5 )
				output += "-";
		}
	}
	// return value
	phone.value = output;
}

function fixPostalCode( pc ) {
	// return value in uppercase
	pc.value = pc.value.toUpperCase();
}

// popup windows
function popup( url, name, w, h ) {
	var x = (screen.width - w) / 2;
	var y = (screen.availHeight - h) / 2;
	var page = window.open(url,name,"toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=auto,width=" + w + ",height=" + h + ",screenX=" + x + ",screenY=" + y + ",top=" + y + ",left=" + x + "");
	page.focus();
}

function recipe( id ) {
	var url = "printrecipe.php?id=" + id;
	var name = "recipe";
	var w = 550;
	var h = 600;
	var x = (screen.width - w) / 2;
	var y = (screen.availHeight - h) / 2;
	var page = window.open(url,name,"toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=yes,width=" + w + ",height=" + h + ",screenX=" + x + ",screenY=" + y + ",top=" + y + ",left=" + x + "");
	page.focus();
}

function setCookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

// *********************************************************
// Simple Ajax Code, does get or post on passed Query String

function Ajax2Page( fct, url, formStr, successMsg, errorMsg ) {
	success = successMsg;
	failure = errorMsg; 
	// branch for native XMLHttpRequest object
	if ( window.XMLHttpRequest ) {
		req = new XMLHttpRequest();
	}
	else if ( window.ActiveXObject ) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	
	if ( req ) {
		req.onreadystatechange = processReqChange;
		if ( fct == "POST" ) {
			req.open ( "POST", url, true );
			req.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
			req.setRequestHeader( "Content-length", formStr.length );
			req.setRequestHeader( "Connection", "close" );
			req.send( formStr );
		}
		else {
			req.open( "GET", url + "?" + formStr, true );
			req.send( null );
		}
	}
}

function processReqChange() {
	// only if req shows "complete"
	if ( req.readyState == 4 ) {
		if ( req.status == 200 ) {  
			if ( success != "" ) {
				alert(success);
			}
		}
		else {
			if ( failure != "" ) {
				alert( failure );
			}
		}
	}
}

