function populateCarriers(carrierid) {
	var aCarrierList = document.getElementById("carrier");
	var usedCarriers = [];
	var aCount = 1;
	for (var anElement = 0; anElement < PhoneObject.length; anElement++) {
		if (!contains(usedCarriers, PhoneObject[anElement].carrier)) {
			usedCarriers[usedCarriers.length] = PhoneObject[anElement].carrier;
			var insertElement = document.createElement("option");
			insertElement.value = PhoneObject[anElement].carrier;
			insertElement.text = PhoneObject[anElement].carrier;

			if (PhoneObject[anElement].carrier == carrierid) {
				insertElement.selected = "selected";
			}

			aCarrierList[aCount] = insertElement;
			aCount++;
		}
	}

	aPhoneMakersList = document.getElementById("phoneMakersDiv");
	aPhoneMakersList.style.visibility = "hidden";

	aPhoneModelList = document.getElementById("phoneModelsDiv");
	aPhoneModelList.style.visibility = "hidden";
}


function populatePhoneMakers(makerid, carrierid) {

	if(!carrierid) {
	   var carrierid = document.getElementById("carrier").value;
	}


	var aPhoneMakersList = document.getElementById("phoneMakersDiv");
	var aPhoneModelList = document.getElementById("phoneModelsDiv");
	aPhoneModelList.style.visibility = "hidden";
	aPhoneMakersList.style.visibility = "visible";



	if (carrierid == "None") {
		aPhoneMakersList.style.visibility = "hidden";
		aPhoneModelList.style.visibility = "hidden";
		return;
	}

	var usedCarriers = [];
	var optionsHTML = '<select id="phoneMakers" name="phoneMakers" onchange="populatePhones(null);"> <option value="None"> Select a manufacturer</option> ';

	for (var anElement = 0; anElement < PhoneObject.length; anElement++) {
		if (PhoneObject[anElement].carrier == carrierid) {//&& PhoneObject[anElement].v7 == "1") {
			var phone = PhoneObject[anElement].phone;

			if (!contains(usedCarriers, phone)) {
				usedCarriers[usedCarriers.length] = phone;
				if (phone == makerid) {
					optionsHTML += '<option value="'+phone+'" selected>'+phone+'</option> ';
				} else {
					optionsHTML += '<option value="'+phone+'">'+phone+'</option> ';
				}
			}
		}
	}
	aPhoneMakersList.innerHTML = optionsHTML+ " </select>";
}

function contains(a, obj) {
	var i = a.length;
	while (i--) {
		if (a[i] === obj) {
			return true;
		}
	}
	return false;
}

function populatePhones(phoneid, makerid, carrierid) {
	if (!makerid) {
		var makerid = document.getElementById("phoneMakers").value;
	}

	if(!carrierid) {
		var carrierid = document.getElementById("carrier").value;
	}

	var aPhoneMakerList = document.getElementById("phoneMakers");

	var aPhoneModelList = document.getElementById("phoneModelsDiv");
	aPhoneModelList.style.visibility = "visible";

	if (aPhoneMakerList.value == "None") {
		aPhoneModelList.style.visibility = "hidden";
		return;
	}

	aPhoneModelList.innerHTML = " ";
   var optionsHTML1 = '<select id="phoneModels" name="phoneModels" onchange="Redirect('+fromversion+')"> <option>Select a phone</option> ';

	for (var anElement = 0; anElement < PhoneObject.length; anElement++) {
		if ((PhoneObject[anElement].phone == makerid) && (PhoneObject[anElement].carrier == carrierid)) {//&& PhoneObject[anElement].v7 == "1") {

			if (PhoneObject[anElement].model == phoneid) {
				optionsHTML1 += '<option value="'+PhoneObject[anElement].id+'" selected>'+PhoneObject[anElement].model+'</option> ';
			} else {
				optionsHTML1 += '<option value="'+PhoneObject[anElement].id+'">'+PhoneObject[anElement].model+'</option> ';
			}
		}
	}
   aPhoneModelList.innerHTML = optionsHTML1 + " </select>";
}

