
var site_root = '/';
var cursor_type = (document.all) ? 'hand' : 'pointer';
var content_div;
var banner_div;
var body_tag;

//	Browser detection
var w3c = (document.getElementById) ? true : false;
var ie = (document.all && !w3c) ? true : false;
var ns4 = (document.layers) ? true : false;





function init () {
	content_div = document.getElementById('content');
	banner_div = document.getElementById('banner');
	body_tag = document.getElementsByTagName('BODY')[0];
	
	createFontResizer();
	setPDFlinks();
	logoPress();
	
	var font_size = getFontCookie();
	if (font_size != null) {
		body_tag.className = font_size;
	}
}

if (w3c) { 	window.onload=init; }


function createFontResizer() {
	var h1_tag = document.getElementsByTagName('H1')[0];
	var size_imgs = new Array (3);
		
	if (h1_tag && banner_div) {
		//alert('Yeah mon!');
		fontResizer_div = document.createElement('DIV');
		fontResizer_div.id = 'fontResizer';
		
		fontsize_img = document.createElement('IMG');
		fontsize_img.src = site_root + 'images/font_size.gif';
		
		fontResizer_div.appendChild(fontsize_img);
		
		for (var aa=0; aa<size_imgs.length; aa++) {
			size_imgs[aa] = document.createElement('IMG');
			size_imgs[aa].src = site_root + 'images/font_size_' + (aa+1) + '.gif';
			size_imgs[aa].assoc_classname = 'larger' + (aa+1);
			size_imgs[aa].onclick = function () {
				body_tag.className = this.assoc_classname;
				setFontCookie(this.assoc_classname)
			}
			size_imgs[aa].onmouseover = function () {
				this.style.cursor = cursor_type;
			}
			fontResizer_div.appendChild(size_imgs[aa]);
		}
		banner_div.insertBefore(fontResizer_div,h1_tag);
	}
}

function setFontCookie(class_name) {
	
	//format expiry date
	font_date = new Date();
	font_date.setTime(font_date.getTime() + ( 365 *24*60*60*1000));

	if (class_name == 'larger1') {
		font_date.setTime(0); //	Deletes the cookie
	}
	document.cookie = 'fontSize=' + class_name
		+ '; expires=' + font_date.toGMTString() 
		+ '; path=/';
}


function getFontCookie() {
	font_cookie = null;	
	
	if (document.cookie && document.cookie.indexOf('fontSize')!=-1) {
		font_cookie = document.cookie.split('fontSize=');
		font_cookie = font_cookie[1].split(';');
	}
	// alert(font_cookie);
	
	return font_cookie;
}

function logoPress() {
	var logo_link = document.getElementById('logoLink');
	var logo_img = logo_link.getElementsByTagName('IMG')[0];
	
	if (logo_img) {
		logo_link.onmousedown = function() {
			logo_img.src = site_root + 'images/aapm_down.gif';
		}
		logo_link.onmouseup = function() {
			logo_img.src = site_root + 'images/aapm.gif';
		}
	}
}

function setPDFlinks() {
	var all_links = document.getElementsByTagName('A');
	var all_areas = document.getElementsByTagName('AREA');
	
	for (var aa=0; aa<all_areas.length; aa++) {		
		if (all_areas[aa].target.toLowerCase() == 'pdflink') {		
			all_areas[aa].onclick = function() {
				popPDF(this);
				return false;
			}
		}
	}
	
	for (var bb=0; bb<all_links.length; bb++) {		
		if (all_links[bb].target.toLowerCase() == 'pdflink') {		
			all_links[bb].onclick = function() {
				popPDF(this);
				return false;
			}
		}
	}
}

//	Popup window code
var page_position = (parseInt(navigator.appVersion) > 3) ? 'left=0,top=0,screenX=0,screenY=0,' : "";
var popup_features = 'menubar=yes,status=yes,scrollbars=yes,resizable=yes,width=785,height=550';

function popupWindow(url, target, features) {
	if (isUndefined(features)) {
		features = popup_features;
	}
	if (isUndefined(target)) {
		target = '_blank';
	}
	var the_window = window.open(url, target, page_position+features);
	the_window.focus();
	return the_window;
}

function linkPopup(src, features) {
	return popupWindow(src.getAttribute('href'),src.getAttribute('target') || '_blank',features);
}//	End Popup window code

