// do when DOM is ready
$(document).ready(function() {
	
	//hide the error container
	$(".formErrorContainer").hide();
	
	//assign the validator
	var formErrorContainer = $('div.formErrorContainer');
	var validator = $("#subscriberInfoForm").validate({
		errorContainer: formErrorContainer,
		errorLabelContainer: $("ol", formErrorContainer),
		wrapper: 'li',
		meta: "validate",
		rules: {
				telepinasManilaPlan: {
					required:function(element) {
								return $("input[@name='product']:checked").val() == 'TelePinas Manila';
							 }

				},
				telepinasMobilePlan: {
					required:function(element) {
								return $("input[@name='product']:checked").val() == 'TelePinas Mobile';
							  }

				},
				telepinasDirectPlan: {
					required:function(element) {
								return $("input[@name='product']:checked").val() == 'TelePinas Direct';
							  }

				},
				number_abroad: {
					required:function(element) {
								return ($("input[@name='product']:checked").val() != 'TelePinas Direct') && ($("input[@name='product']:checked").val() != undefined);
							 }

				},
				contact_location: {
					required:function(element) {
								return $("input[@name='product']:checked").val() == 'TelePinas Mobile';
							 }

				},
				num_philippines_manila: {
					required:function(element) {
								return $("input[@name='product']:checked").val() == 'TelePinas Manila';
							 }

				},
				num_philippines_mobile: {
					required:function(element) {
								return $("input[@name='product']:checked").val() == 'TelePinas Mobile';
							 }

				},
				num_philippines_direct: {
					required:function(element) {
								return $("input[@name='product']:checked").val() == 'TelePinas Direct';
							 }

				},
				othersState: {
					required:function(element) {
							var state = $("select[@name='state']").val();
							//alert("state:"+state);
							if(state == "OTHERS"){
								return true;	
							}
							else{
								return false;
							}
						
					}
				}
			}
		}
	);
	
	
	var productType = $("input[@name='product']:checked").val();
	showCallFieldsByProductType(productType);
	
	$("input[@name='product']").click(
		function()
		{
			
			var productType = $("input[@name='product']:checked").val();
			//alert("payment_type:"+payment_type);
			showCallFieldsByProductType(productType);
			$("#subscriberInfoForm").validate().form();
			
		}	 								   
	
	);
	
	
	$("select[@name='state']").change(
		function()
		{
			
			var state = $("select[@name='state']").val();
			if(state == "OTHERS"){
				$("#othersState").show();	
			}
			else{
				$("#othersState").hide();	
			}
			
			//alert($("select[@name='state']")[0].selectedIndex);
			
			if(($("select[@name='state']")[0].selectedIndex  >= 1) && ($("select[@name='state']")[0].selectedIndex  <= 51)){
				
				$("select[@name='country']")[0].selectedIndex = 1; //united states
			}
			else if(($("select[@name='state']")[0].selectedIndex  >= 52) && ($("select[@name='state']")[0].selectedIndex  <= 64)){
				
				$("select[@name='country']")[0].selectedIndex = 2; //canada
			}
			else{
				$("select[@name='country']")[0].selectedIndex = 0; //others	
			}
			
			$("#subscriberInfoForm").validate().form();
		}	 								   
	
	);
	
	
	if($("select[@name='state']").val() != 'OTHERS'){
		$("#othersState").hide();
	}
	else{
		$("#othersState").show();
	}
	
});


function onClickSubscriberInfo(){

	 //alert("onClickSubscriberInfo");
	  
	  // bind form using 'ajaxForm' 
	 if($("#subscriberInfoForm").validate().form()){
		  //alert("valid!");
	 }
	else{
		 //alert("invalid!");	
	}
}


function showCallFieldsByProductType(pType){
	
	switch(pType){
		
		case "TelePinas Manila":
			$("#numberAbroadBox").show();
			$("#numberPhilippinesManilaBox").show();
			$("#numberPhilippinesMobileBox").hide();
			$("#numberPhilippinesDirectBox").hide();
			$("#subscriptionTypeBox").hide();
			$("#contactLocationBox").hide();
			$("#bayanihanCodeBox").show();
			$("#callFieldsSeparatorBox").show();
			$("#telepinasManilaPlanBox").show();
			$("#telepinasMobilePlanBox").hide();
			$("#telepinasDirectPlanBox").hide();
		break;
		
		case "TelePinas Mobile":
			$("#numberAbroadBox").show();
			$("#numberPhilippinesManilaBox").hide();
			$("#numberPhilippinesMobileBox").show();
			$("#numberPhilippinesDirectBox").hide();
			$("#subscriptionTypeBox").show();
			$("#contactLocationBox").show();
			$("#bayanihanCodeBox").show();
			$("#callFieldsSeparatorBox").show();
			$("#telepinasManilaPlanBox").hide();
			$("#telepinasMobilePlanBox").show();
			$("#telepinasDirectPlanBox").hide();
		break;
		
		case "TelePinas Direct":
			$("#numberAbroadBox").hide();
			$("#numberPhilippinesManilaBox").hide();
			$("#numberPhilippinesMobileBox").hide();
			$("#numberPhilippinesDirectBox").show();
			$("#subscriptionTypeBox").show();
			$("#contactLocationBox").show();
			$("#bayanihanCodeBox").show();
			$("#callFieldsSeparatorBox").show();
			$("#telepinasManilaPlanBox").hide();
			$("#telepinasMobilePlanBox").hide();
			$("#telepinasDirectPlanBox").show();
		break;
		
		
		default:
			$("#numberAbroadBox").hide();
			$("#numberPhilippinesManilaBox").hide();
			$("#numberPhilippinesMobileBox").hide();
			$("#numberPhilippinesDirectBox").hide();
			$("#subscriptionTypeBox").hide();
			$("#contactLocationBox").hide();
			$("#bayanihanCodeBox").hide();
			$("#callFieldsSeparatorBox").hide();
			$("#telepinasManilaPlanBox").hide();
			$("#telepinasMobilePlanBox").hide();
			$("#telepinasDirectPlanBox").hide();
		break;
	}
	
	
}


