// JavaScript Document

// Included functions:
//	new_win
//	is_valid_date
// 	test_url
//	print_version

// **********************************************************************
//		Method Name:	new_win(sURL, sName, iWidth, iHeight, blnScroll, blnResize, blnMenu, blnTool)
//					
//		Description:	Opens a new instance of a browser.
//
//				EX INPUT: new_win("MyPage.asp","MyPage",600,400,"yes","no","no","no")
//				OUTPUT:	  NA
//		
//		Return Type:	None
//
//		Functions Used: None
//
// **********************************************************************
//		Parameter Listing
// **********************************************************************
//		Name:			Type:		Description:
//		--------		--------	-------------------------
//		sURL			String		URL location of page to be displayed in window
//		sName		String		Name of window
//		iWidth		Number		Window width
//		iHeight		Number		Window height
//		blnScroll		String		Scrollbars for window (yes,no)
//		blnResize		String		Resizeable window (yes,no)
//		blnMenu		String		Menu bar for window (yes,no)
//		blnTool		String		Tool bar displayed on window (yes, no)
// **********************************************************************
function new_win(sURL,sName,iWidth,iHeight,blnScroll,blnResize,blnMenu,blnTool)
{
	opts = 'width=' + iWidth + ',height=' + iHeight + ',scrollbars=' + blnScroll + ',resizable=' + blnResize + ',menubar=' + blnMenu + ',toolbar=' + blnTool;     
	var win = window.open('', sName,opts);
	win.location.href = sURL	
}

// **********************************************************************
//		Method Name:	is_valid_date(dateStr)
//		Description:	Returns 0 if the parameter is a date with valid
//				numbers for the month, day, and year.
//				Checks for the following valid date formats:
//				MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY.
//				Also separates date into month, day, and year variables.
//				Can be modified to validate only 4-digit years.
//
//				Returns a specific error code if the paramter is
//				invalid. Can be modified to cause an alert or
//				return a boolean value.
//
//				EX INPUT: is_valid_date(03/45/05)
//				OUTPUT:	  3 (or false)
//		
//		Return Type:	Number error code (or boolean)
//
//		Functions Used: None
//
// **********************************************************************
//		Parameter Listing
// **********************************************************************
//		Name:		Type:		Description:
//		--------	--------	-------------------------
//		dateStr		Date string	String to test			
//		
// **********************************************************************
function is_valid_date(dateStr) {
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables

	// To allow a 2 or 4 digit year entry, use this line instead:
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

	// To require a 4 digit year entry, use this line:
	//   var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) {
		//alert("Date is not in a valid format.")
		return false;
		//return "1";
	}
	
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];

	//******************************************//
	//added from isDate function (Graphics js file)
	// Verify that all fields are integers.
	/*if (!parseInt(month) || !parseInt(day) || !parseInt(year))
		//alert("Month, day, and year fields must be integers.");
		return false;
		//return "6";*/
	//******************************************//

	if (month < 1 || month > 12) { // check month range
		//alert("Month must be between 1 and 12.");
		return false;
		//return "2";
	}

	if (day < 1 || day > 31) {
		//alert("Day must be between 1 and 31.");
		return false;
		//return "3";
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		//alert("Month "+month+" doesn't have 31 days!")
		return false
		//return "4";
	}
	
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			//alert("February " + year + " doesn't have " + day + " days!");
			return false;
			//return "5";
		   }
	}
	
	return true;  // date is valid
	//return "0";
}

// **********************************************************************
//		Method Name:	testURL(formObject)
//		Description:	Tests url entered in textbox
//
//				EX INPUT: is_valid_date(document.frmName.txtName)
//				OUTPUT:	  
//		
//		Return Type:	None
//
//		Functions Used: None
//
// **********************************************************************
//		Parameter Listing
// **********************************************************************
//		Name:		Type:		Description:
//		--------	--------	-------------------------
//		formObject	form object	form object containing value to test,
//									usually textbox
//		
// **********************************************************************

function testURL(formObject) {
	var url;
	url = formObject.value;
	// check if website entered - if not, display warning
	if (url.length <= 0) {
		alert("Please enter a website to test.");
		formObject.focus();
		return;
	}
	// modify url to include http://
	url = "http://" + url
	// open new window with url
	new_win(url,"testWin",700,500,"yes","yes","no","no");
		
}

// **********************************************************************
//		Method Name:	print_version(formObject)
//		Description:	Opens new window with printer-friendly version
//
//				EX INPUT: 
//				OUTPUT:	  
//		
//		Return Type:	None
//
//		Functions Used: new_win
//
// **********************************************************************
//		Parameter Listing
// **********************************************************************
//		Name:		Type:		Description:
//		--------	--------	-------------------------
//		
// **********************************************************************

function print_version() {
	
	var url;
	url = "view_print.asp?id=" + document.frmRequest.request_id.value;
	
	new_win(url,'',775,600,'yes','yes','yes','yes');
	
}
