/*
'======================================================================
' AUTHOR: Tim Sewell
' OVERVIEW: 
' Javascript functions
'
' MODIFICATION DETAILS (LAST 2)
' LAST EDITED: Tim Sewell - 28/04/2004
' COMMENTS:
' 
'======================================================================
*/

/*======================================================================
								OPEN POPUP WINDOWS
======================================================================*/

function loadPageExtras()
{
}

/*======================================================================
								LIST PAGING
======================================================================*/

function goNextPage(page,firstPage,lastPage)
{
	frm = document.forms.nextPage;
	frm.page.value = Number(page);
	frm.firstPage.value = Number(firstPage);
	frm.lastPage.value = Number(lastPage);
	frm.submit();
}

function clearPage()
{
	frm = document.forms.nextPage;
	frm.page.value = 0;
	frm.firstPage.value = "";
	frm.lastPage.value = "";
}

function checkKeyInput(e)
{
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	
 if ((code == 13))
 {
	frm = document.forms.nextPage;
 	frm.page.value = 0;
 	frm.firstPage.value = "";
 	frm.lastPage.value = "";
  	frm.submit();
 }
}

function showViaLoadingPage(url,xWidth,xHeight)
{
	popupMainWindow1('loading.asp',xWidth,xHeight);
	setTimeout("mainWindow1.location.href='"+url+"';",550);

}


function reorderResults(frmName,frmField,orderBy)
{
	
	var frm = document.forms[frmName];
	var elem = document.forms[frmName].elements[frmField];
	elem.value = orderBy;
	frm.submit();
	
}

function showFormProcessing()
{	

	if (document.getElementById('formSubmitButtons'))
	{
		
		document.getElementById('formSubmitButtons').style.display = 'none';

		if (document.getElementById('formSubmitProcessing'))
		{
		document.getElementById('formSubmitProcessing').style.display = 'inline';
		}
	}
}


function reloadOpenerAndClose()
{
	if ( window.opener && !window.opener.closed )
	{
		if (window.opener.nextPage)
			window.opener.nextPage.submit();
		else
			window.opener.location.reload();
			
		window.opener.focus();
	}
	
	window.close();
}


function closeAndFocusOpener()
{
	
	if ( window.opener && !window.opener.closed )
	{
		window.opener.focus();
	}
	window.close();
}


function popupMainWindow(url,height,width) 
{
	var wHeight = screen.height
	var wWidth = screen.width
	var isHeight=Number(height);
	var isWidth=Number(width);
	var wTop = (wHeight - isHeight) / 2
	var wLeft = (wWidth - isWidth) / 2
	mainWindow  =   window.open(url,"mainWindow",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+isWidth+'px,height='+isHeight+'px,top='+wTop+',left='+wLeft);
	mainWindow.focus();
}

function popupMainWindow1(url,height,width) {
	
	var wHeight = screen.height
	var wWidth = screen.width
	var isHeight=Number(height);
	var isWidth=Number(width);
	var wTop = (wHeight - isHeight) / 2
	var wLeft = (wWidth - isWidth) / 2
	mainWindow1  =   window.open(url,"mainWindow1",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+isWidth+',height='+isHeight+',top='+wTop+',left='+wLeft);
	mainWindow1.focus();
}

function popupMainWindow2(url,height,width) {
	
	var wHeight = screen.height
	var wWidth = screen.width
	var isHeight=Number(height);
	var isWidth=Number(width);
	var wTop = (wHeight - isHeight) / 2
	var wLeft = (wWidth - isWidth) / 2
	mainWindow2  =   window.open(url,"mainWindow2",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+isWidth+',height='+isHeight+',top='+wTop+',left='+wLeft);
	mainWindow2.focus();
}

function centerScreen(xWidth,xHeight)
{
	var wHeight = screen.height
	var wWidth = screen.width
	var uAgent = navigator.userAgent
	if(uAgent.indexOf("Windows NT 5.1")!=-1)
	{
		xHeight = xHeight+30;
	}
	window.resizeTo(xWidth,xHeight);
	var wHeight = screen.height-xHeight;
	var wWidth = screen.width-xWidth;
	window.moveTo(wWidth/2,wHeight/2);
}

