function getObj(n, d) {
  var p,i,x;  
  if(!d) {
		d=document; 
	}
	if((p=n.indexOf("?")) > 0 && parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; 
    n=n.substring(0,p);
  }
  if(!(x=d[n]) && d.all) {
		x=d.all[n]; 
	}
	for (i=0; !x && i<d.forms.length; i++) {
		x=d.forms[i][n];
	}
  for(i=0; !x&&d.layers && i<d.layers.length; i++) {
		x=getObj(n,d.layers[i].document);
	}
  if(!x && d.getElementById) {
		x=d.getElementById(n); 
	}
	return x;
}

function getThingForPreview(thing) {
	var parentPage = window.dialogArguments;
	stuff = eval('parentPage.document.theform.' + thing + '.value');
	stuff = replace(stuff,'../media/',"media/");
	document.write(stuff);
}

function replace(argvalue, x, y) {
  if ((x == y) || (parseInt(y.indexOf(x)) > -1)) {
    errmessage = "replace function error: \n";
    errmessage += "Second argument and third argument could be the same ";
    errmessage += "or third argument contains second argument.\n";
    errmessage += "This will create an infinite loop as it's replaced globally.";
    alert(errmessage);
    return false;
  }
    
  while (argvalue.indexOf(x) != -1) {
    var leading = argvalue.substring(0, argvalue.indexOf(x));
    var trailing = argvalue.substring(argvalue.indexOf(x) + x.length, 
	argvalue.length);
    argvalue = leading + y + trailing;
  }

  return argvalue;
}

function candid_valstep1() {
	var bad='';
	if (getObj('c_FName').value == '') {
		bad += 'First Name\n';
	}
	if (getObj('c_SName').value == '') {
		bad += 'Surname\n';
	}
	if (getObj('c_DayTel').value == '') {
		bad += 'Day Time Tel.\n';
	}
	if (getObj('c_location').options.value == 0) {
		bad += 'Home Location\n';
	}
	if (emailCheck(getObj('c_Email').value) == false) {
		bad += 'Email Address\n';
	}
	if (getObj('c_pass1').value.length < 5) {
		bad += 'PassWord must be 5 characters in Length\n';
	}
	if (getObj('c_pass2').value.length < 5) {
		bad += 'Confirm PassWord must be 5 characters in Length\n';
	}
	if (getObj('c_pass1').value != getObj('c_pass2').value) {
		bad += 'Passwords do not match';
	}
	if (getObj('c_sec_answer').value == '') {
		bad += 'Security Answer\n';
	}
	if (getObj('c_Sec_Question').options.value == 0) {
		bad += 'Security Question\n';
	}
	if (bad != '') {
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	} else {
		getObj('next').value = '1';
		document.candidate.submit();
	}
	
}
function candid_valstep2(nextprev) {
	var bad='';
	var checks = false;
	
	for(i=0; i<document.forms[0].elements.length; i++){
		if (document.forms[0].elements[i].type == "checkbox") {
			 if (document.forms[0].elements[i].checked) {
			 	checks = true;
			 }
		}
	}
	if (getObj('c_AvailableDate_month').options.value == 0) {
		bad += 'Please select a start work month\n';
	}
	if (getObj('c_AvailableDate_year').options.value == 0) {
		bad += 'Please select a start work year\n';
	}
	if (checks==false) {
		bad += 'Please select where you be prepared to work.\n';
	}	

	if (bad != '') {
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	} else {
		getObj('next').value = '1';
		document.candidate.submit();
	}
}

function candid_valstep3(nextprev) {
	var bad='';
	
	if (getObj('cvuploaded').value == '') {
		bad += 'You must upload your CV.\n';
	}
	if (bad != '') {
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	} else {
		getObj('next').value = '1';
		document.candidate.submit();
	}
}
function candid_valstep4(nextprev) {
	var bad='';
	
	if (getObj('c_jobtitle').value == '') {
		bad += 'Job / Role Title.\n';
	}
	if ((getObj('c_profile').value == '') || (getObj('c_profile').value == 'Important - please list your technical skills here')) {
		bad += 'Profile\n';
	}
	if (bad != '') {
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	} else {
		getObj('next').value = '1';
		document.candidate.submit();
	}
}


