/* common javascript code */
function doChange(a) {
var i;
var f=document.forms[0];
var stIndex=-1;
for (i=0;i<f.elements.length;i++) {
    if (f.elements[i]==a) {
	stIndex=i;
	break;
    }
}
if (stIndex>-1) {
    for (i=stIndex+1;i<f.elements.length;i++) {
	if (f.elements[i].name=='deleteSpec') {
	    break;
	}
	f.elements[i].checked=f.elements[stIndex].checked;
    }
}
}

function emptyBoxes(item) {
if (item.checked==true) {
    var f=document.forms[1];
    var stIndex=-1;
    for (i=0;i<f.elements.length;i++) {
	if (f.elements[i]==item) {
	    stIndex=i;
	    break;
	}
    }
    if (!f.elements[stIndex+1].disabled) {
	// see if there any disabled checkboxes with are true, in whcih case no turn off
	var found=false;
	for (i=2;i<7;i++) {
	    if (f.elements[stIndex+i].disabled && f.elements[stIndex+i].checked==true) {
		found=true;
		break;
	    }
	}
	if (!found) {
	    f.elements[stIndex+1].checked=false;
	}
    }
    if (!f.elements[stIndex+2].disabled) 
	f.elements[stIndex+2].checked=false;
    if (!f.elements[stIndex+3].disabled) 
	f.elements[stIndex+3].checked=false;
    if (!f.elements[stIndex+4].disabled) 
	f.elements[stIndex+4].checked=false;
    if (!f.elements[stIndex+5].disabled) 
	f.elements[stIndex+5].checked=false;
    if (!f.elements[stIndex+6].disabled) 
	f.elements[stIndex+6].checked=false;
}
}

function removeNone(item,offset) {
var i;
if (item.checked==true) {
    var f=document.forms[1];
    var stIndex=-1;
    for (i=0;i<f.elements.length;i++) {
	if (f.elements[i]==item) {
	    stIndex=i;
	    break;
	}
    }
    f.elements[stIndex-offset].checked=false;
    if (offset!=1) { // turn on view
	f.elements[stIndex-offset+1].checked=true;
    }
} else if (offset==1) { // can't turn off jsut view..
    var f=document.forms[1];
    var stIndex=-1;
    for (i=0;i<f.elements.length;i++) {
	if (f.elements[i]==item) {
	    stIndex=i;
	    break;
	}
    }
    for (i=1;i<6;i++) {
	if (f.elements[stIndex+i].checked==true) {
	    alert("You must have with View permissions if any of the others are selected");
	    f.elements[stIndex].checked=true;
	    break;
	}
    }
}
}

var popUpWin=0;
function popWindow(url,popUpName,width,height) {
if (popUpWin) {
    if (!popUpWin.closed) popUpWin.close();
}
popUpWin = open(url, popUpName, 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height);
}

