//Product Page functions for the TopFinder, Version 2

var ImagePath = '';
var BaseProdOptID = '';
var VendorID = '';
var ColorBoundOptionPicked = 0;
var isPontoon = 0;
var isAnSTBimmini = false;
var isATowerBimmini = false;
var TopPicked = false;
var StripeTopPicked = false;
var BodyColor = 'WH';
var BodyColorName = 'White';
var StripeColor = 'WH';
var StripeColorName = 'White';
var SameColorAlert = "You cannot select the same main color and stripe color.\n\nChoose the regular Sharkskin option if you want a solid-color bimini top.";
var BubbleSemaphore = 0;
var BubbleStatus = 0;
var TextContainerStatus = 0;
var CurrentVisibleUpgradeText = '';
var NextUpgradeText = '';

function Init(SetImagePath, Vendor, PontoonFlag, STFlag, TowerFlag)
{
	ImagePath = SetImagePath;
	VendorID = Vendor;
	isPontoon = PontoonFlag;
	isAnSTBimmini = STFlag;
	isATowerBimmini = TowerFlag;
	document.productForm.color_id.value = 'NONE';
	return true;
}

function ShowUpgrade(FloatWindowName)
{
	BubbleSemaphore = 1;
	if (BubbleStatus == 1 && CurrentVisibleUpgradeText != FloatWindowName){
		FadeOutUpgrade();
	}
	
	if (BubbleStatus == 0){
		ShowBubble();
	}
	
	if (CurrentVisibleUpgradeText == '' || CurrentVisibleUpgradeText != FloatWindowName){
		NextUpgradeText = FloatWindowName;
		setTimeout("SwitchUpgradeText();", 180);
	}

	return true;
}

function AllowBubbleHide(TransitionTime)
{
	BubbleSemaphore = 0; 
	setTimeout('HideUpgrade()', TransitionTime);	
}

function ShowBubble()
{
	var BubbleTarget = $('UpgradeBubbleContainer');
	BubbleTarget.removeClass('UpgradeBubbleHidden');
	var myFx = new Fx.Style(BubbleTarget, 'opacity', {duration:600, transition: Fx.Transitions.Cubic.easeOut}).start(0,1);
	BubbleStatus = 1;	
	return true;	
}

function FadeBubble(TransitionTime)
{
	var BubbleTarget = $('UpgradeBubbleContainer');
	var myFx = new Fx.Style(BubbleTarget, 'opacity', {duration:TransitionTime, transition: Fx.Transitions.Cubic.easeOut}).start(1,0);
	BubbleStatus = 0;
	setTimeout("HideBubble();", TransitionTime);
}

function HideBubble()
{
	var BubbleTarget = $('UpgradeBubbleContainer');
	BubbleTarget.addClass('UpgradeBubbleHidden');
	BubbleStatus = 0;
	return true;
}

function HideUpgrade(FloatWindowName)
{
	if (BubbleSemaphore == 1 || BubbleStatus == 0)	{
		return false;	
	}
	else if (BubbleSemaphore == 0 && BubbleStatus == 1){
		FadeBubble(2000);	
		return true;
	}
}

function SwitchUpgradeText()
{
	var NewBubbleContents = document.getElementById(NextUpgradeText);
	ActiveUpgradeTextContainerDOM = document.getElementById('UpgradeTextContainer');
	ActiveUpgradeTextContainerDOM.innerHTML = NewBubbleContents.innerHTML;
	CurrentVisibleUpgradeText = NextUpgradeText;
	
	FadeInUpgrade();
	
	return true;
}


function FadeOutUpgrade()
{
	if (BubbleStatus == 1 && TextContainerStatus == 1){
		var ElementFader = $('UpgradeTextContainer');
		var myFx = new Fx.Style(ElementFader, 'opacity', {duration:200}).start(1,0);
		TextContainerStatus = 0;	
	}	
	return true;	
}

function FadeInUpgrade()
{
	if (BubbleStatus == 0){
		ShowBubble();
	}
	
	if (BubbleStatus == 1 && TextContainerStatus == 0){
		var ElementFader = $('UpgradeTextContainer');
		var myFx = new Fx.Style(ElementFader, 'opacity', {duration:200}).start(0,1);
		TextContainerStatus = 1;	
	}	
	return true;	
}

