// Main Javascript for quote_step1.wcs

var strPetType = "";
var strBreedType = "";

function showBreeds(ctl) {
	var objBreedtype = document.forms[0].elements.breed_type;
	var objBreed = document.getElementById("breed");
	objBreedtype.disabled = false;
	objBreed.disabled = false;
	if (ctl.value.toLowerCase() == "cat" || ctl.value.toLowerCase() == "dog")
	{
		for(var i=0;i<objBreedtype.length;i++)
		{
			objBreedtype[i].disabled = false;
		}
		objBreed.disabled = false;
	}
	else
	{
		for(var i=0;i<objBreedtype.length;i++)
		{
			objBreedtype[i].disabled = true;
		}
		objBreed.disabled = true;
	}
}

function showBreedsOnType() {

	var animaltypes = document.forms[0].elements.animal_type;
	var objBreedtype = document.forms[0].elements.breed_type;
	var objBreed = document.getElementById("breed");
	for(i=0;i<document.forms[0].elements.animal_type.length;i++)
	{
//		if (document.forms[0].elements.animal_type[i].checked && document.forms[0].elements.animal_type[i].value.toLowerCase() == "cat")
//		{
//			if(objBreedtype[0].checked) {
//				objBreed.className = "dropdownDisabled";
//				objBreed.disabled = true;
//			} else {
//				objBreed.disabled = false;
//				objBreed.className = "dropdown";
//			}
//		}
	}

}



function getBreeds(ctl) {
	if(ctl.value.toLowerCase() == "cat" || ctl.value.toLowerCase() == "dog") {
		showBreeds(ctl);
	}
	var requestUrl = "/getaquote/getBreed.asp?"

	if (ctl.name == "animal_type") {
		strPetType = ctl.value.toLowerCase();
	}
	else if (ctl.name == "breed_type") {
		strBreedType = ctl.value.toLowerCase();
	}

	//if (strPetType == "cat") {
		requestUrl = requestUrl + "PetType=" + strPetType + "&BreedType=all";
	//}
	//else 	if (strPetType != "" && strBreedType != "") {
	//	requestUrl = requestUrl + "PetType=" + strPetType + "&BreedType=" + strBreedType;
	//}

	sendRequest(requestUrl,"GET",HandleResponse,null);
}

