// JavaScript Document
$("document").ready(function (){
	doBannerEmbed();
	
	//color rows
	$("table.color tr:even").addClass("row2");
	
	//deletion check
	$("a.delete").click(function(){
		var href=$(this).attr("href");
		$.alerts.okButton="Yes";
		$.alerts.cancelButton="No";
		jConfirm("Are you sure you want to delete this record?", "Delete Record", function(r){
				if(r){
					window.location=href;
				}
		});
		return false;
	});
	
	$("a.blank").click(function(){
			window.open($(this).attr("href"), "newwin");
			return false;
	});
	
	if($('input.dpicker').length>0){
		Date.format = 'mm/dd/yyyy';
		$('input.dpicker').datePicker({startDate:'01/01/2009', clickInput: false});
	}
	
	$('input[name=adstartdate]').change(function(){
			if($('input[name=adenddate]').val()==""){
				var val = $(this).val();
				var d=new Date(val);
				d.addDays(30);
				$('input[name=adenddate]').val(d.asString('mm/dd/yyyy'));
			}
	});
			 
	$('input[name=textad]').change(function(){
		var val=$('input[name=textad]:checked').val();
		
		if(val==0){
			$('input.filead').removeAttr("disabled");
			$('input.filead').next('span').css("display", "none");
			$('input.textad').val("");
			$('input.textad').attr("disabled","disabled");
			$('input.textad').next('span').css("display", "none");
		}else if(val==1){
			$('input.textad').removeAttr("disabled");
			$('input.filead').val("");
			$('input.filead').attr("disabled","disabled");
				$('input.textad').next('span').css("display", "none");
				$('input.filead').next('span').css("display", "none");
		}
	});
	
	$('select[name=provstate]').change(function(){
			var val=$(this).val();
			if(val=="other"){
				$('input[name=otherprov]').removeAttr("disabled");	
			}else{
				$('input[name=otherprov]').val("");
				$('input[name=otherprov]').attr("disabled", "disabled");	
			}
	});
		
	$('select[name=discounttype]').change(function(){
		var val=$(this).val();
		if(val=="amount"){
			clearDropDown("discountamount");
			for(i=5; i<=50; i=i+5){
				addOption("discountamount", i, "$"+i);
			}
		}else if(val=="percent"){
			clearDropDown("discountamount");
			for(i=5; i<=50; i=i+5){
				addOption("discountamount", i, i+"%");
			}
		}
		makeCouponNote();
	});
	
	$('select[name=discountamount]').change(function(){
			makeCouponNote();								 
	});
	
	$('#fingerprintgo').click(function(){
	 });
});

function makeCouponNote(){
		var type=$('select[name=discounttype]').val();
		var amount=$('select[name=discountamount]').val();
		var discounttext="";
		if(type=="amount")
			discounttext="$"+amount+" off";
		else if(type=="percent")
			discounttext=amount+"% off";
		$('textarea[name=note]').val(discounttext);			
}
function doBannerEmbed(){
	var hasflash=$.flash.available;
	if(hasflash){
		$("#flash-banner").html("");
		$("#flash-banner").flash({
			swf: '/flash/newflash.swf',
			width: 742,
			height:150,
			menu:false
		});
	}
}

function addOption(sel_id, value, name){
	if(document.getElementById(sel_id)!=null){
		var opt=document.createElement("option");
		opt.text=name;
		opt.value=value;
		document.getElementById(sel_id).options.add(opt);
	}
}

function clearDropDown(sel_id){
	if(document.getElementById(sel_id)!=null){
		document.getElementById(sel_id).options.length=0;
	}	
}