function MM_showHideLayers() { //v3.0
var i,p,v,obj,args=MM_showHideLayers.arguments;
for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
obj.visibility=v; }
}

function MM_displayLayers() { //v3.0
var i,p,v,obj,args=MM_displayLayers.arguments;
for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
if (obj.style) { obj=obj.style; v=(v=='show')?'block':(v='hide')?'none':v; }
obj.display=v; }
}

function returnShowHide(x){
var i,p,v,obj,args=MM_displayLayers.arguments;
for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
if (obj.style) { obj=obj.style; v=(v=='show')?'block':(v='hide')?'none':v; }
obj.display=v; }
	
}

if (!Array.prototype.push) {  //Fix for older browsers
	function Array_push() {
	  var A_p = 0
	  for (A_p = 0; A_p < arguments.length; A_p++) {
	   this[this.length] = arguments[A_p]
	  }
	  return this.length
	}
	Array.prototype.push = Array_push
}

if(!Array.prototype.splice) { //fix for older browsers to the splice will work
    function array_splice(ind,cnt){
        if(arguments.length == 0) return ind;
        if(typeof ind != "number") ind = 0;
        if(ind < 0) ind = Math.max(0,this.length + ind);
        if(ind > this.length) {
            if(arguments.length > 2) ind = this.length;
            else return [];
        }
        if(arguments.length < 2) cnt = this.length-ind;
        cnt = (typeof cnt == "number") ? Math.max(0,cnt) : 0;
        removeArray = this.slice(ind,ind+cnt);
        endArray = this.slice(ind+cnt);
        this.length = ind;
        for(var i=2;i<arguments.length;i++){
            this[this.length] = arguments[i];
        }
        for(var i=0;i<endArray.length;i++){
            this[this.length] = endArray[i];
        }
        return removeArray;
    }
	Array.prototype.splice = array_splice;
}

function isArray(a) {
    return isObject(a) && a.constructor == Array;
}
function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
function isFunction(a) {
    return typeof a == 'function';
}


function tellafriendShow(i,x){
	//getTellaFriendPosition(i,x); //lets set the position of the element
	getTellaFriendPosition(i,x);
	MM_displayLayers(i,'','show'); //turn it on...
	
}

function tellafriendHide(i){
	MM_displayLayers(i,'','hide'); //turn it on...
}

function getTellaFriendPosition(i,y){
	obj=MM_findObj(y);
	popupobj=MM_findObj(i);
	popupfixedlayer=MM_findObj('tafmain');
	//get popup objects location
	ypopup=findPosY(popupfixedlayer);
	
	if(document.all){
		c = 9;
	}else{
		c = 0;	
	}

	
	x = findPosX(obj);
	y = findPosY(obj) - ypopup - 238 + c;
	
	window.scrollTo(0, y+100);
	
	popupobj.style.top = y+'px';
	//popupobj.style.left = x+'px';
}


function showhidelayer(i){
	
	obj=MM_findObj('layer'+i);

	if(obj.style.display == "none"){
		MM_displayLayers('layer'+i,'','show');
		MM_swapImage('img'+i,'','images/minus.gif',1);
	}else{
		MM_displayLayers('layer'+i,'','hide');
		MM_swapImage('img'+i,'','images/plus.gif',1);
	}

}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getPosition(i){
	obj=MM_findObj(i);
	popupobj=MM_findObj('popup');
	
	x = findPosX(obj);
	y = findPosY(obj) - 115;

	
	popupobj.style.top = y+'px';
	popupobj.style.left = x+'px';
	
	
	MM_showHideLayers('popup','','show');
	
}

function closePopup(){
	MM_showHideLayers('popup','','hide');
}

function presetDropDown(form,site,layer){
	//cuts up the hidden array//
	formValue = document[form][site].value;
	prepopArray = formValue.split("|");
	element = new Array();
	for(i=0;i<prepopArray.length;i++){
		element[i] = prepopArray[i].split(":");
	}
	if(layer == -1){ //this means this is a new entry
		layerElement = 0;
		locationElement = 0;
		milestoElement = '';
		layer = document[form][site].value == '' ? 0:prepopArray.length;
	}else{
		layerElement = element[layer][0];
		locationElement = element[layer][1]
		milestoElement = element[layer][2];
	}
	//now lets set the state dropdown menus to the correct setting//
	setDropDown(form, 'location', layerElement);
	//This changes the destination select menu to the correct options//
	changeSelectMenu(layerElement);
	//now lets set the destination dropdown menus to the correct setting//
	setDropDown(form, 'destination', locationElement);
	//Sets the miles too...
	document[form].milesto.value = milestoElement;

	document[form].temp.value = site+':'+layer;
	
}

