//	when passed a file name it checks for PDF file types

function checkPDFFileExtensions(fileObj){
	var ext = getFileExtension(fileObj.value);

	if (ext.toUpperCase() != 'PDF' && ext != '')
	{
		alert("Please select only pdf file for upload.");
		fileObj.focus();
		return false;
	}

	return true;
}

//	when passed a file name it checks for compatible file types that are allowed

function checkImageFileExtensions(fileObj){
	var ext = getFileExtension(fileObj.value);

	if (ext.toUpperCase() == 'GIF')
	{
		if (confirm("JPEG/JPG image is recommended for uploading.\nDo you want to continue uploading a gif image?"))
		{
			return true;
		}
		else
		{
			fileObj.focus();
			return false;
		}
	}

	if (ext.toUpperCase() != 'JPG' && ext != '')
	{
		alert("Please select only jpeg or gif image for upload.");
		fileObj.focus();
		return false;
	}

	return true;
}

function getFileExtension(filePath) { //v1.0
  fileName = ((filePath.indexOf('/') > -1) ? filePath.substring(filePath.lastIndexOf('/')+1,filePath.length) : filePath.substring(filePath.lastIndexOf('\\')+1,filePath.length));
  return fileName.substring(fileName.lastIndexOf('.')+1,fileName.length);
}
 
function OpenNewWindow(imageurl){
var Imagepage = imageurl;var windowheight = 480;var windowwidth = 640;
var windowleft = (screen.width - parseInt(windowwidth)) / 2;
var windowtop = (screen.height - parseInt(windowheight)) / 2;
var windowname = "PopupWindow";
windowproperties = 'resizable=no,status=no,toolbar=no,height='+windowheight+',width='+windowwidth+',top='+windowtop+',left='+windowleft+',scrollbars=yes'
setwindow = window.open(Imagepage, windowname, windowproperties)
	if (CheckNavigatorVersion()==true){
			setwindow.window.focus();}
}  

function popup(url , windowname, width , height){

var top = 100;
var left = 100;
var windowleft = (screen.width - parseInt(width)) / 2;
var windowtop = (screen.height - parseInt(height)) / 2;
var windowname = "PopupWindow";
windowproperties = 'resizable=no,status=no,toolbar=no,height='+height+',width='+width+',top='+top+',left='+left+',scrollbars=yes'

setwindow = window.open(url, windowname, windowproperties)
	if (CheckNavigatorVersion()==true){
			setwindow.window.focus();}
}  

function CheckNavigatorVersion()
{
	if (parseInt(navigator.appVersion) >= 4)
		{
			return true;
		}
    	return false;

}

function MustEnter(obj, field)
{
	//if (  isObject(obj)  )
	{
		if (obj.value == "") 
		{
			alert("Please enter " + field);
			obj.focus();
			obj.select();
			return false;
		}
	}
	return true;
}

function MustEnterInt(obj, field)
{
//	if (  isObject(obj)  )
	{
		if ( MustEnter(obj, field) )
		{
			if (isNaN(obj.value) )
			{
				alert("Please enter a valid integer for " + field);
				obj.focus();
				obj.select();
				return false;
			}
		}	
		return true;
	}
	
	return false;
}

function validateEmail(field, fieldName){
	var str
	str = field.value;
		
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(^\s)/;
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; 
	  
	if (!reg1.test(str) && reg2.test(str)) { 
     return true;
	}

	return false;
}
function OpenNewWindow(imageurl){
var Imagepage = imageurl;var windowheight = 510;var windowwidth = 640;
var windowleft = (screen.width - parseInt(windowwidth)) / 2;
var windowtop = (screen.height - parseInt(windowheight)) / 2;
var windowname = "PopupWindow";
windowproperties = 'resizable=no,status=no,toolbar=no,height='+windowheight+',width='+windowwidth+',top='+windowtop+',left='+windowleft+',scrollbars=yes'
setwindow = window.open(Imagepage, windowname, windowproperties)
	if (CheckNavigatorVersion()==true){
			setwindow.window.focus();}
} 

function ValidateDate(dateStr, message){
//alert(dateStr);
	if (!isDate(dateStr)){
		alert(message);
		return false;
	}

	return true;
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf("/");
	var pos2=dtStr.indexOf("/",pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=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 : mm/dd/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;
	}

	return true;
}

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 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 );
}

//	functions for validating date range

	function ValidateDateRange(frm, message){
		var startDate, endDate;

		startDate = new Date();
		endDate = new Date();

		startDate.setYear(frm.drpStartYear.value);
		startDate.setDate(frm.drpStartDate.value);
		startDate.setMonth(frm.drpStartMonth.value - 1);

		endDate.setYear(frm.drpEndYear.value);
		endDate.setDate(frm.drpEndDate.value);
		endDate.setMonth(frm.drpEndMonth.value - 1);

		if (startDate > endDate){
			alert(message);
			return false;
		}

		return true;
	}
