var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function datevalidation(oObject)
{
var dt=oObject.value

if (isDate(oObject.value)==false)
{
    oObject.focus()
    return false
}

return true;
}

function loginwindow()
{
	newwindow('/intranet/startpage.fwx','menu_staff','top=0,left=0,width=970,height=700')
}

function trustloginwindow()
{
	newwindow('/trusts/menu.fwx','menu_trust','top=0,left=0,width=950,height=700,status,resizable,scrollbars',true)
}

function locumloginwindow()
{
	newwindow('/locums/menu.fwx','menu_locum','top=0,left=0,width=950,height=700,status,resizable,scrollbars',true)
}

function newwindow(url,target,windowproperties,forceurl) {
	cwindowproperties = ""

	if (windowproperties!="undefined")
		{cwindowproperties=windowproperties}

	if (forceurl == "undefined")
		{forceurl = false}

	whandle = window.open("",target,cwindowproperties)

	if (navigator.appName == 'Netscape')
		{forceurl = true}

	if (whandle.location.href == "about:blank" || forceurl == true)
		{whandle.location = url}

	whandle.focus()
}

function datepicker(id,justday) {
	cdate = eval('document.form.'+id+'.value');
	newwindow("datepicker.fwx?ID="+id+"&date="+cdate+"&justday="+justday,"datepicker","height=180,width=220");
}

function datepicker_return(cData,id) {
	eval('document.form.'+id+'.value = cData');
}

function find_trust(id,id2) {
	newwindow("trust_find.fwx?popup=yes&ID="+id+"&ID2="+id2,"trustfind","left=0,top=0,scrollbars,resizable,width=560,height=700");
}

function accounts_find_trust(id,id2) {
	newwindow("accounts_trust_find.fwx?popup=yes&ID="+id+"&ID2="+id2,"trustfind","left=0,top=0,scrollbars,resizable,width=560,height=700");
}

function find_trust_return(ref,name,id,id2) {
	eval('document.form.'+id+'.value = ref');
	eval('document.form.'+id2+'.value = name');
}

function find_locum(id,id2) {
	newwindow("locum_find.fwx?popup=yes&ID="+id+"&ID2="+id2,"locumfind","left=0,top=0,scrollbars,resizable,width=560,height=700");
}

function accounts_find_locum(id,id2) {
	newwindow("accounts_locum_find.fwx?popup=yes&ID="+id+"&ID2="+id2,"locumfind","left=0,top=0,scrollbars,resizable,width=560,height=700");
}

function find_locum_all(id,id2) {
	newwindow("locum_find_all.fwx?popup=yes&ID="+id+"&ID2="+id2,"locumfind","left=0,top=0,scrollbars,resizable,width=560,height=700");
}

function filledbylocum(id,id2,positionid) {
	newwindow("positions_filledby.fwx?popup=yes&ID="+id+"&ID2="+id2+"&PositionID="+positionid,"locumfind","left=0,top=0,scrollbars,resizable,width=560,height=700");
}

function find_locum_return(ref,name,id,id2) {
	eval('document.form.'+id+'.value = ref');
	eval('document.form.'+id2+'.value = name');
}

function resetfilter() {
	document.form.method.value = "reset";
	document.form.submit();
}

function positions_filter_cleartrust() {
	document.form.method.value = "cleartrust";
	document.form.submit();
}

function formvalidate(){
	return true;
}

function formsave() {
	if (formvalidate()==true){
	document.form.method.value = "save";
	document.form.submit();
	}
}

function formview() {
	if (formvalidate()==true){
	document.form.method.value = "view";
	document.form.submit();
	}
}

function formcancel() {
	document.form.method.value = "cancel";
	document.form.submit();
}

function formrestore() {
	document.form.method.value = "restore";
	document.form.submit();
}

function formsubmit(cObjectName,cValue) {
	eval('document.form.'+cObjectName+'.value = cValue');
	document.form.submit();
}

function formsubmitwithvalidate(cObjectName,cValue) {
	if (formvalidate()==true) {
	eval('document.form.'+cObjectName+'.value = cValue');
	document.form.submit();
	}
}
 
function formforward() {
	if (formvalidate()==true){
	if (confirm("This will mark all selected locums as having been forwarded?") == true)
	{document.form.method.value = "forward";
	document.form.submit();
	}}
}

function formtrustpositionfill() {
	if (confirm("This will mark this position as being filled by you?") == true)
	{document.form.method.value = "filled";
	document.form.submit();
	}
}

function formsendemailalerts() {
	if (formvalidate()==true){
	if (confirm("This will send all selected locums a EMAIL alert?") == true)
	{document.form.method.value = "emailalert";
	document.form.submit();
	}}
}

function formsendsmsalerts() {
	if (formvalidate()==true){
	if (confirm("This will send all selected locums a SMS alert?") == true)
	{document.form.method.value = "smsalert";
	document.form.submit();
	}}
}

function formsendtosuppliers() {
	if (confirm("This will send this position to all secondery suppliers?") == true)
	{document.form.method.value = "sendtosuppliers";
	document.form.submit();
	}
}

function formconfirm() {
	if (formvalidate()==true){
	document.form.method.value = "confirm";
	document.form.submit();
	}
}

function registrationsavepage() {
	if (formvalidate()==true){
	document.form.method.value = "save";
	document.form.submit();
	}
}

function formupload() {
	if (formvalidate()==true){
	change_object_stat('UploadLayer','visibility','hidden');
	change_object_stat('PleaseWaitLayer','visibility','visible');
	document.form.submit();
	}
}

