function isDate(strDate) {
// use dd/mm/yyyy 
	var myDate = strDate;
	var delimiterFirstInstance;
	var delimiterSecondInstance;
	var delimiterType;
	var monthPart;
	var dayPart;
	var yearPart;

	//accepts delimiting characters of either "/" or "-"
	
	delimiterFirstInstance = myDate.indexOf("/");
	if (delimiterFirstInstance == -1) {
		//check for the other allowed delimiter
		delimiterFirstInstance = myDate.indexOf("-");
		//if it is still not found, return false
		if (delimiterFirstInstance == -1) {
			return false;
		}
		else { 
		delimiterType = "-";
		}
	} 
	else { 
		delimiterType = "/";
	}

	delimiterSecondInstance = myDate.indexOf(delimiterType, (delimiterFirstInstance + 1));
	if (delimiterSecondInstance == -1) {
		return false;
	}
	
	
	monthPart =  myDate.substring((delimiterFirstInstance + 1), (delimiterSecondInstance));
	if(validateMonth(monthPart) == false) {
		 return false;
	}
	
	yearPart = myDate.substring((delimiterSecondInstance + 1), (myDate.length));

	if(validateYear(yearPart) == false) {
		return false;
	}

	dayPart = myDate.substring(0, delimiterFirstInstance);
	if(validateDay(monthPart, dayPart, yearPart) == false) {
		return false;
	} 
	else { 
		return true;
	}
}

function validateDay(m, d, y) {
	if((isNaN(d)) || d == "") {
		return false;
	}

	var mo = parseInt(m, 10);
	var da = parseInt(d, 10);
	var ye = parseInt(y, 10);

	if (da < 1) {
	 return false;
	}

	if ((mo == 4) || (mo == 6) || (mo == 9) || (mo == 11)) {
   //it is a 30 day month
		if (da > 30) {
			return false;
		}
	} 
	else if(mo == 2) {
	// it is february (either 28 or 29 depending on leap year)
		if (isLeapYear(ye) == true) {
			if (da > 29) {
			//leap years have 29 days in february
				return false;
			}
		} 
		else {
			if (da > 28) {
			//non leap years have 28 days in february
				return false;
			}
		}
	} 
	else {
	// it is a 31 day month
		if (da > 31) {
			return false;
		}
	}
	//if we made it through all of the above without falling out,
	//it must be a valid day for the given month and year
	return 
}

function validateMonth(mnth) {
	if((isNaN(mnth)) || mnth == "") {
		return false;
	}

	var intMonth = parseInt(mnth, 10);
	if((intMonth < 1) || (intMonth > 12)) {
		return false; //month must be between 1 and 12 (inclusive)
	} 
	else { 
		return true;
	}
}

function validateYear(yr) {
	if((isNaN(yr)) || yr == "") {
		return false;
	}
	
	var intYear = parseInt(yr, 10);

	if((intYear < 1970) || (intYear > 9999)) {
		return false; //year must be between 1970 and 9999 (inclusive)
	} 
	else { 
		return true;
	} 
}

function isLeapYear(yr) {
/* classic leap year calculation:
if the year is:
 evenly divisible by 4 and not evenly divisible by 100
 or
 evenly divisible by 400
then it is a leap year,
Otherwise it is not a leap year
*/

	if (((yr % 4 == 0) && (yr % 100 != 0)) || (yr % 400 == 0)) {
		return true;
	} 
	else { 
		return false;
	} 
}

function trim(inString) {
	var Output;
	
	Output = inString.replace(/\s+$/gi, "");
	Output = Output.replace(/^\s*/gi, "");
	
	return Output;
}

function checkTextFilled(strName, objText) {
	if (!objText) return true;
	
	var valueToCheck = trim(objText.value);
	
	if (valueToCheck == '') {
		alert('Please insert ' + strName);
		objText.focus();
		return false;
	}
	else
		return true;
}

function ValidateDateField(strFieldName, objDateText) {
	var ValidDateStrRe = /\b\d{2}[\/-]\d{2}[\/-]\d{4}\b/;

	if ( ValidDateStrRe.test(objDateText.value) && isDate(objDateText.value) ){
		return true;
	}
	else {
		alert("Please input dd/mm/yyyy for " + strFieldName);
		objDateText.focus();
		return false;
	}
}

function ValidDateFieldAllowEmpty(inTitle, inObject) {
	if ((trim(inObject.value) == '') || ValidateDateField(inTitle, inObject)) {
		return true;
	}
	else
		return false;
}

