// JavaScript Document

function toggleMenu(objID) {
	if (!document.getElementById) return;
	var ob = document.getElementById(objID).style;
	ob.display = (ob.display == 'block') ? 'none' : 'block';
}

function toggleSwapText(objID, textID) {
	toggleMenu(objID);
	var txt = document.getElementById(textID);
	if(txt.innerHTML == '[show]') {txt.innerHTML = '[hide]';}
	else if(txt.innerHTML == '[hide]') {txt.innerHTML = '[show]';}
}

function showMenu(objID) {
	if (!document.getElementById) return;
	var ob = document.getElementById(objID).style;
	ob.display = 'block';
}

function hideMenu(objID) {
	if (!document.getElementById) return;
	var ob = document.getElementById(objID).style;
	ob.display = 'none';
}

function switchArrow(objID) {
	if (!document.getElementById) return;
	var ob = document.getElementById(objID);
	if(ob.src == "/images/expand_icon.png") {ob.src = "/images/collapse_icon.png";}
	//else if(ob == "/images/expand_icon.png") {ob = '/images/collapse_icon.png';}
}

function change_stats(id) {
	var the_div = document.getElementById(id);
	var href_edit = document.getElementById('href_edit');
	var pass1 = document.getElementById('password1');
	var pass2 = document.getElementById('password2');
	
	if(the_div.style.display == 'none') {
		the_div.style.display = 'block';
		href_edit.innerHTML = 'Click here to keep your password unchanged.';
		pass1.setAttribute('req','y');
		pass2.setAttribute('req','y');
	} else {
		the_div.style.display = 'none';
		href_edit.innerHTML = 'Click here to change your password.';
		pass1.removeAttribute('req');
		pass2.removeAttribute('req');
	}
}

function goto_chapter(id) 
{ 

	var index=document.getElementById(id).options[document.getElementById(id).selectedIndex].value;
	if(index!=-1)
	{
		if(id=='choose_chapter')
			window.location='../chapter/chapter_info.php?chapter_id='+index;
		else
			window.location='../chapter/region_info.php?region_id='+index;
	}
}

var hover = "enabled";
var root = "nav";

function show(objID, objNum) {
	if(hover == "enabled") {
		ob = document.getElementById(objID + '' + objNum);
		ob.style.display = "block";
		//ob.style.visibility = "visible";
	}
}

function hide(objID, objNum) {
	if(hover == "enabled") {
		ob = document.getElementById(objID + '' + objNum);
		ob.style.display = "none";
		//ob.style.visibility = "hidden";
	}
}

function mouseEventChange(objID, objNum, linkID) {
	linker = document.getElementById(linkID + '' + objNum);
	ob = document.getElementById(objID + '' + objNum);
	if(ob.style.display == "block" && hover == "enabled") {
	//if(ob.style.visibility == "visible" && hover == "enabled") {
		hover = "disabled";
		linker.style.backgroundColor = "#401E09";
		linker.style.color = "#FFFFFF";
	} else if(ob.style.display == "block" && hover == "disabled") {
	//} else if(ob.style.visibility == "visible" && hover == "disabled") {
		hover = "enabled";
		linker.style.backgroundColor = "";
		linker.style.color = "";
	} else if(ob.style.display == "none" && hover == "disabled") {
	//} else if(ob.style.visibility == "hidden" && hover == "disabled") {
		var li_count = 0;
		//var a_count = 0;
		nodes = document.getElementById(root).childNodes;
		for(var j = 0; j < nodes.length; j++) {
			if(nodes.item(j).nodeName == "LI") {li_count++;}
			//if(nodes.item(j).nodeName == "DIV") {a_count++;}
		}
		for(var i = 1; i <= li_count; i++) {
		//for(var i = 1; i <= a_count; i++) {
			linker = document.getElementById(linkID + '' + i).style;
			ob = document.getElementById(objID + '' + i).style;
			if(i != objNum) {
				ob.display = 'none';
				//ob.visibility = 'hidden';
				linker.backgroundColor = "";
				linker.color = "";
			}
			if(i == objNum) {
				ob.display = (ob.display == 'block') ? 'none' : 'block';
				//ob.visibility = (ob.visibility == 'visible') ? 'hidden' : 'visible';
				linker.backgroundColor = "#401E09";
				linker.color = "#FFFFFF";
			}
		}
	}
}

