var open_div = null;
var fade_interval = null;
var field_inited = new Array();
field_inited["fbpopup_improve"] = 0;
field_inited["fbpopup_features"] = 0;
field_inited["fbpopup_first_name"] = 0;
field_inited["fbpopup_middle_initial"] = 0;
field_inited["fbpopup_last_name"] = 0;
field_inited["fbpopup_email"] = 0;
field_inited["fbpopup_phone"] = 0;
field_inited["nlpopup_email"] = 0;

function initField(p_field){
	var l_div = document.getElementById(p_field);
	if (l_div != null){
		if (field_inited[p_field] == null || field_inited[p_field] == 0){
			field_inited[p_field] = 1;
			l_div.value = "";
		}
	}
}

function limitToPosFloat(p_cell,p_places){
	var f = document.getElementById(p_cell);
	var l_num;
	if (f != null){
		if (f.value == "-"){
			f.value = "";
		} else if (f.value != "" && f.value != "."){
			l_num = limitStringToNumbers(f.value,p_places);
			if (l_num.charAt(0) == "-") l_num = l_num.substr(1,l_num.length);
			f.value = l_num;
		}
	}
}

function limitStringToNumbers(p_value,p_places){
	var l_new_str = p_value;
	if (isNaN(p_value)){
		var l_dot = false;
		var l_old_str = String(p_value);
		var i;
		l_new_str = "";
		for (i=0;i<l_old_str.length;i++){
			if (!isNaN(Number(l_old_str.charAt(i)))){
				l_new_str += l_old_str.charAt(i);
			} else if (i==0 && l_old_str.charAt(i) == "-") {
				l_new_str += l_old_str.charAt(i);
			}else if (l_old_str.charAt(i) == "." && l_dot == false){
				l_new_str += l_old_str.charAt(i);
				l_dot = true;
			}
		}
	}
	if (l_new_str == "") l_new_str = "0";
	if (p_places != null && !isNaN(p_places) && l_new_str.indexOf(".") >= 0){
		l_new_str = l_new_str.substring(0,l_new_str.indexOf(".")+p_places+1);
	}
	return l_new_str;
}

//From Quirks Mode at http://www.quirksmode.org/js/detect.html
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

//menu scripts
var cur_menu = null;
var cur_menu_item = null;
//var cur_menu_image = null;
//var restore_file = null;

function menuClick(p_menu_item, p_menu_id){
	var l_menu = document.getElementById(p_menu_id);
	var l_link_item = document.getElementById(p_menu_item);

	if (cur_menu != null){
		cur_menu.style.display = "none";
	}
	if (cur_menu_item != null){
		cur_menu_item.className = "menu_top";
	}

	if (l_menu == cur_menu && cur_menu != null){
		cur_menu = null;
		cur_menu_item == null;
	} else if (l_menu != null){
		cur_menu = l_menu;
		cur_menu_item = l_link_item;
		l_menu.style.visibility = "visible";
		if (BrowserDetect.browser=="Explorer"){
			l_menu.style.display = "inline-block";
		} else {
			l_menu.style.display = "inline";
		}
		l_link_item.className="menu_top_cur";
	} else if (l_link_item != null){
		cur_menu_item = l_link_item;
		l_link_item.className="menu_top_cur";
	}
}

/*function menuClick(p_image_id, p_menu_id, p_restore_file){
	var l_menu = document.getElementById(p_menu_id);
	var l_image = document.getElementById(p_image_id);

	if (cur_menu != null){
		cur_menu.style.display = "none";
//		cur_menu.style.visibility = "hidden";
	}
	if (cur_menu_image != null && restore_file != null){
		cur_menu_image.src = restore_file;
	}
	if (l_menu == cur_menu){
		restore_file = null;
		cur_menu = null;
		cur_menu_image = null;
	} else if (l_menu != null && l_image != null){
		restore_file = p_restore_file;
		cur_menu = l_menu;
		cur_menu_image = l_image;
		l_menu.style.visibility = "visible";
		if (BrowserDetect.browser=="Explorer"){
			l_menu.style.display = "inline-block";
		} else {
			l_menu.style.display = "inline";
		}
	}
}*/

function menuImageChange(p_image_id, p_file){
	var l_image = document.getElementById(p_image_id);

	if (l_image != cur_menu_image && l_image != null){
		l_image.src = p_file;
	}
}

function setImage(p_image_id, p_file, p_width, p_height){
	var l_img = document.getElementById(p_image_id);
	if (l_img != null){
		l_img.src = p_file;
		if (p_width != null && !isNaN(p_width)) l_img.width = p_width;
		if (p_height != null && !isNaN(p_width)) l_img.height = p_height;
	} else {
	}
}

function setDivContent(p_div_id,p_content){
	var l_div = document.getElementById(p_div_id);
	if (l_div != null && l_div.innerHTML != null){
		l_div.innerHTML = unescape(p_content);
	}
}

