function setField(f,a,d){
  if(a=='f') f.value=(f.value!=d)?f.value:''; // FOCUS
  if(a=='b') f.value=(f.value!='')?f.value:d; // BLUE
}

var alltags=document.all? document.all : document.getElementsByTagName("*");

function trimString(str) {
  str = str.replace( /^\s+/g, "" );// strip leading
  return str.replace( /\s+$/g, "" );// strip trailing
}

function getElementbyClass(rootobj, classname){
  var temparray=new Array();
  var inc=0;
  var rootlength=rootobj.length;
  for (i=0; i<rootlength; i++){
    if (rootobj[i].className==classname)
      temparray[inc++]=rootobj[i];
    }
  return temparray;
}

function doCloseAll(omit){
  var i=0;
  var collDiv=getElementbyClass(alltags, 'accContent');
  var collH3=getElementbyClass(alltags, 'accTitle open');
  while (collDiv[i]){
    if (collDiv[i].id!=omit){
	  collDiv[i].className='accContent hidden';
	  collH3[i].className='accTitle';
	}
	i++;
  }
}

function doExpand(curobj, cid){
  var acc = document.getElementById(cid);
  doCloseAll(cid);
  curobj.className=(curobj.className!='accTitle open')? 'accTitle open' : 'accTitle';
  acc.className=(acc.className=='accContent hidden')? 'accContent' : 'accContent hidden';
}

function submitThis(fm,fl){
  var thisForm = eval('document.'+fm);
  var thisField = eval('document.'+fm+'.'+fl);
  if(thisField.value!='')thisForm.submit(); 
}

function showAddressDetails(cb){
  if(cb.checked){
    document.getElementById("formSignUpPostalDetails").className = '';
  }else{
    document.getElementById("formSignUpPostalDetails").className = 'hidden';
  }
}

function isValidEmail(address) {
   if (address != '' && address.search) {
      if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
      else return false;
   }
   // allow empty strings to return true - screen these with either a 'required' test or a 'length' test
   else return true;
}

function isValidEmailStrict(address) {
   if (isValidEmail(address) == false) return false;
   var domain = address.substring(address.indexOf('@') + 1);
   if (domain.indexOf('.') == -1) return false;
   if (domain.indexOf('.') == 0 || domain.indexOf('.') == domain.length - 1) return false;
   return true;
}

