// JavaScript Document

function annotateLinks(oneId, newwindow, warning) {
	if (!document.getElementById || !document.getElementById(oneId)) {
		return null;
	}
	
	var aList=document.getElementById(oneId).getElementsByTagName('a');
	var aListLength = aList.length;

	var onLocalSite = /https?:\/\/((www\.)?(sfmuni|sfmta|picosearch)\.(com|org)|128\.121\.89\.101|sfmuni.securesites.net|sanfrancisco.granicus.com)\/(?!redirect)/i;
	var onLocalDrive = /file:\/\/\//;
	var eMailAddr = /mailto:/i;
	var forHttp = /https?:/i;
	var imgLink = /<img [^>]*alt=("([^"]*)"|'([^']*)')[^>]*>/i;
	var imgTestResult;
	var openWhatDesc;

	for (var count=aListLength-1;count>-1;count--) {
		if (aList[count].href) {
			if ((aList[count].href.search(onLocalSite) == -1) && aList[count].href.search(eMailAddr) == -1)  {
				if (aList[count].getAttribute("href").search(onLocalDrive) > -1) { // local drive or network link
					linkClone = aList[count].cloneNode(true);
					linkClone.style.backgroundColor = "#ffff00";
					linkClone.title = 'Warning! Public cannot link to hard drive/network for "' + aList[count].innerHTML + '"';
					linkParent = aList[count].parentNode;
					aList[count].title = 'Warning! Public cannot link to hard drive/network for "' + aList[count].innerHTML + '"';
					aList[count].innerHTML = '<img src="' + warning + '" class="newwindow">';
					linkParent.insertBefore(linkClone, aList[count]);
				} else if (aList[count].getAttribute("href").search(forHttp) > -1) { // off-site link
					linkClone = aList[count].cloneNode(true);
					linkParent = aList[count].parentNode;
					aList[count].target = "_blank";
					if (imgTestResult = imgLink.exec(aList[count].innerHTML)) { // = not ==
						if (imgTestResult.length > 1) {
							openWhatDesc = imgTestResult[1].substring(1, imgTestResult[1].length - 1);
						} else {
							openWhatDesc = aList[count].innerHTML;
						}
					} else {
						openWhatDesc = aList[count].innerHTML;
					}
					aList[count].title = 'Open link in new window for "' + openWhatDesc + '"';
					aList[count].innerHTML = '<img src="' + newwindow + '" class="newwindow">';
					linkParent.insertBefore(linkClone, aList[count]);
				}
			}
		}
	}
}

function annotateLinksById (normalIdList,inverseIdList) {
	var imgPath = "http://www.sfmta.com/cms/img/ui/"; 
	var inversewindow = imgPath + "newwindowinverse.gif";
	var normalwindow = imgPath + "newwindow.gif";	
	var warnlocal = imgPath + "warnlocal.gif";
	var inversewarnlocal = imgPath + "warnlocalinverse.gif";
	for (thisItem in normalIdList) {
		annotateLinks(normalIdList[thisItem], normalwindow, warnlocal);
	}
	for (thisItem in inverseIdList) { 
		annotateLinks(inverseIdList[thisItem], inversewindow, inversewarnlocal);
	} 
	
}

function fixHorzScrollItem(fixedHorzScrollItem) {
	if (document.getElementById && xGetElementById(fixedHorzScrollItem)) { 
		fixedObject = xGetElementById(fixedHorzScrollItem);
		fixedObjectClone = fixedObject.cloneNode(true);
/* fix the position here */
		fixedObjectParent = fixedObject.parentNode;
		xAppendChild(fixedObjectParent,fixedObjectParent);
	} else {
		return null;
	}
}

function fixHorzScroll(fixedHorzScrollList) {
	for (thisItem in fixedHorzScrollList) {
		fixHorzScrollItem(fixedHorzScrollList[thisItem]);
	}
}


function doOnLoad (normalIdList,inverseIdList,fixedHorzScrollList) {
	annotateLinksById(normalIdList,inverseIdList);	
	document.close();
//	fixHorzScroll(fixedHorzScrollList);
}


