$(document).ready(function(){
	$('#user_cart_quickview_link').mouseover(function(){ $('#user_cart_quickview').removeClass('hide'); });
	$('#user_cart_quickview_link').mouseout(function(){ $('#user_cart_quickview').addClass('hide'); });
	
	$('.no_link').live('click',function(){ return false; });
	$('.tabLink').click(function(){
		var linkage = 'user'+$(this).text();
		$('.content_inner').hide();
		$('#'+linkage).show();
		$('.tabLink').parent().removeClass('active');
		$(this).parent().addClass('active');
		return false; 
	});
	
	$('#go2cart').click(function(){ document.location.href='/user-checkout/' });
	
	$('.product_choice').change(function(){
		var rel = $(this).attr('rel');
		$("#addtocarttick"+rel).addClass('hide');
 		var str = new Array;
 		$('#product_choice'+rel+' option:selected').each(function () {
			str = $(this).attr('rel').split(',');
		});
 		$('#productprice'+rel).text(str[0]);
 		if(str[1] == 'm') { $('#productterm'+rel).text(' / month') }
 		else if(str[1] == 'a') { $('#productterm'+rel).text(' / year') }
 		else { $('#productterm'+rel).text('') }
 		$('#cartlink'+rel).attr('href','/user-cart/?add='+str[2]);
 		$('#cartlink'+rel).attr('rel',str[2]);
	});

	$('.cartlink').click(function(event){
		var productid = $(this).attr('rel');
		$("#addtocarttick"+rel).removeClass('hide');
		loadLSCart('new',productid,1);
		return false;
	});
	$('#go2cart').click(function(){ document.location.href='/user-cart/' });

	$('.renewlink').click(function(event){
		var productid = $(this).attr('rel');
		loadLSCart('new',productid,1,0,'',goToCart);
		return false;
	});

	
	$('.product-remove').live('click',function(event){
		var productid = $(this).attr('rel');
		loadLSCart('remove',productid,0);
		return false;
	});
	$('.product-qty').live('change',function(){
		loadLSCart('update',$(this).attr('rel'),$(this).val());
	});
	$('#currentCoupon').live('change',function(){
		loadLSCart('coupon',$(this).val());
	});
	$('.product-rec').live('click',function(){
		var thisChecked = 0;
		if($(this).attr('checked') == true) { thisChecked = 1 }  
		loadLSCart('recurring',$(this).attr('rel'),thisChecked);
	});
	$('#billing_reset').live('click',function(){
		// resets the billing address form
		var fArray = new Array('billing_id','billing_attn','billing_address1','billing_address2','billing_city','billing_prov','billing_postal','billing_phone','billing_fax');
		for(i=0;i<fArray.length;i++) {
			$('#'+fArray[i]).val('');
		}
		$('#billing_country').val($('#usercountry').val())
	});
	$('#billing_editinit').click(function(){
		$('#cart-billing-address-show').hide();
		$('#cart-billing-address-edit').show();
	});
	$('#ebilling_cancel').click(function(){
		$('#cart-billing-address-edit').hide();
		$('#cart-billing-address-show').show();
	});
	$('#billing_addinit').click(function(){
		$('#cart-billing-address-show').hide();
		$('#cart-billing-address-add').show();
	});
	$('#billing_cancel').click(function(){
		$('#cart-billing-address-add').hide();
		$('#cart-billing-address-show').show();
	});
	$('#billingaddress').live('change',function(){
		var thisObj = new Object();
		thisObj.addrid = $(this).val();
		$.ajax({
			url:		"/LEAP/MODULES/LS_Cart/loadAddress.lasso",
			data:		thisObj,
			async:		true,
			type:		'post',
			cache:		false,
			dataType:	'json',
		
		
			success: function(xhr) {
				$('#d_attn').html(xhr.attn);
				$('#d_address1').html(xhr.address1);
				$('#d_address2').html(xhr.address2);
				$('#d_city').html(xhr.city);
				$('#d_state').html(xhr.state);
				$('#d_postal').html(xhr.postal);
				$('#d_country').html(xhr.country);
				$('#d_phone').html(xhr.phone);
	
				$('#ebilling_country4tax').val(xhr.country);
				
				$('#ebilling_attn').val(xhr.attn);
				$('#ebilling_address1').val(xhr.address1);
				$('#ebilling_address2').val(xhr.address2);
				$('#ebilling_city').val(xhr.city);
				$('#ebilling_state').val(xhr.state);
				$('#ebilling_postal').val(xhr.postal);
				$('#ebilling_country').val(xhr.country);
				$('#ebilling_phone').val(xhr.phone);
				
				if(xhr.country == 'CA') {
					var tax = (Number($("#subtotalhold").val()) - Number($("#eduhold").val()) - Number($("#couponhold").val())) * 0.13;
					$("#taxhold").val(tax)
				} else {
					$("#taxhold").val('0');
				}
				calcTotals();
			}
		});
	});
	$('#checkoutnowplease').live('click',function(){
		// into checkoutformholder
		$.ajax({
			url:		"/LEAP/MODULES/LS_Cart/checkout.lasso",
			async:		true,
			type:		'post',
			cache:		false,
			dataType:	'json',
		
		
			success: function(xhr) {
				if(xhr.billingaddress == 0) {
					alert('Please enter a billing address and try again.')
				} else {
					if(xhr.resp.length) {
						$("#checkoutformholder").html(xhr.resp);
						$('#isform2submit').submit();
					}
				}
			}
		});
	});
	/* =========================================
	 * Loads the cart into the head
	 ========================================= */
	loadLSCart('load');
	
	$('.togglenext').click(function(){
		var closed = '&#x25ba;';
		var open = '&#x25bc;';
		if($(this).next('div').css('display') == 'none') { $(this).find('.arrow').html(open) } else { $(this).find('.arrow').html(closed) }
		$(this).next('div').toggle();
	});
	$('#openCoPr').click(function(){
		$('#description_view').hide();
		$('#description_edit').show();
	});
	$('#closeCoPr').click(function(){
		$('#description_view').show();
		$('#description_edit').hide();
	});
	$('#saveCoPr').click(function(){
		//acc_display_profile
		//acc_companyType (select)
		// acc_supportedVersions fieldset, child checkboxes
		
	});
	$('.invtoggle').click(function(){
		$('#idetail'+$(this).attr('rel')).toggle();
		if($('#itogglespan'+$(this).attr('rel')).hasClass('down')){ $('#itogglespan'+$(this).attr('rel')).removeClass('down').addClass('up') } else { $('#itogglespan'+$(this).attr('rel')).removeClass('up').addClass('down') }
		return false;
	});
	$('#searchClose').click(function(){ document.location.href="/search?reset"; });
	$('#userProfileEditSubmit').click(function(){ 
		if($('#pwd1').val().length > 0){
			if($('#pwd1').val().length < 6)				{ alert('Your password must be 6 characters or more, please try again'); return false }
			if($('#pwd1').val() != $('#pwd2').val()) 	{ alert('Your password and confirm password must match, please try again'); return false }
		}
		$('#userProfileEdit').submit(); 
	});
	$('#PhotoUploadTrigger > button').click(function(){
		$('#PhotoUploadTrigger').hide();
		$('#PhotoUploadArea').show();		
	});
	$('#cancelPhotoUpload').click(function(){
		$('#PhotoUploadTrigger').show();
		$('#PhotoUploadArea').hide();		
	});
	$('#LogoUploadTrigger > button').click(function(){
		$('#LogoUploadTrigger').hide();
		$('#LogoUploadArea').show();		
	});
	$('#cancelLogoUpload').click(function(){
		$('#LogoUploadTrigger').show();
		$('#LogoUploadArea').hide();		
	});
	$('#downloadlist > p > a.linkdir').live('click',function(){
		$('#downloadstatus').show();
		loadDownloads($(this).attr('rel'));
	});
	$('#downloaddirs > a').live('click',function(){
		$('#downloadstatus').show();
		loadDownloads($(this).attr('rel'));
	});
	
	$('#oldLassoVersion').change(function(){
		if($('#oldLassoVersion').val() == "0") { $('.updateText').hide(); return }
		if($('#newLassoVersion').val() == "0") { $('.updateText').hide(); return }
		updateCalc()
	});
	$('#newLassoVersion').change(function(){
		if($('#oldLassoVersion').val() == "0") { $('.updateText').hide(); return }
		if($('#newLassoVersion').val() == "0") { $('.updateText').hide(); return }
		updateCalc()
	});
	$('.extlink').click(function(){
		window.open(this.rel);
        return false;
	});
	$('.emailLink').click(function(){
		var newm = $(this).attr('rel');
		newm = newm.replace('//','@');
		newm = newm.replace(/\//g,'.');
		document.location.href='mailto:'+newm+'?subject=Contact%20from%20LassoSoft%20Website';
	});
	
});


function updateCalc(){
	var oldV = $('#oldLassoVersion').val();
	var newP = Number($('#newLassoVersion').val());
//	alert(oldV);
//	alert(newP);
	var newC = newP * 0.8;
	switch(oldV){
		case '8.5':
			newC = newP * 0.6;
			break;
		case '8.6':
			newC = newP * 0.4;
			break;
		case '9':
			newC = newP * 0.2;
			break;
	}
//	alert(newC);
	if(newC > 0.00){
		$('.updateCost').text('$ '+addCommas(newC.toFixed(2)));
		$('.updateText').show(); 
		return
	} else {
		$('.updateText').hide(); 
		return
	}
}

function loadLSCart(actionis,productid,qty,composite,serialid,callback) {
	/* =========================================
	 * Loads the cart into the head
	error:								function (xhr, textStatus, errorThrown) {
	alert(xhr.responseText);
},
	 ========================================= */
	var thisObj = new Object();
	if(actionis == 'update' || actionis == 'remove' || actionis == 'upgrade' || actionis == 'recurring' || actionis == 'renew' || actionis == 'new') {
		thisObj.actionis = actionis;
		thisObj.productid = productid;
		thisObj.qty = qty;
		thisObj.composite = composite;
		thisObj.serialid = serialid;
	}
	else if(actionis == 'coupon') {
		thisObj.actionis = 'coupon';
		thisObj.productid = productid;
		thisObj.qty = 1;
		thisObj.composite = composite;
		thisObj.serialid = serialid;
	}
//	alert(actionis);
	$.ajax({
		url:		"/LEAP/MODULES/LS_Cart/process.lasso",
		data:		thisObj,
		async:		true,
		type:		'post',
		cache:		false,
		dataType:	'json',
	
	
		success: function(xhr) {

			if(xhr.cart.length) {
				$("#usercart").html(xhr.cart);
			}							
			if(xhr.body.length) {
				$("#cartbody").html(xhr.body);
				$(".subtotalhere").html(xhr.subtotald); //d == display
				$(".edudiscounthere").html(xhr.edud); //d == display
				$("#subtotalhold").val(xhr.subtotal);
				$("#taxhold").val(xhr.tax);
				$("#eduhold").val(xhr.edu);

				$('#couponshow').removeClass('hide')	
				if(Number(xhr.coupon) > 0) {
					$('#couponshow').addClass('hide')
				}
				$("#couponhold").val(xhr.couponhold);
				$("#coupond").html(xhr.coupond);
//				alert(xhr.couponhold);
				$("#currentCoupon").val(xhr.currentCoupon);
				$("#currentCouponDescription").html(xhr.currentCouponDescription);
				calcTotals();
			}
			if(actionis == 'upgrade') {
				$(".group"+serialid).removeClass('activeserial');
				$(".item"+serialid+productid).addClass('activeserial');
			}
			if(callback) { callback.call(); }
		}
	});
}
function goToCart(){
	document.location.href='/user-cart/';
}
function calcTotals(){
	var sub = Number($("#subtotalhold").val());
	var edu = Number($("#eduhold").val());
	var coupon = Number($("#couponhold").val());
	var tax = Number($("#taxhold").val());
	var total = sub - edu - coupon + tax;
	$('.taxhere').html('$'+addCommas(tax.toFixed(2)));
	$('.totalvalhere').html('$'+addCommas(total.toFixed(2)));
}
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 loadDownloads(dir) {
	var thisObj = new Object();
	thisObj.dir = dir;
	$.ajax({
		url:		"/LEAP/MODULES/LS_Downloads/list.lasso",
		data:		thisObj,
		async:		true,
		type:		'post',
		cache:		false,
		dataType:	'json',
	
	
		success: function(xhr) {

//			if(xhr.cart.length) {
//				$("#usercart").html(xhr.cart);
//			}							
			if(xhr.list.length) {
				$("#downloadlist").html(xhr.list);
				$("#downloaddirs").html(xhr.current);
			}
			$('#downloadstatus').hide();
		}
	});
}


