var state = false;
var hideShow = {
	hideContainerIfJsEn:function(){
		if(!document.getElementById("addExtraPersons"))return;
		
		var linkCon = document.getElementById("addExtraPersons");
		var memCon = document.getElementById("moreMembersContainer");
		linkCon.style.display = "block";
		memCon.style.display = "none";		
	},
	container:function(thisObj,objID){
		if(!document.getElementById){return;}
		var cont = document.getElementById(objID);
		if(state == false){
			cont.style.display = "block";
			state = true;			
		}
		else{
			cont.style.display = "none";
			state = false;
		}
	}
};
var validate = {
	educationForm:function(){
		var inps = document.getElementById("mainForm").getElementsByTagName("input");
			
		var errs = 0;
		for(var i=0; i<inps.length; i++){
			if(inps[i].parentNode.className != "memberOther"){
				if(inps[i].value.length < 1){
					inps[i].className = "novalue";
					errs++;
				}
				else{
					inps[i].className = "";		
				}
			}
		}
		
		if(errs != 0){
			window.location = "#mainForm";
			return false;
		}
		return true;
	},
	contactFormGeneral:function(){
		if(!document.getElementById("contactFormContainer"))return;
		
		var inps = document.getElementById("contactFormContainer").getElementsByTagName("input");
		var sel = document.getElementById("contactFormContainer").getElementsByTagName("select");
		var errs = 0;
			
		// select validation
		for(var i=0; i<sel.length; i++){
			if(sel[i].parentNode.className != "validate"){continue;}
			if(sel[i].options[sel[i].selectedIndex].value == ""){
				sel[i].className = "novalue";
				errs++;
			}
			else{
				sel[i].className = "";
			}
		}
		// textbox validation
		for(var i=0; i<inps.length; i++){
			if(inps[i].getAttribute("type") != "text")continue;
			
			if(inps[i].parentNode.className == "validate"){
				if(inps[i].value.length < 1){
					inps[i].className = "novalue";
					errs++;
				}
				else{
					inps[i].className = "";
				}
			}
		}
		//upload file validation
		for(var i=0; i<inps.length; i++){
			if(inps[i].getAttribute("type") != "file")continue;
			
			if(inps[i].value != ""){
				var errMess = document.getElementById("errorMessage");
				var val = inps[i].value.toLowerCase();
				var ext = validate.getExtension(val).toLowerCase();
				
				if(validate.isExtensionAllowed(ext) == false){
					errMess.innerHTML = fileError + "<br />" + extensions;
					errMess.className = "errorMessage";
					errs++;
				}
				else{
					errMess.className = "";
					errMess.innerHTML = attachLabel;
				}
			}
		}
		
		if(errs != 0){
			window.location = "#contactFormContainer";
			return false;
		}
		return true;
	},
	isExtensionAllowed:function(fileExt){
			var arrExts = extensions.split(',');
			
			for(var i=0; i<arrExts.length; i++){
				if(genFuncs.trimBoth(arrExts[i]) == genFuncs.trimBoth(fileExt)){
					return true;
				}
			}
		return false;
	},
	getExtension:function(fileN){
		return fileN.substring(fileN.lastIndexOf('.')+1);
	}
};
var genFuncs = {
	trimLeft:function(val){
		while(val.substring(0,1) == " "){
			val = val.substring(1,val.length);
		}
		return val;
	},
	trimRight:function(val){
		while(val.substring(val.length-1,val.length) == " "){
			val = val.substring(0,val.length-1);
		}
		return val;
	},
	trimBoth:function(val){
		
		while(val.substring(0,1) == " "){
			val = val.substring(1,val.length);
		}
		while(val.substring(val.length-1,val.length) == " "){
			val = val.substring(0,val.length-1);
		}
		return val;
	},
	removeLastChar:function(val){
		// this functions is currently not in use
		var lastComma = extensions.lastIndexOf(',');
		var extLen = extensions.length;
		if(lastComma == extLen){
			return val.substring(0,val.length-1);
		}
		else
			return val;
	},
	showBackLink:function(){
		if(!document.getElementById("backLink"))return;
		var cont = document.getElementById("backLink") 
		cont.style.display = "block";
		var linkNode = cont.getElementsByTagName("a");
		linkNode[0].onclick = function(){
			history.go(-1);
			return false;
		}
	},
	createLink:function(href,innerHTML,indexer){
		var a = document.createElement("a");
		a.setAttribute("href", href);
		a.innerHTML = innerHTML;
		a.onclick = function(){
			toc.clearActive();
			toc.showActive("node" + indexer);
		}
		return a;
	}
};

var toc = {
	createToc:function(){
		if(document.createElement && document.getElementById("toc"))
		{
		var lis = document.getElementById("contentContainer").getElementsByTagName("li");
		var toc = document.getElementById("toc");
		var list = document.createElement("ol");
		for(var i=0; i<lis.length; i++){
			
			lis[i].childNodes[0].setAttribute("id","node" + i);
			var listItem = document.createElement("li");									
			listItem.appendChild(genFuncs.createLink("#node"+i,lis[i].childNodes[0].childNodes[0].nodeValue,i));			
			list.appendChild(listItem);
		}
		toc.appendChild(list);
		}
	},
	showActive:function(nodeID){		
		document.getElementById(nodeID).className = "active";
	},
	clearActive:function(){
		var lis = document.getElementById("contentContainer").getElementsByTagName("li");
		for(var i=0; i<lis.length; i++){
			lis[i].childNodes[0].className = "";
		}
	}
};

/*
 	Functions for investigation page
 */
function fillTextArea(obj){
	var textArea = document.getElementById("selectedOption");
	
	if(obj.selectedIndex != 0){
		textArea.innerHTML = obj.value;
		textArea.className = "selectedOption";
		obj.className = ""; // removing validation marking color
		textArea.style.display = "block";
	}
	else{
		textArea.innerHTML = "";
		textArea.className = "";
		textArea.style.display = "none";
	}
}
/*
 	Functions for investigation page
 */
function fillTextArea1(obj){
	var textArea = document.getElementById("selectedOption1");
	
	if(obj.selectedIndex != 0){
		textArea.innerHTML = obj.value;
		textArea.className = "selectedOption";
		obj.className = ""; // removing validation marking color
		textArea.style.display = "block";
	}
	else{
		textArea.innerHTML = "";
		textArea.className = "";
		textArea.style.display = "none";
	}
}
/* ********************'
 * 
 * 
 * 
 ******************** */
function faq(){
	$("#faqContent dd").addClass("hide");
	$("#faqContent dt a").click(function(e){
		e.preventDefault();
		$(this).toggleClass("open").parent("dt").siblings("dd").slideToggle({
				duration: 300,
				easing: "jswing"
		})
	});
};

$(document).ready(function(){
	hideShow.hideContainerIfJsEn();
	toc.createToc();
	genFuncs.showBackLink();
	faq();
});
