// JavaScript Document
function findObj(theObj, theDoc){

var p, i, foundObj;  

	if (!theDoc) theDoc = document;

	if((p = theObj.indexOf("?")) > 0 && parent.frames.length){
		theDoc = parent.frames[theObj.substring(p+1)].document;
		theObj = theObj.substring(0,p);
	}
	
	if (!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
	
	for (i=0; !foundObj && i < theDoc.forms.length; i++) 
		foundObj = theDoc.forms[i][theObj];
	for (i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
		foundObj = findObj(theObj,theDoc.layers[i].document);
  	if (!foundObj && document.getElementById) foundObj = document.getElementById(theObj);

	return foundObj;

}

function ToggleShowDetails(nr){

	var firstrow = findObj('Details_' + nr);
	var nowclass = firstrow.className;

	if (nowclass == 'invisible'){
	
		findObj('Details_' + nr).className = 'showdetails';
	
	}else{
	
		findObj('Details_' + nr).className = 'invisible';
	
	}
				
}
//DEV v.2.04b
function cursor_wait() { 
	
	document.body.style.cursor = 'wait';

}

function toggleFavBTN(){

	var oldtext = findObj('toggleFavlink').innerHTML;
	var newtext = 'Add to Favourites';
	if (oldtext == newtext) newtext = 'Delete from Favourites';
	findObj('toggleFavlink').innerHTML = newtext;
	newtitle = 'Click here to ' + newtext.toLowerCase();
	findObj('toggleFavlink').title = newtitle;
	
}

function HideAllDetails(indicator, numberofrows){
	
	firstpartofname = 'Details_'+indicator+'_';

	for (i=1; i <= numberofrows; i++){
	
		findObj(firstpartofname + i).className = 'invisible';
		
	}
		
}

function setscroll(){

	window.scroll(0, Scroll_Position);

}

function initwindow(){

	setscroll();	

}

function allowNext(allow){

	if (findObj('nextbutton')){
		
		if (allow == 'true'){
			
			findObj('nextbutton').className = '';
			if (findObj('nextbuttonbot')) findObj('nextbuttonbot').className = '';
			
		}else{

			findObj('nextbutton').className = 'invisible';	
			if (findObj('nextbuttonbot')) findObj('nextbuttonbot').className = 'invisible';
		
		}
		
	}

}

function openPrereq(thetext, resetvarname, resetvarvalue, pagecomplete, donext, othertext)
{
	if (thetext == ''){
	
		findObj('prereqdiv').className = 'invisible';
		findObj('prerequisites').innerHTML = 'All prerequisits have been met.';
		
		if (donext == 'donext') allowNext('true');
		
	}else{
		findObj('prereqdiv').className = '';
		findObj('prerequisites').innerHTML = thetext;
		
		if (resetvarname != '') findObj(resetvarname).value = resetvarvalue;
		
		if (pagecomplete != 'true' && donext == 'donext') allowNext('false');
		
	}
	
	if (othertext != '') findObj('extraline').innerHTML = othertext;
		
}

function showMessage(thetext){
	
	if (thetext != ''){
		findObj('messagediv').className = '';
		findObj('messagedivtxt').innerHTML = thetext;
	}
	
}

function DoMessage(buttonname){

	if (findObj('RespondTXT')){
		
		thetext = findObj('RespondTXT').value;
		
		if (thetext == ''){
			
			alert("You cannot send an empty message.");
			
		}else{

			var theForm = findObj("respondform");
			var theaction = theForm.action + '&subaction=' + buttonname;
			theForm.action = theaction;
			theForm.submit();
				
		}
	
	}

}

function CheckNotEmpty(whichclicked){

	if (whichclicked == 'home'){

		if (findObj('Home_EmailAddress').value == ''){
			findObj('EmailRBTN')[1].checked = false;
			if (findObj('Comp_EmailAddress') != '') findObj('EmailRBTN')[0].checked = true;
			return false;
		}else{
			return true;	
		}

	}else if (whichclicked == 'work'){

		if (findObj('Comp_EmailAddress').value == ''){
			findObj('EmailRBTN')[0].checked = false;
			if (findObj('Home_EmailAddress') != '') findObj('EmailRBTN')[1].checked = true;
			return false;
		}else{
			return true;	
		}		
	
	}

}

function checkRadiobutton(which){

	if (which == 'home' && findObj('EmailRBTN')[0].checked == false){
	
		findObj('EmailRBTN')[1].checked = true;
		
	}else if (which == 'work' && findObj('EmailRBTN')[1].checked == false){
		
		findObj('EmailRBTN')[0].checked = true;
	
	}

}


function showfloat(floater, action){

	if (findObj(floater)){
		
		if (action == 1){
			findObj(floater).className = 'floatmenu';
		}else{
			findObj(floater).className = 'invisible';
		}
	
	}

}


function getConfirm(theid){

	var msg = '';
	switch(theid){
	case 1:
		msg = "Are you sure you want to login posing as this account?\n\nLogging in posing as an account gives you all the privileges of the account.\nThis includes changing data, passwords and settings but not making a new message change to 'read'.";
		break;
	case 2:
		msg = "Are you sure you want to end this user?\n\nEnding a candidate disallows access to Boardmatch through the account.\nAll data is kept in the system.";
		break;
	case 3:
		msg = "Are you sure you want to reactivate this user?\n\nReactivating an account allows access to Boardmatch through the account, with all privileges.";
		break;
	case 4:
		msg = "Are you sure you want to confirm this account?\n\nConfirming an account allows a board to enter positions and search for candidates.";
		break;
    case 5:
        msg = "Are you sure you want to end this user?\n\nEnding a board disallows access to Boardmatch through the account.\nAll data is kept in the system.";
        break;
		
	default:
		return false;
		break;
		
	}
	
	return confirm(msg);

}

function showAlert(thealertid){

	var msg = "";
	
	if (thealertid == 1) msg = "This can take a few seconds!\n\nA new popup with 'Finished' will appear afterwards";
	if (thealertid == 2) msg = "Finished";
	if (thealertid == 3) msg = "To access this part of the software the board will have to be confirmed by Boardmatch.";
	
	if (msg != "") return alert(msg);

}

function EmptyInput(theinputname){
	
	findObj(theinputname).value='';

}

function HideDiv(thedivname){
	
	findObj(thedivname).className = 'invisible';

}

function ToggleVis(thedivname){
	
	if (findObj(thedivname).className == 'invisible'){
		findObj(thedivname).className = '';
	}else{
		findObj(thedivname).className = 'invisible';
	}
	
	return false;
	
}

function EireNotEire(what, thelastitem){

	switch(what){
	case 'Comp':
			
		if (findObj('Comp_CountryDDB').value == 1){
		
			EmptyInput('Comp_Postcode');
			HideDiv('Comp_PostCodeTR');
			
		}else{
			
			findObj('Comp_PostCodeTR').className = '';
			if (findObj('Comp_CountryDDB').value != 2) findObj('Comp_DistrictCountyDDB').selectedIndex = thelastitem;
			
			
		
		}
	
		break;
	
	case 'Home':
	
		if (findObj('Home_CountryDDB').value == 1){
		
			EmptyInput('Home_Postcode');
			HideDiv('Home_PostCodeTR');
			
		}else{
			
			findObj('Home_PostCodeTR').className = '';
			if (findObj('Home_CountryDDB').value != 2) findObj('Home_DistrictCountyDDB').selectedIndex = thelastitem;
		
		}
	
		break;
	
	case 'Org':
	
		if (findObj('Address_CountryDDB').value == 1){
		
			EmptyInput('Address_PostcodeTXT');
			HideDiv('Address_PostCodeTR');
			
		}else{
			
			findObj('Address_PostCodeTR').className = '';
			if (findObj('Address_CountryDDB').value != 2) findObj('Address_DistrictCountyDDB').selectedIndex = thelastitem;
		
		}
	
		break;

	}

}

function ReplaceText(where, what){

	var thetextfield = findObj(where);
	if (thetextfield.value == '') findObj(where).value = messagetext_array[what];

}

function ReplaceBTN(intowhat){

	findObj('sendlink').name = intowhat;	

}

function ReplaceHeader(where, what){

	thediv = findObj('headertextdiv');
	thetext = "other ?";
	if (where == 'candidate' && (what == 'resend' || what == 'disclose' || what == 'decline')) thetext = headertext_array[what];
	if (where == 'board' && (what == 'resend' || what == 'decline' || what == 'accept' || what == 'match')) thetext = headertext_array[what];
	thediv.innerHTML = thetext;

}

function CheckMandatoryFields(pagetype){
	
	thefieldvalue1 = findObj('StartYearTXT').value;
	thefieldvalue2 = findObj('EndYearTXT').value;
	thefieldvalue3 = findObj('CurrentCBX').checked;

	if (pagetype == 'edu'){
	
		thefieldvalue4 = findObj('InstitutionNameTXT').value;
		thefieldvalue5 = findObj('LocationTXT').value;
		thefieldvalue6 = findObj('CountryTXT').value;
		
	}else if (pagetype == 'emp'){

		thefieldvalue4 = findObj('JobTitleTXT').value;
		thefieldvalue5 = findObj('LocationTXT').value;
		thefieldvalue6 = findObj('EmployerNameTXT').value;
		
	}else if (pagetype == 'vol'){
	
		thefieldvalue4 = findObj('OrganisationNameTXT').value;
		thefieldvalue5 = findObj('LocationTXT').value;
		thefieldvalue6 = 'n/a';
		
	}
	
	if (thefieldvalue1 == '' || thefieldvalue4 == '' || thefieldvalue5 == '' || thefieldvalue6 == '' || (thefieldvalue2 == '' && thefieldvalue3 == false)){
		
		alert('You have not entered a value in all mandatory fields\n\nThe values have NOT been saved.');

	}else{

		var theForm = findObj('saveform');
		theForm.submit();
				
	}
		
}

function showHelp(thepage){

	window.open('http://help.boardmatchireland.ie/help.php?page='+thepage,'helpwindow','width=750,height=600,scrollbars=yes,toolbar=no,location=no');	

}

function ChangeHelpLink(thepage){

	helppage = thepage;	

}

function MatchAndClose(TheType){
	
	switch (TheType){
	case 'A':
		
		var Field1 = findObj('MatchCandidateCBX[]');
		
		for (var i=0; i < Field1.length; i++){
		
			Field1[i].checked = false;
			
		}
		
		findObj('matchlistdiv').className = '';
		findObj('closelistdiv').className = '';
		findObj('MatchAndCloseActionText').innerHTML = "Matching a Candidate, Sending other Candidates a Message and Closing the Position.";

        var row = document.getElementById("adminMatchTypeRow");
        row.style.display = '';

		break;
		
	case 'B':

		for (var i=0; i < HiddenRowsArray.length; i++){
			
			var therow = 'PossibleMatchRow_' + HiddenRowsArray[i];
			findObj(therow).className = '';
			
		}
		
		findObj('matchlistdiv').className = 'invisible';
		findObj('closelistdiv').className = '';	
		findObj('MatchAndCloseActionText').innerHTML = "Closing the Position; the Organisation has found a person outside of Boardmatch.";

        var row = document.getElementById("adminMatchTypeRow");
        row.style.display = 'none';
		
		break;
		
	case 'C':

		for (var i=0; i < HiddenRowsArray.length; i++){
			
			var therow = 'PossibleMatchRow_' + HiddenRowsArray[i];
			findObj(therow).className = '';
			
		}
		
		findObj('matchlistdiv').className = 'invisible';
		findObj('closelistdiv').className = '';		
		findObj('MatchAndCloseActionText').innerHTML = "Closing the Position - No Match at all.";
		
        var row = document.getElementById("adminMatchTypeRow");
        row.style.display = 'none';

		break;
	
	default:
	
		alert('Invalid Choice');
		break;	
		
	}
	
	findObj('MatchCloseActionHDN').value = TheType;
		
}

function CheckMatch(rowid){
	
	var thevalue = '';
	var alterrow = '';
	var newclassname = '';
	var temparray = new Array();
	
	if (rowid != ''){
	
		var Field1 = findObj('MatchCandidateCBX[]');
		
		for (var i = 0; i < Field1.length; i++){
			
			if (Field1[i].checked){
			
				thevalue = Field1[i].value;
				temparray.push(thevalue);
				if (thevalue == rowid) alterrow = thevalue;	
					
			}
		
		}	
		
		if (alterrow == rowid) newclassname = 'invisible';
			
		var therow = 'PossibleMatchRow_' + rowid;
		findObj(therow).className = newclassname;
		
	}
	
	HiddenRowsArray = temparray;

}

function AdminMatchAndClose(buttonname){
	
	var TheType = findObj('MatchCloseActionHDN').value;
	var cont = true;
	var thevalue = '';	
	var thevaluearray = new Array();

	switch(TheType){
	
	case 'A':
		
		var Field1 = findObj('MatchCandidateCBX[]');
		
		if (Field1.length > 0){

			for (var i = 0; i < Field1.length; i++){
			
				if (Field1[i].checked) thevaluearray.push(Field1[i].value);
	
		
			}
						
			if (thevaluearray.length == 0){

				alert('No Candidate is selected - choose a Match or another option');// 2008-12-19 - pieter: fixed typo
				cont = false;
				
			}
			
			if (findObj('AdminToMatchedCandidateTXT').value == ''){

				alert('No message for matched Candidate entered - please enter a message for the Matched Candidate');
				cont = false;				
				
			}
			
			if (findObj('AdminToOrgAboutMatchedCandidateTXT').value == ''){

				alert('No message for Organisation about matched Candidate entered - please enter such a message for the Organisation');
				cont = false;				
				
			}


			if (thevaluearray.length != Field1.length){
				
				if (findObj('AdminToNotMatchedCandidateTXT').value == ''){
	
					alert('No message for non-matched Candidate(s) entered - please enter such a message for the non-matched Candidate(s)');
					cont = false;				
					
				}
				
				if (findObj('AdminToOrgAboutNotMatchedCandidateTXT').value == ''){
	
					alert('No message for Organisation about non-matched Candidate(s) entered - please enter such a message for the Organisation');
					cont = false;				
					
				}				
				
			}

		}
		
		break;
			
	case 'B':
	case 'C':	
		
		if (findObj('SendEmailCBX[]')){ // changed pieter 2008-12-19: debug added this if to circumvent javascript error when there are no Messages to be send (i.e. when there are no open Contacts between Position and Candidates.
		
			var Field2 = findObj('SendEmailCBX[]');
	
			if (Field2.length > 0){
	
				if (findObj('AdminToNotMatchedCandidateTXT').value == ''){
	
					alert('No message for non-matched Candidate(s) entered - please enter such a message for the non-matched Candidate(s)');
					cont = false;				
					
				}
				
				if (findObj('AdminToOrgAboutNotMatchedCandidateTXT').value == ''){
	
					alert('No message for Organisation about non-matched Candidate(s) entered - please enter such a message for the Organisation');
					cont = false;				
					
				}
				
			}
			
		}
		
		break;
			
	default:
		
		cont = false;
		break;
	}

	if (cont){

		var TheForm = findObj('respondform');
		var theaction = TheForm.action + '&subaction=' + buttonname + '&subadminaction=' + TheType;		
		
		TheForm.action = theaction;
		TheForm.submit();
	
	}

}

function Navi(sectionname, subpagename, tabname, prevposition, setvars){
	
	cursor_wait(); // DEV v.2.04b

	var ThePrev = '';
	var TheSetVars = '';
	
	if (arguments.length > 3) ThePrev = prevposition;
	if (arguments.length > 4) TheSetVars = setvars;

	if (linkDelay != 0 && IsBusy){ // DEV v.2.04b
		
		timer2 = setTimeout('Navi(\''+ sectionname + '\', \'' + subpagename + '\', \'' + tabname + '\', \'' + ThePrev + '\', \'' + TheSetVars + '\')', 100);
		
	}else{
		
		var fromscroll = 0;
		
		if (!document.all){
			fromscroll =  window.pageYOffset;
		}else{
			fromscroll =  document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
		}	
		
		fromlocation = Section+'.'+Sub_Section+'.'+Tab_Page+'.'+fromscroll;
		
		window.location='index.php?section=' + sectionname + '&sub=' + subpagename + '&tab=' + tabname + '&prevposition=' + ThePrev + '&setvars=' + TheSetVars + '&from=' + fromlocation;
	
	}
	
}
