
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
 	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=MM_findObj(n,d.layers[i].document);
 	if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

//////////////////////////////////////////////////////////////////////////////////
//
//	Common JavaScript functions.
//
//	Inside this JS file is a few common functions which can be reused.
//
//	Keith Bamberger 11-09-2002.
//
//////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////
//  Common date functions.
//////////////////////////////////////////////////////////////////////////////////

//This function will take in a date and parse it through another date
//checking function and return the cursor to the starting point
//if it fails or pass out a true for the validity of the date.
function checkdate(objName) 
{
	var datefield = objName;

	if (chkdate(objName) == false) 
	{
		datefield.select();
		datefield.focus();
		return false;
	}
	else 
	{
		return true;
	}
}

//This function will check a date for validity and return a formatted date 
//in the form of dd-mm-yyyy. The date may be entered in many formats, 
//ddmmyyyy, ddmmyy, and with many spearators "-"," ","/","."
function chkdate(objName) 
{

	//var strDatestyle = "US"; //United States date style
	var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var dateSections = false;

	var datefield = objName;

	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;

	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";

	strDate = datefield.value;

	//Check to see if the user has added input.
	if (strDate.length < 1) 
	{
		//Allow focus to move on
		return true;
	}

	//Loop through the input box to see what has been added and sort out the values
	//to make the date check
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) 
	{
		//
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) 
		{
			//split up the date into the 3 sections, day month and year.
			//This split only takes place if the date has been entered
			//with the values "-"," ","/","." otherwise its just a date
			//not separated.
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			
			//Check for the number of separators "-"," ","/","."
			//If there are more or less than 3 items then there was an error
			if (strDateArray.length != 3) 
			{
				err = 1;
				//Bad date
				return false;
			}
			else 
			{
				strDay = strDateArray[2];
				strMonth = strDateArray[1];
				strYear = strDateArray[0];
			}
			dateSections = true;
		}
	}
	
	//if 3 date sections werent found its possible that the
	//date was entered without separators (Just numbers)
	if (dateSections == false) 
	{
		if (strDate.length>5) 
		{
			//Manually split up the date so that it can be checked
			strYear = strDate.substr(0, 4);
			strMonth = strDate.substr(4, 2);
			strDay = strDate.substr(6, 2);
		}
	}

	
	//Taken out because of dat standard change. YYYY-MM-DD
	//Check to see if the year section is 2 numbers or 4.
	//If its only 2 then add 19 or 20 as the year for a full year
	if (strYear.length == 2) 
	{
		//If the year is greater than 50 its probable that the 
		//user intends to use 19. so add it otherwise use 20.
		if (strYear > 50)
		{
			strYear = '19' + strYear;
		}
		else
		{
			strYear = '20' + strYear;
		}
	}
	
	// US style, Uncomment if you are to use the American date format
	//if (strDatestyle == "US") 
	//{
	//	strTemp = strDay;
	//	strDay = strMonth;
	//	strMonth = strTemp;
	//}
	
	//Insert day as a base 10 number to intDay
	intday = parseInt(strDay, 10);
	//Check that intDay is not Not a Number 
	if (isNaN(intday)) 
	{
		err = 2;
		return false;
	}

	//Insert month as a base 10 number to intMonth
	intMonth = parseInt(strMonth, 10);
	//Check that intMonth is not Not a Number 
	if (isNaN(intMonth)) 
	{
		for (i = 0;i<12;i++) 
		{
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) 
			{
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) 
		{
			err = 3;
			return false;
		}
	}

	//Insert year as a base 10 number to intYear
	intYear = parseInt(strYear, 10);
	//Check that intYear is not Not a Number 
	if (isNaN(intYear)) 
	{
		err = 4;
		return false;
	}

	//Check that the month is not outside the 12 month range
	if (intMonth>12 || intMonth<1) 
	{
		err = 5;
		return false;
	}

	//Check for the months that are 31 days
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) 
	{
		err = 6;
		return false;
	}

	//Check for the months that are only 30 days
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) 
	{
		err = 7;
		return false;
	}

	//Check for a leap year	
	if (intMonth == 2) 
	{
		if (intday < 1) 
		{
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) 
		{
			if (intday > 29) 
			{
				err = 9;
				return false;
			}
		}
		else 
		{
			if (intday > 28) 
			{
				err = 10;
				return false;
			}
		}
	}

	//Get the values ready for use outputting.
	var dayStr="0" + intday
	var monthStr="0" + intMonth
		
	//Uncomment the if to deal with American dates
	if (strDatestyle == "US") 
	{
		//Uncomment below if you want to see the Month as a name
		//datefield.value = strMonthArray[intMonth-1] + " " + intday + " " + strYear;
		datefield.value = monthStr.substr(monthStr.length-2, 2) + "-" + dayStr.substr(dayStr.length-2,2) + "-" + strYear;
	}
	else 
	{
		//Uncomment below if you want to see the Month as a name
		//datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
		datefield.value = strYear + "-" + monthStr.substr(monthStr.length-2, 2) + "-" + dayStr.substr(dayStr.length-2,2);
	}
	
	//All checks passed Return TRUE to valid date
	return true;
}

