// JavaScript Document
function is_numeric (mixed_var) {
	if (mixed_var === '') {
		return false;
	}
	return !isNaN(mixed_var * 1);
}
$(function() {
	$("a.fancybox").fancybox({'zoomSpeedIn':700,'zoomSpeedOut':700});
	$("a.fancy").fancybox();
	$("a.fancyajax").fancybox({'frameWidth':180, 'frameHeight':120,'hideOnContentClick':false});
	$("a.fancymeta").fancybox({'hideOnContentClick':false});
	$("a.copartbox").fancybox({'frameWidth':Math.max(800,$(window).width()-100), 'frameHeight':Math.max(600,$(window).height()-100)});
	$("a.fancycopart").fancybox({'frameWidth':Math.max(800,$(window).width()-100), 'frameHeight':Math.max(600,$(window).height()-100),'hideOnContentClick':false});
	$('#searchdate').datepicker({minDate: '+0d',maxDate: '+3m',numberOfMonths: 2});
	
	$("#winbid").keyup(function(){cpbid()});
	$("#virtualbid").click(function(){cpbid()});
	
	$("select#stype").change(function(){
		$(".selblock").block({ message: '<h1><img src="/images/busy.gif" /> Loading Make List...</h1>' });
		$("#model_dropdown").load("/ajax/model_dropdown.php", {type:$("select#stype").val(),make:''});
		$("#make_dropdown").load("/ajax/make_dropdown.php", {type:$("select#stype").val()},loaded);
	});

	$("select#smake").livequery("change",function(){
//	 	$.blockUI({ message: '<h1><img src="/images/busy.gif" /> Searching...</h1>' });
//		sform.submit();
		$(".selblock").block({ message: '<h1><img src="/images/busy.gif" /> Loading Model List...</h1>' });
		$("#model_dropdown").load("/ajax/model_dropdown.php", {type:$("select#stype").val(),make:$("select#smake").val()},loaded);
	});


	$("#admin_button").click(function(e){
	  $("#admin_panel").animate({ 
        left: e.pageX,
        top: 100
		}, 100 )
   	  }); 
	$('#admin_panel').draggable({ handle: '.panel_header', opacity: 0.35 });
	
	var container = $('div.container');
	// validate the form when it is submitted
	var cvalidator = $("#contactform").validate();
	var validator = $("#form1").validate({
		rules: {
			driverlicence: {
				required: 	function(element) {
       							return ($("#country").val() == "USA" || $("#country").val() == "CAN");
     							}
			},
			region: {
				required: 	function(element) {
       							return (($("#country").val() == "USA" || $("#country").val() == "CAN") && $("#region").val() == '');
     							}
			},
			passport: {
				required: 	function(element) {
       							return ($("#country").val() != "USA" && $("#country").val() != "CAN");
     							}
			}
		},
		messages: {
    			region: "You must select a state"
		},
		errorContainer: container,
		errorLabelContainer: $("ol", container),
		wrapper: 'li',
		meta: "validate",
		showErrors: function(errorMap, errorList) {
			if(this.numberOfInvalids() > 0){
				$("#summary").html("Your form contains "
                                  + this.numberOfInvalids() 
                                  + " errors, see details Above the form.  Please correct and press Next to recheck.");
			} else {
								$("#summary").html("");
				}
			this.defaultShowErrors();
		}
	});

	$('#country').blur(function(){$('#passport').valid();$('#region').valid();$('#driverlicence').valid();});

		$("#winbid").val("$8,000");
		$("#winbid").focus();
		cpbid();

	if(typeof page_startup == 'function') { 
		page_startup(); 
	}
});

function cpbid(){
var winbid = $("#winbid").val().replace(/[^0-9\.]/mg, "");

if(is_numeric(winbid)){
			var tfee = parseInt (Math.max(200, winbid * 0.05));
			var buyerfee = parseInt(cpfee(winbid));
			var bidfee = parseInt(cppremb(winbid));
			$("#bidfee").text(addCommas(bidfee));
			$("#buyerfee").text(addCommas(buyerfee));
			$("#transfee").text(addCommas(tfee));
			$("#feetotal").text(addCommas(tfee+buyerfee+bidfee+30+parseInt(winbid)));
			$(".feeud").stop().css({'background-color' : 'white'});
			$(".feeud").effect("highlight", {color:"#FF6622"}, 500);
		}
}
function loaded(){
	$(".selblock").unblock();	
}
function cppremb(val){
	if(val < 1000){
		return 35;
	} else {
		if (val < 2000){
			return 50;
		} else {
			return 75;
		}
	}
}
function cpfee(val){
	switch(true){
		case val < 100:
		return 20;
		break;
		case val < 200:
		return 45;
	    break;
		case val < 400:
		return 75;
	    break;
		case val < 600:
		return 95;
	    break;
		case val < 800:
		return 125;
	    break;
		case val < 1000:
		return 150;
	    break;
		case val < 1200:
		return 175;
	    break;
		case val < 1400:
		return 200;
	    break;
		case val < 1700:
		return 225;
	    break;
		case val < 2000:
		return 250;
	    break;
		case val < 2500:
		return 275;
	    break;
		case val < 3000:
		return 300;
	    break;
		case val < 4000:
		return 325;
	    break;
		case val < 17500:
		return 350;
	    break;
		case val >= 17500:
		return val * 0.02;
	    break;
		default:
  			alert("error");
	}
}
 function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
function ClearCache(){
	$.blockUI({ message: '<h1><img src="/images/busy.gif" /> Clearing Cache...</h1>' });
	$.get('/ajax/clearcache.php', RefreshPage);
}
function LogOut(){
	$.blockUI({ message: '<h1><img src="/images/busy.gif" /> Logging Out...</h1>' });
	$.get('/ajax/logout.php', RefreshPage);
}
function ImportData(){
	$.blockUI({ message: '<h1><img src="/images/busy.gif" /> Importing Spreadsheet...</h1>' });
	$.get('/ajax/importdata.php', RefreshPage);
}
function DeleteCars(){
	$.blockUI({ message: '<h1><img src="/images/busy.gif" /> Deleting Cars...</h1>' });
	$.get('/ajax/deletecars.php', RefreshPage);
}

function RefreshPage(){
	window.location.reload();
}

