//General library of support functions for StaffCV Jobseeker website
var bPopup = false;
var optYes = -1999997237;
var optNo = -1999997236;

var reloading = false;
var bChanged = false;
function changed() {
	bChanged = true;
}

function beforeUnload() {
	bPopup = false;
	if (bChanged) {// && !bPopup ) {
		return 'If you continue you will lose your unsaved changes.\r\nClick Cancel if you wish to save your information using the button at the bottom of the page.';
	}
}

function OpenCertDetails(code){
	thewindow = window.open('https://www.thawte.com/cgi/server/certdetails.exe?code='+code, 'anew', config='height=400,width=550,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,directories=no,status=no');
}	

function textAreaKeyUp(v, length) 
{
	alert( 'Warning: Value has been truncated to the maximum length of ' + length );
	return v.substr( 0, length );
}

/*
function toggleGroup( q ) {
	if( document.getElementById )
		var g = document.getElementById(q);
	else
		var g = document.all(q);
	g.style.display = g.style.display=="none"?"":"none"
	//document.mainForm.goto.value = q;
	//if( eval( 'document.mainForm.' + q + '.value==optYes || document.mainForm.' + q + 'init.value==optYes' ) )
	//	saveChanges_onclick(false);
}
*/
function getSingleElement( id ) {
	if( document.getElementById )
		return document.getElementById(id);
	else
		return document.all(id);
}

function getElements( name ) {
	if( document.getElementsByName )
		return document.getElementsByName(name);
	else
		return document.all(name);
}

function SelectAll(ElementName){
	chk = document.getElementsByName(ElementName);
	selAll = getSingleElement(ElementName + 'SelAllChk').checked;
	for( i=0; i<=chk.length-1; i++ )
		chk[i].checked = selAll;
	SelectedAll(ElementName);
}

function SelectedAll(ElementName){
	var sel=1;
	var c = document.getElementsByName(ElementName);
	for( i=0; i<=c.length-1; i++ ) {
		if (c[i].checked == false) {
			sel=0;
		}
	}
	getSingleElement(ElementName + 'SelAll').value=sel;
	getSingleElement(ElementName + 'SelAllChk').checked=(sel==1);
}

function AnySelected(ElementName){
	var c = document.getElementsByName(ElementName);
	for( i=0; i<=c.length-1; i++ ) {
		if (c[i].checked == true) {
			return true;
		}
	}
	return false;
}
	
function show(parent) {
	var rows = document.getElementById("mainForm").getElementsByTagName("TR");
	var i, row, q;
	for(i=0;i<rows.length;i++) {
		row = rows[i];
		//alert("q" + row.getAttribute("parentQ") + " : " + parent.id );
		if( ("q" + row.getAttribute("parentQ")) == parent.id) {
			//alert(row.getAttribute("parentA"));
			//alert(parent.value + ",");
			//alert( row.getAttribute("parentA").indexOf(parent.value + ",") != -1 );
			if( getAncestor(parent,"TR").style.display == '' && row.getAttribute("parentA").indexOf(parent.value + ",") != -1  )
				row.style.display = '';
			else
				row.style.display = 'none';
			q = row.getElementsByTagName("SELECT");
			if(q.length>0) show(q[0]);
		}
	}
}


// duplicated in empsite
function toggleGroup( q ) {
 var i;
 
 var g = getElements("q"+q+"g");
 var no = getElements("q"+q+"gn");
 var h = getSingleElement("q"+q+"h");
 var a = getSingleElement('q'+q).value;
 
 //alert( eval( 'document.mainForm.q' + q + '.value' ) );
 //alert( optYes );
 //alert( eval( 'document.mainForm.q' + q + '.value' ) == optYes );
 
 if( a == optYes ) {
  h.className = "questionGroupHeader";
  for(i=0;i<g.length;i++)
   g[i].style.display = "";
  for(i=0;i<no.length;i++)
   no[i].style.display = "none";
 }
 
 if( a == optNo ) {
  h.className = "questionGroupHeader";
  for(i=0;i<g.length;i++) {
   g[i].style.display = 'none';
  }
  for(i=0;i<no.length;i++)
   no[i].style.display = "";
 }
 
 if( a != optYes && a != optNo ) {
  for(i=0;i<g.length;i++)
   g[i].style.display = "none";
  for(i=0;i<no.length;i++)
   no[i].style.display = "none";
 } 
}

