$().ready(function() {
	
	var servlet = "";
	if($('#contextPath').html().indexOf('/secure/') != -1){
		servlet = servlet+"../";
	}
	
	$("#locationSelect").val($("#CURRENT_LOCATION").val());
	
	$.ajax({
		   type: "POST",
		   url: servlet+"servlet/MenuItemServlet",
		   data: "operation=LOADTODAYSSPECIALS",
		   dataType: 'xml',
		   timeout: 1000,
		   success: function(xml){
				$(xml).find('Menu').each(function(){
					var cc = 1;
					$(this).find('TodaysSpecials').each(function(){
						$('#special'+cc+'Name').html($(this).find('ItemName').text());
						$('#special'+cc+'Price').html('$'+$(this).find('ItemNetPrice').text());
						cc++;
			        });
			    });
		   }
	});
	
	$('#locChangeButton').click(function (e){
		$.ajax({
			   type: "POST",
			   url: servlet+"servlet/SessionOrderCartServlet",
			   data: "operation=DEFAULTLOCATION&location="+$('#locationSelect').val(),
			   dataType: 'html',
			   timeout: 1000,
			   success: function(html){
					location.reload();
			   }
		});
	});
	
	$('#RESET').click(function(){
		$('#fnameID').val("");
		$('#lnameID').val("");
		$('#emailID').val("");
		$('#cityID').val("");
	});

	$('#SUBSCRIBE').click(function(){
		if($('#fnameID').val() == ""){
			alert('Please Enter Your First Name');
			$('#fnameID').focus();
			return false;
		}
		if($('#lnameID').val() == ""){
			alert('Please Enter Your Last Name');
			$('#lnameID').focus();
			return false;
		}
		if($('#emailID').val() == ""){
			alert('Please Enter Your Email Address');
			$('#emailID').focus();
			return false;
		}else if (validateEmail($("#emailID").val()) != ""){
			alert(validateEmail($("#emailID").val()));
			$("#emailID").focus();
			return false;
		}
		
		if($('#cityID').val() == ""){
			alert('Please Enter Your City');
			return false;
		}

		var params = "operation=CREATE_SUBSCRIPTION&fname="+$('#fnameID').val()+"&lname="+$('#lnameID').val()+"&email="+$('#emailID').val()+"&city="+$('#cityID').val();
		$.ajax({
			   type: "POST",
			   url: "servlet/CommonUtilServlet",
			   data: params,
			   dataType: 'xml',
			   timeout: 3000,
			   success: function(xml){
					if("SUCCESS" == $(xml).find("Status").text()){
						$('div.success-message').html('Thank you for subscribing');
						$('div.success-message').show();
						setTimeout(function(){$('div.success-message').fadeOut('slow');location.href = 'index.jsp'; }, 3000);
					}else{
						$('div.error-message').html('Some Technical Error has occured, please try again.');
						$('div.error-message').show();
						setTimeout(function(){$('div.error-message').fadeOut('slow'); }, 3000);
					}
			   }
		});
	});
	
	
	function validateEmail(str) {

		var validation = "";

		var at = "@";
		var dot = ".";
		var lat = str.indexOf(at);
		var lstr = str.length;
		var ldot = str.indexOf(dot);
		if (str.indexOf(at) == -1) {
			validation = ("Invalid E-mail ID");
		}

		if (str.indexOf(at) == -1 || str.indexOf(at) == 0
				|| str.indexOf(at) == lstr) {
			validation = ("Invalid E-mail ID");
		}

		if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0
				|| str.indexOf(dot) == lstr) {
			validation = ("Invalid E-mail ID");
		}

		if (str.indexOf(at, (lat + 1)) != -1) {
			validation = ("Invalid E-mail ID");
		}

		if (str.substring(lat - 1, lat) == dot
				|| str.substring(lat + 1, lat + 2) == dot) {
			validation = ("Invalid E-mail ID");
		}

		if (str.indexOf(dot, (lat + 2)) == -1) {
			validation = ("Invalid E-mail ID");
		}

		if (str.indexOf(" ") != -1) {
			validation = ("Invalid E-mail ID");
		}

		return validation;
	}
	
});
