﻿function Scaler()
{
	var MastheadHeight = parseInt(document.getElementById("PageMasthead").offsetHeight);
	var SubMastheadHeight = parseInt(document.getElementById("PageSubMasthead").offsetHeight);
	var NavigationHeight = parseInt(document.getElementById("Navigation").offsetHeight);
	var DocumentContentHeight = parseInt(document.getElementById("ContentRight").offsetHeight);
	var DocumentClientHeight = document.body.clientHeight;
	var DocumentScrollHeight = document.body.scrollHeight;
	
	if (DocumentClientHeight < DocumentScrollHeight){
		DocumentHeight = DocumentClientHeight
	}else{
		if (DocumentContentHeight < DocumentScrollHeight){
			DocumentHeight = DocumentClientHeight
		}else{
			DocumentHeight = DocumentScrollHeight
		}
	}
	
	if (document.all){
		WidthAmount = 191
		Subtractor = 0
	}else{
		WidthAmount = 192
		Subtractor = 300
	}
	if ((MastheadHeight + SubMastheadHeight + NavigationHeight) < DocumentHeight){
		document.getElementById("ContentLeftHeightScaler").style.height = DocumentHeight - MastheadHeight - SubMastheadHeight - NavigationHeight - 1;
		document.getElementById("SubMastheadRightWidthScaler").style.width = parseInt(document.getElementById("PageMasthead").offsetWidth) - WidthAmount;
		document.getElementById("MastheadRightWidthScaler").style.width = parseInt(document.getElementById("PageMasthead").offsetWidth) - WidthAmount;
	}
	if (!document.all){
		document.getElementById("ContentRight").style.width = document.getElementById("SubMastheadRightWidthScaler").width;
		document.getElementById("ContentRight").style.height = document.getElementById("ContentLeftHeightScaler").height - Subtractor;
	}
}


function validate_date(date_field, desc) {
        if (!date_field.value)  
                return true;
        var in_date = stripCharString(date_field.value," ");
        in_date = in_date.toUpperCase();
        var date_is_bad = 0;  
        if (!allowInString(in_date,"/0123456789T+-"))
                date_is_bad = 1; // invalid characters in date
        if (!date_is_bad) { 
                var has_rdi = 0;
                if (in_date.indexOf("T") >= 0){ 
                        has_rdi = 1;
                }
                if (!date_is_bad && has_rdi && (in_date.indexOf("T") != 0)) { 
                        date_is_bad = 2; // relative date index character is not in first position
                }
                if (!date_is_bad && has_rdi && (in_date.length == 1)) { 
                        var d = new Date();
						var return_month = parseInt(d.getMonth() + 1).toString();
						return_month = (return_month.length==1 ? "0" : "") + return_month; 
						var return_date =  parseInt(d.getDate()).toString();
						return_date = (return_date.length==1 ? "0" : "") + return_date; 
				        in_date = return_month + "/" + return_date + "/" + get_full_year(d, in_date);		
                        has_rdi = 0; // date doesn't have rdi char anymore (will also cause failure of add'l rdi checks, which is a good thing)
                }
                if (!date_is_bad && has_rdi && (in_date.length > 1) && !(in_date.charAt(1) == "+" || in_date.charAt(1) == "-")) {
                        date_is_bad = 3; // length of rdi string is greater than 1 but second char is not "+" or "-"
                }
                if (!date_is_bad && has_rdi && isNaN(parseInt(in_date.substring(2,in_date.length),10))) {
                        date_is_bad = 4; // rdi value is not a number
                }
                if (!date_is_bad && has_rdi && (parseInt(in_date.substring(2,in_date.length),10) < 0)) {
                        date_is_bad = 5; // rdi value is not a positive integer
                }
                if (!date_is_bad && has_rdi) {
                        var d = new Date();
                        ms = d.getTime();
                        offset = parseInt(in_date.substring(2,in_date.length),10);
                        if(in_date.charAt(1) == "+") {
                                ms += (86400000 * offset);
                        } else {
                                ms -= (86400000 * offset);
                        }
                        d.setTime(ms);
						var return_month = parseInt(d.getMonth() + 1).toString();
						return_month = (return_month.length==1 ? "0" : "") + return_month; 
						var return_date =  parseInt(d.getDate()).toString();
						return_date = (return_date.length==1 ? "0" : "") + return_date; 
				        in_date = return_month + "/" + return_date + "/" + get_full_year(d, in_date);	
                        has_rdi = 0;
                }
        } 
        if (!date_is_bad) {
                var date_pieces = new Array();
                date_pieces = in_date.split("/");
                if (date_pieces.length == 2) {
                        var d = new Date();
                        in_date = in_date + "/" + get_full_year(d, in_date);
                        date_pieces = in_date.split("/");
                }
                if (date_pieces.length != 3 || parseInt(date_pieces[0],10) < 1 || parseInt(date_pieces[0],10) > 12 
                                || parseInt(date_pieces[1],10) < 1 || parseInt(date_pieces[1],10) > 31 
                                || (date_pieces[2].length != 2 && date_pieces[2].length != 4)) {
                        date_is_bad = 6;  // date is not in format of m[m]/d[d]/yy[yy]
                }
        }
        if (date_is_bad) {
                alert(desc + " must be in the format of mm/dd/yy, mm/dd/yyyy, t, t+n or t-n.");
                date_field.focus();
                return (false);
        }
        
        var ms = Date.parse(in_date);
        var d = new Date();
        d.setTime(ms);
		var return_date = d.toLocaleString();
		var return_month = parseInt(d.getMonth() + 1).toString();
		return_month = (return_month.length==1 ? "0" : "") + return_month; 
		var return_date =  parseInt(d.getDate()).toString();
		return_date = (return_date.length==1 ? "0" : "") + return_date; 
        return_date = return_month + "/" + return_date + "/" + get_full_year(d, in_date);
        date_field.value = return_date;
        return true;
}       // normalize the year to yyyy


// Changed the following function so that it takes the TheDate parameter.  This is used to check for dates
// that are already in four-digit-year format so that the are not messed up.  - dlt 6/18/01
function get_full_year(d, TheDate) {
		var y = "";
		//alert (TheDate.substr(TheDate.length-3,3)).indexOf("/")
		if ((TheDate.substr(TheDate.length-3,3)).indexOf("/") != -1)
		{
			if (d.getFullYear() != null)
			{
				y = d.getFullYear();
				if (y < 1970) y+= 100;		
			} else
			{	
			     y = d.getYear();
			     if (y > 69  && y < 100) y += 1900;
			     if (y < 1000) y += 2000;
			}
		}
		else
		{
			y = TheDate.substr(TheDate.lastIndexOf("/") + 1);
		}
      return y;
}

// The following functions were written by Gordon McComb
// More information can be found here: http://www.javaworld.com/javaworld/jw-02-1997/jw-02-javascript.html
function stripCharString (InString, CharString)  {
        var OutString="";
   for (var Count=0; Count < InString.length; Count++)  {
        var TempChar=InString.substring (Count, Count+1);
      var Strip = false;
      for (var Countx = 0; Countx < CharString.length; Countx++) {
        var StripThis = CharString.substring(Countx, Countx+1)
         if (TempChar == StripThis) {
                Strip = true;
            break;
         }
      }
      if (!Strip)
        OutString=OutString+TempChar;
   }
        return (OutString);
}
function allowInString (InString, RefString)  {
        if(InString.length==0) return (false);
        for (var Count=0; Count < InString.length; Count++)  {
        var TempChar= InString.substring (Count, Count+1);
      if (RefString.indexOf (TempChar, 0)==-1)  
        return (false);
   }
   return (true);
}
