// JavaScript Document
var t;
var selected_item;
var first_load = false;
var btn_clicked = false;
var curr_menu;
var over_btn = false;	// Is the mouse hovering over a button on top of a button?
var errors = 0;
function winOpen(addy,w,h) {
	LeftPosition = (screen.width) ? (screen.width - w)/2 : 0;
	TopPosition = (screen.height) ? ((screen.height - h)/2)-50 : 0;
	settings = 'width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+",status=yes,scrollbars=yes";
	mwin = window.open(addy,'win1',settings);
}

function check_searchbox(form,element,text) {
	searchbox = document.forms[form].elements[element];
	if (searchbox.value == "") {
		searchbox.value = text;
	}
	else if (searchbox.value == text) {
		searchbox.value = "";
	}
}

function write_mailto(domain,user,display_name) {
	if (display_name == "") {
		display_name = user+"@"+domain;
	}
	document.write('<a href="mailto:'+user+'@'+domain+'">'+display_name+'</a>');
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 7;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string\'s characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn\'t whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function check_phone_num(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
// Roll div on or off screen:
function roll_div(div_id,direction) {
	if (direction == "down") {
		window.scroll(0,0);
		Effect.Appear("cover_div",{from: 0,to: 0.6,duration: 0.5,afterFinish: function() { roll_in(div_id); }});
	}
	else if (direction == "up") {
		Effect.SlideUp(div_id,{duration: 0.5,afterFinish: cover_fade});
	}
}
function roll_in(div_id) {
	$("content_div_container").show();
	Effect.SlideDown(div_id,{duration: 0.5});
}
function cover_fade(obj) {
	write_content(obj.element.id,'');
	Effect.Fade("cover_div",{duration: 0.4,afterFinish: hide_content});
}
function hide_content(obj) {
	$("content_div_container").hide();
}
// Close a rolled out div popup
function close_me() {
	clearTimeout(t);
	if (document.Feedback_Form) {
		document.Feedback_Form.reset();
	}
	selected_item = "";
	roll_div("content_div","up");
}
// Show/hide throbber animation (to let user know when AJAX div is loading):
function show_throbber() {
	a = show_throbber.arguments;
	if (a.length > 0) {
		throbber_name = a[0];
	}
	else {
		throbber_name = "throbber";
	}
	if (document.images[throbber_name]) {
		document.images[throbber_name].src = "/images/"+throbber_name+"_on.gif";
	}
}

function hide_throbber() {
	a = hide_throbber.arguments;
	if (a.length > 0) {
		throbber_name = a[0];
	}
	else {
		throbber_name = "throbber";
	}
	if (document.images[throbber_name]) {
		document.images[throbber_name].src = "/images/"+throbber_name+"_off.gif";
	}
}
function show_div(div_id) {
	el = document.getElementById(div_id);
	el.style.display = "block";
}
function hide_div(div_id) {
	el = document.getElementById(div_id);
	el.style.display = "none";
}
function val_radio(group) {
	selected = false;
	for (i=0;i < group.length;i++) {
		if (group[i].checked == true) {
			selected = true;
			break;
		}
	}
	return selected;
}
function val_select(selector) {
	return (selector.selectedIndex != 0)
}
// Show a status message while processing AJAX form submit:
function show_status() {
	ajax_request("change_popup","contact","show_msg=1");
}
function val_passwords() {
	form_el = document.registration_form;
	return (form_el.password.value != "" && form_el.pass_confirm.value != "" && form_el.password.value.length > 6 && form_el.pass_confirm.value.length > 6 && form_el.password.value == form_el.pass_confirm.value);
}
// Functions for selecting an item.  These are needed to control the selection of buttons underneath buttons
function select_item(item_id,id_label,page_name,form_el) {
	a = parseInt(item_id);
	item_id = ((a != 0) ? "'"+item_id+"'" : item_id);
	// Wait for a moment before actually selecting the item in case a button over top of it was clicked:
	setTimeout("do_select("+item_id+",'"+id_label+"','"+page_name+"','"+form_el+"');",250);
}
function do_select(item_id,id_label,page_name,form_el) {
	if (btn_clicked == false) {
		// If a button over top was not clicked, perform the selection action
		selected_item = item_id;
		ajax_request("popup",page_name,id_label+"="+item_id,"");
		first_load = true;
		t = setTimeout("check_errors('"+form_el+"')",250);
	}
	else {
		btn_clicked = false;
	}
}
function close_msg_box() {
	if (window.Effect) {
		Effect.SlideUp("usr_msg",{duration: 0.1,afterFinish: clear_msg_box});
	}
	else {
		write_content("usr_msg",'');
		hide_div("usr_msg");
	}
}
function clear_msg_box(obj) {
	write_content(obj.element.id,'');
}
// Write content into any div:
function write_content(elid,str) {
	if (document.getElementById) {
		showContentObj=document.getElementById(elid);
	}
	else if (document.all) {
		eval("showContentObj=document.all."+elid+";");
	}
	if (showContentObj) {
		showContentObj.innerHTML=str;
	}
}
function login() {
	new Ajax.Request('/framework/index.php',{
		parameters: {
			page: 'login',
			ajax_request: 1,
			request_type: 'login',
			login_form: 1,
			'login_info[username]': $F('login_info_username'),
			'login_info[password]': $F('login_info_password')
		},
		evalScripts: true,
		onSuccess: function(transport) {
			if (transport.responseText === '-ERROR' || transport.responseText === '' || transport.responseText == null) {
				Effect.Shake('main',{distance: 12, duration: 0.3});
			}
			else if (transport.responseText === "+OK") {
				$('lh_login_form').submit();
			}
		}
	});
	return false;
}
