var Qualification = {
	//BIND PAGE EVENTS
	'bindEvents': function(){
		
		$("input[@id='input_zip_code']").livequery('click', function(){
			 zip = $('#zipcode').val();
						$.getJSON(
							'/admin/stafforder/update-cart-zip/'+zip,
							function (data) {
								if( ! data.success)
									alert(data.message);
							}
						);
		});
		
		
		$("input[@id='button_have_installed']").livequery('click', function(){
			if($('#enter_zip').is(':visible') ){
				alert("You must enter your zip code in order to add this product to your cart");
				return false;
			}else{
					href = $(this).attr('name');
					result = $.ajax({url: href, data: "", async: false}).responseText;
					if (result == '{ success: 1 }')
							window.location = '/cart';
					else
							alert('Sorry, some error has occured');
			}
			
		});
		
		
		/////// validation///////
		$("form").submit(function() {
			
			res = true;
			
			if($(this).attr('id') == 'ac_form'){
			
				
				if($('#ac_select_1').val() == '0'){
					$('#ac_select_1').parent().attr('style', 'color:red;');
					res = false;
				}
				
				if($('#ac_select_2').val() == '0'){
					$('#ac_select_2').parent().attr('style', 'color:red;');
					res = false;
				}
				
				
				count = $('#ac_select_2').val();
				for(i=1;i<=count;i++){
					if($('#select_3_'+i).val() == '0'){
						$('#select_3_'+i).parent().next().attr('style', 'color:red;');
						res = false;
					}
				}
				
				//clear blocks for  Air Cleaners form
				if(res){
						count++;
						for(i=count;i<=4;i++)
							$('#system'+i).html('');
				}
				
			}else{
				
				if($(this).attr('id') == 'd_form' && ($('#d_many_systems_li').is(':hidden')))
					$("#d_many_systems_li option[value='0']").val('');
				
				
				if($('select[@value=0]').val() == '0'){
					$('select[@value=0]').parent().attr('style', 'color:red;');
					res = false;
				}
		}
		
			if(res)
				return true;
			else
				return false;
			
		});
		
		$('select').livequery('change',function() {
			
			if($(this).val() != '0')
					$(this).parent().attr('style', '');
			else
					$(this).parent().attr('style', 'color:red;');
			
			
			if($('#ac_form').length>0){
				$(this).parent().next().attr('style', '');
			}
		});
		
		
		
		
				
		
/////////////////////Air Cleaners//////////////////////////
		
		$('#ac_select_2').livequery('change', this.blockSelect);
		
		//click on IMAGES A,B,C and D
		$("img[@choise='a']").livequery('click', function(){
			level = Qualification.getLavel($(this));
			
			Qualification.clearStyleImages(level);
			Qualification.changeAcSelect(level,1);
			
			$(this).attr('style', "border: 1px solid red;");
			$('#ac_radio1_'+level).attr('checked', "true");
		});
		
		
		$("img[@choise='b']").livequery('click', function(){
			level = Qualification.getLavel($(this));
			
			Qualification.clearStyleImages(level);
			Qualification.changeAcSelect(level,1);
			
			$(this).attr('style', "border: 1px solid red;");
			$('#ac_radio2_'+level).attr('checked', "true");
		});
		
		
		$("img[@choise='c']").livequery('click', function(){
			level = Qualification.getLavel($(this));
			
			Qualification.clearStyleImages(level);
			Qualification.changeAcSelect(level,2);
			
			$(this).attr('style', "border: 1px solid red;");
			$('#ac_radio3_'+level).attr('checked', "true");
		});
		
		
		$("img[@choise='d']").livequery('click', function(){
			level = Qualification.getLavel($(this));
			
			Qualification.clearStyleImages(level);
			Qualification.changeAcSelect(level,3);
			
			$(this).attr('style', "border: 1px solid red;");
			$('#ac_radio4_'+level).attr('checked', "true");
		});
		
		
		//click on IMPUT A,B,C and D
		$("input[@choise='a']").livequery('click', function(){
			level = Qualification.getLavel($(this));
			
			Qualification.changeAcSelect(level,1);
			Qualification.clearStyleImages(level);
			
			$('#ac_image_a_'+level).attr('style', "border: 1px solid red;");
		});
		
		$("input[@choise='b']").livequery('click', function(){
			level = Qualification.getLavel($(this));
			
			Qualification.changeAcSelect(level,1);
			Qualification.clearStyleImages(level);
			
			$('#ac_image_b_'+level).attr('style', "border: 1px solid red;");
		});
		
		$("input[@choise='c']").livequery('click', function(){
			level = Qualification.getLavel($(this));
			
			Qualification.changeAcSelect(level,2);
			Qualification.clearStyleImages(level);
			
			$('#ac_image_c_'+level).attr('style', "border: 1px solid red;");
		});
		
		
		$("input[@choise='d']").livequery('click', function(){
			level = Qualification.getLavel($(this));
			
			Qualification.changeAcSelect(level,3);
			Qualification.clearStyleImages(level);
			
			$('#ac_image_d_'+level).attr('style', "border: 1px solid red;");
		});
		
	
		
/////////////////////Dehumidifer//////////////////////////
		$("#d_square_footage").livequery('change', this.changeQuestion);
		$("#d_age_home").livequery('change', this.changeQuestion);
		$("input[@name='d_location']").livequery('change', this.changeQuestion);
		
		$("input[@name='location']").livequery('change', function(){
			
			id = $(this).attr('id');
			
			if(id == 'location_a'){
				$("#question").load("a.htm");
			}
			if(id == 'location_b'){
				$("#question").load("b.htm");
			}
			if(id == 'location_c'){
				$("#question").load("c.htm");
			}
		});
		
	},
	
	

/////////////////////Air Cleaners/////////////////////////

	'blockSelect': function() {
		
		count = $('#ac_select_2').val();
		
		if(count>0){
			$('#ar_systems_li').show();
		}else{
			$('#ar_systems_li').hide();
		}
		
		for(i=1;i<=4;i++){
			$('#system'+i).hide();
		}
		
		for(i=1;i<=count;i++){
			$('#system'+i).show();
		}
	},
	
	
	'changeAcSelect': function(level,type) {

		if(type == '1')
			text = $("#system_select_div_1").html();
		
		if(type == '2')
			text = $("#system_select_div_2").html();
		
		if(type == '3')
			text = $("#system_select_div_3").html();
		
		$("#span_system_select_"+level).html(text);
		$('#span_system_select_'+level+' select#select_3').attr('name','system_select_'+level);
		$('#span_system_select_'+level+' select#select_3').attr('id','select_3_'+level);
		
	},
	
	
	'getLavel': function(obj) {
			id = obj.attr('id');
			len = id.length;
			level = id.substring(len-1,len);
		return level;
	},
	
	
	
	'clearStyleImages': function(level) {
		
		$('#ac_image_a_'+level).attr('style', "");
		$('#ac_image_b_'+level).attr('style', "");
		$('#ac_image_c_'+level).attr('style', "");
		$('#ac_image_d_'+level).attr('style', "");

	},
	
	
	//////////////////////////////////////////////////////////////////
	'changeQuestion': function() {
		
		location_d = $("input[@name='d_location']:checked").val();
		age_home   = $("#d_age_home").val();
		square_footage = $("#d_square_footage").val();
		
		$("#d_many_systems_li option[value='0']").val('');
		$("#d_many_systems_li").hide();
		
		if((location_d == 'a' || location_d == 'b') && age_home == 'Older than 25 years old' && (square_footage == '3,500 - 4,500 Square Feet' || square_footage == '4,500 - 5,000 Square Feet')){
			$("#d_many_systems_li option[value='']").val('0');
			$("#d_many_systems_li").show();
		}
		
		if(location_d == 'c'){
			
			if(age_home == 'Older than 25 years old' && (square_footage == '1,500 - 2,500 Square Feet' || square_footage == '2,500 - 3,500 Square Feet')){
				$("#d_many_systems_li option[value='']").val('0');
				$("#d_many_systems_li").show();
			}
			
			if(age_home != 'Less than 5 years old' && age_home != 'Older than 25 years old' && square_footage == '2,500 - 3,500 Square Feet'){
				$("#d_many_systems_li option[value='']").val('0');
				$("#d_many_systems_li").show();
			}
			
			if(square_footage == '3,500 - 4,500 Square Feet' || square_footage == '4,500 - 5,000 Square Feet'){
				$("#d_many_systems_li option[value='']").val('0');
				$("#d_many_systems_li").show();
			}
		}
	}
};

$(document).ready(function(){
	Qualification.bindEvents();
});
