//*************************************************
// formCheck()  -	If textbox is blank, 
//						notify user to enter value
//*************************************************/
function formCheck()
{
	var dealsizelimit = 922337203685477;
	var sharesofferedlimit = 2147483647;

	var tickerbox = document.frmIPOsearch.ipo_ticker;
	var companynamebox = document.frmIPOsearch.ipo_companyname;
	var marketsbox = document.frmIPOsearch.ipo_markets;
	var dealstatusbox = document.frmIPOsearch.ipo_dealstatus;
	var dealtypebox = document.frmIPOsearch.ipo_dealtype;
	var frdealsizebox = document.frmIPOsearch.ipo_FrDealSize;
	var todealsizebox = document.frmIPOsearch.ipo_ToDealSize;
	var frsharesofferedbox = document.frmIPOsearch.ipo_FrSharesOffered;
	var tosharesofferedbox = document.frmIPOsearch.ipo_ToSharesOffered;
	var underwirterbox = document.frmIPOsearch.ipo_underwirter;
	var auditorbox = document.frmIPOsearch.ipo_auditor;
	var leagalcounselbox = document.frmIPOsearch.ipo_leagalcounsel;
	var quietperiodbox = document.frmIPOsearch.ipo_quietperiod;
	var lockupperiodbox = document.frmIPOsearch.ipo_lockupperiod;
	var timeperiodbox = document.frmIPOsearch.ipo_timeperiod;
	var frdaterangebox = document.frmIPOsearch.ipo_FrDateRange;
	var todaterangebox = document.frmIPOsearch.ipo_ToDateRange;
	
	var blnEmpty1=isAllSpaces(timeperiodbox.value);
	var blnEmpty2=isAllSpaces(frdaterangebox.value);
	var blnEmpty3=isAllSpaces(todaterangebox.value);
	var blnEmpty4=isAllSpaces(quietperiodbox.value);
	var blnEmpty5=isAllSpaces(lockupperiodbox.value);
	var blnEmpty6=isAllSpaces(tickerbox.value);
	var blnEmpty7=isAllSpaces(companynamebox.value);
	var blnEmpty8=isAllSpaces(frsharesofferedbox.value);
	var blnEmpty9=isAllSpaces(tosharesofferedbox.value);
	var blnEmpty10=isAllSpaces(frdealsizebox.value);
	var blnEmpty11=isAllSpaces(todealsizebox.value);
	var blnEmpty12=isAllSpaces(dealstatusbox.value);
	
	//Both a Company Name and a Ticker can't be entered at the same time.
	if (!blnEmpty6 && !blnEmpty7)
	{
		alert("You can not enter both a Ticker and a Company Name.");
		return false;	
	}	
	
	//A Company Name search must consist of at least 3 characters.
	if (!blnEmpty7 && companynamebox.value.length < 3)
	{
		alert("Please enter at least 3 characters for a Company Name search");
		return false;		
	}
	
	//The Deal Size must be a numeric value.
	if (!blnEmpty10 || !blnEmpty11)
	{
		if (isNaN(frdealsizebox.value) || isNaN(todealsizebox.value))
		{
		alert("Please a numeric value for Deal Size.");
		return false;			
		}
	}

	//The Deal Size Range must go from low to high if both of them are entered.	
	if (!blnEmpty10 && !blnEmpty11)
	{
		if(frdealsizebox.value > todealsizebox.value)
		{
		alert("Please enter the Deal Size range from low to high.");
		return false;		
		}
	}

	//Deal size must be less than 922,337,203,685,477
	if ( (frdealsizebox.value > dealsizelimit) || (todealsizebox.value > dealsizelimit) )
	{
		alert("You have exceeded the limit on Deal Size : $922,337,203,685,477.\n\nPlease enter a smaller Deal Size.");
		return false;			
	}
	
	//The Shares Offered must be a numeric value.
	if (!blnEmpty8 || !blnEmpty9)
	{
		if (isNaN(frsharesofferedbox.value) || isNaN(tosharesofferedbox.value))
		{
		alert("Please a numeric value for Shares Offered.");
		return false;			
		}
	}
	
	//The Shares Offered Range must go from low to high if both of them are entered.	
	if (!blnEmpty8 && !blnEmpty9)
	{		
		if(frsharesofferedbox.value > tosharesofferedbox.value)
		{
		alert("Please enter the Shares Offered range from low to high.");
		return false;		
		}
	}	
	
	//Shares Offered must be less than or equal to 2,147,483,647
	if ( (frsharesofferedbox.value > sharesofferedlimit) || (tosharesofferedbox.value > sharesofferedlimit) )
	{
		alert("You have exceeded the limit on Shares Offered : 2,147,483,647.\n\nPlease enter a smaller Shares value.");
		return false;			
	}
	
	//If a Quiet Period is entered, then a Lockup Period can't be entered
	if (!blnEmpty4 && !blnEmpty5)
	{
		alert("You can not enter both a Quiet Period and a Lockup Period.");
		return false;	
	}

	//If a Quiet Period or Lockup Period is entered, then a Timeperiod or Date Range can't be entered
	if (!blnEmpty4 ||!blnEmpty5)
	{
		if (!blnEmpty1 ||!blnEmpty2 || !blnEmpty3)
		{
			alert("You can not enter an Time Period or a Date Range with a Lockup Period or a Quiet Period.");
			return false;	
		}
		else
		{
			return true;
		}		
	}

//	if Time Period or Date Range have values, and Deal Status has no value
		if (!blnEmpty1 || !blnEmpty2 || !blnEmpty3) {
			if (blnEmpty12) {
				alert('You must choose a Deal Status if you select a Time Period or Date Range.');
				return false
			}
		}

	//If a Time Period is entered, then a Date Range can't be entered
	if (!blnEmpty1)
	{
		if (!blnEmpty2 || !blnEmpty3)
		{
			alert("You can not enter an Time Period and a Date Range.");
			return false;	
		}
		else{
			return true;
		}
	}

	//Validate the Date Range
	if (!blnEmpty2 || !blnEmpty3)
	{
		if (compareDate(frdaterangebox,todaterangebox)){
			return true;
		}
		else{
			return false;
		}	
	}

}