//	Utility function [currently used in popupWindow()]
function isUndefined(v) { 
	//	Returns true if [v] is not defined, false otherwise
	//	IE 5.0 does not support the undefined keyword, so we cannot 
	//	do a direct comparison such as v===undefined.
	var undef;
	return v===undef;
}


//	Pop up a PDF!!!
var the_pdf;
function popPDF(src,features) {
	the_pdf = src;
	var pdf_target = src.getAttribute('target') || 'PDFlink';
	if (isUndefined(features)) {
		features = 'menubar=yes,status=yes,scrollbars=yes,resizable=yes,width=750,height=485';
	}
	//alert(the_target);
	return popupWindow(site_root + 'pdf/pdf_launcher.html',pdf_target,features);
}

function doPDF(win_ref) {
	win_ref.location.href = the_pdf;
	//location.href=location.search.substring(1);
}



function doPrint() {
	if (window.print) {
		window.print();
	} else {
		alert('To print this page, click on the File menu and choose Print \nor press Cntrl + P on your keyboard.');
	}
}

function addRow(qid)
{
   var numi = document.getElementById('h'+qid);
   var num = (document.getElementById('h'+qid).value -1)+ 2;
   numi.value = num;
   var myRow = document.getElementById('myTable'+qid).insertRow(num);

   myRow.setAttribute("id","tr"+num);
   
   var removeCell = myRow.insertCell(0);
   var firstCell = myRow.insertCell(1);
   var secondCell = myRow.insertCell(2);
   var thirdCell = myRow.insertCell(3);
   
  //Insert form fields in cells
 //removeCell.innerHTML = "<a href='javascript: deleteRow(" + num + ", " + qid + ");'>[remove" + num + "]</a>";
 removeCell.innerHTML = "<a href='javascript:void(0)' onclick='deleteRow(this,"+qid+");'>[remove]</a>";
 firstCell.innerHTML = "<div align='center'><textarea name='sis"+qid+"[]' cols='30' rows='2'></textarea></div>"; 
 secondCell.innerHTML = "<div align='center'><input type='text' name='dsi"+qid+"[]' id='dsi"+num+"' size='15' value='' \></div>";
 thirdCell.innerHTML = "<div align='center'><input type='text' name='dpf"+qid+"[]' id='dpf"+num+"' size='15' value='' \></div>";
 //alert(document.getElementById("tr"+num).id);
}

function deleteRow(r,qid)
{
	var numi = document.getElementById('h'+qid);
   	var num = (document.getElementById('h'+qid).value -1);
  	numi.value = num;
	
	var i=r.parentNode.parentNode.rowIndex;
	document.getElementById('myTable'+qid).deleteRow(i);
	//alert(document.getElementById("tr"+i).id);
	//document.getElementById('myTable'+qid).deleteRow(i);
}

/***********************************************************************************************/
// Declaring valid date character, minimum year and maximum year
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,qid){
	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)
	//alert(dtStr.length);
	if (dtStr.length>0 && (pos1==-1 || pos2==-1)){
		alert("The date format should be : mm/dd/yyyy for question "+qid+".")
		return false
	}
	if (dtStr.length>0 && (strMonth.length<1 || month<1 || month>12)){
		alert("Please enter a valid month for question "+qid+".")
		return false
	}
	if (dtStr.length>0 && (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])){
		alert("Please enter a valid day for question "+qid+".")
		return false
	}
	//alert(strYear.length);
	//if (strYear.length != 4 || strYear.length != 2|| year==0 || year<minYear || year>maxYear){
	  if (dtStr.length>0 && strYear.length != 4 && strYear.length != 2){
		//alert("Please enter a valid 2 or 4 digit year between "+minYear+" and "+maxYear)
		alert("Please enter a valid 2 or 4 digit year for question "+qid+".")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date for question "+qid+".")
		return false
	}
return true
}

function ValidateForm(obj){
	//var dt=document.thisform.obj
	//var dt=document.thisform.sis;
	var r = obj.name.replace("[]","");
	var qid = r.substr(3);
	//alert(qid);
	if (isDate(obj.value,qid)==false){
		obj.select();
		document.thisform.subm.disabled = true;
		return false
	} else {
		document.thisform.subm.disabled = false;
	}
    //return true
 }
 
function doMPID(id)
{
	document.getElementById("mp"+id).value = id;	
}

/******************************************************/









