/*
 * Scripts
 *
 */
jQuery(function($) {
 
	var Engine = {
		utils : {
			links : function(){
				$('a[rel*=external]').click(function(e){
					e.preventDefault();
					window.open($(this).attr('href'));						  
				});
			},
			mails : function(){
				$('a[href^=mailto:]').each(function(){
					var mail = $(this).attr('href').replace('mailto:','');
					var replaced = mail.replace('/at/','@');
					$(this).attr('href','mailto:'+replaced);
					if($(this).text() == mail) {
						$(this).text(replaced);
					}
				});
			}
		},
		forms : {
			contact : function(){
				$('form.contact-form textarea').keydown(function(){
					if(this.value.length >= 1024) {
						this.value=this.value.substring(0,1023);			
					}		
				});
			}
		},
		fixes : {
			loginLink : function(){
				$('#li').each(function(){						

					if ($(this).text() == '1') {
						$('#nav a:contains(Login)').html('Log out').attr('href','/LogOutProcess.aspx').parent().before('<li><a href="/my-account">My Account</a></li>');
					}
				});
			},
            orderList : function(){
				$('.order-list li').each(function(){
					var total = $(this).find('.invoice-amount').text().replace('$','');
					var outstanding = $(this).find('.outstanding').text().replace('$','');
					var invoice = $(this).find('.invoice-number').text();
					if (outstanding == '0.00'){
						$(this).find('.action').html('<strong>Paid</strong>')
					}
					else {
						$(this).find('.action').html('<a href="/pay-invoice?INV='+invoice+'&amp;AMT='+total+'">Pay Now</a>')
					}
				})
			}
		}
	};

	Engine.utils.links();
	Engine.utils.mails();
	
	Engine.forms.contact();
	
	Engine.fixes.loginLink();
	Engine.fixes.orderList();
	
	/* grab vars from url */
	function getVar(value){   name = value.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");   var regexS = "[\\?&]"+value+"=([^&#]*)";   var regex = new RegExp( regexS );   var results = regex.exec( window.location.href );   if( results == null ) return ""; else return results[1];
	}

		
	var  inv = getVar('INV');
	var  amt = getVar('AMT');
	
	var amt = amt.replace("$", "");

	if(inv){$("#InvoiceNumber").val(inv);}
	if(amt){$("#Amount").val(amt);}
		
	// force cart link
	$(".cartLink").text("Shopping cart is empty. Continue Shopping.").attr("href","/rent-a-booth");
	
	// make human choose a state at beginning of order
	$("#catshopbuy").click(function(){
		
		var currentCityDropDown = $("#currentCityDropDown").val();
		var cartLinkage = $("#catshopbuy").attr("href");
		
		if(currentCityDropDown == "-1"){
			alert("Please choose from one of our Avaliable Cities");
			$("#currentCityDropDown").css("border","solid 1px red");
			$("#currentCityDropDown").css("padding","3px");
			return false;
		}else{
			//alert("You chose "+currentCityDropDown);
			$("#catshopbuy").attr("href",cartLinkage+"&currentCity="+currentCityDropDown);
		}
	});
	
	// spit that chosen city from above into register/buy form
	function getVar(value){   name = value.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");   var regexS = "[\\?&]"+value+"=([^&#]*)";   var regex = new RegExp( regexS );   var results = regex.exec( window.location.href );   if( results == null ) return ""; else return results[1];
}
	// getVar
	var currentCity = getVar('currentCity');

	// place var in billing 
	$("#BillingCity").val(currentCity);
	
	

});




