// Fujifilm Global Warranty
//
// copyright Fujifilm Corporation
//
// site client scripts
//
var globalRegionSelect = false;
//
var ajaxHTTP = createAjax();
//
function init() {
	//
	manageProfileForm();
	//
	cacheImages();	
	//
	clearProductList();
}
//
function cacheImages() {
	//
	var imageRoot = '/sites/all/themes/mfp_warranty/img/shared/';
 	var imgCache = new Array();
  	imgCache[0] = new Image;
  	imgCache[0].src = imageRoot + 'site/bg_countryselect_panel_01.png';	
  	imgCache[1] = new Image;
  	imgCache[1].src = imageRoot + 'site/bg_countryselect_arrow_01.png';
}
//
function clearProductList() {
	//
	if (document.getElementById("edit-model-nid") && ajaxHTTP) {
		if (document.forms['warranty-registration-form'].elements['edit-segment'].value == "") {
			document.forms['warranty-registration-form'].elements['edit-model-nid'].options.length = 0;
			document.forms['warranty-registration-form'].elements['edit-model-nid'].options[0] = new Option("-", "", true, false);
		} else {
			updateProducts();
		}
	} 
}
//
function manageProfileForm() {
	//
	var coppaAge = 13;
	var minimumYear = new Date().getFullYear() - coppaAge;
	//
	if ($('form#warranty-registration-form select.birthYear').val() < minimumYear ) {
		$('form#warranty-registration-form .dateExtra').hide();
	} else {
		// add class to allow legacy ie to expand panel
		$('form#warranty-registration-form').addClass('formExpanded');
	}
	//
	$('form#warranty-registration-form select.birthYear').change(function() {		
		if ($(this).val() >= minimumYear) {
			$('form#warranty-registration-form .dateExtra').fadeIn(250);
			$('form#warranty-registration-form').addClass('formExpanded');
		} else {
			$('form#warranty-registration-form .dateExtra').fadeOut(250);
			$('form#warranty-registration-form').removeClass('formExpanded');
		}
	});
}
//
function createAjax() {
	//
	var ajax = false;
	if (window.XMLHttpRequest) {
		ajax = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try	{
			ajax = new ActiveXObject("MSXML3.XMLHTTP");
		}
		catch(e) {
			try	{
				ajax = new ActiveXObject("MSXML2.XMLHTTP.3.0");
			}
			catch(e) {
				try {
					ajax = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e) {
					try	{
						ajax = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch(e) {
						ajax = false;
					}
				}
			}
		}
	
	} else {
		ajax = false;
	}
	return ajax;
}
//
function isAjaxReady(ajax) {
	//
	if (ajax.readyState == 4) {
		return true;
	} else {
		return false;
	}
}
//
function updateProducts() {
	//
	var selectOption = document.forms['warranty-registration-form'].elements['edit-segment'].value;
	//
	if (selectOption) {
		displayThrober('wrapper-model', 'on');
		ajaxHTTP.open('GET','/cameras/segment/' + selectOption, true);
		ajaxHTTP.onreadystatechange = handleHttpResponse;
		ajaxHTTP.send(null);
	}
}
//
function handleHttpResponse() {
	if (isAjaxReady(ajaxHTTP)) {
		var xmlDoc = ajaxHTTP.responseXML;
		var productIds = xmlDoc.getElementsByTagName("nid");
		var productTitles = xmlDoc.getElementsByTagName("title");
		var selectedIndex = document.forms['warranty-registration-form'].elements['edit-model-nid'].value;
		var selectedOption;
		document.forms['warranty-registration-form'].elements['edit-model-nid'].options.length = 0;
		document.forms['warranty-registration-form'].elements['edit-model-nid'].options[0] = new Option("- " + translated['please_choose'] + " -", "", true, false)
		for (var i = 0; i < productIds.length; i ++) {
			if (productIds[i].childNodes[0].nodeValue == selectedIndex) {
				selectedOption = true;
			} else {
				selectedOption = false;
			}
			document.forms['warranty-registration-form'].elements['edit-model-nid'].options[i + 1] = new Option(productTitles[i].childNodes[0].nodeValue, productIds[i].childNodes[0].nodeValue, false, selectedOption);
		}
		displayThrober('wrapper-model', 'off');
	}	
}
//
function displayThrober(objectId, op) {
	//
	var throberObject = document.getElementById(objectId);
	//
	switch (op) {
		case "on":
			var lang = document.getElementById("baseBgImageTray").className;
			if (lang.indexOf('dir-rtl') == -1) {
				throberObject.style.background = "url(/sites/all/themes/mfp_warranty/img/shared/site/loading.gif) 0px 29px no-repeat";
			} else {
				throberObject.style.background = "url(/sites/all/themes/mfp_warranty/img/shared/site/loading.gif) 512px 29px no-repeat";
			}
			break;
		case "off":
			throberObject.style.background = 'none';
			break;
		default:
			throberObject.style.background = 'none';
			break;
	}	
}
//
function showOptions(option) {
	//
	regions = new Array(
		'na',
		'la',
		'eu', 
		'mea', 
		'ap'
	);
	//
	regionColours = new Array(
		'#FFD800',
		'#FFD800',
		'#FFD800',
		'#FFD800',
		'#FFD800'
	);
	//
	var regionColourDefault = '#9C9C9C';
	//
	var display;
	//
	globalRegionSelect = option;
	//
	for (var i = 0; i < regions.length; i ++) {
		//
		if (option == regions[i]) {
			display = 'block';
			document.getElementById('countryLinks').style.backgroundImage = 'none';
			document.getElementById('textStage').style.display = 'none';
			document.getElementById('region-link-' + regions[i]).style.backgroundColor = regionColours[i];
			document.getElementById('region-' + regions[i]).style.marginTop = 0;	// IE7 fix
		} else {
			display = 'none';
			document.getElementById('region-link-' + regions[i]).style.backgroundColor = regionColourDefault;
		}
		//
		document.getElementById('region-' + regions[i]).style.display = display;
	}	
}
//
function rolloverBlock(region) {
	//
	var rolloverColour = '#FFE65B';
	//
	if (region != globalRegionSelect) {
		document.getElementById('region-link-' + region).style.backgroundColor = rolloverColour;
	}	
}
//
function rolloutBlock(region) {
	//
	var rolloutColour = '#9C9C9C';
	//
	if (region != globalRegionSelect) {
		document.getElementById('region-link-' + region).style.backgroundColor = rolloutColour;
	}	
}
