// JavaScript Document
$(document).ready(function(){ 
	loc=window.location.href.split("#")[0];
	//hrefSections = window.location.href.split("/"); 
	//var fileName = hrefSections[hrefSections.length - 2]; 
	//alert(fileName);
	$("#docsbox1").click(function() {window.location='/get-a-document-storage-quote.asp';});
	$("#docsbox2").click(function() {window.location='/archive-document-storage-new.asp';});
	$("#docsbox3").click(function() {window.location='/archive-document-retrieval.asp';});
	$("#docsbox4").click(function() {window.location='/archive-document-destruction.asp';});
	$("#box1").click(function() {window.location='/get-a-personal-self-storage-quote.asp';});
	$("#box2").click(function() {window.location='/size-estimator.asp';});
	$("#box3").click(function() {window.location='/TheStudentCase.asp';});
	$("#box4").click(function() {window.location='/self-storage-packing-materials.asp';});
	$("#sb1").click(function() {window.location='/online-store.asp';});
	$("#sb2").click(function() {window.location='/self-storage-packing-materials-instore.asp';});
	$("#st1").click(function() {window.location='/TheStudentCase.asp';});
	$("#st2").click(function() {window.location='/storage-features-and-benefits.asp';});
	if ($("#accordion").length > 0 ) {
		$("#accordion").accordion({active: false, autoHeight: false, collapsible: true});
		if(loc.match("1")){$("#accordion").accordion('activate' , 0);}
		if(loc.match("2")){$("#accordion").accordion('activate' , 1);}
		if(loc.match("3")){$("#accordion").accordion('activate' , 2);}
		if(loc.match("4")){$("#accordion").accordion('activate' , 3);}
	
	$("#accordion a.head").click(function() {
		destination = $("#acchead").offset().top-20;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
	});
	}
	if ($("#streetvan").length > 0 ) {
		$("#streetvan").accordion({active: false, autoHeight: false, collapsible: true});
	$("#streetvan a.head").click(function() {
		destination = $("#acchead").offset().top-20;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
	});
	}
	$("ul#navbar").superfish(); 
	$("a").each(function() {
		if(this.href == window.location.href.split("#")[0]) {
			if ($(this).parent().parent().parent().parent().get(0).id=="navbar") {
				$(this).parent().parent().parent().addClass("selected");
			} else {
				$(this).parent().addClass("selected");
			}
		}
	});
	$(".questions a").click(function() {
		showid=$(this).attr("rel");
		$('.questions a').css("color","#000");
		$(this).css("color","#006F51");
		$("#answers > *").hide();
		$("#a" + showid).fadeIn(300);
	});
	if ($("#answers").length > 0 ) {
		$("#answers > *").hide();
		$("#a1").fadeIn(300);
		$("a[rel='1']").css("color","#006F51");
	}
	if ($(".examples").length > 0 ) {
		illcurrent="1";
		$('.examples a').css("color","#006F51");
		$(".examples a[rel='1']").css("color","#0079C1");
		$("#illustrations > *").hide();
		$("#ill"+illcurrent).show("slide", {}, 1000);
		$(".examples a").click(function() {
			destination = $("#content").offset().top-5;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );

			$('.examples a').css("color","#006F51");
			$(this).css("color","#0079C1");
			illid=$(this).attr("rel");
			if (illid!=illcurrent){
			$("#ill"+illcurrent).hide("slide", { direction: "right" }, 1000, function() {
				illcurrent=illid;
				$("#ill"+illcurrent).show("slide", { direction: "left" }, 1000);
			});
			}
		});

	}
}); 
function checkphone() {
	if ($("#con_tel").val()==$("#con_mobile").val()) {
		alert('Phone numbers must be different'); 
	}
}
function valpc(tf){
	if(!checkPostCode(tf.postcode.value)) {
		alert('Please enter a valid postcode');
		return false;
	}
	tf.postcode.value=checkPostCode(tf.postcode.value)
	return true;
	
}
function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  
  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  //alert(postCode);
  if (valid) {return postCode;} else return false;
}

function outyear() {
	var time=new Date();
	var year=time.getYear();
	if (year < 2000) {	year = year + 1900; }
	document.write(year);
}

function monwedonly(date) {
		var day = date.getDay();
		return [(day == 1 || day == 2 || day == 3 || day == 4 || day == 5), ''];
}

