function populateForm(xml,form){
	
	$('form#'+form).find(":text").each(function(){
		
		var str = $(xml).find($(this).attr("name")).text();
		if(str.length > 0)	
			$(this).attr("value", str);
		
	});
	
	$('form#'+form).find("input:hidden").each(function(){
		var str = $(xml).find($(this).attr("name")).text();
		if(str.length > 0)	
			$(this).attr("value", str);
		
	});
	
	$('form#'+form).find(":radio").each(function(){
	
		
		var str = $(xml).find($(this).attr("name")).text();
			
			if(str == $(this).attr("value")){
				$(this).attr("checked", "checked");
			}
		
	});
	
	$('form#'+form).find(":checkbox").each(function(){
		var checkbox = $(this);
		checkbox.attr("checked", ""); //uncheck all boxes.
		var checkbox_name = checkbox.attr("name");
		//if checkbox name has [] strip them when checking to see if item is in xml doc
		if(checkbox.attr("name").substr(checkbox.attr("name").length -2, 2) == '[]')
			checkbox_name = checkbox.attr("name").substr(0,checkbox.attr("name").length -2);
		
		
		$(xml).find(checkbox_name).each(function(){
			if(checkbox.attr("value") == $(this).text()){
				checkbox.attr("checked", "checked");
			}
		});
	});
	
	//$('#distance').attr('value', $(xml).find('distance').text()) ;
}

function loadTripTemplate(id, form){
	
	if(id != ""){
		$.ajax({url: '/index.php?controller=Trip&action=get_triptemplate_xml&id=' + id,
				type: 'GET',
				dataType: 'xml',
				timeout: 9000,
				error: function(){
					alert('Whoops. There was a problem loading your saved trip. Please try again.\n');
					$('.loading').hide();
				},
				success: function (xml){
					//alert('I should do something with the xml' + xml);
					populateForm(xml, form);
					$('.loading').hide();
				}
				});
	}
}



function tableruler()
{
 if (document.getElementById && document.createTextNode)
  {
   var tables=document.getElementsByTagName('table');
   for (var i=0;i<tables.length;i++)
   {
    if(tables[i].className=='data-list')
    {
     var trs=tables[i].getElementsByTagName('tr');
     for(var j=0;j<trs.length;j++)
     {
      if(trs[j].parentNode.nodeName=='TBODY' && trs[j].parentNode.nodeName!='TFOOT')
       {
       trs[j].onmouseover=function(){this.className='ruled';return false}
	   if((j+1)%2 == 0) //ugly but it takes into account first th row. 
       	trs[j].onmouseout=function(){this.className='even';return false}
	   else
		trs[j].onmouseout=function(){this.className='odd';return false}
     }
    }	
   }
  }
 }
}


	function loadModalWindow(id){
		//if(windowStatus == 0){
			$("#modal-background").css({"opacity": "0.7"});
			$("#modal-background").fadeIn("fast");
			$("#"+id).fadeIn("fast");
		//	windowStatus = 1;
			
		//}
		
		centerModalWindow(id);
		
	}
	
	function centerModalWindow(id){
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var modalHeight = $("#" + id).height();
		var modalWidth = $("#" + id).width();
		
	/*	$("#" + id).css({  
		"position": "absolute",  
		"top": windowHeight/2-modalHeight/2,  
		"left": windowWidth/2-modalWidth/2  
		}); */ 
		
		
		$("#"+id).css({marginLeft: '-' + parseInt((modalWidth / 2), 10) + 'px'});
		if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
			$("#"+id).css({marginTop: '-' + parseInt((modalHeight / 2),10) + 'px'});
		}
		
		//only need force for IE6  

	/*	$("#modal-background").css({  
		"height": windowHeight  
		});*/
		
		
		
	}
	
	
	function hideModalWindow(){
		
		//if(windowStatus ==1){
			$("#modal-background").fadeOut("fast");
			$(".modal-content").fadeOut("fast");
			//windowStatus = 0;
		//}
	}
	