function valfriend() {
	var bad='';
	if (emailCheck(getObj('friendEmail').value) == false) {
		bad += 'Colleagues Email Address\n';
	}
	if (bad != '') {
		msg = 'The following fields are incorrect.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	} else {
		document.recfriend.submit();
	}
}

function selectUK() {
	for(i=0; i<document.forms[0].elements.length; i++){
		var theplace = document.forms[0].elements[i].value;
		var isUK = theplace.indexOf('UK');
		if (isUK > -1) {
			document.forms[0].elements[i].checked = true;
		}
	}
}




function emailCheck(emailStr) {
	var emailPat = /^([.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
	var matchArray = emailStr.match(emailPat);
	if (matchArray == null) {
		return false;
	}

	var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
	if (IPArray != null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
      }
    }
	}
	return true;
}

function UploadCV()
{
	var winl = (screen.width - 400) / 2;
	var wint = (screen.height - 200) / 2; 
    mywindow = open('customscripts/upload.asp','upload','status=yes,resizable=no,width=400,height=200,top=' + wint + ',left=' + winl);
    if (mywindow.opener == null)
    {
        mywindow.opener = self;
    }
}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) { 
		field.value = field.value.substring(0, maxlimit);
	} else {
		countfield.value = maxlimit - field.value.length;
	}
}

function validate() {
	var bad='';
	if (getObj('FName').value == '') {
		bad += 'First Name\n';
	}
	if (getObj('SName').value == '') {
		bad += 'Surname\n';
	}
	if (emailCheck(getObj('Email').value) == false) {
		bad += 'Email\n';
	}
	if (getObj('Hospital').value == '') {
		bad += 'Hospital\n';
	}
	if (getObj('Department').value == '') {
		bad += 'Department\n';
	}
	if (getObj('Jobtitle').value == '') {
		bad += 'Job Title\n';
	}
	if (getObj('Tel').value == '') {
		bad += 'Telephone\n';
	}
	if (getObj('pass1').value.length < 5) {
		bad += 'PassWord must be 5 characters in Length\n';
	}
	if (getObj('pass2').value.length < 5) {
		bad += 'Confirm PassWord must be 5 characters in Length\n';
	}
	if (getObj('pass1').value != getObj('pass2').value) {
		bad += 'Your Passwords do not match';
	}
	
	if (bad != '') {
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	} else {
		document.contact.submit();
	}
}



function validateclientprofile() {
	var bad='';
	if (getObj('FName').value == '') {
		bad += 'First Name\n';
	}
	if (getObj('SName').value == '') {
		bad += 'Surname\n';
	}
	if (emailCheck(getObj('Email').value) == false) {
		bad += 'Email\n';
	}
	if (getObj('Hospital').value == '') {
		bad += 'Hospital\n';
	}
	if (getObj('Department').value == '') {
		bad += 'Department\n';
	}
	if (getObj('Jobtitle').value == '') {
		bad += 'Job Title\n';
	}
	if (getObj('Tel').value == '') {
		bad += 'Telephone\n';
	}
	if (getObj('password1').value == '') {
		bad += 'Your Passwords are incorrect or do not match\n';
	}
	if (getObj('password1').value != getObj('password2').value) {
		bad += 'Your Passwords are incorrect or do not match\n';
	}

	if (bad != '') {
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	} else {
		document.editclientprofile.submit();
	}
}

function starttour() {
	var winl = (screen.width - 500) / 2;
	var wint = (screen.height - 450) / 2; 
    mywindow=open('tour.htm','max20Tour','status=yes,resizable=no,width=500,height=450,top=' + wint + ',left=' + winl);
    //if (mywindow.opener == null) mywindow.opener = self;
}

function submitsearch() {
	if (getObj('keyword').value == '') {
		alert('Please enter a Keyword');
	} else {
		document.search.submit();
	}
}

//FORUM FUNCTIONS
//----------------------------------------------------------------------
function chkmsg() {
	var bad='';
	if (getObj('name').value == '')
		bad += 'Name\n';

	if (getObj('email').value == '')
		bad += 'Email\n';

	if (getObj('msgtitle')) {
		if (getObj('msgtitle').value == '')
			bad += 'Title\n';
	}

	if (getObj('msg').value == '')
		bad += 'Message\n';
	
	if (bad != '') {
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	} else {
		document.postmsg.submit();
	}
}

