// JavaScript Document
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  return false;
}

jQuery(document).ready(function(){
	jQuery('#MultiForm_Calendar').hide();
	jQuery('#LocationID').hide();
	jQuery('#Location').hide();
	jQuery('#Activity').hide();
	jQuery('#errormsg').hide();
	jQuery('#outer-calendar').hide();
	jQuery('#times').hide();
	jQuery('#RVMultiForm_RVMultiForm_action_next').attr("disabled", true); 
	jQuery('#times ul a')
	.livequery('click', function() {  
		jQuery(this)
			jQuery('#RVMultiForm_RVMultiForm_action_next').attr("disabled", false);
			jQuery('#RVMultiForm_RVMultiForm_ScheduleID').val(this.id);
			jQuery('.tick').removeClass('tick');
			jQuery(this).addClass('tick');
			jQuery('.action').attr('src', '/themes/blackcandy/images/continue.jpg');
			jQuery('.action').attr('disabled', false);
		return false;
	});
	
	jQuery('#extrascart ul li a')
	.livequery('click', function() {  
		jQuery(this)
			var itemVal = jQuery(this).attr('id');
			jQuery.ajax({ 
				method: "get",url: "./redeem-voucher/redeem-voucher/" + itemVal,
				cache: false,
				success: function(html){ //so, if data is retrieved, store it in html
				jQuery('#extrascart').html(html);
			} 
		 }); //close jQuery.ajax(
		return false;
	});
	
	jQuery('#VoucherCheck').click(function () {
		var issuer = jQuery('#RVMultiForm_RVMultiForm_Issuer option:selected').val();
		var vouchercode = jQuery('#RVMultiForm_RVMultiForm_VoucherCode').val();
		if(jQuery('#RVMultiForm_RVMultiForm_VoucherCode').val() == "") {
			alert('You must enter a voucher code');
			return false;
		}
		myRegxp = /^[a-z0-9]+$/i;
		if(!myRegxp.test(vouchercode)) {
			var newstr = vouchercode.replace(/[^a-zA-Z0-9]+/g, '')
			jQuery('#RVMultiForm_RVMultiForm_VoucherCode').val(newstr);
			vouchercode = newstr;
		}
		
		jQuery.ajax({
			method: "get", url: "./mysite/code/ajax/checkVoucher.php",data: "issuer=" + issuer + "&code=" + vouchercode,
			beforeSend: function() { },
			cache: false,
			success: function(html) {
				if(html) {
					//do stuff
					switch(html) {
						case 'expired':
							jQuery('#errormsg').show();
							jQuery('#Location').hide();
							jQuery('#Activity').hide();
							jQuery('#errormsg').html('Voucher has expired');
						break;
						case 'redeemed':
							jQuery('#errormsg').show();
							jQuery('#Location').hide();
							jQuery('#Activity').hide();
							jQuery('#errormsg').html('Voucher has already been redeemed');
						break;
						case 'invalid':
							jQuery('#errormsg').show();
							jQuery('#Location').hide();
							jQuery('#Activity').hide();
							jQuery('#errormsg').html('Vouchercode is not valid');
						break;
						case 'valid':
							jQuery('#errormsg').show();
							jQuery('#Location').show();
							jQuery('#Activity').show();
							jQuery('#outer-calendar').show();
							jQuery('#IssuerVoucherCodeVoucherSubmit').hide();
							jQuery('#errormsg').html('Vouchercode has been accepted');
						break;
						default:
							strSplit = new Array();
							if(strSplit = html.split('|')) {
								for(i = 1; i <= (jQuery("#RVMultiForm_RVMultiForm_Activity option").length+1); i++) {
									if(i != strSplit[1]) {
										jQuery("#RVMultiForm_RVMultiForm_Activity").removeOption("" + i + "");		
									}
								}
							}
							jQuery('#IssuerVoucherCodeVoucherSubmit').hide();
							jQuery('#errormsg').show();
							jQuery('#errormsg').html('Vouchercode has been accepted');
							jQuery('#Location').show();
							jQuery('#outer-calendar').show();
							jQuery('#Activity').show();
					}
					
				} else {
					// do nothing
					return false;
				}
			}
		});
	});
						   
	jQuery('#RVMultiForm_RVMultiForm_Activity').change(function () {
		var activity = jQuery('#RVMultiForm_RVMultiForm_Activity option:selected').val();
		if(jQuery('#RVMultiForm_RVMultiForm_LocationID option:selected').val() == undefined) {
			var location = jQuery('#RVMultiForm_RVMultiForm_LocationID').val();
		} else {
			var location = jQuery('#RVMultiForm_RVMultiForm_LocationID option:selected').val();
		}
		var month = getQueryVariable("month");
		
		if(month) {
			month = "&month=" + month;
		} else {
			month = "";
		}
		
		jQuery.ajax({ 
			method: "get",url: "./mysite/code/ajax/getCalendar.php",data: 'loc=' + location + '&act=' + activity + month, 
			cache: false,
			beforeSend: function() {
				jQuery('#MultiForm_Calendar').addClass('loading').html("");
			},	
			success: function(html){ //so, if data is retrieved, store it in html
				jQuery('#MultiForm_Calendar').removeClass('loading');
				jQuery('#MultiForm_Calendar').html(html);
				jQuery('#MultiForm_Calendar').show("slow");
				jQuery('#times').hide();
			} 
	 	}); //close jQuery.ajax(
		jQuery('#MultiForm_Calendar').show();
		jQuery('#RVMultiForm_RVMultiForm_ActivityID').val(activity);
		/**/
	});
	
	
	
	
	jQuery('#MultiForm_Calendar #previousmonth_link')
	.livequery('click', function() {  
		var month = jQuery('#previousmonth_link').attr('title');
		var location = jQuery('#RVMultiForm_RVMultiForm_LocationID').val();
		var activity = jQuery('#RVMultiForm_RVMultiForm_ActivityID').val();
		jQuery.ajax({ 
			method: "get",url: "mysite/code/ajax/getCalendar.php",data: 'month=' + month + "&loc=" + location + "&act=" + activity, 
			cache: false,
			beforeSend: function() {
				jQuery('#MultiForm_Calendar').addClass('loading').html("");
			},
			success: function(html){ //so, if data is retrieved, store it in html
				jQuery('#MultiForm_Calendar').removeClass('loading');
				jQuery('#MultiForm_Calendar').html(html);
			} 
	 	}); //close jQuery.ajax(
		return false;
	});
	
	jQuery('#MultiForm_Calendar #nextmonth_link')
	.livequery('click', function() {  
		var month = jQuery('#nextmonth_link').attr('title');
		var location = jQuery('#RVMultiForm_RVMultiForm_LocationID').val();
		var activity = jQuery('#RVMultiForm_RVMultiForm_ActivityID').val();
		jQuery.ajax({ 
			method: "get",url: "mysite/code/ajax/getCalendar.php",data: 'month=' + month + "&loc=" + location + "&act=" + activity, 
			cache: false,
			beforeSend: function() {
				jQuery('#MultiForm_Calendar').addClass('loading').html("");
			},
			success: function(html){ //so, if data is retrieved, store it in html
				jQuery('#MultiForm_Calendar').removeClass('loading');
				jQuery('#MultiForm_Calendar').html(html);
			} 
	 	}); //close jQuery.ajax(
		return false;
	});

});