function popWindowToolBar(url,popUpName,width,height) {
if (popUpWin) {
    if (!popUpWin.closed) popUpWin.close();
}
popUpWin = open(url, popUpName, 'toolbar=yes,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height);
}

function openInMainWindow(url) {
	opener.document.location.href=url;
	opener.focus();
}

function performContact(toId,viaId) {
var f=document.forms["contactForm"];
f.toId.value=toId;
f.viaId.value=viaId;
f.submit();
}

function doSaveApplication() {
var f=document.forms["contactForm"];
f.toId.value=toId;
f.viaId.value=viaId;
f.submit();
}

function performRemoteContact(toId,client,server) {
var f=document.forms["contactRemoteForm"];
f.toId.value=toId;
f.client.value=client;
f.server.value=server;
f.submit();
}

function performTextMsg(toId,viaId,client,server) {
var f=document.forms["composeTextMsgForm"];
f.toId.value=toId;
f.client.value=client;
f.server.value=server;
f.submit();
}

function goToReg1Page(){
window.location.href="reg1.jsp";
}

function characterCount(field,remaining)
{
	var maxCharacters = 160;
	if (field.value.length > maxCharacters)
	{
		field.value = field.value.substring(0, maxCharacters);
	}
	else
	{
		remaining.value = maxCharacters - field.value.length;
	}
}

function getElementByNameFromForm(theForm, name) {
	for (var i = 0; i < theForm.length; i++) {
		var elem = theForm.elements[i];
	    if (elem.name == name) {
		return elem;
	    }
	}
	alert("form element with name '" + name + "' not found.");
	return null;
}

function getElementByName(name) {
	return getElementByNameFromForm(document.forms[1], name);
}


function trimChar(inputString, trimChar) {
	if (typeof inputString != "string") { return inputString; }
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == trimChar) {
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == trimChar) {
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	return retValue;
}

function trim(s) {
	var inputString = s;
	var len = inputString.length;
	var newLen = 0;
	while (len > newLen) {
		inputString = trimChar(inputString, " ");
		inputString = trimChar(inputString, "\n");
		inputString = trimChar(inputString, "\t");
		len = newLen;
		newLen = inputString.length;
	}
	return inputString;
}

function getElementByValue(value) {
	theForm = document.forms[1];
	for (var i = 0; i < theForm.length; i++) {
		var elem = document.forms[1].elements[i];
	    if (elem.value == value) {
		return elem;
	    }
	}
	alert("form element with value '" + value + "' not found.");
	return null;
}

function confirmDelete(actionLink) {
 
// CONFIRM REQUIRES ONE ARGUMENT
 var message = "Please confirm that it is OK to delete";
 
 // CONFIRM IS BOOLEAN. THAT MEANS THAT
 // IT RETURNS TRUE IF 'OK' IS CLICKED
 // OTHERWISE IT RETURN FALSE
 var return_value = confirm(message);
 
 // TEST TO SEE IF TRUE|FALSE RETURNED
 if (return_value ) {
  window.location.href=""+actionLink;
 }

}

function confirmActivate(actionLink, item) {
 
// CONFIRM REQUIRES ONE ARGUMENT
 var message = "Please confirm that it is OK to activate this " + item;
 
 // CONFIRM IS BOOLEAN. THAT MEANS THAT
 // IT RETURNS TRUE IF 'OK' IS CLICKED
 // OTHERWISE IT RETURN FALSE
 var return_value = confirm(message);
 
 // TEST TO SEE IF TRUE|FALSE RETURNED
 if (return_value ) {
  window.location.href=""+actionLink;
 }

}

function confirmDeactivate(actionLink, item) {
 
// CONFIRM REQUIRES ONE ARGUMENT
 var message = "Please confirm that it is OK to deactivate this " + item;
 
 // CONFIRM IS BOOLEAN. THAT MEANS THAT
 // IT RETURNS TRUE IF 'OK' IS CLICKED
 // OTHERWISE IT RETURN FALSE
 var return_value = confirm(message);
 
 // TEST TO SEE IF TRUE|FALSE RETURNED
 if (return_value ) {
  window.location.href=""+actionLink;
 }

}

function confirmReset(actionLink) {
// CONFIRM REQUIRES ONE ARGUMENT
 var message = "Please confirm that it is OK to reset the password";
 
 // CONFIRM IS BOOLEAN. THAT MEANS THAT
 // IT RETURNS TRUE IF 'OK' IS CLICKED
 // OTHERWISE IT RETURN FALSE
 var return_value = confirm(message);
 
 // TEST TO SEE IF TRUE|FALSE RETURNED
 if (return_value ) {
  window.location.href=""+actionLink;
 }
}

function openInMainWindow(url) {
 opener.document.location.href=url;
 opener.focus();
 }
//<%String url = "deleteVacancy.do?mode=allNHS&id="+vacancy.getId();%>
//<a href="javascript:confirmDelete('<%=url%>');" >Delete</a>

function movepic(img_name,img_src) {
	document[img_name].src=img_src;
}