function checkForm(f){
  var strMessage = '<strong>The following fields are required:</strong><ul>';
  var iCount = 0;
  var strError = '';
  var thisFld;
  
  // ##################
  // formFeedback
  // ##################
  if(f.id=='formFeedback'){
	  
	  var arrFields = f.requiredFields.value.split(",");
	  var arrNames = f.requiredNames.value.split(",");
	  for(var i=0; i<arrFields.length; i++){
		thisFld = eval('f.'+arrFields[i]);
		if(thisFld.value==''){
		  strError += '<li>' + arrNames[i] + '</li>';
		  thisFld.className='invalidField';
		}else{
		  thisFld.className='';
		  if(arrFields[i]=='txtEmail'){
			if(!isValidEmailStrict(thisFld.value)){
			  strError += '<li>Email address does not appear to be valid</li>';
			  thisFld.className='invalidField';
			}
		  }
		}
	  }
	  
  }
  
  
  // ##################
  // formAlumniRegister
  // ##################
  else if(f.id=='formAlumniRegister'){
	  
	  var arrFields = f.requiredFields.value.split(",");
	  var arrNames = f.requiredNames.value.split(",");
	  for(var i=0; i<arrFields.length; i++){
		thisFld = eval('f.'+arrFields[i]);
		if(thisFld.value==''){
		  strError += '<li>' + arrNames[i] + '</li>';
		  thisFld.className='invalidField';
		}else{
		  thisFld.className='';
		  if(arrFields[i]=='txtUsername'){
			if((thisFld.value).length<6){
			  strError += '<li>Username must be 6 or more characters</li>';
			  thisFld.className='invalidField';
			}
		  }
		  if(arrFields[i]=='txtEmail'){
			if(!isValidEmailStrict(thisFld.value)){
			  strError += '<li>Email address does not appear to be valid</li>';
			  thisFld.className='invalidField';
			}
		  }
		  if(arrFields[i]=='filePhoto'){
			var checkFileRegex = (/^.+\.((jpg)|(jpeg))$/i.test(thisFld.value));
			if(!checkFileRegex){
			  strError += '<li>Your photo must be JPG format</li>';
			  thisFld.className='invalidField';
			}
		  }
		}
	  }
	  
  }
  
  // ##################
  // formApply
  // ##################
  else if(f.id=='formApply'){
	  
	  if(f.ddlJobSelect.value=='' && f.ddlDepartment.value=='')strError += '<li>Please select either a job or a department</li>';
	  
	  var arrFields = f.requiredFields.value.split(",");
	  var arrNames = f.requiredNames.value.split(",");
	  for(var i=0; i<arrFields.length; i++){
		thisFld = eval('f.'+arrFields[i]);
		if(thisFld.value==''){
		  strError += '<li>' + arrNames[i] + '</li>';
		  thisFld.className='invalidField';
		}else{
		  thisFld.className='';
		  if(arrFields[i]=='txtEmail'){
			if(!isValidEmailStrict(thisFld.value)){
			  strError += '<li>Email address does not appear to be valid</li>';
			  thisFld.className='invalidField';
			}
		  }
		  if(arrFields[i]=='fileAttachment'){
			var checkFileRegex = (/^.+\.((pdf)|(doc)|(txt)|(rtf))$/i.test(thisFld.value));
			if(!checkFileRegex){
			  strError += '<li>Your CV must be either PDF, DOC, TXT or RTF format</li>';
			  thisFld.className='invalidField';
			}
		  }
		}
	  }
	  
  }

  // ##################
  // formSignUp
  // ##################
  else if(f.id=='formSignUp'){
	  
	  if(f.cbNewsletters1.checked)iCount++;
	  if(f.cbNewsletters2.checked)iCount++;
	  if(f.cbNewsletters3.checked)iCount++;
	  if(f.cbNewsletters4.checked)iCount++;
	  if(f.cbNewsletters5.checked)iCount++;
	  if(f.cbNewsletters6.checked)iCount++;
	  if(f.cbNewsletters7.checked)iCount++;
	  if(f.cbNewsletters8.checked)iCount++;
	  if(iCount==0)strError += '<li>Please select the newsletter(s) you would like to receive</li>'
	  
	  var arrFields = f.requiredFields1.value.split(",");
	  var arrNames = f.requiredNames1.value.split(",");
	  for(var i=0; i<arrFields.length; i++){
		thisFld = eval('f.'+arrFields[i]);
		if(thisFld.value==''){
		  strError += '<li>' + arrNames[i] + '</li>';
		  thisFld.className='invalidField';
		}else{
		  thisFld.className='';
		  if(arrFields[i]=='txtEmail'){
			if(!isValidEmailStrict(thisFld.value)){
			  strError += '<li>Email address does not appear to be valid</li>';
			  thisFld.className='invalidField';
			}
		  }
		}
	  }
	  
	  iCount = 0;
	  if(f.cbSendOptions1.checked)iCount++;
	  if(f.cbSendOptions2.checked)iCount++;
	  if(iCount==0)strError += '<li>Please choose email and/or mail delivery</li>'
	
	  if(f.cbSendOptions2.checked){
		arrFields = f.requiredFields2.value.split(",");
		arrNames = f.requiredNames2.value.split(",");
		for(var i=0; i<arrFields.length; i++){
		  thisFld = eval('f.'+arrFields[i]);
		  if(thisFld.value==''){
			strError += '<li>' + arrNames[i] + '</li>';
			thisFld.className='invalidField';
		  }else{
			thisFld.className='';
		  }
		}
	  }
	  
	  if(!f.cbAgree.checked)strError += '<li>Please indicate that you have read and agree to the <a href="#" title="#" style="font-weight:normal;">terms &amp; conditions</a></li>'; 
	  
  }

  // deal with results
  if(strError!=''){
	var v = document.getElementById("validationMessage");
	v.innerHTML = strMessage+strError+'</ul>';
	v.className = '';
	return false;
  }else{
	f.submit()
  }
}

function generateUsername(v){
  if(v==''){
	var fn = trimString(document.formAlumniRegister.txtFirstName.value);
	var ln = trimString(document.formAlumniRegister.txtLastName.value);
	var un = (fn.substr(0,1) + ln).toLowerCase();
	if(un.length>=6){
	  document.formAlumniRegister.txtUsername.value = un;
	}
  }
}

function toggleJobSelect(v){
  var j = document.getElementById('ddlJobSelect');
  var d = document.getElementById('ddlDepartment');
  d.disabled = false;
  if(v=='j' && j.value!='' && j.value!='Any job'){
	d.selectedIndex = 0;
	d.disabled = true;
  }
  if(v=='d' && d.value!='' && d.value!='Any department'){
	j.selectedIndex = 0;
  }
}

function bookmarksite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}

function setColor(o,state){
	if (!o) return;
	if(state && o.style) o.style.backgroundColor='#eeeeee';
	else if (o.style) o.style.backgroundColor='transparent';
}

function openPrintWindow(id) {
	if(id!=''){
		window.open('/common/print_profile?id='+id,'profileprint','width=600,height=350,scrollbars=yes');
	}
}
function openPrintQuotes(ids) {
	if(ids!=''){
		window.open('/common/print_quote?ids='+ids,'quoteprint','width=600,height=350,scrollbars=yes');
	}
}