function ValidateMMYYYY(strFieldName, objDateText) {
	var ValidDateStrRe = /\b\d{2}[\/-]\d{4}\b/;

	if ( ValidDateStrRe.test(objDateText.value) && isDate('01/' + objDateText.value) ){
		return true;
	}
	else {
		alert("Please input mm/yyyy for " + strFieldName);
		objDateText.focus();
		return false;
	}
}

function isInteger(a){
	return a==parseInt(a,10)
}

function isNumeric(a){
	return !isNaN(a)
}

function ValidateNumberField(strFieldName, objNumberText) {
	var ValueToCheck;
	
//	ValueToCheck = objNumberText.value.replace(/,/g, '');
	ValueToCheck = objNumberText.value;
	
	if ((trim(ValueToCheck) != '') && isNumeric(ValueToCheck) ){
		return true;
	}
	else {
		alert("Please input valid number for " + strFieldName);
		objNumberText.focus();
		return false;
	}
}

function ValidateNumberFieldAllowEmpty(inTitle, inObject) {
	if ((trim(inObject.value) == '') || ValidateNumberField(inTitle, inObject)) {
		return true;
	}
	else
		return false;
}

function ValidateIntField(strFieldName, objText) {
	if (isNumeric(objText.value) && isInteger(objText.value)){
		return true;
	}
	else {
		alert("Please input integer value for " + strFieldName);
		objDateText.focus();
		return false;
	}
}

function ValidIntFieldAllowEmpty(inTitle, inObject) {
	if ((trim(inObject.value) == '') || ValidateIntField(inTitle, inObject)) {
		return true;
	}
	else
		return false;
}

function menu_mouse_over(inObjParent) {
    inObjParent.className = 'menu_main_over';
}

function menu_mouse_out(inObjParent) {
    inObjParent.className = 'menu_main';
}

function menu_arrow_over(inObj) {
    inObj.firstChild.src = '/images/menu_arrow_over.gif';
}

function menu_arrow_out(inObj) {
    inObj.firstChild.src = '/images/menu_arrow.gif';
}

function cancel_and_go(in_next_loc) {
	var confirm_msg = 'Are you sure you want to cancel?';
	
	if (confirm(confirm_msg)) {
		location.href = in_next_loc;
	}
}

function OpenNewWindow(NewWindowName, NewLocation, inHeight, inWidth, inScroll, inMenuBar) {
	var NewWindow;
	var w=screen.width; 
	var x=((w/2)-(inWidth/2));
	var h=screen.height; 
	var y=((h/2)-(inHeight/2));
    var extrTxt = '';
    
    if (inScroll) {
        extrTxt += ', scrollbars=yes';
    }

    if (inMenuBar) {
        extrTxt += ', menubar=yes';
    }

	NewWindow = window.open(NewLocation, NewWindowName,
		' height=' + inHeight + ', width=' + inWidth + ', left=' + x + ', top=' + y +
        ', resizable=yes' + extrTxt);
	NewWindow.focus();
}

function OpenFileManager(in_web_root, in_type) {
	var loc;

	loc = in_web_root + 'tools/file_manager.php?type=' + in_type;
		
	OpenNewWindow(in_type + '_file_manager', loc, 450, 600, false, false);
}

function OpenImageManager(in_web_root, in_type) {
	var loc;

	loc = in_web_root + 'tools/image_manager.php?type=' + in_type;
		
	OpenNewWindow(in_type + '_image_manager', loc, 500, 650, false, false);
}

function doOnFileView(in_file_loc) {
	OpenNewWindow('', in_file_loc, 550, 750, true, true);
}

function GetFileExtension(inFilename) {
	var tempArray = inFilename.split('.');
  return tempArray[tempArray.length-1].toLowerCase();
}

function setPointer() {
    if (document.all) {
        for (var i=0;i<document.all.length; i++) {
             document.all(i).style.cursor = 'wait';
        }
    }
}

function doOnReadMoreHover(objAnchor) {
  objAnchor.innerHTML = '> read more';
}

function doOnReadMoreHoverOut(objAnchor) {
  objAnchor.innerHTML = 'read more';
}

function doOnFindOutMoreHover(objAnchor) {
  objAnchor.innerHTML = '> Find Out More';
}

function doOnFindOutMoreHoverOut(objAnchor) {
  objAnchor.innerHTML = 'Find Out More';
}