if(!Array.indexOf){Array.prototype.indexOf=function(obj){for(var i=0;i<this.length;i++){if(this[i]==obj){return i;}}return -1;}}
function checkAjax() {var ajaxRequest;try {ajaxRequest=new XMLHttpRequest();}catch(e){try{ajaxRequest=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{ajaxRequest=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser does not support AJAX! Please upgrade your browser.");return false;}}}return ajaxRequest;}
function loadData() {var loading='<a href="javascript:closeForm();" class="close" title="Close">Close ×</a><p class="center"><img src="/img/loading.gif" alt="Loading" title="Loading" /><br />Loading...Please Wait.</p></div>';return loading;}
function addLoadEvent(func) {var oldonload=window.onload;if (typeof window.onload!='function') {window.onload=func;} else {window.onload=function() {oldonload();func();}}}

function addForm(form,id){
	if((document.getElementById('darken'))||(document.getElementById('formdiv'))) {
		closeForm();
	}
	var postRequest=checkAjax();
	var newdiv=document.createElement('div');
	newdiv.id='darken';
	document.getElementsByTagName('body')[0].appendChild(newdiv);
	var newdiv=document.createElement('div');
	newdiv.id='formdiv';
	document.getElementsByTagName('body')[0].appendChild(newdiv);
	window.scroll(0,0);
	postRequest.onreadystatechange=function() {
		if(postRequest.readyState==1){
			document.getElementById('formdiv').innerHTML=loadData();
		}
		if(postRequest.readyState==4){
			setTimeout(function() {document.getElementById('formdiv').innerHTML=postRequest.responseText;},150);
		}
	}
	postRequest.open('GET','/inc/form.' + form + '.php?id=' + id,true);
	postRequest.send(null);
}

function insertForm(parentid,action,form){
	var postRequest=checkAjax();
	var parent_id=document.getElementById(parentid);
	postRequest.onreadystatechange=function() {
		if(postRequest.readyState==4){
			document.getElementById(parentid).innerHTML=postRequest.responseText;
		}
	}
	postRequest.open('GET','/inc/form.' + form + '.php?action=' + action,true);
	postRequest.send(null);
}

function postForm(action,id) {
	var postRequest=checkAjax();
	var forms=document.getElementById(id);
	var selects=forms.getElementsByTagName('select');
	var inputs=forms.getElementsByTagName('input');
	var textarea=forms.getElementsByTagName('textarea');
	var query='';
	var parent=forms.parentNode;
	window.scroll(0,0);
	postRequest.onreadystatechange=function() {
		if(postRequest.readyState==1){
			parent.innerHTML=loadData();
		}
		if(postRequest.readyState==4){
			setTimeout(function() {parent.innerHTML=postRequest.responseText;},150);
		}
	}
	for(i=0;i<selects.length;i++) {
		if(selects[i].name) {
			var option=encodeURIComponent(selects[i].getElementsByTagName('option')[selects[i].selectedIndex].value);
			query+=selects[i].name + '=' + option + '&';
		}
	}
	for(i=0;i<inputs.length;i++) {
		if(inputs[i].name) {
			query+=inputs[i].name + '=' + encodeURIComponent(inputs[i].value) + '&';
		}
	}
	for(i=0;i<textarea.length;i++) {
		if(textarea[i].name) {
			query+=textarea[i].name + '=' + encodeURIComponent(textarea[i].value) + '&';
		}
	}
	query=query.replace(/\&$/,'');
	postRequest.open('POST','/inc/actions.' + action + '.php',true)
	postRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	postRequest.send(query);
}

function closeForm(){
	var darken=document.getElementById('darken');
	var formdiv=document.getElementById('formdiv');
	darken.parentNode.removeChild(darken);
	formdiv.parentNode.removeChild(formdiv);
}

function deleteItem(action,form,parentid,id) {
	var parent_id=document.getElementById(parentid);
	if(action=='delete') {
		parent_id.innerHTML=' Are you sure? <a href="javascript:deleteItem(\'cancel\',\'' + form + '\',\'' + parentid + '\',\'' + id + '\');" class="edit">Cancel</a> <a href="javascript:deleteItem(\'confirm\',\'' + form + '\',\'' + parentid + '\',\'' + id + '\');" class="delete">Delete</a>';
	} 
	if (action=='confirm') {
		var postRequest=checkAjax();
		postRequest.onreadystatechange=function() {
			if(postRequest.readyState==4){
				parent_id.parentNode.innerHTML=postRequest.responseText;
			}
		}
		postRequest.open('GET','/inc/actions.' + form + '.delete.php?id=' + id,true);
		postRequest.send(null);
	}
	if (action=='cancel')  {
		parent_id.innerHTML='<a href="javascript:deleteItem(\'delete\',\'' + form + '\',\'' + parentid + '\',\'' + id + '\');" class="delete">delete</a>';
	}
}

function moveMenu(parentid,direction,id) {
	var postRequest=checkAjax();
	var parent_id=document.getElementById(parentid);
	postRequest.onreadystatechange=function() {
		if(postRequest.readyState==4){
			parent_id.parentNode.innerHTML=postRequest.responseText;
		}
	}
	postRequest.open('GET','/inc/actions.menu.move.php?direction=' + direction + '&id=' + id,true);
	postRequest.send(null);
}

function changeTemplate(pageid,templateid) {
	var postRequest=checkAjax();
	var parent_id=document.getElementById('template-layout');
	postRequest.onreadystatechange=function() {
		if(postRequest.readyState==4){
			parent_id.innerHTML=postRequest.responseText;
		}
	}
	postRequest.open('GET','/inc/form.template-chooser.php?pageid=' + pageid + '&templateid=' + templateid,true);
	postRequest.send(null);
}

function changeAccess(id) {
	var postRequest=checkAjax();
	var parent_id=document.getElementById('access-' + id);
	postRequest.onreadystatechange=function() {
		if(postRequest.readyState==4){
			parent_id.innerHTML=postRequest.responseText;
		}
	}
	postRequest.open('GET','/inc/actions.users.access.php?id=' + id,true);
	postRequest.send(null);
}
function resetPassword(id) {
	var postRequest=checkAjax();
	var parent_id=document.getElementById('password-' + id);
	postRequest.onreadystatechange=function() {
		if(postRequest.readyState==4){
			parent_id.innerHTML=postRequest.responseText;
		}
	}
	postRequest.open('GET','/inc/actions.users.password.php?id=' + id,true);
	postRequest.send(null);
}
function showHover(id) {
	var div=document.getElementById('showhover');
	if(id==0) {
		var content='<h3>Focused Search Results</h3><p>Based on the criteria you set and our understanding of your organization and culture, Elinvar develops a search plan that is tailored to find candidates who possess the exact requirements you need for the position.</p>';
	}
	if(id==1) {
		var content='<h3>Easier Internal Consensus</h3><p>Elinvar\'s job benchmarking process includes steps that ensure everyone on your team can come to a consensus by the time you are ready to make a decision.</p>';
	}
	if(id==2) {
		var content='<h3>Assimilation</h3><p>Our assimilation process ensures your new leader is effective and committed to your organization from the very beginning. This process promotes a quick and positive impact which also leads to a higher retention rate for key new employees.</p>';
	}
	if(id==3) {
		var content='<h3>Lower Total Investment</h3><p>We determine our search fee at the beginning of each project. This fee reflects the combined resources of our two organizations and is typically no more than a contingency fee. In addition, our process greatly reduces the amount of time you and your staff spend in the process, adding to your cost savings.</p>';
	}
	if(id==4) {
		var content='<h3>Custom Solution</h3><p>Elinvar works with you to determine how to best leverage your resources as we create a plan for each search. We only engage for searches we are confident we can complete.</p>';
	}
	if(id==5) {
		var content='<h3>Enhanced Communications</h3><p>Because we are your partners in this process, the lines of communication are open - focused, specific and clear.</p>';
	}
	div.innerHTML=content;
}