//Rudolph Henninger(c)  2003
//all of this code was written by Rudolph Henninger except where so noted




//**************************************************************************
// open a Small popup window
//**************************************************************************

nvHeight = 640;
nvWidth = 720;
topCoord = (window.screen.height-nvHeight)/2;
leftCoord = (window.screen.width-nvWidth)/2;

function OpenSmallWindow(URL)
{
  atw = window.open( 
    URL,
    "test",
    "top="+topCoord+",left="+leftCoord+",width="+nvWidth+",height="+nvHeight+",scrollbars=yes,resizable=yes");
  atw.focus();
}


//**************************************************************************
// open a confirmation box
//call: <a href="" onClick="return confirmSubmit()">Delete</a>
//**************************************************************************

function confirmDelete()
{
	var agree=confirm("Are you sure you want do delete that?");
	if (agree)
		return true ;
	else
		return false ;
}


//**************************************************************************
// Submit a form
// call: <a href="javascript:subform('');">UPDATE</a>
//**************************************************************************

function SubmitForm(frm, act) 
{
      document.form.action.value = act;
      document.frm.submit();
}


//**************************************************************************
// Close a popup and Refresh the Parent
// call: <a href="javascript:;" onclick="refreshParentandClose();">Click Here</a>
//**************************************************************************

function refreshParentandClose() 
{
  window.opener.location.href = window.opener.location.href;
  self.close();
}


//**************************************************************************
// Close a popup and Redirect the parent
// call:  <a href="javascript:;" onclick="redirectParentandClose('http://www.google.com');">Click Here</a>
//**************************************************************************

function redirectParentandClose(address) 
{
  window.opener.location.href = address;
  self.close();
}


//**************************************************************************
// Client side Requote
//**************************************************************************

function Requote(toquote)
{
     var teststring = new String();
     var reg =  new RegExp("'");
     teststring = toquote;
     return teststring.replace(reg,"''");
 }  


//**************************************************************************
// Check All 
// call: <A HREF="javascript:checkAll(document.myform.list);">CHECK ALL</A>
//**************************************************************************

function checkAll(field)
{
	if(field.length)
	{
  	   for (i = 0; i < field.length; i++)
	      field[i].checked = true ;
	}
	else
	   field.checked = true;
}


//**************************************************************************
// UnCheck All 
// call: <A HREF="javascript:uncheckAll(document.myform.list);">UNCHECK ALL</A>
//**************************************************************************

function uncheckAll(field)
{
	if(field.length)
	{
	   for (i = 0; i < field.length; i++)
	      field[i].checked = false;
	}
	else
	   field.checked=false;
}


//**************************************************************************
// Reset Form
// call: <input type="submit" value="Reset" onClick="return ResetForm(document.filterform);">
//**************************************************************************

function ResetForm(which){
	var pass=true
	var first=-1

	for (i=0;i<which.length;i++)
	{
		var tempobj=which.elements[i]
		 if (tempobj.type=="text")
		{
			  eval(tempobj.value="")
			  if (first==-1) {first=i}
		 }
		 else if (tempobj.type=="checkbox") 
		{
			  eval(tempobj.checked=0)
			  if (first==-1) {first=i}
		 }
		//else if (tempobj.col!="") 
		//{
		//  	eval(tempobj.value="")
		//  	if (first==-1) {first=i}
		// }
	}
	which.elements[first].focus()
	return false
}


//**************************************************************************
// Check For a Valid Date String
// FROM: http://www.breakingpar.com/bkp/home.nsf/Doc!OpenNavigator&U=75A5C758B9DF576087256AFB00140789
//**************************************************************************

function checkValidDate(dateStr) {
    // dateStr must be of format month day year with either slashes
    // or dashes separating the parts. Some minor changes would have
    // to be made to use day month year or another format.
    // This function returns True if the date is valid.
    var slash1 = dateStr.indexOf("/");
    if (slash1 == -1) { slash1 = dateStr.indexOf("-"); }
    // if no slashes or dashes, invalid date
    if (slash1 == -1) { return false; }
    var dateMonth = dateStr.substring(0, slash1)
    var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
    var slash2 = dateMonthAndYear.indexOf("/");
    if (slash2 == -1) { slash2 = dateMonthAndYear.indexOf("-"); }
    // if not a second slash or dash, invalid date
    if (slash2 == -1) { return false; }
    var dateDay = dateMonthAndYear.substring(0, slash2);
    var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
    if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ) { return false; }
    // if any non-digits in the month, invalid date
    for (var x=0; x < dateMonth.length; x++) {
        var digit = dateMonth.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text month to a number
    var numMonth = 0;
    for (var x=0; x < dateMonth.length; x++) {
        digit = dateMonth.substring(x, x+1);
        numMonth *= 10;
        numMonth += parseInt(digit);
    }
    if ((numMonth <= 0) || (numMonth > 12)) { return false; }
    // if any non-digits in the day, invalid date
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text day to a number
    var numDay = 0;
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        numDay *= 10;
        numDay += parseInt(digit);
    }
    if ((numDay <= 0) || (numDay > 31)) { return false; }
    // February can't be greater than 29 (leap year calculation comes later)
    if ((numMonth == 2) && (numDay > 29)) { return false; }
    // check for months with only 30 days
    if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) { 
        if (numDay > 30) { return false; } 
    }
    // if any non-digits in the year, invalid date
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text year to a number
    var numYear = 0;
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        numYear *= 10;
        numYear += parseInt(digit);
    }
    // Year must be a 2-digit year or a 4-digit year
    if ( (dateYear.length != 2) && (dateYear.length != 4) ) { return false; }
    // if 2-digit year, use 50 as a pivot date
    if ( (numYear < 50) && (dateYear.length == 2) ) { numYear += 2000; }
    if ( (numYear < 100) && (dateYear.length == 2) ) { numYear += 1900; }
    if ((numYear <= 0) || (numYear > 9999)) { return false; }
    // check for leap year if the month and day is Feb 29
    if ((numMonth == 2) && (numDay == 29)) {
        var div4 = numYear % 4;
        var div100 = numYear % 100;
        var div400 = numYear % 400;
        // if not divisible by 4, then not a leap year so Feb 29 is invalid
        if (div4 != 0) { return false; }
        // at this point, year is divisible by 4. So if year is divisible by
        // 100 and not 400, then it's not a leap year so Feb 29 is invalid
        if ((div100 == 0) && (div400 != 0)) { return false; }
    }
    // date is valid
    return true;
}