function Redirect(fromversion) {

	var aPhoneModel = document.getElementById("phoneModels").value;

	var aDate = new Date();
	aDate.setTime(aDate.getTime() + 1000*60*60*24);

	//document.cookie="phoneID=" + aPhoneModel + "; expires=" + aDate.toGMTString() + "; path=/";
	//window.location.href = "/products/pilot-my-cast/index.jsp?rand=" + Math.random(1) + "#tab-panels";

	if (PhoneObject[aPhoneModel].ver == "8") {
		if (fromversion == 7) {
			result = confirm("The phone you have selected supports Pilot My-Cast version 8.\n\nTo view the specific features and learn how to subscribe to Pilot My-Cast version 8, click OK.");
			if (!result) {
				return;
			}
		}
		document.cookie="pphoneID=" + aPhoneModel + "; expires=" + aDate.toGMTString() + "; path=/";
		window.location.href = "/products/pilot-my-cast/index.jsp?rand=" + Math.random(1) + "#tab-panels";
	} else if (PhoneObject[aPhoneModel].ver == "7" || PhoneObject[aPhoneModel].ver == "4") {
		if (fromversion == 8) {
			result = confirm("The phone you have selected requires a previous version of Pilot My-Cast. Please note that the features are slightly different.\n\nTo view the specific features and learn how to subscribe to Pilot My-Cast, click OK.");
			if (!result) {
				return;
			}
		}
		document.cookie="pphoneID=" + aPhoneModel + "; expires=" + aDate.toGMTString() + "; path=/";
		window.location.href = "/products/pilot-my-cast/v7/index.jsp?rand=" + Math.random(1) + "#tab-panels";
	}


	/*if (PhoneObject[aPhoneModel].ver == "8") {
		if (fromversion == 4 || fromversion == 7) {
			result = confirm("The phone you have selected supports Pilot My-Cast version 8.\n\nTo view the specific features and learn how to subscribe to Pilot My-Cast version 7, click OK.");
			if (!result) {
				return;
			}
		}
		document.cookie="phoneID=" + aPhoneModel + "; expires=" + aDate.toGMTString() + "; path=/";
		window.location.href = "/products/pilot-my-cast/";
	} else if (fromversion == 7) {
		if (PhoneObject[aPhoneModel].ver == "8") {
			result = confirm("The phone you have selected supports Pilot My-Cast version 8.\n\nTo view the specific features and learn how to subscribe to Pilot My-Cast version 8, click OK.");
			if (!result) {
				return
			}
				document.cookie="phoneID=" + aPhoneModel + "; expires=" + aDate.toGMTString() + "; path=/";
				window.location.href = "/products/pilot-my-cast/";
		} else if (PhoneObject[aPhoneModel].ver == "4") {
			result = confirm("The phone you have selected requires the previous version of Pilot My-Cast, version 4. Please note that the features are slightly different.\n\nTo view the specific features and learn how to subscribe to Pilot My-Cast version 4, click OK.");
			if (!result) {
				return;
			}
		document.cookie="phoneID=" + aPhoneModel + "; expires=" + aDate.toGMTString() + "; path=/";
		window.location.href = "/products/pilot-my-cast/";
		}
	} else if (fromversion == 4){
		if (PhoneObject[aPhoneModel].ver == "8") {
			result = confirm("The phone you have selected supports Pilot My-Cast version 8.\n\nTo view the specific features and learn how to subscribe to Pilot My-Cast version 8, click OK.");
			if (!result) {
				return
			}
				document.cookie="phoneID=" + aPhoneModel + "; expires=" + aDate.toGMTString() + "; path=/";
				window.location.href = "/products/pilot-my-cast/";
		} else if (PhoneObject[aPhoneModel].ver == "7") {
			result = confirm("The phone you have selected supports Pilot My-Cast version 7.\n\nTo view the specific features and learn how to subscribe to Pilot My-Cast version 7, click OK.");
			if (!result) {
				return
			}
				document.cookie="phoneID=" + aPhoneModel + "; expires=" + aDate.toGMTString() + "; path=/";
				window.location.href = "/products/pilot-my-cast/";
		}
	}*/
}

function populateAll(carrierid, makerid, phoneid) {
	populateCarriers(carrierid);
	if (makerid && makerid.length > 0) {
		populatePhoneMakers(makerid, carrierid);
	}
	if (phoneid && phoneid.length > 0) {
		populatePhones(phoneid, makerid, carrierid);
	}
}