//----------- COMPARE DATE

function compareDate(objBeginDate,objEndDate){
	if (isValidDate(objBeginDate)){
		if (isValidDate(objEndDate)){
			var blnEmpty=isAllSpaces(objBeginDate.value);
			var blnEmpty1=isAllSpaces(objEndDate.value);
			if (!blnEmpty){
				if (!blnEmpty1){
					var datePat = /^(.+)\/(.+)\/(.+)$/;
					var bdateArray = objBeginDate.value.match(datePat);
					var bdayValue = bdateArray[2];	
					var bmonthValue = bdateArray[1];
					var byearValue = bdateArray[3];

					var edateArray = objEndDate.value.match(datePat);
					var edayValue = edateArray[2];
					var emonthValue = edateArray[1];
					var eyearValue = edateArray[3];		

					var dtStart = new Date(byearValue, bmonthValue, bdayValue, 0, 0, 0, 0);
					var dtEnd = new Date(eyearValue, emonthValue, edayValue, 0, 0, 0, 0);
		
					if (dtStart.getTime() > dtEnd.getTime()) {
					     alert("Please enter an end date later than the beginning date.");
					     return false;
					}
					else {
					    return true;
					}
				}
				else{
					return true;
				}
			}
			else{
				return true;
			}
		}
		else {
			return false;
		}
	}
	else {
		return false;			
	}
}

//----------- IS VALID DATE

function isValidDate(obj){
	var strValue=obj.value;	
	var blnEmpty=isAllSpaces(strValue);
	
	if (!blnEmpty){
		var datePat = /^(.+)\/(.+)\/(.+)$/;
		var dateArray = strValue.match(datePat);

		if (dateArray == null) {
			alert("Please enter a valid date in the format 'mm/dd/yyyy'");
		    obj.select();
			obj.focus();     
			return false;
		 }

		var dayValue = dateArray[2];
		var monthValue = dateArray[1];
		var yearValue = dateArray[3];

		if (monthValue < 10 && monthValue.length!=2) {
			monthValue = "0" + monthValue;
		}
		if (dayValue < 10 && dayValue.length!=2) {
			dayValue = "0" + dayValue;
		}

		var generalPat = /\D/;        

		var dayCheck = dayValue.match(generalPat);
		var monthCheck = monthValue.match(generalPat);
		var yearCheck = yearValue.match(generalPat);

		if ((dayCheck != null) || (monthCheck != null) || (yearCheck != null)) {
		    alert("Please enter a valid date in the format 'mm/dd/yyyy'");
		    obj.focus();
		    return false;        
		    }

		if ((dayValue > 31) || (monthValue > 12)) {
		    alert("You entered an invalid date. Please try again...");
		    obj.focus();
		    return false;
		    }

		if ((dayValue == 0) || (monthValue == 0)) {
		    alert("You entered an invalid date. Please try again...");
		    obj.focus();
		    return false;
		    }
			    		
		if( !(dayValue.length == 2) || !(monthValue.length == 2) || !(yearValue.length == 4)){
			alert("Invalid date format. Please enter a valid date in the format 'mm/dd/yyyy'");
			obj.focus();
			return false;
			}
		if (chkdate(obj) == false) {
		    alert("You entered an invalid date. Please try again...");
		    obj.focus();
		    return false;
			}
		return true;
	}
	else
	{
		//alert("Please enter both a Start and End date.");
		//obj.focus();
		return true;
	}
	return false;
}

//----------- CHK DATE

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 sDay;
	var sMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = 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;

	if (strDate.length < 1) {
		return true;
	}

	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				return false;
			}
		else {
			strDay = strDateArray[0];
			strMonth = strDateArray[1];
			strYear = strDateArray[2];
		}
		booFound = true;
		}
	}

	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
	   }
	}

	if (strYear.length == 2) {
		strYear = '20' + strYear;
	}

	// US style
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	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;
	   }
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return false;
	}
	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;
			}
		}
	}

//Commented this part to make it work for isValidDate function 04/10/2003
/*	if (strDatestyle == "US") {
		datefield.value = sMonth + "/" + sDay+"/" + strYear;
	}
	else {
		datefield.value = sDay + "/" + sMonth + "/" + strYear;
	}
*/
	return true;
}

//----------- LEAP YEAR

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
		}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

//----------- return wether the string passed has blanks or not

function hasSpaces(strValue)
{
	var blnHasWhiteSpace = false;
	var strChar;
	var strWhiteSpaceTest= " \f\n\r\t\x0b";
	
	for(var i=0; i<strValue.length; i++){
		strChar = strValue.charAt(i).toLowerCase();
			
		if(strWhiteSpaceTest.indexOf(strChar) != -1){
			blnHasWhiteSpace = true;
			break;
		}
	}
	
	return blnHasWhiteSpace;
}

//----------- return wether the string passed is blank or not

function isAllSpaces(strValue)
{
	var blnHasNonWhiteSpace = false;
	var strChar;
	var strWhiteSpaceTest= " \f\n\r\t\x0b";
	
	for(var i=0; i<strValue.length; i++){
		strChar = strValue.charAt(i).toLowerCase();
			
		if(strWhiteSpaceTest.indexOf(strChar) == -1){
			blnHasNonWhiteSpace = true;
		}
	}
	
	return !(blnHasNonWhiteSpace);
}