function HandleResponse()
{
	if(xmlHttp.readyState == 4)
	{
		if(xmlHttp.status == 200)
		{
			populateBreeds(xmlHttp.responseText);
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

function populateBreeds(breedsNode) {
	var breedList = document.getElementById("breed");
	for (var count = breedList.options.length-1; count >-1; count--) {
		breedList.options[count] = null;
	}
	var breedNodes = breedsNode.split("|");
	var list;
	var optionItem;

	//if ( document.forms[0].elements.animal_type[1].checked && document.forms[0].elements.breed_type[0].checked ) {
	if ( document.forms[0].elements.breed_type[0].checked ) {
		optionItem = new Option("--Choose Dominant Breed","--Choose Dominant Breed", false, false);
		breedList.options[0] = optionItem;
	} else {
		optionItem = new Option("--Choose Breed","--Choose Breed", false, false);
		breedList.options[0] = optionItem;
	}

	for (var count = 0; count < breedNodes.length-1; count++) {
		list = breedNodes[count].split(" ");

		outTxt = "";
		for(var x=1;x<list.length;x++)
			outTxt += list[x]+((x==list.length)?"":" ");

		if (list[0] == sBreed) {
			optionItem = new Option( outTxt, list[0],  false, true);
		} else {
			optionItem = new Option( outTxt, list[0],  false, false);
		}
		if (list[0] == "--") {
			optionItem.disabled = true ;
		}

		breedList.options[breedList.length] = optionItem;

	}
}

function checkForm( msgType ) {

	if (msgType != "alert") {
		msgType = "text";
	}

	var errorInfo = new Array("<li>Pet's Name is required.</li>",
							"<li>Type of Pet is required.</li>",
							"<li>Date of Birth is required.</li>",
							"<li>We are unable to quote on Cats under 6 weeks old.</li>",
							"<li>We are unable to quote on Dogs under 8 weeks old.</li>",
						"<li>Sorry, we cannot provide cover for cats above 9 years and 11 months.</li>",
						"<li>Sorry, we cannot provide cover for dogs above 7 years and 11 months. </li>",
						"<li>Sorry, we cannot provide cover for dogs of a specified breed above 5 years and 11 months.</li>",
							"<li>Breed Type is required.</li>",
							"<li>Breed is required.</li>",
							"<li>Postcode is required.</li>",
							"<li>Full postcode is required.</li>",
							"<li>Email Address is required.</li>",
							"<li>Email Address format is invalid.</li>") ;

	var invalidIndex = [false,false,false,false,false,false,false,false,false,false,false,false,false,false];
	var isValid = false, invalidType = 1;   //-1:reqiured; 0:invalid format; 1:valid;

//	var txtArray = new Array("aname","postcode","email");
//	var txtIndex = new Array(0,10,12);

	var txtArray = new Array("aname","postcode");
	var txtIndex = new Array(0,10);

	for (var i=0;i<txtArray.length ;i++ ) {
		isValid = requireTextbox(txtArray[i]);
		if (!isValid) {
			invalidIndex[txtIndex[i]] = true;
			invalidType = -1;
		}
	}

	var animal_type = document.forms[0].elements.animal_type;
	isValid = requireRadioGroup(animal_type);
	if (!isValid) {
		invalidIndex[1] = true;
		invalidType = -1;
	}

	var animalSelectedIndex;
	for (var i = 0;i<animal_type.length;i++) {
		if (animal_type [i].checked) {
			animalSelectedIndex	= i;
			break;
		}
	}

	// breed type if dog/cat selected
	if (animalSelectedIndex == 0 || animalSelectedIndex == 1) {

		var breed_type = document.forms[0].elements.breed_type;
		isValid = requireRadioGroup(breed_type);
		if (!isValid) {
			invalidIndex[8] = true;
			invalidType = -1;
		}


		// breed if breed type selected
		//if (!(animalSelectedIndex == 0 && document.form1.breed_type[0].checked)) {
			isValid = checkDropdown("breed");
			if (!isValid) {
				invalidIndex[9] = true;
				invalidType = -1;
			} else if (document.forms[0].elements.breed.value == "--") {
				invalidIndex[9] = true;
				invalidType = -1;
			}
		//}
	}
	// age
	//isValid = checkDropdown("years") || checkDropdown("months");
	isValid = checkDropdown("year") && checkDropdown("month") && checkDropdown("day");
	if (!isValid) {
		invalidIndex[2] = true;
		invalidType = -1;

	} else {


		isValid = validDate( document.form1.day.value, document.form1.month.value, document.form1.year.value );
		if (!isValid) {
			invalidIndex[2] = true;
			invalidType = -1;

		} else {

			//----- check the pets age ----- ?????
			isValid = checkAge(false);
			if (!isValid) {
				if (document.form1.animal_type[0].checked) {
					invalidIndex[3] = true;
				}
				if (document.form1.animal_type[1].checked) {
					invalidIndex[4] = true;
				}
				invalidType = -1;
			}

			isValid = checkYears(false);
			if ( isValid != "OK" )
			{
				if ( isValid == "CAT" ) {
					invalidIndex[5] = true;

				} else if ( isValid == "DOG" ) {
					invalidIndex[6] = true;

				} else {
					invalidIndex[7] = true;
				}
				invalidType = -1;
			}
		}
	}

	// validate the email format
	if (invalidIndex[12] == false) {
		isValid = isEmail(document.getElementById("email").value);
		if (!isValid) {
			invalidType = 0;
			invalidIndex[13] = true;
		}
	}

	if (invalidType != 1) {
		if (msgType == "text") {
			writeValidationMessage(invalidType, invalidIndex, errorInfo);
		} else {
			var msgText = "";
			if (invalidType == -1) {
				msgText = "Please enter the following details:\n\n" ;
			} else {
				msgText = "Sorry, you seem to have entered some details incorrectly. Please check and try again.\n\n";
			}
			for (var i=0; i<invalidIndex.length; i++)
			{
				if (invalidIndex[i])
				{
					msgText += errorInfo[i];
				}
			}
			while (msgText.indexOf( "<li>" ) > 0) {
				msgText = msgText.replace( "<li>", "" );
				msgText = msgText.replace( "</li>", "\n" );
			}

			alert( msgText );
		}

		return false;
	}

	document.getElementById("errorMessage").style.display = "none";
	document.getElementById("errorDetails").style.display = "none";
	return true;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function checkYears(lAlert) {

	var oneYear = 1000 * 60 * 60 * 24 * 365.25

	var strDoB = document.form1.year.value +'/' +document.form1.month.value +'/' +document.form1.day.value;
	var dDoB = new Date(strDoB)	;

	var dNow = new Date();

	var diff_ms = dNow - dDoB;
	var diff_yrs = diff_ms / oneYear;

	if (diff_yrs < 6){
		return "OK";
	}

	// cat and over 10 (over age)
	if (document.form1.animal_type[0].checked && diff_yrs >=10){
		if (lAlert==true) {
			alert( "Sorry, we cannot provide cover for cats above 9 years and 11 months.");
		}
		return "CAT";
	}

	// dogs
	if (document.form1.animal_type[1].checked){

		// specified cross breeds
		// specified breeds
		var b = parseInt(document.form1.breed.value,10);

		var specBreeds = new Array(
		5637146329,5637146331,5637146336,5637146337,5637146338,5637146344,5637146346,5637146347,5637146353,
		5637146354,5637146356,5637146359,5637146365,5637146366,5637146367,5637146368,5637146369,5637146371,
		5637146374,5637146375,5637146380,5637146383,5637146386,5637146387,5637146388,5637146389,5637146390,
		5637146391,5637146392,5637146393,5637146396,5637146400,5637146401,5637146407,5637146410,5637146414,
		5637146415,5637146416,5637146417,5637146418,5637146419,5637146424,5637146436,5637146437,5637146438,
		5637146439,5637146440,5637146441,5637146442,5637146445,5637146448,5637146450,5637146451,5637146453,
		5637146460,5637146461,5637146466,5637146467,5637146468,5637146471,5637146474,5637146484,5637146488,
		5637146489,5637146490,5637146527,5637146528,5637146534,5637146536,5637146543,5637146546,5637146548,
		5637146552,5637146553,5637146554,5637146559,5637146560,5637146561,5637146583,5637146584,5637146585,
		5637146594,5637146595,5637146601,5637146611,5637146617,5637146649,5637146650,5637146656,5637146661,
		5637146676,5637146677,5637146492,5637146498,5637146499,5637146505,5637146506,5637147584);

		var lSpec = false;
		for (var i = 0; i < specBreeds.length-1; i++){
			if (specBreeds[i] == b) {
				lSpec = true;
				break;
			}
		}

		// specified breeds
		if (lSpec==true && diff_yrs >= 6) {
			if (lAlert==true) {
				alert( "Sorry, we cannot provide cover for dogs of a specified breed above 5 years and 11 months." );
			}
			return "SPEC";
		}

		// other dogs
		if (lSpec==false && diff_yrs >=8) {
			if (lAlert==true) {
				alert( "Sorry, we cannot provide cover for dogs above 7 years and 11 months.");
			}
			return "DOG";
		}

	}

	return "OK";
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function checkAge( lAlert ) {

	var oneWeek = 1000 * 60 * 60 * 24 * 7

	var strDoB = document.form1.year.value +'/' +document.form1.month.value +'/' +document.form1.day.value;
	var dDoB = new Date(strDoB)	;

	var dNow = new Date();

	var diff_ms = dNow - dDoB;
	var diff_wks = diff_ms / oneWeek;

	// if cat under 6 weeks
	if (document.form1.animal_type[0].checked && diff_wks<6) {
		if (lAlert==true) {
			alert( "We are unable to provide a quote for cats under 6 weeks old.");
		}
		return false;
	}

	// if dog under 8 weeks
	if (document.form1.animal_type[1].checked && diff_wks<8) {
		if (lAlert==true) {
			alert( "We are unable to provide a quote for dogs under 8 weeks old.");
		}
		return false;
	}

	return true;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function fillForm() {
//	document.form1.aname.value = sAname;
	if ( sAnimalType == "CAT" ) {
		document.form1.animal_type[0].checked = true;
		getBreeds(document.form1.animal_type[0]);

	} else if ( sAnimalType == "DOG" ) {
		document.form1.animal_type[1].checked = true;
		getBreeds(document.form1.animal_type[1]);
	}

	if ( sBreedType == "CROSS" ) {
		document.form1.breed_type[0].checked = true;
		getBreeds(document.form1.breed_type[0]);showBreedsOnType();

	} else if ( sBreedType == "PEDIG" ) {
		document.form1.breed_type[1].checked = true;
		getBreeds(document.form1.breed_type[1]);showBreedsOnType();
	}

//	document.form1.years.value = sYears;
//	document.form1.months.value = sMonths;

	document.form1.breed.value = sBreed;
//	alert( sBreed );
//	alert( document.form1.breed.length );
//	for (var i=0;i<document.form1.breed.length;i++) {
//		if (document.form1.breed.option[0].value == sBreed){
//			alert("Hi");
//		}
//	}

}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function ShowPetType() {


	if (document.forms[0].animal_type[0].checked) {
		document.getElementById('cat').style.fontWeight='bold';
		document.getElementById('cat').style.color='#FFBA00';
		document.getElementById('dog').style.fontWeight='normal';
		document.getElementById('dog').style.color='#666666';
	} else if (document.forms[0].animal_type[1].checked) {
		document.getElementById('cat').style.fontWeight='normal';
		document.getElementById('cat').style.color='#666666';
		document.getElementById('dog').style.fontWeight='bold';
		document.getElementById('dog').style.color='#FFBA00';
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function ShowBreedType() {

	if (document.forms[0].breed_type[0].checked) {
		document.getElementById('cross').style.fontWeight='bold';
		document.getElementById('cross').style.color='#FFBA00';
		document.getElementById('pedigree').style.fontWeight='normal';
		document.getElementById('pedigree').style.color='#666666';
	} else if (document.forms[0].breed_type[1].checked) {
		document.getElementById('cross').style.fontWeight='normal';
		document.getElementById('cross').style.color='#666666';
		document.getElementById('pedigree').style.fontWeight='bold';
		document.getElementById('pedigree').style.color='#FFBA00';
	}
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function initForm() {

	//--- Pet Type ---
	if ( document.forms[0].animal_type[0].checked ) {
		document.forms[0].animal_type.value = "CAT";
	}
	if ( document.forms[0].animal_type[1].checked ) {
		document.forms[0].animal_type.value = "DOG";
	}
	getBreeds(document.forms[0].animal_type);
	ShowPetType();

	//-- Pet Type ---
	if ( document.forms[0].breed_type[0].checked ) {
		document.forms[0].breed_type.value = "CROSS";
	}
	if ( document.forms[0].breed_type[1].checked ) {
		document.forms[0].breed_type.value = "PEDIGREE";
	}
//	getBreeds(document.forms[0].breed_type);
	showBreedsOnType();
	ShowBreedType();

	document.getElementById("aname").focus();
}