function setDropDown(form, SelectBox, defValue){
  for (var i=0;i<document[form][SelectBox].options.length;i++){
    if (document[form][SelectBox].options[i].value==defValue){
      document[form][SelectBox].selectedIndex = i;
      break;
    }
  }
}


function changeSelectMenu(statecode){
	d = document.owner;
	var sel = document.owner.destination;
	
	//Delete everything in the menu
	sel.options.length = 0;

	//Inserts into the dropdown//
	var opt = new Option('Select a Location', '');
	sel.options[sel.options.length] = opt;
	if(statecode != 0){
		for(i=0;i<destination[statecode].length;i++){
			//lets split the array up//
			n = destination[statecode][i].split(":");
			//alert(n);
			if(d.site_id.value == n[0]){
				var opt = new Option(n[1], n[2]);
				sel.options[sel.options.length] = opt;
			}
		}
	}

}

function setValues(){
	d = document.owner;
	var x = d.temp.value.split(":");
	//x[0] = site
	//x[1] = what layer deep in the hidden var
	
	var site = d[x[0]].value.split("|");
	
	//Now lets update that layer//
	state = d.location.options[d.location.selectedIndex].value;
	dest = d.destination.options[d.destination.selectedIndex].value;
	milesto = d.milesto.value;
	site[x[1]] = state+':'+dest+':'+milesto;
	var joinArray = site.join("|");
	d[x[0]].value = joinArray;
	
	//Now lets close the popup layer.//
	closePopup();
	
	//Now lets write the new changes to the page//
	writeLayer(x[0]);
	
	
}




function writeLayer(site){  //re-write the site layer when there is a change.

	//first lets get the text info from the hidden field//
	d = document.owner;
	milesto = '';
	name = '';
	var elementVal = new Array();
	elementVal = d[site].value.split("|");
	if(elementVal == ""){//write blank//
		_writeBlank(site);
	}else{
		
		for(i=0;i<elementVal.length;i++){
			var elementItem = new Array();
			elementItem = elementVal[i].split(":");
				
			//get destination from destination array//
			destX = destination[elementItem[0]];
			
			for(x=0;x<destination[elementItem[0]].length;x++){ //find in the destinations array the same number in the sites value
				dest = destX[x].split(":");
				if(dest[2] == elementItem[1]){
					name += dest[1]+':';
					break;
				}
			}
			
			milesto += elementItem[2]+':';
			//now that we have the name and miles we can write the html//
			
		}
			name = name.substr(0,name.length-1);
			milesto = milesto.substr(0,milesto.length-1);
		writeHTML = _writehtml(site, name, milesto);
		
		
	}
	
}



function deleteItem(site, x){

	d = document.owner;
	var elementVal = new Array();
	elementVal = d[site].value.split("|");
	elementVal.splice(x,1); //note: this returns the array that was removed
	d[site].value=elementVal.join("|");
	
	//write the layer again
	writeLayer(site);
	
}


function _writeBlank(site){
	innerLayer = document.getElementById(site+"Layer");
	innerLayer.innerHTML = '<img src"images/spacer.gif" width="1" height="0">';
}