/*======================================================================
								CHECK FOR DIGITS
======================================================================*/

function isDigits(str) {
	var i
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if ((mychar < "0" || mychar > "9") && mychar !=".")
			return false;
	}
	return true;
}

function IsNumeric(strString)
  //  check for valid numeric strings	
  {
  var strValidChars = "0123456789.-";
  var strChar;
  var blnResult = true;

  if (strString.length == 0) return false;

  //  test strString consists of valid characters listed above
  for (i = 0; i < strString.length && blnResult == true; i++)
     {
     strChar = strString.charAt(i);
     if (strValidChars.indexOf(strChar) == -1)
        {
        blnResult = false;
        }
     }
  return blnResult;
  }

/*======================================================================
								CHECK FOR VALID EMAIL
======================================================================*/

function checkemail(s)
{
    var str = s;
    var filter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
    if (filter.test(str))
        testresults=true
    else
    {
        testresults=false
    }
    return (testresults)
}

function checkPassword(txtField) 
    {
	
	var error = 0;
	var errorChar = 1;
	var errorNum = 1;
	var validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWQYZ1234567890";
	var reNum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWQYZ"
	var reAlph = "1234567890"

	var password = txtField.value;

	// iterate through string and check against allowed character list
	for (x=0; x<password.length; x++)
	{
		c = password.charAt(x);
		if (validChars.indexOf(c) == -1)
		{
			error = 1;
			break;
		}
	}
	
	// check to make sure contains numbers and characters
	for (x=0; x<password.length; x++)
	{
		c = password.charAt(x);
		if (reNum.indexOf(c) != -1)
		{
			errorChar = 0;
			break;
		}
	}
	// check to make sure contains numbers and characters
	for (x=0; x<password.length; x++)
	{
		c = password.charAt(x);
		if (reAlph.indexOf(c) != -1)
		{
			errorNum = 0;
			break;
		}
	}

	// display appropriate message
	

	if ((error == 1) || (password.length < 6) || (errorChar == 1) || (errorNum == 1))
	{
		alert("The PASSWORD must be a minimum of 6 characters and contain a mixture of letters and numbers")
		txtField.focus();
		return false;
	}
	else
		return true;
}

function checkLimit(txtField, prompt, minchars, maxchars) 
{
	if (txtField.value == ''){alert("Please enter data for "+prompt);txtField.focus();return false;}
	else
	{
		if(txtField.value.length < minchars || txtField.value.length > maxchars)
		{
			alert(prompt+" length should be between "+minchars+" to "+maxchars+" characters.");
			txtField.focus();
			return false;
		}
		else return true;
	}
}