//Check for a leap year. Enter a number as the year.
function LeapYear(intYear)
{
	//Check for year divisible by 100 exactly
	if (intYear % 100 == 0) 
	{
		//if it is now check if it is divisible by 400 exactly
		if (intYear % 400 == 0) 
		{ 
			//If divisible by 400 and 100 exactly then Its
			//a leap year, so return Leap year TRUE
			return true; 
		}
	}
	//If year not divisible by 100 and 400 exactly
	else 
	{
		//Check to see if year is divisible by 4 exactly
		if ((intYear % 4) == 0) 
		{ 
			//if divisible by 4 exactly then its
			//a leap year, so return leap year TRUE.
			return true; 
		}
	}
	return false;
}

//Checking for between dates. If 2 dates are entered it may be that the
//user must enter 2 dates which are to occurr after each other.
function doDateCheck(from, to) 
{
	//If the dates are sequential then all is ok
	
	strDateFrom = from.value
	strDateTo = to.value
	
	var dateFrom = new Date(strDateFrom.substr(0, 4), strDateFrom.substr(5, 2) - 1, strDateFrom.substr(8, 2))
	var dateTo = new Date(strDateTo.substr(0, 4), strDateTo.substr(5, 2) - 1, strDateTo.substr(8, 2))
		
	if (dateFrom <= dateTo) 
	{
		//alert("The dates are valid.");
		return true;
	}
	//if they arent sequential
	else 
	{	
		//If you must Enter dates then comment out the code below
		//
		//if there is no dates at all then alert the user
		//if (from.value == "" || to.value == "") 
		//{
		//	alert("Both dates must be entered.");
		//	return false;
		//}
		//	
		//The dates are not sequential alert the user
		//else
		//{
			//alert("To date must occur after the from date.");
			//return false;
		//}
		
		
		
		//if there is no dates at all then the user isnt spcifying dates
		if (from.value == "" || to.value == "") 
		{
			return true;
		}
		else
		{
			//The dates are not sequential alert the user
			alert("To date must occur after the from date.");
			return false;
		}
		
		
	}
}


//////////////////////////////////////////////////////////////////////////////////
//  Common ListBox functions.
//////////////////////////////////////////////////////////////////////////////////

//This function will loop through a Listbox and check a value
//passed in to see if it exists. If it exists already then alert the user 
//and return a false for an existing entry.
//
//object = the listbox to check for existing items
//objecttoCheck = the object to check data from could be a text box, list box etc...
//errorString = error to give user in the alert
function checkListforExistingValue(object, objecttocheck)
{		
	
	//Firstly check to see if the Listbox is there.
	if (!object)
	{
		return true;
	}
	

	//Loop through the list box that you are checking
	for (var i = 0; i < object.length; i++) 
	{
		//check if objecttocheck is a text box
		if(objecttocheck.type=="text")
		{
			if(object.options[i].text == objecttocheck.value)
			{
				alert("already exists");
				return false;				
			}
		}
		
		//check if objecttocheck is a listbox
		if(objecttocheck.type=="select-one")
		{	
			if(object.options[i].text == objecttocheck.options[objecttocheck.selectedIndex].text)
			{
				alert("already exists");
				return false;
			}		
		}
	}		
}



