(function($) {
	$(function() {
		var delete_timer;
		function toggleZoneLayer(val){
			if(val == "0") {
				$("#priceAlert .zonewrapper").show();
			} else {
				$("#priceAlert .zonewrapper").hide();
			}		
		}
		function enableButton() {
			$("#priceAlert #setAlert").removeClass("disabled");			
		}
		
		function isFormValid(allzoneval, selectedCb){
		   //form validation using jquery
		   var priceObj = $("#price");
		   var price =priceObj.val();
		   if( price == "" || price == "0" || !IsNumeric(price) ){    				
				$("#price_error").removeClass("hideOnLoad").attr("style","margin-left: 10px;"); 
				return false;
		   }else{    
		   		
				$("#price_error").addClass("hideOnLoad").attr("style","margin-left: 10px;"); 				
		   }
		   
		   //checkbox validation using jquery 
		   if(allzoneval == "0" && selectedCb == ""){		   		
				$('#zone_error').removeClass("hideOnLoad").attr("style","");
				return false;
		   }else{
				$("#zone_error").addClass("hideOnLoad");
		   }
		   
		   return true;
		} 

		function IsNumeric(sText){
		   var ValidChars = "0123456789.$";
		   var IsNumber=true;
		   var Char;
		   var isDollar = 0;
		   var re = new RegExp(",","g");
		   sText = sText.replace(re,"");
		 
		   for (i=0; i<sText.length && IsNumber == true; i++){ 
		      Char = sText.charAt(i); 
			  if(Char == '$')
				isDollar += 1;
		      if (ValidChars.indexOf(Char) == -1 || isDollar >1){
		    	 IsNumber = false;
	          }

		   }

		   return IsNumber;
	    }
		
		function appendF(url){
			var param = '?f=1';
			if(url != ''){
				var f = queryString(url,'f');
				if(typeof(f) == 'undefined')
					param = '&f=1';		
				else
					param = '';
			}
			return param;
		}
		function queryString(url, ji) {
			hu = url.substring(1);
			gy = hu.split("&");
			for (i=0;i<gy.length;i++) {
				ft = gy[i].split("=");
				if (ft[0] == ji) {
					return ft[1];
				}
			}
		}
		
		// put bindings here;
		$("#priceAlert form input:radio").live('click', function(){
			toggleZoneLayer(this.value);
		});
		
		$("#priceAlert form input:radio").livequery(function(){
			toggleZoneLayer(this.checked?this.value:"");
		});
		
		$("#priceAlert #container").livequery(function(){
			if(typeof(pa_disableForm) != 'undefined' && pa_disableForm) {
				var el = $('<div></div>').appendTo('#priceAlert')
				.addClass('formdisabled').css({					
					width: $(this).width(),
					height: $(this).height(),
					top: $(this).position().top,
					left: $(this).position().left
				});	
				//bgiframe hides the drop down
				//el.bgiframe();
				$('#priceAlert form select').attr("disabled","disabled");				
				$('#container').attr('disabled','true');				
			}
		});
		
		$("#priceAlert #setAlert").live("click", function(){
			if($(this).hasClass("disabled")) {
				return false;
			}

			//take out the value for all zone
			var allzoneval = "1";
			if($("#priceAlert .zonewrapper").is(':visible')){
				allzoneval = "0";
			}
			
			var submitUrl = "&quantity="+$("#quantity").val()+"&price="+$("#price").val()+"&allZoneInd="+allzoneval;
			var selectedCb = "";
			var re = new RegExp("venueZoneId_","g");
			
			$('input:checkbox').each( function() {					
				if(this.checked){
					selectedCb += "&" + this.id + "=" + this.value; 
				}
			});

			
			if(!isFormValid(allzoneval, selectedCb)){
				
				return false;
			}
			
			submitUrl += selectedCb.replace(re,"Zone");

			$("#priceAlertDiv #priceAlert").html("<div id='priceAlert' style='height:46px;padding-top:15px;'><div id='progressSection' style='display:inline'>"+$("#priceAlert #progressSection").html()+"</div></div>");			
			
			var submit_timer = window.setTimeout(function () { 
			$.getJSON(priceAlertUrl + submitUrl + '&action=submit&format=json&jsoncallback=?', function(resp){
				$("#priceAlertDiv").html(resp.data);
				$("#priceAlert #progressSection").show();
					if (typeof(pa_isSubmitSuccessful) != "undefined" && pa_isSubmitSuccessful) {
						if (pa_isFromMyAccount && typeof(pa_paramMap) != "undefined" && pa_paramMap) {
							var url = 'https://' + window.location.hostname + '/myaccount/pricealert';
							url += '/' + pa_paramMap.pageNum + '/' + pa_paramMap.eventId + '/edit/1';
							location.href = url;
						}
						else {
							location.href = location.href + appendF(window.location.search);
						}
					}
					else {
						$("#priceAlert #progressSection").hide();
					}				
			});
			},1500);
		});

		$("#deletePriceAlert #btnYes").live("click", function(){
			$("#deletePriceAlert #progressSection").show();
			$("#deletePriceAlert #deleteSection").hide();			
			
            var delete_timer = window.setTimeout(function () { 
    			$.getJSON(deleteAlertUrl + '&action=submit&format=json&jsoncallback=?', function(resp){
					$("#removeAlertDiv").html(resp.data);
					$("#deletePriceAlert #progressSection").show();
					if(pa_isFromMyAccount && typeof(pa_paramMap) != "undefined" && pa_paramMap) {
						var url = 'https://' + window.location.hostname + '/myaccount/pricealert';
						url += '/' + pa_paramMap.pageNum + '/' + pa_paramMap.eventId + '/delete/1';
						location.href = url ;
					}else{
						location.href = location.href + appendF(window.location.search);
					}
    				$("#deletePriceAlert #btnNo").trigger("click");
    			});
            	
            }, 1500);
			//return false;
		});		
	});	
})(jQuery);