function _writehtml(site, name, milesto){
	d = document.owner;
	t = '';
	innerLayer = document.getElementById(site+"Layer");
	name = name.split(":");
	milesto = milesto.split(":");
	for(i=0;i<name.length;i++){	
	c = i + 1;
		t += '<table width="650" border="0" cellspacing="0" cellpadding="0">'
		t += '<tr>'
		t += '<td width="17" style=" background-image:url(images/black-dotline.gif); background-repeat:repeat-y;" valign="bottom"><img src="images/spacer.gif" width="1" height="21"></td>'
		t += '<td width="633">'
		t += '<table width="633" border="0" cellspacing="0" cellpadding="0">'
		t += '<tr>'
		t += '<td width="13" valign="bottom">'
		t += '<table width="100%" border="0" cellspacing="0" cellpadding="0">'
		t += '<tr>'
		t += '<td style="background-image:url(images/black-dotline-horz.gif); background-repeat:repeat-x;"><img src="images/spacer.gif" width="1" height="6"></td>'
		t += '</tr>'
		t += '</table>'
		t += '</td>'
		t += '<td width="620">'
		t += '<table width="100%" border="0" cellspacing="0" cellpadding="0">'
		t += '<tr>'
		t += '<td width="35%" style="padding-left:2px" id="'+site+i+'">'+c+'. '+name[i]+' - '+milesto[i]+' miles</td>'
		t += '<td width="7%"><a href="javascript:;" onClick="setSiteID('+d.site_id.value+'); presetDropDown(\'owner\',\''+site+'\','+i+'); getPosition(\''+site+i+'\')"><img src="images/owner-milesto-edit.gif" width="17" height="14" align="absmiddle">edit</a></td>'
		t += '<td width="2%">|</td>'
		t += '<td width="56%"><a href="javascript:;" onClick="setSiteID('+d.site_id.value+'); deleteItem(\''+site+'\', '+i+');"><img src="images/owner-milesto-delete.gif" width="17" height="14" align="absmiddle">delete</a></td>'
		t += '</tr>'
		t += '</table>'
		t += '</td>'
		t += '</tr>'
		t += '</table>'
		t += '</td>'
		t += '</tr>'
		t += '</table>'
	}
	innerLayer.innerHTML = t;
}


function setSiteID(i){
	d = document.owner;
	d.site_id.value = i;
}



function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	//alert(coordinates.x)
	return coordinates;
	}

// getAnchorWindowPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the window
function getAnchorWindowPosition(anchorname) {
	var coordinates=getAnchorPosition(anchorname);
	var x=0;
	var y=0;
	if (document.getElementById) {
		if (isNaN(window.screenX)) {
			x=coordinates.x-document.body.scrollLeft+window.screenLeft;
			y=coordinates.y-document.body.scrollTop+window.screenTop;
			}
		else {
			x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
			y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
			}
		}
	else if (document.all) {
		x=coordinates.x-document.body.scrollLeft+window.screenLeft;
		y=coordinates.y-document.body.scrollTop+window.screenTop;
		}
	else if (document.layers) {
		x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
	}
function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
	}	
function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
	}
function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
	}

function checkSubmit(url_str, form_obj) {
   var do_submit = confirm('Do want to save changes to this page?');
   if(do_submit) {
      form_obj.redirect_to.value = url_str;
      form_obj.submit();
   }
   else {
      window.location = url_str;
   }
}

function initChecklist() {
    if (document.all && document.getElementById) {
        // Get all unordered lists
	   var lists = document.getElementsByTagName("ul");
        
        for (i = 0; i < lists.length; i++) {
            var theList = lists[i];
            
		  // Only work with those having the class "checklist"
            if (theList.className.indexOf("checklist") > -1) {
                var labels = theList.getElementsByTagName("label");
                
			 // Assign event handlers to labels within
                for (var j = 0; j < labels.length; j++) {
                    var theLabel = labels[j];
                    theLabel.onmouseover = function() { this.className += " hover"; };
                    theLabel.onmouseout = function() { this.className = this.className.replace(" hover", ""); };
                }
            }
        }
    }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function print_flag(theme, flag, flag_link, flag_img, flag_alt) {
   document.write('<a href="' + flag_link + 'set_lang=' + flag + '" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'' + flag + '\',\'\',\'/images/' + theme + '/lang-' + flag + '.gif\',1)"><img src="/images/flag_' + flag_img + '.gif" alt="' + flag_alt + '" width="31" height="16" border="0" /></a>');
}

/*------------------- IS NUMERIC for text form feilds-------------------------------*/
//how to use: onkeypress="return numbersonly(this, event)"
function numbersonly(myfield, e, dec){
	var key;
	var keychar;

	if (window.event){
		key = window.event.keyCode;
	}else if (e){
		key = e.which;
	}else{
		return true;
	}

	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ){
		return true;
	}else if ((("0123456789").indexOf(keychar) > -1)) {// numbers
		return true;
	}else if (dec && (keychar == ".")) {  // decimal point jump
		myfield.form.elements[dec].focus();
		return false;
	}else{
		return false;
	}
}

function poptastic(url)
{
	newwindow=window.open(url,'name','height=600,width=570,scrollbars=yes');
	if (window.focus) {newwindow.focus()}
}