function setBirthdateChooser(elementName) {
	var bd = document.getElementById(elementName + 'Chooser');
	bd.DateChooser = new DateChooser();
	bd.DateChooser.setUpdateField(elementName, 'm/d/Y');
	bd.DateChooser.setCloseTime(300);
	bd.DateChooser.setXOffset(-158);
	bd.DateChooser.setYOffset(22);
	bd.DateChooser.setIcon('/dotAsset/16034.gif', elementName);	
	return true;
}

function populateSelectBox(selectBox, jsonArray) {

	for (i=0; i < jsonArray.length; i++) {
		var opt = document.createElement("OPTION");
		if (jsonArray[i].name != null) {
			opt.text = jsonArray[i].name;
			opt.value = jsonArray[i].id;
		} else {
			opt.text = jsonArray[i].productName;
			opt.value = jsonArray[i].productId;
		}
		selectBox.options.add(opt);
	}
}

function showPlan(i){
	$('#plan_info').html(quote.quoteArray[i].array[1].costString + 
	" <strong>OR</strong> " + quote.quoteArray[i].array[0].costString);
}

function getBreeds(species){
	
	document.getElementById('BreedId').options.length = 0;
	mixedBreedId = '557';
	if (species == 2) {
		mixedBreedId = '323';
	}
	var opt = document.createElement("OPTION");
	opt.text = 'Mixed Breed';
	opt.value = mixedBreedId;
	document.getElementById('BreedId').options.add(opt);
	
	var data = {
		SpeciesId : species
	};

	$.ajax({
		url: '/PPIServices/GetBreedListForSpeciesServlet',
		type: "GET",
		data:  data,
		dataType: "json",
		cache: true,
		success: function(breeds){
			populateSelectBox(document.getElementById('BreedId'), breeds);
		}
	}); 
	
}


function getState(zipCode){

	var data = {
		ZipCode: zipCode
	};

	$.ajax({
		url: '/PPIServices/GetStateFromZipCodeServlet',
		type: "GET",
		data:  data,
		dataType: "json",
		cache: true,
		success: function(retVal){
			$('#State').val(retVal.state);
		}
	}); 
}

function submitQuote(marketChannel) {
	
	var species = 1;
	if ($('#cat').attr('checked')) {
		species = 2;
	}
	
	getQuote(marketChannel, species, $('#BreedId').val(), $('#Birthdate').val(),
	$('#ZipCode').val(), $('#State').val(), '');
}

function getQuote(marketChannel, species, breedId, birthdate, zipCode, state, email){
	
	$('#quote1').hide();
	$('#wait_page').show();
	
	var data = {
		MarketChannel: marketChannel, 
		SpeciesId: species, 
		BreedId: breedId, 
		ZipCode: zipCode, 
		State: state, 
		Birthdate: birthdate, 
		Email: email
	};

	$.ajax({
		url: '/PPIServices/GetQuoteServlet',
		type: "GET",
		data:  data,
		dataType: "json",
		cache: true,
		success: function(quoteReturn){
		
		quote = quoteReturn;
		
			$('#wait_page').hide();
			
			if (quote.error == null) {
				document.getElementById('plan_select').options.length = 0;
				populateSelectBox(document.getElementById('plan_select'), quote.quoteArray);
					
				$('#quote2').show();
				showPlan(0);
				$('#plan_select').focus();
				
			} else {
				$('#quote_error').html("We're having trouble retrieving plan quote for you." +
					"Try again, or call us at our toll-free number, 1-866-725-2747 for more help." +
					" <br /><br />  <span class='bold'>Error:</span> " + quote.error +
					" <br/> <input type='submit' class='yellow_button' value='Start Over' onclick='$(\"#quote_error\").hide(); $(\"#quote1\").show();' />");
				$('#quote_error').show();
			} 
			
		}
	}); 
	 
}

function sendQuoteEmail() {
				
	var species = 1;
	if ($('#cat').attr('checked')) {
		species = 2;
	}	
			
	saveQuote('AKC', $('#plan_select').val(), species, $('#BreedId').val(), $('#Birthdate').val(), $('#ZipCode').val(), 
	$('#State').val(), $('#quote_email').val(), $('requestFollowUp').attr('checked', true), IPAddress);
	
	$('#quote3').hide();
	$('#quote4').show();
		
}

function saveQuote(marketChannel, planId, species, breedId, birthdate, zipCode, state, email, requestFollowUp, ipAddress) {

	var data = {
		MarketChannel: marketChannel, 
		SpeciesId: species, 
		BreedId: breedId, 
		ZipCode: zipCode, 
		State: state, 
		Birthdate: birthdate, 
		Email: email,
		Plan: planId,
		RequestFollowUp: requestFollowUp,
		IPAddress: ipAddress
	};

	$.ajax({
		url: '/PPIServices/SaveQuickQuoteServlet',
		type: "POST",
		data:  data,
		dataType: "json",
		cache: true,
		success: function(quoteReturn){
			successMessage = quoteReturn;
		}
	}); 

}

function getPlanQuote(marketChannel, name) {
	retVal = "Unavailable for your pet";		

	for (i=0; i < quote.quoteArray.length; i++) {
		if (quote.quoteArray[i].productName == name) {
			retVal = quote.quoteArray[i].array[1].costString + "<br />" + quote.quoteArray[i].array[0].costString;
			retVal += '<br /><a class="blue_button" style=" margin: 3px 0px 0px 15px;" href="https://petpartnersinc.us/BHIACMS/Do?h=bhia.web.Signup&MarketChannel=' + 
			marketChannel + '&RefererURL=PPI">Purchase</a>';
		}
	}
	return retVal;
}

var quoteDown = false;
function toggleQuote(){
	if (quoteDown) {
		$('#quote_pushdown_wrapper').slideUp('normal');
		$('#quote_pushdown_button').css('background-position', 'top');
	} else {
		if ($('#BreedId').length < 3) {
			getBreeds(1);
		}
		$('#quote_pushdown_wrapper').slideDown('normal');
		$('#quote_pushdown_button').css('background-position', 'bottom');
	}
	quoteDown = !quoteDown;
}
