/**
 * AJAX FUNCTIONS
 *
 */
function ajaxClassList(class_type)
{
	
	$(document.getElementById(class_type+'[class_list]')).load('/ajax-class-list/'+class_type);
	$(document.getElementById(class_type+'[class_list][content]')).show();
	return true;
	/*
	var classLoad = new Ajax.Updater(class_type+'[class_list]', '/ajax-class-list/'+class_type,
									{evalScripts: true,
									  onLoading:function(){$(class_type+'[class_list]').show();$(class_type+'[class_list][loader]').show();},  
									  onSuccess:function(){$(class_type+'[class_list][loader]').hide();$(}
									  });
				*/
	return true;
	
}

function ajaxClassSchedule(class_slug, cart_slug)
{
	var classLoader = "<div style=\"text-align: center; width: 100%\">Loading Class Information..<br /><img src=\"../../images/ajax/window_overlay_loader.gif\"></div>";
	
	$('#\\[class_schedule\\]'+class_slug).addClass("class_schedule_loader");
	$('#\\[class_schedule\\]'+class_slug).html(classLoader); 
	$('#\\[class_schedule_row\\]'+class_slug).show();
	
	$.ajax({
		   url: '/ajax-class-schedule/'+class_slug+'/'+cart_slug,
		   success: function(html){
			     $('#\\[class_schedule\\]'+class_slug).html(html);
				 $('#\\[class_schedule\\]'+class_slug).removeClass("class_schedule_loader");
		    }
		   });
		
	return true;
	
	
}
function ajaxHideClassSchedule(class_slug)
{
	if ($('#\\[class_schedule_row\\]'+class_slug) != undefined)
	{
		$('#\\[class_schedule_row\\]'+class_slug).hide();
	}
}

/**
 * Assist with payment form setup
 *
 */
var PaymentSetup = function(total, shipping, delimiter, currency)
{
	this.total = total;
	this.shipping = shipping;
	this.total_update = 'payment_total_area';
	this.shipping_update = 'shipping_update_area';
	this.total_verify_update = 'cart_total_verify_update';
	this.currency = currency?currency:"$";
	this.delimiter = delimiter;
	
	this.updateShipping = function(ship_data)
	{
		parseInt(this.total, 10);
		var data = ship_data.split(this.delimiter);
		var ship_cost = data[1]!=undefined?data[1]:0;
		this.total -= parseFloat(this.shipping, 99);
		this.total += parseFloat(ship_cost, 99);
		this.shipping = parseFloat(ship_cost, 99);
		
		document.getElementById(this.shipping_update).innerHTML = this.currency+this.shipping.toFixed(2);
		document.getElementById(this.total_update).innerHTML = this.currency+this.total.toFixed(2);
		document.getElementById(this.total_verify_update).innerHTML = this.currency+this.total.toFixed(2);
		
	}
	
	this.getTotal = function()
	{
		return this.currency+this.total;
	}
	
	this.roundNumber = function(num, dec) 
	{
		var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
		return result;
	}
}

/**
 * Form validation
 *
 */
function validatePaymentForm(click_button, loading_target, form, arr_name, include_shipping)
{
	if (include_shipping)
		var shipping = form.elements[arr_name+'[shipping_option]'].value;
	
	var billing_name = form.elements[arr_name+'[card_full_name]'].value;
	var card_number = form.elements[arr_name+'[card_num]'].value;
	var exp_month = form.elements[arr_name+'[exp_month]'].value;
	var exp_year = form.elements[arr_name+'[exp_year]'].value;
	var sec_code = form.elements[arr_name+'[cvv]'].value;
	
	var email = form.elements[arr_name+'[email]'].value;
	
	var errors = new Array();
	
	if (include_shipping && shipping.length < 2)
		errors.push("Please select a shipping method.");
	
	if (!isValidString(billing_name))
		errors.push("Please enter the billing name EXACTLY as it appears on the card.");
	
	if (!isValidCC(card_number))
		errors.push("The credit/debit card number you have entered is not valid.");
	
	var date_now = new Date();
	var int_year = date_now.getFullYear();
	
	if (exp_month < 1 || exp_month > 12)
		errors.push("Please select an expiration month.");
	
	if (exp_year < int_year || exp_year > (int_year+10))
		errors.push("Please select an expiration year.");
	
//	if (!isValidEmail(email))
//		errors.push("The e-mail you have entered is not valid.");
	
	if (sec_code.length != 3 && sec_code.length != 4)
		errors.push("The security code you have entered is not valid.");

	
	if (errors.length > 0)
	{
		alert("Please correct the following error(s):\n\n"+errors.join("\n"));
		return false;
	}
	
	document.getElementById(click_button).style.display = "none";
	document.getElementById(loading_target).style.display = "block";
	
	form.submit();
}
//is string letters, numbers, spaces, periods and apostrophe's only
function isValidString(str)
{
	if (str.length == 0)
		return false;
	
	var reg = new RegExp("^[A-Za-z0-9\'\. ]+$");
	return reg.test(str);
	
}

function isValidCC (s) 
{
	 // remove non-numerics
	 w = getdigits(s);
	 
	 // validate number
	 j = w.length / 2;
	 if (j < 6.5 || j > 8 || j == 7) return false;
	 k = Math.floor(j);
	 m = Math.ceil(j) - k;
	 c = 0;
	 for (i=0; i<k; i++) 
	 {
		 a = w.charAt(i*2+m) * 2;
		 c += a > 9 ? Math.floor(a/10 + a%10) : a;
	 }
	 for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
	 return (c%10 == 0);
}
function isValidEmail(fData)
{
      var reg = new RegExp("^[0-9a-zA-Z._]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$");
      return reg.test(fData);
}

// This returns a string with everything but the digits removed.
function getdigits (s) 
{
   return s.replace (/[^\d]/g, "");
}
