var grindr = window.grindr || {};
grindr.test = null;
/**
 * Checks which OS is selected and filters the phone models select element
 *		@param {Object} event object that fired the event.
 */
grindr.filterPhoneModels = function(event) {
	var sValue = event.value,
		oType = {'BB':false,'WinMobile':false,'Symbian':false,'Palm':false,'Android':false,'Other':false};
	switch (sValue) {
		case '187314':
			//Blackberry
			oType.BB = true;
			break;
		case '187315':
			//Android
			oType.Android = true;
			break;
		case '187316':
			//Palm
			oType.Palm = true;
			break;
		case '187317':
			//Symbian
			oType.Symbian = true;
			break;
		case '646958':
			//Windows Mobile
			oType.WinMobile = true;
			break;
		case '188157':
			//Other
			oType.Other = true;
			break;
		default:
			oType.Other = true;
			break;
	}
	
	// Goes in every element filtering the options by OS
	var oSelectPhones = Element.extend($$("select[name=cm-fo-jijrud]")[0]);
	if (grindr.test === null) {
		grindr.test = oSelectPhones.innerHTML;
	} else {
		/*oSelectPhones.childElements().each(function(oOption) {
			oSelectPhones.removeChild(oOption);
		});
		oSelectPhones.insert(Element.extend(grindr.test).childElements());*/
		oSelectPhones.update(grindr.test);
	}
	oSelectPhones.childElements().each(function(oOption) {
		// Gets the value of the option element
		var sValue = oOption.readAttribute('value');
		// Looks for the type and shows or hides the option
		switch (sValue) {
			case '187319' :
			case '187320' :
			case '187321' :
			case '187322' :
			case '187323' :
			case '187324' :
			case '187325' :
			case '187326' :
			case '187327' :
			case '187330' :
			case '187329' :
			case '187328' :
			case '187331' :
				// Blackberry
				if(!oType.BB && !oType.Other) {
					oOption.remove();
				}
				break;
			case '646985':
			case '646986':
			case '646987':
			case '646988':
			case '646989':
			case '646990':
			case '646991':
			case '646992':
			case '646993':
			case '647599':
			case '646994':
				// Windows Mobile
				if(!oType.WinMobile && !oType.Other) {
					oOption.remove();
				}
				break;
			case '187358':
			case '187360':
				// Symbian
				if(!oType.Symbian && !oType.Other) {
					oOption.remove();
				}
				break;
			case '187357' :
			case '187356' :
			case '187355' :
			case '187384' :
			case '187372' :
				// Palm
				if(!oType.Palm && !oType.Other) {
					oOption.remove();
				}
				break;
			case '187347' :
			case '187391' :
			case '187341' :
			case '187353' :
			case '187333' :
			case '187349' :
			case '187337' :
			case '187346' :
			case '187340' :
			case '187335' :
			case '187352' :
			case '187342' :
			case '310522' :
			case '187348' :
			case '310523' :
			case '187334' :
			case '310524' :
			case '187345' :
			case '310525' :
			case '187343' :
			case '187344' :
			case '310530' :
			case '187339' :
			case '187338' :
			case '310531' :
			case '187332' :
			case '187354' :
			case '310532' :
			case '187336' :
			case '310533' :
			case '310534' :
			case '187350' :
			case '310535' :
			case '310536' :
				// Android
				if(!oType.Android && !oType.Other) {
					oOption.remove();
				}
				break;
			default:
				// Hides if is not listed.
				if(!oType.Other) {
					oOption.remove();
				}
				break;
		}
	});
};

/**
 * Checks if the user selects other, to show the custom mobile device type.
 *		@param {Object} event object that fired the event.
 */
grindr.checkSelection = function(event) {
	// if other
	switch (event.value) {
		case '187354': //Other Android Device
		case '187331': //Other Blackberry Device
		case '187357': //Other Palm Device
		case '187360': //Other Symbian Device
		case '647599': //Other Windows 7 Device
		case '188158': //Other
			$('otherDeviceBox').show();
			break;
		default:
			$('otherDeviceBox').hide();
	}
};