function SwitchInfo(material_name, color_name, price, image, prod_opt_id, color_id, w_length, material_id, is_stripe)
{
	BaseProdOptID = prod_opt_id;

	var SelectionValid = 1;
	var IsAStripedBimini = new RegExp('single stripe', 'i');
	
	if (TopPicked == false){
		var CurrentWidget = document.getElementById('DefaultTextContainer');
		CurrentWidget.className = 'UpgradeOptionHidden';
		
		CurrentWidget = document.getElementById('PreviewTextContainer');
		CurrentWidget.className = 'UpgradeOptionVisible';
	}
	
	if (IsAStripedBimini.test(material_name) == 1)
	{
		if (is_stripe == 1)	{
			if (BodyColor == color_id && StripeTopPicked == true){
				SelectionValid = 0; alert(SameColorAlert); 
			}else{
				StripeColor = color_id; StripeColorName = color_name; StripeTopPicked = true;
			}
		}
		else{
			if (StripeColor == color_id && StripeTopPicked == true){
				 SelectionValid = 0; alert(SameColorAlert);
			}else{
				BodyColor = color_id; BodyColorName = color_name; StripeTopPicked = true;
			}
		}	
		
		if (SelectionValid == 1){
			var CurrentWidget = document.getElementById('ColorName');
			CurrentWidget.innerHTML = BodyColorName + ' - ' +  StripeColorName;
		
			var ImageName = ImagePath+'/colors/biminis/bimini_stripe.cgi?color='+BodyColor+'&stripe='+StripeColor;
			document.images.PreviewImage.src = ImageName;
			document.productForm.color_id.value = BodyColor+StripeColor;
		}
	}
	else{
		var CurrentWidget = document.getElementById('ColorName');
		CurrentWidget.innerHTML = color_name;
		
		document.images.PreviewImage.src = ImagePath+'/colors/'+image;
		document.productForm.color_id.value = color_id;
		
		BodyColor = 'WH'; BodyColorName = 'White';
		StripeColor = 'WH'; StripeColorName = 'White';
		StripeTopPicked = false;
	}
	
	if (isATowerBimmini == 1 && VendorID == 20){
		document.productForm.prod_opt_id.value = BaseProdOptID;	
	}
	else if (ColorBoundOptionPicked == 0){
		var poRef = document.getElementById('prod_opt_id_option');
		poRef.value = document.productForm.color_id.value;
		poRef.name = 'prod_opt_id_'+prod_opt_id+'_option';
	}
	
	var CurrentWidget = document.getElementById('FabricName');
	CurrentWidget.innerHTML = material_name;
	
	CurrentWidget = document.getElementById('PriceName');
	CurrentWidget.innerHTML = '$' + price;
	
	CurrentWidget = document.getElementById('PreviewText');
	CurrentWidget.className = 'Warranty' + w_length;
	
	if ((material_id != CurrentMaterial) && (MaterialOptionsOnPage.length != 0)){
		ShowMaterialUpgrades(material_id);
	}
	else{
		document.productForm.prod_opt_id.value = prod_opt_id;			
	}
	
	ValidateUpgradeSelection(material_id, 'none', prod_opt_id);
	CurrentMaterial = material_id;
	TopPicked = true;

	return false;
}

function ShowMaterialUpgrades(Material_id)
{
	if (MaterialOptionsOnPage.length == 0){
		return true;
	}
	
	for (UpgradeIndex = 0; UpgradeIndex < MaterialsOnPage.length; UpgradeIndex++){
		var CurrentUpgradeRow = document.getElementById('UpgradeOptionRow_Material_' + MaterialsOnPage[UpgradeIndex]);
		if (CurrentUpgradeRow){
			if (MaterialsOnPage[UpgradeIndex] == Material_id){
				CurrentUpgradeRow.className = 'UpgradeOptionVisible';
			}else{
				CurrentUpgradeRow.className = 'UpgradeOptionHidden';
			}

			if (Material_id > 6 && VendorID != 20 && Material_id != 92){
				CurrentUpgradeRow = document.getElementById('UpgradeOptionRow_Material_99');
				
				if (CurrentUpgradeRow){
					CurrentUpgradeRow.className = 'UpgradeOptionVisible';
				}
			}			

			if (VendorID == 20 && isATowerBimmini == 0){ // Hot Shot Frame
				var poRef = document.getElementById('UpgradeCheckBox_20__included_frame');
				if (Material_id == 20){
					poRef.value = 1;
				}else{
					poRef.value = 0;
				}
			}
		}
	}
	
	var UpgradeHeader = document.getElementById('UpgradeHeader');
	UpgradeHeader.className = 'UpgradeOptionVisible';
	
	return true;
}

function UpgradeCheckBoxSwitch(Material_id, type_id)
{
	if (MaterialOptionsOnPage.length == 0){
		return true;
	}
	
	for (OptionIndex = 0; OptionIndex < MaterialOptionsOnPage.length; OptionIndex++){
		if (MaterialOptionsOnPage[OptionIndex] == Material_id + '__' + type_id)	{
			var CurrentOptionRow = document.getElementById('UpgradeCheckBox_' + MaterialOptionsOnPage[OptionIndex]);
			if (CurrentOptionRow.disabled == false){
				CurrentOptionRow.checked = !(CurrentOptionRow.checked);
			}
			return true;
		}
	}
}