function add_field(div_name,subfix,width,max_elem)
{
	var elms = document.getElementById(div_name).getElementsByTagName("*");
	if(elms.length>=max_elem)
	{
	}
	else
	{
		var values = new Array();
		for(var i = 0, maxI = elms.length; i < maxI; i++)
		{
			var elm = elms[i];
			values[i] = elm.value
		}

		document.getElementById(div_name).innerHTML = document.getElementById(div_name).innerHTML + '<input style=\"background-color:rgba(255,255,255,0.5);display:block;width:'+width+'px;\" id=\'input'+(elms.length+1)+subfix+'\' type=\'text\' >';
		for(var i = 0;i<values.length; i++)
		{
			elms[i].value = values[i];	
		}
	}
}


String.prototype.endsWith = function(str) {
	return (this.indexOf(str) == this.length - str.length);
}

function clearFile(field) {
	field.value = ""; //this doesn't work in IE
}
        
function checkFiles(field, format) {
	var fileName = field.value.toLowerCase();
	
	if (format == "doc") {
		if (!(fileName=="" || fileName.endsWith(".pdf") || fileName.endsWith(".doc") || fileName.endsWith(".docx"))) {
			alert('Documents should be PDF or Word Formats only.');
			clearFile(field);
		}
	} else if (format == "pic") {
		if (!(fileName=="" || fileName.endsWith(".jpg") || fileName.endsWith(".png") || fileName.endsWith(".gif"))) {
			alert('Pictures should be jpg, png, or gif files only.');
			clearFile(field);
		}
	}
}


//this function is to call ajax that will set the current language of the page to the newly chosen 
function new_lang(language)
{
	//alert(language+' =)');
	$('#ajax_lang222').load('../page/lang_update.php?lang='+language); 	
}


/*** This function handles the resizing of iFrames to 100% of the screen size ***/
// The -130 and -30 take care of space used by the header and footer
// the id "framer" must be included on iframes that are being resized
function resize_iframe() {
	var height = window.innerWidth - 130 - 30; //Firefox
	if(document.body.clientHeight) {height = document.body.clientHeight - 130 - 30;} //IE
	//resize the iframe according to the size of the window (all these should be on the same line)
	if(document.getElementById("framer")) {
		document.getElementById("framer").style.height = parseInt(height - document.getElementById("framer").offsetTop - 8) + "px";
	}
}

// this will resize the iframe every time you change the size of the window.
 window.onresize = resize_iframe;
 
 
///////////////////////////// FUNCTIONS TO CHECK THE NAME (for Grant Rights) ///////////////////////////////////////////
	/*** This portion of script deals with the auto-suggesting and related timer ***/
	
	var prev_input = ""; // The previously typed value
	var time; // variable used for the Timer
	var delay = 250; // set the amount of time (delay) after typing before querying the database
	
	/* takes in the typed string and won't query it until the delay has completed */
	function search_timer(input_str, number, text_field, button) {
		input = input_str; // record typed string
		num = number; // the id number of all the div that connect to a particular officer
		field = text_field; // the root name of the text field where the search criteria is entered and the results will be filled
		disbutton = button; // button to disable/enable depending on selections
		if (time) {clearTimeout(time);} // clear any previous delay settings
		time = setTimeout("lookup(input, num, field, disbutton)", delay); // run the lookup() function after the delay
	} // end search_timer()

	/* takes in the typed string and queries the database */
	function lookup(input_str, number, text_field, button) {
		document.getElementById(text_field+'_id'+number).value = "";
		if(button != '') {document.getElementById(button).disabled = true;}
		
		// clear out spaces at the beginning and the end of the string so it has a better chance of matching
		input_str = input_str.replace(/(^\s*)|(\s*$)/g, "");
		//num = number; // the id number of all the div that connect to a particular paper
		
		// Check the current string against the last entered query
		// If the text to search has changed then continue with the query
		if(prev_input != input_str) {
			prev_input = input_str; // save the current string for future comparison
			
			var suggest = text_field+'_suggestions'+number;
			if(input_str != "") { // also make sure the string isn't blank
				// run ajax to display the results of the query
				$("#"+suggest).load("../member/auto_suggest.php?name="+input_str+"&num="+number+"&field="+text_field+"&button="+button);
			} else {
				// if the text input is empty remove the contents of the query results
				document.getElementById(suggest).innerHTML = "";
			}
		}
	} // lookup()

	// takes the selected value from the suggestion list and puts it into the text input
	function fill(thisValue, id, number, text_field, button) {
		prev_input = thisValue; // set the selected value as the previously searched for string
		//num = number; // the id number of all the div that connect to a particular paper
		
		document.getElementById(text_field+number).value = thisValue; // put the selected value in the text input
		document.getElementById(text_field+'_id'+number).value = id; // put the selected id in the hidden text input
		if(button != '') {document.getElementById(button).disabled = false;}
		
		// empties the suggestion list once one has been selected
		var suggest = text_field+'_suggestions'+number;
		document.getElementById(suggest).innerHTML = "";
	} // fill()
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