function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function CB_MM_validateForm() { //v3.0
// modified by CB on 6/22/00 from MM_validateForm in order to display custom field names
	var i,p,q,nm,custom,test,num,min,max,errors='',errorTarget='',args=CB_MM_validateForm.arguments; // cb 6/22/00
	whatForm=args[0];
	focusTarget='';
	altFocusTarget='';
	for (i=1; i<(args.length-4); i+=5) { 
		test=args[i+2]; 
		val=MM_findObj(args[i]);
//		saveObj=val;
		objName=(args[i]);
		custom=args[i+1]; // cb 6/22/00 set value of custom field to our custom text
		target=args[i+3]; // cb 9/9/02
		altObjName=args[i+4]; // cb 9/9/02
		if (test=='choice') { // all within cb 9/9/02
			choiceMade=0;
			for (j=0; j<(whatForm.elements[objName].length); j+=1) {
				if (whatForm.elements[objName][j].checked) {
					choiceMade+=1;
				}
			}
			if (choiceMade<1) {
				errors+='- You need to choose '+custom+'. \n'; // cb 6/22/00 use our custom field and wording
				if (errorTarget=='') {
					errorTarget=target;
					altFocusTarget=altObjName;
				}
			}
		}
		else if (val) { 
			nm=val.name; 
			if ((val=val.value)!="") {
				if (test.indexOf('isEmail')!=-1) {
					p=val.indexOf('@');
					if (p>1) {
						p=val.indexOf('.', p);
					}							
					if (p<1 || p==(val.length-1)) {
						errors+='- The box for '+custom+' must contain a valid e-mail address.\n'; // cb 6/22/00 use our custom field and wording
						if (errorTarget=='') {
							errorTarget=target;
//							focusTarget=saveObj;
							focusTarget=objName;
						}
					}
				} else if (test!='R') { 
					num = parseFloat(val);
					if (val!=''+num) {
						errors+='- The box for '+custom+' must contain a number.\n'; // cb 6/22/00 use our custom field and wording
						if (errorTarget=='') {
							errorTarget=target;
//							focusTarget=saveObj;
							focusTarget=objName;
						}
					}
					if (test.indexOf('inRange') != -1) { 
						p=test.indexOf(':');
						min=test.substring(8,p); 
						max=test.substring(p+1);
						if (num<min || max<num) {
							errors+='- The box for '+custom+' must contain a number between '+min+' and '+max+'.\n'; // cb 6/22/00 use our custom field and wording
							if (errorTarget=='') {
								errorTarget=target;
//								focusTarget=saveObj;
								focusTarget=objName;
							}
						}
					} 
				}
			} else if (test.charAt(0) == 'R') {
				errors += '- Please make an entry in the box for '+custom+'.\n'; 
				if (errorTarget=='') {
					errorTarget=target;
//					focusTarget=saveObj;
					focusTarget=objName;
				}
			}
		} // cb 6/22/00 use our custom field and wording
	} 
	if (errors) {
		alert('The following error(s) occurred:\n'+errors);
		document.MM_returnValue = false;
	} else {
		document.MM_returnValue = true;
	}
// we are not going to type into the alternate focus target; this is just so tabbing will take us to our real target, radio buttons, which cannot be the object of a focus()
	if (altFocusTarget!='') {
//		if (document.all) {
//			document.getElementById(altFocusTarget).focus();
//		} else {
			document.forms['mailform'].elements[altFocusTarget].focus();
//		}
	}
	if (errorTarget!='') location.hash=errorTarget;
// focus on text fields must take place after setting location, so Mozilla will make the text field immediately typeable
	if (focusTarget!='') {
			document.forms['mailform'].elements[focusTarget].focus();
	}
}

function adjustIFrameSize(thisFrame)
{
	if ( thisFrame.contentDocument ) // standart
	{
		thisFrame.height = thisFrame.contentDocument.scrollHeight;
	}
	else // IE
	{
		thisFrame.style.height = thisFrame.contentWindow.document.body.scrollHeight;
	}
}
