// function for disabling right click
	var message="Sorry, right-click has been disabled";
	///////////////////////////////////
	function clickIE() {
		if (document.all) {
			(message);
			return false;
		}
	}
	
	function clickNS(e) {
		if (document.layers||(document.getElementById&&!document.all)) {
			if (e.which==2||e.which==3) {
				(message);
				return false;
			}
		}
	}
	
	if (document.layers) {
		document.captureEvents(Event.MOUSEDOWN);
		document.onmousedown=clickNS;
	} else { 
		document.onmouseup=clickNS;
		document.oncontextmenu=clickIE;
	}
	
	document.oncontextmenu=new Function("return false")

// Images and functions used to display add and delete buttons
// Created: May 17, 2005
	addImageNormal 	= "images/add_normal.gif";  
	addImageHover	= "images/add_hover.gif";
	addImageClicked	= "images/add_clicked.gif";
	delImageNormal 	= "images/del_normal.gif";  
	delImageHover	= "images/del_hover.gif";
	delImageClicked	= "images/del_clicked.gif";
	editImageNormal = "images/edit_normal.gif";  
	editImageHover	= "images/edit_hover.gif";
	editImageClicked= "images/edit_clicked.gif";

	function imgAddHover(imgname) {
    	imgname.src = addImageHover;
	}
	function imgAddNormal(imgname) {
    	imgname.src = addImageNormal;
	}
	function imgAddClicked(imgname) {
    	imgname.src = addImageClicked;
	}
	function imgDelHover(imgname) {
    	imgname.src = delImageHover;
	}
	function imgDelNormal(imgname) {
    	imgname.src = delImageNormal;
	}
	function imgDelClicked(imgname) {
    	imgname.src = delImageClicked;
	}
	function imgEditHover(imgname) {
    	imgname.src = editImageHover;
	}
	function imgEditNormal(imgname) {
    	imgname.src = editImageNormal;
	}
	function imgEditClicked(imgname) {
    	imgname.src = editImageClicked;
	}

// functions used for manipulating cookies
// Created: 
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;

	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}  

function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	
	document.cookie = name + "=" + escape (value) +
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
		((path == null) ? "" : ("; path=" + path)) +
		((domain == null) ? "" : ("; domain=" + domain)) +
		((secure == true) ? "; secure" : "");
	}
// End -->

function getElement(object, path) {  //path only needed by netscape4
	if (document.getElementById) object = document.getElementById(object); //W3C Compliant (IE5+/NS6+)
		else if (document.all) object = document.all[object]; //IE4
  	else if (document.layers) { //Netscape 4
    		if (!path) path='document';
    		object = eval(path+'.'+object);
  	}

  	return object;
}

// When called by framed page, break out of frames
function putOnTopAlways() {
	if (window.location.href != window.top.location.href) {
  		window.top.location.replace(window.location.href);
  	}
}

function displayATOLogo() {
	document.write('<div id=ATOLogo style="position: absolute; top: 6; left: 5; z-index:0">')
	document.write('<img src="images/head.jpg"></img>')
	document.write('</div>')
}

function displayFooter() {
	document.write('<div id=Footer>')
	document.write('<table width="100%" style="border-bottom: 0px dashed #336699; margin: 3px;"><tr>')
	document.write('<tr><td>&nbsp;</td></tr>')
	document.write('<tr><td width="30"><div id=icons style="position: relative;"><a href="javascript:window.print();" title="Print this page."><img src="images/print.gif" alt="Print this page." style="border: #ddd solid 0px;"/></a></td>')
	document.write('<td><a href="mailto:?subject=Emailing link to ATO Airmen Licensing Office&body=Click here: '+location.href+'" title="Email this page to a friend."><img src="images/email.gif" alt="Email this page to a friend." style="border: #ddd solid 0px;"/></a></td>');
	document.write('<td><a href="#top"><img src="images/top.gif" border="0" align="right" alt="Top of page."></a></td>')
	document.write('</div>')
	document.write('<tr><td colspan=3 height="25" align="center" style="font-size: 11px; font-weight: bold;">|<img src="images/dot.gif" width=10 /><a href="underRenovation.htm">Contact ALS</a>')
	document.write('<img src="images/dot.gif" width=10 />|<img src="images/dot.gif" width=10 /><a href="alsMain.htm">ALS Home Page</a>')
	document.write('<img src="images/dot.gif" width=10 />|<img src="images/dot.gif" width=10 /><a href="siteMap.htm">Site Map</a>')
	document.write('<img src="images/dot.gif" width=10 />|</td></tr></table>')
	document.write('</div>')
}

function displaySubFooter() {
	document.write('<div id=Footer>')
	document.write('<table width="100%" style="border-bottom: 0px dashed #336699; margin: 3px;"><tr>')
	document.write('<tr><td>&nbsp;</td></tr>')
	document.write('<tr><td width="30"><div id=icons style="position: relative;"><a href="javascript:window.print();" title="Print this page."><img src="images/print.gif" alt="Print this page." style="border: #ddd solid 0px; cursor: pointer" onClick="printPage()" /></a></td>')
	document.write('<td><a href="mailto:?subject=Emailing link to ATO Airmen Licensing Office&body=Click here: '+location.href+'" title="Email this page to a friend."><img src="images/email.gif" alt="Email this page to a friend." style="border: #ddd solid 0px;"/></a></td>');
	document.write('<td><a href="#top"><img src="images/top.gif" border="0" align="right" alt="Top of page."></a></td>')
	document.write('</div>')
}