function setpcs() {
	//if ($("#pickuppc1").attr("selectedIndex")==0) { $("#pickuppc1").attr("selectedIndex",$("#dropoffpc1").attr("selectedIndex"))}
	//if ($("#redelpc1").attr("selectedIndex")==0) { $("#redelpc1").attr("selectedIndex",$("#dropoffpc1").attr("selectedIndex"))}
	if ($("#redelpc1").val()=="") { $("#redelpc1").val($("#dropoffpc1").val())}
	
	//if ($("#pickuppc2").val()=="") { $("#pickuppc2").val($("#dropoffpc2").val()) }
	if ($("#redelpc2").val()=="") { $("#redelpc2").val($("#dropoffpc2").val()) }
	$("#ppc1").html($("#dropoffpc1").val());
	$("#ppc2").html($("#dropoffpc2").val());
}
function submitstep1() {
	fail="";
	$("#dropoffpc2").val($("#dropoffpc2").val().toUpperCase());
	//$("#pickuppc2").val($("#pickuppc2").val().toUpperCase());
	$("#redelpc2").val($("#redelpc2").val().toUpperCase());

	if (
		$("#dropoffdate").val()=="" ||
		$("#pickupdate").val()=="" ||
		$("#redeldate").val()=="" ||
		$("#dropoffpc2").val()=="" ||
		//$("#pickuppc2").val()=="" ||
		$("#redelpc2").val()=="" ||
		$("#dropoffpc1").val()=="" ||
		//$("#pickuppc1").attr("selectedIndex")==0 ||
		$("#redelpc1").val()==""
		) {
		fail="incomplete";
	}
	if (fail=="incomplete") {
		alert('Please complete all the fields before continuing');
	}
	if (fail=="") {
		$quantity=$("#quantity").val();
		$roomservice=$('input[name=Roomservice]:checked').val();

		$("#f_quantity").html($quantity);
		if ($("#quantity").val()>1){$(".plural").html("s");}
		$("#f_dropoffdate").html($("#dropoffdate").val());
		$("#f_dropoffpc").html($("#dropoffpc1").val()+" "+$("#dropoffpc2").val());
		$("#f_pickupdate").html($("#pickupdate").val());
		$("#f_pickuppc").html($("#dropoffpc1").val()+" "+$("#dropoffpc2").val());
		$("#f_redeldate").html($("#redeldate").val());
		$("#f_redelpc").html($("#redelpc1").val()+" "+$("#redelpc2").val());
		 $("#form_dropoffpc").html($("#dropoffpc1").val()+" "+$("#dropoffpc2").val());
		 $("#form_redelpc").html($("#redelpc1").val()+" "+$("#redelpc2").val());
		
		$dropoffdate=$("#dropoffdate").datepicker('getDate');
		$startdate=$("#pickupdate").datepicker('getDate');
		$enddate=$("#redeldate").datepicker('getDate');
		
		var d = new Date();
		var curr_year = d.getFullYear();
		$startincluded=new Date(curr_year+' Jun 1');
		$endincluded=new Date(curr_year+' Aug 31');
		var one_day=1000*60*60*24;
		$totaldays=Math.round(($enddate-$startdate)/one_day);
		$totalprice=0;
		$("#calc").html("");
		//Add extra days before included time
		if ($startdate<$startincluded) { 
			$extrastart=($startincluded-$startdate)/one_day;
			$("#calc").append("<div class='qu_date'>"+$("#pickupdate").val()+" &#8211; 31-05-2010</div>");
			$("#calc").append("<div class='qu_days'>"+$extrastart+" day(s)</div>");
			$("#calc").append("<div class='qu_price'>&pound;"+$extrastart*($quantity*2)+" (&pound;2 per case per day)</div>");
			$("#calc").append("<br clear='all' />");
			$totalprice=$totalprice+($extrastart*($quantity*2));
		}

		$offerprice=99;
		if ($quantity>1) { $offerprice=$offerprice+(59*($quantity-1)); }
		if ($enddate<$endincluded) { 
			$("#calc").append("<div class='qu_date'>01-06-2011 &#8211; "+$("#redeldate").val()+"</div>");
		} else {
			$("#calc").append("<div class='qu_date'>01-06-2011 &#8211; 31-08-2011</div>");
		}
		$("#calc").append("<div class='qu_days'>Summer Offer</div>");
		if ($quantity==1) {
		$("#calc").append("<div class='qu_price'>&pound;"+$offerprice+"</div>");
		} else {
		$("#calc").append("<div class='qu_price'>&pound;99 for the first case plus <br />&pound;59 for each subsequent case</div>");
		}
			$("#calc").append("<br clear='all' />");
			$totalprice=$totalprice+($offerprice);

		//Add extra days after included time
		if ($enddate>$endincluded) { 
			$extraend=Math.round(($enddate-$endincluded)/one_day);
			$("#calc").append("<div class='qu_date'>01-09-2010 - "+$("#redeldate").val()+"</div>");
			$("#calc").append("<div class='qu_days'>"+$extraend+" day(s)</div>");
			$("#calc").append("<div class='qu_price'>&pound;"+$extraend*($quantity*2)+" (&pound;2 per case per day)</div>");
			$("#calc").append("<br clear='all' />");
			$totalprice=$totalprice+($extraend*($quantity*2));
		}
		
		//Add surcharge for collection on different day to delivery
		if ($dropoffdate.getDate()!=$startdate.getDate()) { 
			$("#calc").append("<div class='qu_date'>Box collection surcharge</div>");
			$("#calc").append("<div class='qu_days'>&nbsp;</div>");
			$("#calc").append("<div class='qu_price'>&pound;25</div>");
			$("#calc").append("<br clear='all' />");
			$totalprice=$totalprice+25;
		}
		
		//Add total
		$extraend=Math.round(($enddate-$endincluded)/one_day);
		$("#calc").append("<div class='qu_date'><strong>TOTAL</strong></div>");
		$("#calc").append("<div class='qu_days'><strong>"+$totaldays+" days</strong></div>");
		$("#calc").append("<div class='qu_price'><strong>&pound;"+$totalprice+".00</strong></div>");
		$("#calc").append("<br clear='all' />");
		$("#hid_price").val($totalprice);

		$("#tabs").tabs("enable",1);
		$("#tabs").tabs("select",1);
		destination = $("#caseform").offset().top-20;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );

	}
}