function showHidePopup(p_layer,p_show_hide, p_show_bg){
	var l_div = document.getElementById(p_layer);
	var l_div_bg = document.getElementById("popup_div");
	var l_width = 800;
	
	if (p_show_bg == null) p_show_bg = true;

	p_show_hide = p_show_hide.toLowerCase();

	if (fade_interval != null) clearInterval(fade_interval);
	if (l_div != null){
		if (p_show_hide == "show"){
			if (open_div != l_div && open_div != null) open_div.style.visibility = "hidden";
			open_div = l_div;
			if (BrowserDetect.browser=="Explorer"){
				if (l_div_bg != null && p_show_bg){
					l_div_bg.style.visibility = "visible";
					l_div_bg.style.display = "inline-block";
				}
				l_div.style.visibility = "visible";
			} else if (l_div_bg.style.visibility == "visible" || p_show_bg == false){
				l_div.style.filter = "alpha(opacity=1)";
				l_div.style.opacity = "1";
				l_div.style.visibility = "visible";
				l_div.style.display = "inline";
			} else {
				if (l_div_bg != null){
					l_div_bg.style.filter = "alpha(opacity=0)";
					l_div_bg.style.opacity = "0";
	//				l_div_bg.style.-moz-opacity = "0";
					l_div_bg.style.visibility = "visible";
					l_div_bg.style.display = "inline";
				}
				l_div.style.filter = "alpha(opacity=0)";
				l_div.style.opacity = "0";
				l_div.style.visibility = "visible";
				fade_interval = setInterval(handleFade,50,this);
			}
		} else if (p_show_hide == "hide"){
			if (open_div == l_div) open_div = null;
			if (l_div_bg != null){
				l_div_bg.style.visibility = "hidden";
				l_div_bg.style.display = "none";
			}
			l_div.style.visibility = "hidden";
		}
	}
}

function submitForm(p_form_id){
	var l_form = document.getElementById(p_form_id);
	if (l_form != null){
		l_form.submit();
	}
}

function resetForm(p_form_id){
	var l_form = document.getElementById(p_form_id);
	if (l_form != null){
		l_form.reset();
	}
}

function handleFade(p_window){
	var l_div = p_window.open_div;
	var l_div_bg = document.getElementById("popup_div");
	var l_opacity;
	if (l_div != null && l_div_bg != null){
		l_opacity = parseFloat(l_div.style.opacity);
		if (l_div.style.opacity < 1.0	){
			l_opacity = l_opacity + .35;
			if (l_opacity >= 1){
				l_opacity = 1;
				clearInterval(p_window.fade_interval);
			}
			l_div.style.filter = "alpha(opacity="+l_opacity+")";
			l_div.style.opacity = l_opacity;
			l_div.style.visibility = "visible";
			l_opacity = l_opacity * .75;
			l_div_bg.style.filter = "alpha(opacity="+l_opacity+")";
			l_div_bg.style.opacity = l_opacity;
			l_div_bg.style.visibility = "visible";
		} else {
			clearInterval(p_window.fade_interval);
		}
	} else {
		clearInterval(p_window.fade_interval);
	}
}

function showSideBarImage(p_path,p_width, p_height, p_image){
	var l_img = document.getElementById("image_viewport_image");
	l_img.src = p_path;
	l_img.width = p_width;
	l_img.height = p_height;
	showHidePopup("image_viewport","show", false);
}

function setupForm(p_form, p_submit_page,p_secure){
	var l_form = document.getElementById(p_form);
	if (p_secure == null) p_secure = false;
	if (l_form != null){
		if (p_secure){
			l_form.action = "https://www.savewright.org/"+p_submit_page+".php";
		} else {
			l_form.action = p_submit_page+".php";
		}
	}
}

function setupEmailForm(p_type, p_id, p_topic){
	var l_type_field = document.getElementById("emtype");
	var l_id_field = document.getElementById("emid");
	if (p_topic == null) p_topic = "";
	setDivContent("email_topic","<span class=\"TopNews_bodycopy\">"+p_topic+"<"+"/span>")
	if (p_type == null) p_type = "";
	if (p_id == null) p_id = "";
	if (l_type_field != null) l_type_field.value = p_type;
	if (l_id_field != null) l_id_field.value = p_id;
}

function submitBulletinOrder(){
	var l_form = document.getElementById("bulletin_order_form");
	if (l_form != null){
		l_form.submit();	
	}
}

function finalSubmitBulletinOrder(){
	var l_form = document.getElementById("final_b_order");
	if (l_form != null){
		l_form.submit();	
	}
}

function deleteComment(p_id){
	var l_form = document.getElementById("form_news_comment");
	var l_field = document.getElementById("comment_id");
	var l_action = document.getElementById("comment_action");
	if (l_form != null && l_field != null && l_action != null){
		l_action.value = "delete";
		l_field.value = p_id;
		setupForm('form_news_comment', 'news_submit');
		l_form.submit();
	}
}

function submitLogin(){
	var l_form = document.getElementById("member_login");
	if (l_form != null){
		setupForm('member_login', 'login', true);
		if (checkLogin()){
			l_form.submit();
		}
	}
}

function checkLogin(){
	var l_name = document.getElementById("login_username");
	var l_pass = document.getElementById("login_password");
	if (l_name != null && l_pass != null){
		l_name = l_name.value.replace(/^\s+|\s+$/g, '') ;
		if (l_name == ""){
			alert("Please enter a Username.");
			return false;
		} else if (l_pass.value == ""){
			alert("Please enter a Password.");
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}

function toggleMemberMenuReveal(){
	var l_div = document.getElementById("member_menu_div");
	var l_text = document.getElementById("member_reveal_text");
	if (l_div != null){
		if (l_div.style.display == "none"){
			l_div.style.display = "inline";
			l_text.innerHTML = "<a href=\"javascript:toggleMemberMenuReveal();\" class=\"UniveralLinks_orange\">&gt;&gt;&nbsp;Hide Menu</a>";
		} else {
			l_div.style.display = "none";
			l_text.innerHTML = "<a href=\"javascript:toggleMemberMenuReveal();\" class=\"UniveralLinks_orange\">&gt;&gt;&nbsp;Show Menu</a>";
		}
	}
}