function clearmsg() {
	var themsg='';
	themsg = getObj('c_profile').value;
	if (themsg == 'Important - please list your technical skills here') {
		getObj('c_profile').value = '';
	}
}
function entermsg() {
	var themsg='';
	themsg = getObj('c_profile').value;
	if (themsg == '') {
		getObj('c_profile').value = 'Important - please list your technical skills here';
	}
}

//-----------------------------------------------------------------------------
function ChangePager(newPage, formName, theType)
{
    var thePage = getObj('pager');
    thePage.value = newPage;
    
    var theForm = eval('document.' + formName);
    
    if (getObj('page_search_type') != null)
    {
        getObj('page_search_type').value = theType;
    }
    
    theForm.submit();
    
    return;
}


//-----------------------------------------------------------------------------
function matchheights() {
	var leftdiv = getObj('leftcol');
	var rightdiv = getObj('rightcol');
	var L_Height=0;
	var R_Height=0;
	
	if(leftdiv.offsetHeight){ 
		L_Height=leftdiv.offsetHeight; 
	} 
	else if (leftdiv.style.pixelHeight) { 
		L_Height=leftdiv.style.pixelHeight; 
	} 
	if(rightdiv.offsetHeight){ 
		R_Height=rightdiv.offsetHeight; 
	} 
	else if (rightdiv.style.pixelHeight) { 
		R_Height=rightdiv.style.pixelHeight; 
	} 

	maxHeight=Math.max(L_Height,R_Height);
	if (maxHeight < 400)
		maxHeight = 400;
	leftdiv.style.height = maxHeight + 'px';
	rightdiv.style.height = (maxHeight + 42) + 'px';
}

//-----------------------------------------------------------------------------
function matchhomeheights() {
	var leftdiv = getObj('leftcol');
	var rightdiv = getObj('rightcol');
	var L_Height=0;
	var R_Height=0;
	
	if(leftdiv.offsetHeight){ 
		L_Height=leftdiv.offsetHeight; 
	} 
	else if (leftdiv.style.pixelHeight) { 
		L_Height=leftdiv.style.pixelHeight; 
	} 
	if(rightdiv.offsetHeight){ 
		R_Height=rightdiv.offsetHeight; 
	} 
	else if (rightdiv.style.pixelHeight) { 
		R_Height=rightdiv.style.pixelHeight; 
	} 

	maxHeight=Math.max(L_Height,R_Height);
	leftdiv.style.height = maxHeight + 'px';
	rightdiv.style.height = maxHeight + 'px';
	
	var home_right_content = getObj('home_right_content');
	home_right_content.style.height = maxHeight + 65 + 'px';
	
	var home_left_content = getObj('home_left_content');
	home_left_content.style.height = maxHeight + 65 + 'px';
	
	
}

//-----------------------------------------------------------------------------
function OverLoginCand(bOver)
{
    var theImg = getObj('login_btn_cand');
    if (bOver == true)
    {
        theImg.src = 'fx/login_on_cand.gif';
    }
    else
    {
        theImg.src = 'fx/login_off_cand.gif';
    }
}

//-----------------------------------------------------------------------------
function OverLoginClient(bOver)
{
    var theImg = getObj('login_btn_client');
    if (bOver == true)
    {
        theImg.src = 'fx/login_on_client.gif';
    }
    else
    {
        theImg.src = 'fx/login_off_client.gif';
    }
}

//-----------------------------------------------------------------------------
function OverSearch(bOver)
{
    var theImg = getObj('search_btn');
    if (bOver == true)
    {
        theImg.src = 'fx/search_on.gif';
    }
    else
    {
        theImg.src = 'fx/search_off.gif';
    }
}

//-----------------------------------------------------------------------------
function OpenCenteredWindow(url, height, width, name, parms)
{
    var left = Math.floor( (screen.width - width) / 2);
    var top = Math.floor( (screen.height - height) / 2);
    var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
    if (parms)
    {
        winParms += "," + parms;
    }
    var win = window.open(url, name, winParms);
    if (parseInt(navigator.appVersion) >= 4 && win != null)
    {
        win.window.focus();
    }
}

//-----------------------------------------------------------------------------
function ViewCV(id)
{
    OpenCenteredWindow('customscripts/viewCV.asp?id=' + id, '500', '500', null, 'status=yes, resizable=yes, scrollbars=yes');
}