function gotab(ind) {
		$("#tabs").tabs("enable",ind);
		$("#tabs").tabs("select",ind);
		destination = $("#caseform").offset().top-20;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
}

function checkEmail(stremail) {
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   return emailPattern.test(stremail);
}
function valcontact() {
	fail="";
	if ( $("#con_title").attr("selectedIndex")==0 ||
		 $("#con_firstname").val()=="" ||  
		 $("#con_lastname").val()=="" ||  
		 $("#con_tel").val()=="" ||  
		 $("#con_mobile").val()=="" ||  
		 $("#con_email").val()=="" ||  
		 $("#uni_name").val()=="" ||  
		 $("#uni_halls").val()=="" ||  
		 $("#uni_room").val()=="" ||  
		 $("#uni_address").val()=="" ||  
		 $("#redel_uni_address").val()=="" ||  
		 $("#uni_course").val()=="" ||  
		 $("#alt_title").attr("selectedIndex")==0 ||
		 $("#alt_firstname").val()=="" ||  
		 $("#alt_lastname").val()=="" ||  
		 $("#alt_tel").val()=="" ||  
		 $("#alt_mobile").val()=="" ||  
		 $("#alt_email").val()=="") 
		{ fail="yes"; 
	}

	//if (echeck($("#con_email").val)) { fail="email"; }
	if (fail=="yes") {
		alert("Please complete all the form fields");
	} else if (!checkEmail($("#con_email").val())) {
		alert("Please enter a valid email address");
	} else if ($("#con_tel").val()==$("#con_mobile").val()) {
		alert('Phone numbers must be different');
	} else {
		 $("#sum_price").html("&pound;"+$("#hid_price").val());
		 $("#sum_dropoffdate").html($("#dropoffdate").val());
		 $("#sum_dropoffpc").html($("#dropoffpc1").val()+" "+$("#dropoffpc2").val());
		 
		 $("#sum_pickupdate").html($("#pickupdate").val());
		 $("#sum_pickuppc").html($("#dropoffpc1").val()+" "+$("#dropoffpc2").val());
		 //$("#sum_pickuppc").html($("#pickuppc1").val()+" "+$("#pickuppc2").val());
		 $("#sum_redeldate").html($("#redeldate").val());
		 $("#sum_redelpc").html($("#redelpc1").val()+" "+$("#redelpc2").val());
		 
		 $("#sum_quantity").html($("#quantity").val());
		 //$("#sum_Roomservice").html($('input[name=Roomservice]:checked').val());

		 $("#sum_con_title").html($("#con_title").val());
		 $("#sum_con_firstname").html($("#con_firstname").val());
		 $("#sum_con_lastname").html($("#con_lastname").val());
		 $("#sum_con_tel").html($("#con_tel").val());
		 $("#sum_con_mobile").html($("#con_mobile").val());
		 $("#sum_con_email").html($("#con_email").val());

		 $("#sum_uni_name").html($("#uni_name").val());
		 $("#sum_uni_year").html($("#uni_year").val());
		 $("#sum_uni_halls").html($("#uni_halls").val());
		 $("#sum_uni_room").html($("#uni_room").val());
		 $("#sum_uni_address").html($("#uni_address").val());
		 $("#sum_uni_course").html($("#uni_course").val());
		 $("#sum_redel_address").html($("#redel_uni_address").val());
		 
		 $("#sum_alt_title").html($("#alt_title").val());
		 $("#sum_alt_firstname").html($("#alt_firstname").val());
		 $("#sum_alt_lastname").html($("#alt_lastname").val());
		 $("#sum_alt_tel").html($("#alt_tel").val());
		 $("#sum_alt_mobile").html($("#alt_mobile").val());
		 $("#sum_alt_email").html($("#alt_email").val());

		$("#tabs").tabs("enable",3);
		$("#tabs").tabs("select",3);
		destination = $("#caseform").offset().top-20;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
	}
	
} 

function confirmcase() {
	if($("#terms:checked").val()!="yes") {
		alert('You must accept the terms and conditions to place your order');
	} else {
		$("#caseform").submit();
	}
}