function ValidateUpgradeSelection(Material_id, type_id, prod_opt_id)
{
	if (MaterialOptionsOnPage.length == 0 || Material_id == 99 || (VendorID == 20 && isATowerBimmini == 1)){
		return true;
	}
	
	var MaterialUpgradeSelected = 0;
	var BootFrameSelected = 0;
	var Material_99_Exists = 0;
	var MaterialBoundOptionPattern = new RegExp(Material_id + '__', 'i');
	var GenericOptionPattern = new RegExp('99__', 'i');
	
	for (OptionIndex = 0; OptionIndex < MaterialOptionsOnPage.length; OptionIndex++){
		var CurrentOptionRow = document.getElementById('UpgradeCheckBox_' + MaterialOptionsOnPage[OptionIndex]);		
		if (GenericOptionPattern.test(MaterialOptionsOnPage[OptionIndex])){
			Material_99_Exists = 1;
		}
		if (type_id == 'boot_frame' || !(GenericOptionPattern.test(MaterialOptionsOnPage[OptionIndex]))){
			if (MaterialBoundOptionPattern.test(MaterialOptionsOnPage[OptionIndex])) {
				if (MaterialOptionsOnPage[OptionIndex] == Material_id + '__' + type_id){
					var poRef = document.getElementById('prod_opt_id_option');
					
					if (VendorID == 704 && (Material_id == 13 || Material_id == 14 || Material_id == 11) && (type_id == 'boot' || type_id == 'boot_frame'))	{
						document.productForm.prod_opt_id.value = null;	
						poRef.name = 'prod_opt_id_'+prod_opt_id+'_option';
						poRef.value = document.productForm.color_id.value;
						ColorBoundOptionPicked = 1;
					}
					else if (VendorID != '20' && !isAnSTBimmini){
						poRef.name = 'prod_opt_id_'+prod_opt_id+'_option';
						document.productForm.prod_opt_id.value = null;	
						ColorBoundOptionPicked = 1;
					}
					
					if (CurrentOptionRow.checked == true){
						if (type_id == 'boot_frame'){
							BootFrameSelected = 1;
						}
						MaterialUpgradeSelected = 1;
					}
					
					// Hot Shot Boot Color
					if (VendorID == 20 && Material_id == 20 && type_id == 'boot'){
						var poRef = document.getElementById('UpgradeCheckBox_20__boot_option');
						poRef.value = document.productForm.color_id.value;
						ColorBoundOptionPicked = 1;
					}
					
					// ST Bimini Boot Color
					if (VendorID == 644 && type_id == 'boot' && isAnSTBimmini){
						var poRef = document.getElementById('UpgradeCheckBox_'+Material_id+'__boot_option');
						poRef.value = document.productForm.color_id.value;
						ColorBoundOptionPicked = 1;
					}
				}   
				else{   
					CurrentOptionRow.checked = false;
				}   
			}
			else{
				if (CurrentOptionRow){
					CurrentOptionRow.checked = false;
				}
			}
		}
	}
	
	if (Material_99_Exists == 1 && !(isATowerBimmini && VendorID == 704)){
		var BootOption = document.getElementById('UpgradeCheckBox_' + Material_id + '__boot');
		var SS_Fittings = document.getElementById('UpgradeCheckBox_99__stainless_steel_fittings');
		if (BootFrameSelected){
			BootOption.disabled = true;
			BootOption.checked = false;
			SS_Fittings.disabled = true;
			SS_Fittings.checked = false;
		}	
		else{
			SS_Fittings.disabled = false;
			if (BootOption){
				BootOption.disabled = false;
			}
		}
	}
	
	// Hot Shot Frame
	if (VendorID == 20 && Material_id != 20 && isPontoon == 0){
		var poRef = document.getElementById('UpgradeCheckBox_20__included_frame');
		poRef.value = 0;
	}
	
	if (MaterialUpgradeSelected == 0){
		var poRef = document.getElementById('prod_opt_id_option');
		poRef.name = 'prod_opt_id_'+BaseProdOptID+'_option';
		document.productForm.prod_opt_id.value = BaseProdOptID;	   
		ColorBoundOptionPicked = 0; 
	}
	return true;	
}

function ValidateForm()
{
	if (document.productForm.color_id.value == 'NONE' || TopPicked == false){
		alert('You have not chosen a fabric or a color yet. Please click on the colored boxes under the fabric titles to change your color and fabric selection.');
		return false;
	}
	else{
		SaveTracking(document.productForm.cart_id.value);	
		document.productForm.submit();
		return true;
	}
}