// functions used for validating form entries
function getFormVariable(name) {
	var elementPos = -1; 

	for(var i = 0; i < document.formData.elements.length; i++) { 
		if(document.formData.elements[i].name == name) {
			return document.formData.elements[i];

//			if(document.formData.elements[i].type == 'select' || document.formData.elements[i].type == 'select-one') {
// 				return document.formData.elements[i].options[document.formData.elements[i].selectedIndex];
//			} else {
//				return document.formData.elements[i];
//			}	

		}
	}
}

function getFormVariableValue(name) {
	var ans = getFormVariable(name);
	if (ans.type=='checkbox') {
		if (ans.checked) {
			return ans.value;
		} else {
    		return '';
		}
	}
	return ans.value;
}

function setFormVariableValue(name, value) {
	var ans = getFormVariable(name);
	if (ans.type=='checkbox') {
		if (ans.checked) {
			ans.checked = value;
		} else {
    		ans.value = value;
		}
	} else {
   		ans.value = value;
	}

}

/*
function getRadioValue(name) {
	var retVal;
	
	retVal = '';
	
	for (n = 0; n < formData.elements[name].length; n++) {
		if (formData.elements[name][n].checked) {
			retVal = formData.elements[name][n].value;
			return (retVal);
		}
	}
	
	return (retVal);
}	
*/

function getRadioValue(name) {

	var retVal='';

	for(var i = 0; i < document.formData.elements.length; i++) { 
		if(document.formData.elements[i].name == name) {
			if (document.formData.elements[i].checked) 
				retVal = document.formData.elements[i].value;
		}
	}	

	return (retVal);
}	

function getFormVariableLength(name) {
	var ans = getFormVariableValue(name);
	return ans.toString().length;
}

function validate() {
	
	if (isValid()) {
		return true;
	} else {
		return false;
	}
}

function validateDate(dateField) {
	var dDate = new Date(getFormVariableValue(dateField));
	
	if (isNaN(dDate)) {
		alert("You have entered an invalid date.  Please use the format MM/DD/YYYY.");
		return false;
   }
   return true;
}

function alert_message(message) {
		alert(message);
}

// function used to close popup windows
function cancelEntry() {
	var ans = confirm("Press OK to stop data entry and close window");
	
	if (ans) {
		window.close();
	} else {
		return (false);
	}
}
			
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}

function printPage() {
	if(document.all) {
		document.all.divButtons.style.visibility = 'hidden';
		window.print();
		document.all.divButtons.style.visibility = 'visible';
	} else {
		document.getElementById('divButtons').style.visibility = 'hidden';
		window.print();
		history.go(0);
//		document.getElementById('divButtons').style.visibility = 'visible';
	}
}

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

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 );
}
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 isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,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 provide a valid month.")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please provide a valid day.")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please provide a valid 4 digit year between "+minYear+" and "+maxYear+".")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please provide a valid date.")
		return false
	}
return true
}

function isEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}


function isValidTime(timeStr) {
// Checks if time is in HH:MM:SS AM/PM format.
// The seconds and AM/PM are optional.

// Usage: if (IsValidTime(document.timeform.time.value)
						  
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	
	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
		alert("Time is not in a valid format.");
		return false;
	}

	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];
	
	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }
	
	if (hour < 0  || hour > 23) {
		alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
		return false;
	}

	if  (hour > 12 && ampm != null) {
		alert("You can't specify AM or PM for military time.");
		return false;
	}

	if (minute<0 || minute > 59) {
		alert ("Minute must be between 0 and 59.");
		return false;
	}

	if (second != null && (second < 0 || second > 59)) {
		alert ("Second must be between 0 and 59.");
		return false;
	}

	return true;
}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isPhoneNumber(str){
	
	if (checkInternationalPhone(str)==false){
		alert("Please provide a valid Phone Number entry.\ne.g. (632) 638-9822")
		return false
	}
	return true
 }

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function getAge(dtStr) {
	if (dtStr != "" && dtStr != null)  { 
		if (!isDate(dtStr))  { 
			return '';
		}
	}

	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,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)

	var curDate = new Date();
	var curDay=curDate.getDate();
	var curMonth=curDate.getMonth()+1;
	var curYear=curDate.getFullYear();
	
	var yrDiff;
	
	if (month>curMonth) {
		yrDiff=curYear-year-1;
	} else if (month<curMonth) {
		yrDiff=curYear-year;
	} else { 
		if (month==curMonth && day > curDay) {
			yrDiff=curYear-year-1;
		} else {
			yrDiff=curYear-year;
		}
	}
	
	return (yrDiff.toString());
}


