var check=0;

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function uploadDone() { //Function will be called when iframe is loaded
	var data = frames['upload_target'].document.getElementsByTagName("body")[0].innerHTML;		
	if(check == 0){
		if(trim(trim(data,'\n'),'') == "success") { //This part happens when the image gets uploaded.
			var upload=document.getElementById("Upload");
			upload.value="Apply For This Job";
			alert("Your application with CV recieved successfully.\nOne of our consultants will be in touch within the next 24 hours.");
			check=1;
			overlay();
		}
		else if(trim(trim(data,'\n'),'') == "failure") { //Upload failed - show user the reason.
			var upload=document.getElementById("Upload");
			upload.value="Apply For This Job";
			alert("Your application failed to submit.\nThe CV is a required field and make sure the file type is one of the following:\n\n doc, docx, txt, rtf, pdf\n\nPlease try again.");
			check=1;
		}	
	}
}


function init(){	
	var stretchers = document.getElementsByClassName('box');
	var toggles = document.getElementsByClassName('tab');
	var myAccordion = new fx.Accordion(
	   toggles, stretchers, {opacity: false, height: true, width:false, transition: fx.linear, duration:600}
	);
	//hash functions
	var found = false;
	toggles.each(function(h3, i){
		var div = Element.find(h3, 'nextSibling');
			if (window.location.href.indexOf(h3.title) > 0) {
				myAccordion.showThisHideOpen(div);
				found = true;
			}
		});
		if (!found) myAccordion.showThisHideOpen(stretchers[0]);	
	
	
}



function validate_form(element){
	var emailRegxp = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	var email = document.getElementById(element).value;
	if (emailRegxp.test(email) != true){
		document.getElementById(element).focus();
		alert('Please enter a valid email');
		return false;
	}
	
}


function validate_app_form(email,name,phone){
	if(validate_form('email') == false){
		return false;
	}else if(document.getElementById('name').value == ""){
		document.getElementById('name').focus();
		alert('Please enter a valid name');
		return false;
	}else if(document.getElementById('phone').value == ""){
		document.getElementById('phone').focus();
		alert('Please enter a valid phone number');
		return false;
	}else{
		var phone=document.getElementById('phone');
		
		if(phone.value.length < 11){
			document.getElementById('phone').focus();
			alert('Phone number is invalid');
			return false;
		}
	}
	var upload=document.getElementById("Upload");
	upload.value="Sending Information ...";
}


var digitsOnly = /[1234567890]/g; 
var integerOnly = /[0-9\.]/g; 
var alphaOnly = /[A-Z]/g; 
function restrictCharacters(myfield, e, restrictionType) { 
if (!e) var e = window.event 
if (e.keyCode) code = e.keyCode; 
else if (e.which) code = e.which; 
var character = String.fromCharCode(code); 
// if they pressed esc... remove focus from field... 
if (code==27) { this.blur(); return false; } 
// ignore if they are press other keys 
// strange because code: 39 is the down key AND ' key... 
// and DEL also equals . 
if (!e.ctrlKey && code!=9 && code!=8 && code!=36 && code!=37 && code!=38 && (code!=39 || (code==39 && character=="'")) && code!=40) { 
if (character.match(restrictionType)) { 
return true; 
} else { 
return false; 
} 
} 
} 


var state = 'block'; 

function showhide(layer_ref) { 

if (state == 'block') { 
state = 'none'; 
} 
else { 
state = 'block'; 
} 
if (document.all) { //IS IE 4 or 5 (or 6 beta) 
eval( "document.all." + layer_ref + ".style.display = state"); 
} 
if (document.layers) { //IS NETSCAPE 4 or below 
document.layers[layer_ref].display = state; 
} 
if (document.getElementById &&!document.all) { 
hza = document.getElementById(layer_ref); 
hza.style.display = state; 
} 
} 

function overlay(ref) {
	jobRef = document.getElementById("ref");
	jobRef.innerHTML=ref;
	jobRefInput = document.getElementById("jobref");
	jobRefInput.value=jobRef.innerHTML;
	el = document.getElementById("progressBackgroundFilter");
	el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
	el1 = document.getElementById("overlay");
	el1.style.visibility = (el1.style.visibility == "visible") ? "hidden" : "visible";
}