function leftTrim(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function rightTrim(sString) 
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function msgBox(strMessage)
{
	alert(strMessage);
}

function submitFormCouncilorDetails()
{
	frm = document.forms.f1;
	
	if (frm.partyid.value == '')
	{
		alert("Please select your party");
		frm.partyid.focus();
		return false;
	}
	
	if (frm.wardid.value == '')
	{
		alert("Please select your ward");
		return false;
	}
	
	showFormProcessing();
	frm.submit();
}

function submitFormCouncilorPages()
{
	frm = document.forms.f1;
	
	if (frm.name.value == '')
	{
		alert("Please type a name for the page");
		frm.partyid.focus();
		return false;
	}
	
	if (frm.content.value == '')
	{
		alert("Please provide content");
		return false;
	}
	
	showFormProcessing();
	frm.submit();
}

function submitFormCouncilorDetailsMod()
{
	frm = document.forms.f1;
		
	showFormProcessing();
	frm.submit();
}

function retrieveCouncilorDetails()
{
	frm = document.forms.f1;
	frm.action = "/myWycombe.asp?step=15";
		
	showFormProcessing();
	frm.submit();
}

//--------------------------------------------------------------------------
function submitFormParishDetails()
{
	frm = document.forms.f1;
	
 	if (frm.contactName.value == ''){alert("Please enter contact name");frm.contactName.focus();return false;}
 	if (frm.address1.value == ''){alert("Please enter first line of address");frm.address1.focus();return false;}
 	if (frm.postcode.value == ''){alert("Please enter postcode");frm.postcode.focus();return false;}
 	if (frm.tel.value == ''){alert("Please enter telephone");frm.tel.focus();return false;}
	
	showFormProcessing();
	frm.submit();
}

function submitFormParishPages()
{
	frm = document.forms.f1;
	
	if (frm.name.value == '')
	{
		alert("Please type a name for the page");
		frm.partyid.focus();
		return false;
	}
	
	if (frm.content.value == '')
	{
		alert("Please provide content");
		return false;
	}
	
	showFormProcessing();
	frm.submit();
}

function submitFormParishDetailsMod()
{
	frm = document.forms.f1;
		
	showFormProcessing();
	frm.submit();
}

function retrieveParishDetails()
{
	frm = document.forms.f1;
	frm.action = "/myWycombe.asp?step=115";
		
	showFormProcessing();
	frm.submit();
}
//--------------------------------------------------------------------------

function confirmDelete()
{
	var agree = confirm("Are you sure you want to DELETE this content?")
	if (agree)
		return true;
	else
		return false;

}

function deleteMyWycombeContent(contentID,step)
{
	if(confirmDelete() == true)
	{
		document.location.href = "/myWycombe.asp?step="+step+"&contentID="+contentID;
	}
}


function orderChanged(obj,currPos,totalItems,selectItems)
{
	var i=0;
	var currentValue = Number(document.getElementById(selectItemsCurrValue[currPos-1]).value);
	if(checkIfPushRequired(obj,currentValue,totalItems,selectItems) == true)
	{
		if(Number(obj.value) > 0)
		{
			if(Number(obj.value) < currentValue)
			{
				for(i=0; i < totalItems; i++)
				{
					if(Number(document.getElementById(selectItems[i]).value) > 0 && obj != document.getElementById(selectItems[i]) && 
					   Number(document.getElementById(selectItems[i]).value)  >= Number(obj.value) && 
					   i + 1 < currPos)
					{
						document.getElementById(selectItems[i]).value = Number(document.getElementById(selectItems[i]).value) + 1;
					}
				}
			}
			else
			{
					if(currentValue > 0)
					{
					for(i=0; i < totalItems; i++)
					{
						if(Number(document.getElementById(selectItems[i]).value) > 0 &&  obj != document.getElementById(selectItems[i]) && 
						   Number(document.getElementById(selectItems[i]).value)  <= Number(obj.value) && 
						   i + 1 > currPos)
						{
							document.getElementById(selectItems[i]).value = Number(document.getElementById(selectItems[i]).value) - 1;
						}
					}
				}
				else
				{
					for(i=0; i < totalItems; i++)
					{
						if(Number(document.getElementById(selectItems[i]).value) > 0 && obj != document.getElementById(selectItems[i]))
						{
							if(Number(document.getElementById(selectItems[i]).value) >= Number(obj.value))
							{
								document.getElementById(selectItems[i]).value = Number(document.getElementById(selectItems[i]).value) + 1;
							}
						}
					}
				}
			}
		}
		else
		{
			for(i=0; i < totalItems; i++)
			{
					if(Number(document.getElementById(selectItems[i]).value) > 0 && obj != document.getElementById(selectItems[i]) && 
					   Number(document.getElementById(selectItems[i]).value) >= Number(obj.value) &&
					   i + 1 > currPos)
				{
					document.getElementById(selectItems[i]).value = Number(document.getElementById(selectItems[i]).value) - 1;
				}
			}
		}
	}
	document.forms["nextPage"].submit();
}

function checkIfPushRequired(obj,currentValue,totalItems,selectItems)
{
	if(Number(obj.value) == 0 && currentValue > 0)
	{
		return true;
	}
	else
	{
		var i=0;
		for(i=0; i < totalItems; i++)
		{
			if(Number(document.getElementById(selectItems[i]).value) > 0)
			{
				if(Number(document.getElementById(selectItems[i]).value) == Number(obj.value) && obj != document.getElementById(selectItems[i]))
				{
					return true;
				}
			}
		}
		return false;
	}
}


function printWindow(){
   bV = parseInt(navigator.appVersion)
   window.focus();
   if (bV >= 4) window.print()
}
