// JavaScript Document

/*---------------------------------------------------
* String Trim Functions
*---------------------------------------------------*/
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

/*-------------------------------------------------------------*/
/* Modify "String" prototype for trim() functionality
/*-------------------------------------------------------------*/
String.prototype.trim = function () {
  return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};

/*---------------------------------------------------
* Get Browser's Width and Height
*---------------------------------------------------*/
function getScreenSize() {
	var w = 0;
	var h = 0;
	if(typeof(window.innerWidth) == 'number'){
		w = window.innerWidth;
		h = window.innerHeight;
	}else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	return w+'x'+h;
}

/*-------------------------------------------------------------*/
/* Get the current X and Y coordinates of the user's mouse
/*-------------------------------------------------------------*/
function getMouseXY(e){
	var x = 0;
	var y = 0;
	if(!e) var e = window.event;
	if(e.pageX || e.pageY){
		x = e.pageX;
		y = e.pageY;
	}else{
		x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	return x+"x"+y;
}

/*-------------------------------------------------------------*/
/* Checks whether or not the "ENTER" key was pressed
/*-------------------------------------------------------------*/
function isEnterKey(e){
	e = e || window.event;
	if(e.keyCode == 13){
		return true;
	}
	return false;
}
	
function keyPressed(e, keyCode){
	e = e || window.event;
	if(e.keyCode == keyCode){
		return true;
	}
	return false;
}

/*-------------------------------------------------------------*/
/* Fix for 'protected' Email javascript response text  
/*-------------------------------------------------------------*/
function insertEmail(id, alink){
	var elem = document.getElementById(id);
	elem.innerHTML = alink;
}

/*-------------------------------------------------------------*/
/* Checks whether string is a number
/*-------------------------------------------------------------*/
function isNumeric(str){
	if(isNaN(str))
		return false;
	else
		return true;
}

/*-------------------------------------------------------------*/
/* Converts a numeric STRING to an INT
/*-------------------------------------------------------------*/
function intval(str){
	if(isNaN(str)){
		return 0;
	}else{
		if(str.charAt(0) == '0' && str.length > 1){
			str = str.substring(1);	
		}
		return parseInt(str);
	}
}

/*-------------------------------------------------------------*/
/* Capitalizes the first letter of the string
/*-------------------------------------------------------------*/
function ucfirst(str){
	var first = str.charAt(0).toUpperCase();
	str = first + str.substring(1);
	return str;
}

/*-------------------------------------------------------------*/
/* Use jQuery to Fade In and Fade Out the specified element
/*-------------------------------------------------------------*/
function fadeInOut(elem, speed, timeout){
	$(elem).stop();
	$(elem).fadeIn(speed, function(){
		setTimeout('$("'+elem+'").fadeOut("'+speed+'")', timeout);
	});
}

/*-------------------------------------------------------------*/
/* Configure a Date object based on the given parameters
/*-------------------------------------------------------------*/
function mktime(hours, minutes, ampm, month, day, year){
	var dateObj = new Date();
	dateObj.setFullYear(intval(year), intval(month)-1, intval(day));
	hours = intval(hours);
	minutes = intval(minutes);
	if(ampm == "PM" && hours < 12){
		hours += 12;
	}else if(ampm == "AM" && hours == 12){
		hours = 0;
	}
	dateObj.setHours(hours, minutes);
	return dateObj;
}

/*-------------------------------------------------------------*/
/* Disable the "select" functionality for <ul> dropdown
/*-------------------------------------------------------------*/
function disableMenu(menu){
	$("#"+menu+" a.selection").unbind("click");
	$("#"+menu+" a.selection").unbind("focus");
	$("#"+menu+" a.selection").unbind("blur");
	$("#"+menu+" ul").unbind("mouseover");
	$("#"+menu+" ul").unbind("mouseout");
}

/*-------------------------------------------------------------*/
/* Enable the "select" functionality for <ul> dropdown
/*-------------------------------------------------------------*/
function enableMenu(menu){
	$("#"+menu+" li a.selection").click(function(){
 		if($("#"+menu+" ul").attr("focusEnabled") != "true"){
			if($("#"+menu+" ul").css("display") == "block"){
				$(".menu ul li a.selection").css("position", "relative");
				$("#"+menu+" ul").hide();
			}else{
				$(".menu ul li a.selection").not(this).css("position", "static");
				$("#"+menu+" ul").show();
			}
		}
	});
	$("#"+menu+" li a.selection").focus(function(){
		$(".menu ul li a.selection").not(this).css("position", "static");
		$("#"+menu+" ul").attr("focusEnabled", "true");
		$("#"+menu+" ul").show();
	});
	$("#"+menu+" li a.selection").blur(function(){
		if($("#"+menu+" ul").attr("onMenu") == "true"){
			// do nothing
		}else{
			$(".menu ul li a.selection").css("position", "relative");
			$("#"+menu+" ul").hide();
		}
	});
	$("#"+menu+" ul").mouseover(function(){
		$(this).attr("onMenu","true");
	});
	$("#"+menu+" ul").mouseout(function(){
		$(this).attr("onMenu","false");
	});
}

/*-------------------------------------------------------------*/
/* Return DOM reference to the specified Flash object
/*-------------------------------------------------------------*/
function thisMovie(moviename){
	if(navigator.appName.indexOf("Microsoft") != -1){
		return window[moviename];	
	}else{
		return document[moviename];
	}
}

function clearClick(){
	$(".clearClick").each(function(i,elem){
		$(elem).unbind('focus');
		$(elem).unbind('blur');
		$(elem).focus(function(){
			if($(this).val() == $(this).attr("defaultText"))
				$(this).val("");
		});
		$(elem).blur(function(){
			if($(this).val() == "")
				$(this).val($(this).attr("defaultText"));
		});
	});
}

function getBrowser(){
	var name = "";
	var version = jQuery.browser.version.charAt(0);
	if(jQuery.browser.safari) name = "safari";
	if(jQuery.browser.opera) name = "opera";
	if(jQuery.browser.msie){ name = "ie";
		if(navigator.userAgent.search("MSIE 8.0") != -1){
			version = 8;
		}else if(navigator.userAgent.search("MSIE 7.0") != -1){
			version = 7;
		}else{
			version = 6;
		}
	}
	if(jQuery.browser.mozilla) name = "firefox";
	
	if(navigator.appVersion.search("Chrome") != -1) name = "chrome";
	if(navigator.appVersion.search("Macintosh") != -1 && jQuery.browser.mozilla) name = "firefox-mac";
	if(isIPhone()) name = "iphone";
	
	var browser = {
		"name" : name,
		"version" : version
	};
	return browser;
}

function setBrowserClass(){
	var browser = getBrowser();
	var bodyClass = (browser['name'] == "ie") ? browser['name'] + browser['version'] : browser['name'];
	$('body').attr('class', $('body').attr('class') + ' ' + bodyClass);
}

function isIE(version){
	var browser = getBrowser();
	var version = (version != null && intval(''+version) > 0) ? version : 6;
	if(browser['name'] == "ie" && browser['version'] <= version){
		return true;
	}else{
		return false;
	}
}

function isIPhone(){
	if(navigator.appVersion.indexOf("iPhone") != -1){
		return true;
	}else{
		return false;
	}
}

/*---------------------------------------------------
* Get Current URL
*---------------------------------------------------*/
function getURLAddress(){
	return location.href;
}