var ie = document.all;

function check_date(val,t){
	var dt=new Date();
	var y=(dt.getFullYear());
	var m=(dt.getMonth()+1);		
	var d=(dt.getDate());
	var h=(dt.getHours());
	var mn=(dt.getMinutes());
	m=m<10?"0"+m:m;
    d=d<10?"0"+d:d;	h=h<10?"0"+h:h;	mn=mn<10?"0"+mn:mn;	
	var tm=h+":"+mn+":00";	
	var nw=y+"-"+m+"-"+d;	
	var v=val>nw || val==nw && t>=tm;		
	
	return v;
}
function check_book_form(){ 
	var pd=$("#rsv_pick_up_date").val();
	var dd=$("#rsv_drop_off_date").val();
	var ph=$("#rsv_pick_up_time").val();
	var dh=$("#rsv_drop_off_time").val();
	if(!check_date(pd,ph)){
		alert($("#error_rsv_pick_up_date").html());
		return false;
	}
	if(!check_date(dd,dh)){
		alert($("#error_rsv_drop_off_date").html());
		return false;
	}	
	if(dd<pd){
		alert($("#error_book_dates").html());
		return false;	
	}
	
	return openPopup();
	
}

function addModel(t){
	var m=prompt(t);
	var b=$("#car_brd_id").val();		
	if(m && b){
		$.ajax({
			   type: "POST",
			   url: "?p=Models",			   
			   data:{mdl_name:m,mdl_brd_id:b,save:1},
			   cache: false,
			   success: function(msg){ 
				   loadModels(b);			   
			   }
			 });		
	}
}
function loadModels(b){
	var c=document.getElementById("car_mdl_id");
	if(b){
	  $.ajax({
		   type: "POST",
		   url: "?p=Models&getModels="+b,			   
		   cache: false,
		   success: function(msg){ //alert(msg);
			   OPT(c,msg);
			   c.options.selectedIndex=c.options.length-1;
		   }
		 });	
	}
	else{
		rmAllOpt(c,1);
	}
}
function openPopup(url,h,w){
	w = w ? w : 600;
	h = h ? h : 600;		
	t = screen.availHeight/2 - h/2;
	l = screen.availWidth/2 - w/2;		
	window.open("","_popup","toolbars=0,location=0,width="+w+",height="+h+",top="+t+",left="+l);
	return true;
}

function mypopup(url,w,h,n){ 
	w = w ? w : 600;
	h = h ? h : 600;	
	var name = n ? n : '_popup';
	t = screen.availHeight/2 - h/2;
	l = screen.availWidth/2 - w/2;	
	window.open(url,name,'width='+w+',height='+h+',top='+t+',left='+l+',toolbars=no,scrollbars=yes');	
}
function toggleAll(cb,name) {
    var val = cb.checked;
	var name = name ? name : 'delete[]';
	var frm = document.list_frm;
	var len = frm.elements.length;
	var i=0;
	for( i=0 ; i<len ; i++) {
		if (frm.elements[i].name==name && !frm.elements[i].disabled) {
			frm.elements[i].checked=val;
		}
	}
}

function showTip(e,name){

	var b = document.getElementById("tipBox_" + name);	
	if(b){
		if(ie){
			b.style.pixelTop = window.event.clientY + 110;
			b.style.pixelLeft = window.event.clientX - 170;		
		}
		else{
			b.style.top = e.pageY;
			b.style.left = e.pageX - 170;	
		}
		b.style.display='block';
	}
}
function hideTip(e,name){
	var b = document.getElementById("tipBox_" + name);
	if(b){
		b.style.display='none';
	}
}


function obj(id){
	return document.getElementById(id);
}
function objn(name){
	return document.getElementsByName(name);
}
function OPT(c,t){
	rmAllOpt(c,1);
	if(t!=""){
		myarray = t.split( '|' );
		for ( var i = 0; i < myarray.length; i++ ) {
			if ( myarray[i] != "" ) {
				cur = myarray[i].split( ';' );
				addOpt(c,cur[1], cur[0]);
			}
		}
	}
}
function addOpt(c,text,value){
	c.options[c.options.length] = new Option(text,value);
}
function rmOpt(c,i){
	c.options[i] = null;
}
function rmAllOpt(c,firstOpt){
	k=c.length;
	for(i=0;i<k;i++){
		rmOpt(c,firstOpt);
	}
}
function getValueOfRadio(name){
	var t = document.getElementsByName(name);
	for(var i= 0; i<t.length; i++){
		if(t[i].checked == true){
			return t[i].value;
		}
	}
	return "";
}
function changeRadioValue(name,val){
	var t = document.getElementsByName(name);
	for(var i= 0; i<t.length; i++){
		if(t[i].value == val){
			t[i].checked = true;
		}
	}	
}
function checkAll(name,val){
	if(document.getElementsByTagName('input'))  
	{
		var inpts = document.getElementsByTagName('input');
		for ( i=0;i<inpts.length;i++)
		{
			if(inpts[i].type == "checkbox" || inpts[i].type == "radio" )
 			{
				if(!inpts[i].disabled ){
					inpts[i].checked = val;
				}
			}
		}
	}	
}

function gourl(url){
	window.location = url;
}
function ajax_failure(){
	//alert('Something went wrong...')
}
function ajax_loading(){
	//alert('Loading...');
}






function slideShow() {

	//Set the opacity of all images to 0
	$('#gallery a').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('#gallery a:first').css({opacity: 1.0});
	
	//Set the caption background to semi-transparent
	$('#gallery .caption').css({opacity: 0.7});

	//Resize the width of the caption according to the image width
	$('#gallery .caption').css({width: $('#gallery a').find('img').css('width')});
	
	//Get the caption of the first image from REL attribute and display it
	$('#gallery .content').html($('#gallery a:first').find('img').attr('rel'))
	.animate({opacity: 1}, 400);
	
	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('gallery()',5000);
	
}

function gallery() {
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#gallery a.show')?  $('#gallery a.show') : $('#gallery a:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery a:first') :current.next()) : $('#gallery a:first'));	
	
	//Get next image caption
	var caption = next.find('img').attr('rel');	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	$('#gallery .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	$('#gallery .caption').animate({opacity: 0.7},100 ).animate({height: '50px'},500 );
	
	//Display the content
	$('#gallery .content').html(caption);
	
	
}