function formset() {
	if (formvalidate()==true){
	document.form.method.value = "set";
	document.form.submit();
	}
}

function formsend() {
	if (formvalidate()==true){
	document.form.method.value = "send";
	document.form.submit();
	}
}

function timesheetprocess() {
	if (confirm("This will stop users from entering timesheets into this pay period?") == true)
	{document.form.method.value = "process";
	document.form.submit();
	}
}

function timesheetlockdown() {
	if (confirm("This will lock these timesheets down?") == true)
	{document.form.method.value = "lockdown";
	document.form.submit();
	}
}

function parentrefresh() {
	opener.location.reload( true );
}

function refresh() {
	window.location.reload( true );
}

function formreset() {
	document.form.reset();
}

function passwordreset() {
	if (confirm("This will reset the password to its system default?") == true)
	{document.form.method.value = "reset";
	document.form.submit();
	}
}

function formdelete() {
	if (confirm("This will delete the current record?") == true)
	{document.form.method.value = "delete";
	document.form.submit();
	}
}

function formmultidelete() {
	if (confirm("This will delete all selected records?") == true)
	{document.form.method.value = "delete";
	document.form.submit();
	}
}

function showalert(cmessage){
	alert(cmessage);
}

function listbox_moveitem(clistboxfrom,clistboxto){
  olistboxfrom = eval("document.form."+clistboxfrom);
  olistboxto = eval("document.form."+clistboxto);
  if (olistboxfrom.selectedIndex != -1)
  {
    var strtext = new String(olistboxfrom.options[olistboxfrom.selectedIndex].text);
    var strvalue = new String(olistboxfrom.options[olistboxfrom.selectedIndex].value);

    var newItem = new Option();
    newItem.text=strtext.substr(0);
    newItem.value=strvalue.substr(0);
    olistboxto.options[olistboxto.options.length]=newItem;
    olistboxfrom.options[olistboxfrom.selectedIndex]=null;

    olistboxfrom.selectedIndex=-1;
    olistboxto.selectedIndex=-1;
   }else
	{
	alert('Please select from the list');
    return;
	}
}

function fillhiddenlistboxvalues(clistbox,chiddenfield){
 olistbox = eval("document.form."+clistbox);
 ohiddenfield = eval("document.form."+chiddenfield);
 n = olistbox.options.length;
 ncomma = 0
 cstring = ""

 for (var lintAvail=0; lintAvail < n; lintAvail++)
  {
	if (olistbox.options[lintAvail].value != "")
	{
		if (ncomma == 1) 
			{cstring += ","}
		cstring += olistbox.options[lintAvail].value;}
		ncomma = 1;
  }
ohiddenfield.value = cstring;

}

function listbox_remove(clistbox){
  olistbox = eval("document.form."+clistbox)

  var numItems = 0;
  var srcLen = olistbox.options.length;

  if(olistbox.selectedIndex == -1)
  {
    alert('Please, select from the list');
    return;
  } 
    
  olistbox.options[olistbox.selectedIndex]=null;

}

function listbox_add(clistbox,ctextbox){
  olistbox = eval("document.form."+clistbox);
  otextbox = eval("document.form."+ctextbox);

  var strtext = new String(otextbox.value);
  var strvalue = new String(otextbox.value);

  if (strvalue=="") return;

  var newItem = new Option();
  newItem.text=strtext.substr(0);
  newItem.value=strvalue.substr(0);
 
  olistbox.options[olistbox.options.length]=newItem;
  otextbox.value = "";

  olistbox.selectedIndex=olistbox.options.length-1;
}

function span_mouseover(objRow) {
	objRow.style.color = "#c70b27";
}

function span_mouseout(objRow) {
	objRow.style.color = "#3a2a7d";
}

function span_click(link) {
	document.location.href = link;
}

function change_object_stat(objectname,propertyname,newvalue) {
	eval("document.getElementById('"+objectname+"').style."+propertyname+" = '"+newvalue+"'")
}

function closeallwindows() {
	whandle = window.open("","trusts","left=0,top=0,width=10,height=10")
	whandle.close()
	whandle = window.open("","locums","left=0,top=0,width=10,height=10")
	whandle.close()
	whandle = window.open("","registragtions","left=0,top=0,width=10,height=10")
	whandle.close()
	whandle = window.open("","reports","left=0,top=0,width=10,height=10")
	whandle.close()
	whandle = window.open("","maintenance","left=0,top=0,width=10,height=10")
	whandle.close()
	whandle = window.open("","accounts","left=0,top=0,width=10,height=10")
	whandle.close()
	whandle = window.open("logout.fwx","menu_staff","width=950,height=700,top=0,left=0 ")
}

function comparedates(date1,date2) {
	datefrom = new Date(date1.substr(6,4),date1.substr(3,2)-1,date1.substr(0,2));
	dateto = new Date(date2.substr(6,4),date2.substr(3,2)-1,date2.substr(0,2));

	if (datefrom>dateto)
		{return false;}
	return true;
}

function optionchange(oObject) {

	return true;
}

function objectonblur(oObject) {
	return true;
}

function objectonchange(oObject) {
	return true;
}

function invertselection(cObjectname) {
	for(var i=0; i < document.forms['form'].elements.length; i++){
		curr_elem = document.forms['form'].elements[i];
		if(curr_elem.type == 'checkbox' && curr_elem.name == cObjectname){
			curr_elem.checked = !curr_elem.checked;
		}
	}
}