function getAncestor( element, tag )
{
	if( element.tagName == tag ) return element;
	if( element.parentNode ) return getAncestor( element.parentNode, tag );
}

function AssocForm(questionId) {
	var formId=AssocFormId(questionId);
	var fa = getSingleElement("q"+questionId+"fa");
	if(formId) {
		fa.style.display = "";
		}
	else {
		fa.style.display = "none";
		}			
}	

function AssocFormId(questionId) {
	var answer = getSingleElement("q"+questionId).value;
	if (answer) {
		return eval("af"+questionId+"["+answer+"]");
		}
}	

function popUpMoreInfo(questionId, CoId) {
	popupImproved("pre-more.aspx?CoId="+CoId+"&formId="+AssocFormId(questionId), 'asok', 600, 400, false, true);
}	

function confirmQuickApply(OkUrl, CancelUrl) {
	if( confirm( "This job has no application form. Click OK to be considered as an applicant, or Cancel to exit." ) )
		document.location = OkUrl;
	else
	{	
		if(CancelUrl>'') 
			document.location = CancelUrl;
	}
}

function radioSelectedValue(radioButtons) {
	for(var i=0; i < radioButtons.length; i++)
		if (radioButtons[i].checked == true)
				return radioButtons[i].value;
}

function radioSelectValue(radioButtons, valueToSelect) {
	for(var i=0; i < radioButtons.length; i++)
		if (radioButtons[i].value == valueToSelect)
			radioButtons[i].checked=true;
}

function indexOf(a, o) {
	for(i=0;i<a.length;i++) {
		if( a[i]==o ) return i;
	}
	return -1;
}

function overlibCenterMessage(text) {
	overlib('<table class=messagePleaseWait width=350 cellpadding=0 cellspacing=0><tr><td valign=middle width=36px><img src=img/msg-info.gif /></td><td valign=middle align=center>' + text + '</td></tr></table>', CENTERPOPUP, FULLHTML, WIDTH, 350);
}


function checkInvalidChars(e) {
	//just add any character to this string
	var iChars = "<";
	for (var i = 0; i < e.value.length; i++) {
		if (iChars.indexOf(e.value.charAt(i)) != -1) {
			e.value = removeCharacters(e);
			alert ("The character '<' was detected in your answer.\n This is an invalid character and has been removed.");
			return false;
  		}
	}
}

function removeCharacters( obj ) {
	//regexp of characters to remove
	var regChars = /</gi;
	return obj.value.replace(regChars, "");
}


function maskKeyPress(objEvent) {
	//variables
	var iKeyCode, strKey;  
	//browser detection
	var strUserAgent = navigator.userAgent.toLowerCase(); 
	var isIE = strUserAgent.indexOf("msie") > -1; 
	var isNS4 = !isIE && !isNS6  && parseFloat(navigator.appVersion) < 5; 
	var isNS6 = strUserAgent.indexOf("netscape6") > -1; 

	//regular expressions
	var reInValidChars = /</;
	var reKeyboardChars = /[\x00\x03\x08\x0D\x16\x18\x1A]/;
	
	if (isIE) {
		iKeyCode = objEvent.keyCode;
	} else {
		iKeyCode = objEvent.which;
	}
	
	strKey = String.fromCharCode(iKeyCode);

	if (reInValidChars.test(strKey) && !reKeyboardChars.test(strKey) && !checkClipboardCode(objEvent, strKey)) {
		//alert("Invalid Character Detected!\nKeyCode = " + iKeyCode + "\nCharacter =" + strKey);
		return false;
	}
}

function checkClipboardCode(objEvent, strKey) {
	var reClipboardChars = /[cvxz]/i;
	var strUserAgent = navigator.userAgent.toLowerCase(); 
	var isNS6 = strUserAgent.indexOf("netscape6") > -1; 
	if (isNS6)
		return objEvent.ctrlKey && reClipboardChars.test(strKey);
	else
		return false;
}

