function popup(url) {
	var width = popup.arguments.length>1 ? popup.arguments[1] : 400;
	var height = popup.arguments.length>2 ? popup.arguments[2] : 300;
	var target = popup.arguments.length>3 ? popup.arguments[3] : "_blank";
	var scrollbars = popup.arguments.length>4 ? popup.arguments[4] : 0;
	var resizable = popup.arguments.length>5 ? popup.arguments[5] : 0;

	window.open(url, target, "width="+width+",height="+height+",scrollbars="+scrollbars+",resizable="+resizable);
}

function getradiovalue(checkbox) {
	if (checkbox.length) {
		for (var i=0; i<checkbox.length; i++) {
			if (checkbox[i].checked)
				return checkbox[i].value;
		}
	}
	else if (checkbox.checked) {
		return checkbox.value;
	}
	
	return null;
}

function email_valid(email) {
	return email!="" && email.match(/^[0-9a-z\.\-_]+\@[0-9a-z\-_\.]{2,}\.[a-z]{2,4}$/i);
}

function validate_email() {
	var frm = document.email_form;

	if (email_valid(frm.email.value)) {
		alert("Please enter a valid email address");
		frm.email.focus();
		return false;
	}

	return true;
}

if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
	try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
	throw new Error( "This browser does not support XMLHttpRequest." )
};

function show_div(div) {
	var type = arguments.length>1 ? arguments[1] : "block";

	if (typeof(div)=="string")
		div = document.getElementById(div);

	try {
		div.style.display = type;
	}
	catch (e) {}
}

function hide_div(div) {
	if (typeof(div)=="string")
		div = document.getElementById(div);

	try {
		div.style.display = "none";
	}
	catch (e) {}
}

function toggle_div(div) {
	var type = arguments.length>1 ? arguments[1] : "block";

	if (typeof(div)=="string")
		div = document.getElementById(div);

	div.style.display = (div.style.display=="none" ? type : "none");
}

function show_subnav(id) {
	show_div('sidenav_title1');
	hide_div('sidenav_data1');

	show_div('sidenav_title2');
	hide_div('sidenav_data2');

	show_div('sidenav_title3');
	hide_div('sidenav_data3');

	show_div('sidenav_title4');
	hide_div('sidenav_data4');

	show_div('sidenav_title5');
	hide_div('sidenav_data5');


	hide_div('sidenav_title'+id);
	show_div('sidenav_data'+id);
}

function get_random(min, max) {
	var rand_no = Math.floor((max-min+1)*Math.random()) + min;
	return rand_no;	
}

function get_object_position(obj) {
	var stats = new Object();
	
	//calculate new top scroll position
	stats.top = 0;
	stats.left = 0;
	
	var curr_obj = typeof(obj)=="string" ? document.getElementById(obj) : obj;

	stats.width = curr_obj.clientWidth;
	stats.height = curr_obj.clientHeight;
	
	var sanity_check = 0;
	while (curr_obj) {
		if (++sanity_check>100) {
			alert("sanity check failed!");
			break;
		}
			
		stats.top += curr_obj.offsetTop;
		stats.left += curr_obj.offsetLeft;

		curr_obj = curr_obj.offsetParent;
	}

	stats.bottom = stats.top+stats.height;
	stats.right = stats.left+stats.width;

	return stats;
}

var menu_timer = null;
var current_menu = null;

function show_menu(menu) {
	if (menu_timer != null) {
		clearTimeout(menu_timer);
		menu_timer = null;
	}

	if (current_menu!=null && current_menu!=menu) {
		hide_div(current_menu+"_menu");
	}

	current_menu = menu;

	var menu_div = document.getElementById(menu+"_menu");
	var nav_position = get_object_position("nav_"+menu);
	menu_div.style.top = nav_position.top+29;
	menu_div.style.left = nav_position.left-4;

	show_div(menu_div);
	
	// hide flash movie while menus up if using IE
	if (document.all) {
		try {
			document.getElementById("flash_div").style.visibility="hidden";
		} catch (e) {}
	}
}

function hide_menu(menu) {
	menu_timer = setTimeout('menu_timer = null; current_menu = null; hide_div("'+menu+'_menu"); try { document.getElementById("flash_div").style.visibility="visible";} catch (e) {}', 1000);
}

function validate_survey() {
	var frm = document.survey_form;
	var stage = arguments.length>0 ? arguments[0] : 1;
	
	for (var i=1; typeof(frm["q"+i])!="undefined"; i++) {
		if (getradiovalue(frm["q"+i])==null) {
			alert("Please answer all the questions.");
			return false;
		}
	}

	if (stage<=1)
		return true;

	var errors = false;
	
	if (frm.fullname.value=="" || frm.fullname.value=="Name:") {
		errors = true;
		frm.fullname.style.backgroundColor = "pink";
	}
	else
		frm.fullname.style.backgroundColor = "white";

	
	if (frm.email.value=="" || frm.email.value=="Email:" || !email_valid(frm.email.value)) {
		errors = true;
		frm.email.style.backgroundColor = "pink";
	}
	else
		frm.email.style.backgroundColor = "white";

	if (errors)
		return false;
	
	return true;
}

function validate_comment_form(frm) {
	var errors = false;
	
	if (frm.comment_name.value.replace(/ +/, "")=="" || frm.comment_name.value.replace(/ +/, "")=="Name:") {
		errors = true;
		frm.comment_name.style.backgroundColor = "pink";
	}
	else {
		frm.comment_name.style.backgroundColor = "white";
	}

	if (!email_valid(frm.comment_email.value)) {
		errors = true;
		frm.comment_email.style.backgroundColor = "pink";
	}
	else {
		frm.comment_email.style.backgroundColor = "white";
	}

	
	if (frm.comment_text.value.replace(/[ \r\n]+/, "")=="" || frm.comment_text.value.replace(/[ \r\n]+/, "")=="Comment:") {
		errors = true;
		frm.comment_text.style.backgroundColor = "pink";
	}
	else {
		frm.comment_text.style.backgroundColor = "white";
	}

	if (errors)
		return false;
	
	return true;;
}

