/*
	JS Code specific to sleepVantage form (Based on JS Code specific to svuk)
	1.0
	
	Created on 12/06/08
	Matt Law
	
	Last edited on 11/08/10
	Harris
*/

/*
	Function to show additional mask elements on Join Now form
*/

var maxMasks=5;
var maskCount=1;
var lastMaskTabIndex=0;

function addMask()
{
	if(maskCount == maxMasks)
	{
		alert('Sorry, you cannot add anymore masks');
		return;
	}
	
	maskCount++;
	var newMaskTable = $('maskmodeltable_1').clone();
	
	newMaskTable.setProperty('id', 'maskmodeltable_' + maskCount);
	
	var firstRow = newMaskTable.getElement('tr');
		
	if(firstRow != null) firstRow.setStyle('display','none'); 
	
	//Mask Model
	
	var modelSelect = newMaskTable.getElement('select[name=Mask_Model_1]');
	modelSelect.setProperty('selectedIndex', 0);
	if(lastMaskTabIndex == 0)
	{
		lastMaskTabIndex = modelSelect.getProperty('tabindex')+3;
	}
	modelSelect.setProperty('tabindex', lastMaskTabIndex++);
	modelSelect.setProperty('id', 'Mask_Model_' + maskCount);	
	modelSelect.setProperty('name', 'Mask_Model_' + maskCount);
	validator.addFieldToList(modelSelect);
	
	var otherModelInput = newMaskTable.getElement('input[name=Other_Mask_Model_1]');
	otherModelInput.setProperty('value', '');
	otherModelInput.setProperty('id', 'Other_Mask_Model_' + maskCount);	
	otherModelInput.setProperty('name', 'Other_Mask_Model_' + maskCount);
	otherModelInput.setProperty('tabindex', lastMaskTabIndex++);
	validator.addFieldToList(otherModelInput);
	
	//Mask Size
	
	var maskSizeSelect = newMaskTable.getElement('select[name=Mask_Size_1]');
	maskSizeSelect.setProperty('selectedIndex', 0);
	maskSizeSelect.setProperty('tabindex', lastMaskTabIndex++);
	maskSizeSelect.setProperty('id', 'Mask_Size_' + maskCount);
	maskSizeSelect.setProperty('name', 'Mask_Size_' + maskCount);
	validator.addFieldToList(maskSizeSelect);

	var otherMaskSizeInput = newMaskTable.getElement('input[name=Other_Mask_Size_1]');
	otherMaskSizeInput.setProperty('id', 'Other_Mask_Size_' + maskCount);	
	otherMaskSizeInput.setProperty('name', 'Other_Mask_Size_' + maskCount);
	otherMaskSizeInput.setProperty('tabindex', lastMaskTabIndex++);
	validator.addFieldToList(otherMaskSizeInput);

	//Date of Purchase

	var maskDatePurchaseSelect = newMaskTable.getElement('select[name=Date_Purchase_Mask_1]');
	maskDatePurchaseSelect.setProperty('selectedIndex', 0);
	maskDatePurchaseSelect.setProperty('tabindex', lastMaskTabIndex++);
	maskDatePurchaseSelect.setProperty('id', 'Date_Purchase_Mask_' + maskCount);
	maskDatePurchaseSelect.setProperty('name', 'Date_Purchase_Mask_' + maskCount);
	validator.addFieldToList(maskDatePurchaseSelect);

	var maskMonthPurchaseSelect = newMaskTable.getElement('select[name=Month_Purchase_Mask_1]');
	maskMonthPurchaseSelect.setProperty('selectedIndex', 0);
	maskMonthPurchaseSelect.setProperty('tabindex', lastMaskTabIndex++);
	maskMonthPurchaseSelect.setProperty('id', 'Month_Purchase_Mask_' + maskCount);
	maskMonthPurchaseSelect.setProperty('name', 'Month_Purchase_Mask_' + maskCount);
	validator.addFieldToList(maskMonthPurchaseSelect);
	
	var maskYearPurchaseSelect = newMaskTable.getElement('select[name=Year_Purchase_Mask_1]');
	maskYearPurchaseSelect.setProperty('selectedIndex', 0);
	maskYearPurchaseSelect.setProperty('tabindex', lastMaskTabIndex++);
	maskYearPurchaseSelect.setProperty('id', 'Year_Purchase_Mask_' + maskCount);
	maskYearPurchaseSelect.setProperty('name', 'Year_Purchase_Mask_' + maskCount);
	validator.addFieldToList(maskYearPurchaseSelect);

	var MaskOutletTextArea = newMaskTable.getElement('textarea[name=Mask_Outlet_Supplier_by_1]');
	MaskOutletTextArea.setProperty('id', 'Mask_Outlet_Supplier_by_' + maskCount);	
	MaskOutletTextArea.setProperty('name', 'Mask_Outlet_Supplier_by_' + maskCount);
	MaskOutletTextArea.setProperty('tabindex', lastMaskTabIndex++);
	validator.addFieldToList(MaskOutletTextArea);

	newMaskTable.injectAfter('maskmodeltable_' + (maskCount-1));
	
	modelSelect.focus();
}

var maxFlowGens=5;
var fgCount=1;
var lastFGTabIndex=0;

function addFlowGen()
{
	if(maxFlowGens == fgCount)
	{
		alert('Sorry, you cannot add anymore Sleep Devices');
		return;
	}
	
	fgCount++;
	
	var newFGTable = $('flowgentable_1').clone();
	
	newFGTable.setProperty('id', 'flowgentable_' + fgCount);
	
	var firstRow = newFGTable.getElement('tr');
		
	if(firstRow != null) firstRow.setStyle('display','none'); 
	
	var modelSelect = newFGTable.getElement('select[name=Sleep_Device_Model_1]');
	modelSelect.setProperty('selectedIndex', 0);
	if(lastFGTabIndex == 0)
	{
		lastFGTabIndex = modelSelect.getProperty('tabindex')+9;
	}
	modelSelect.setProperty('tabindex', lastFGTabIndex++);
	modelSelect.setProperty('id', 'Sleep_Device_Model_' + fgCount);	
	modelSelect.setProperty('name', 'Sleep_Device_Model_' + fgCount);
	validator.addFieldToList(modelSelect);
	
	var otherModelInput = newFGTable.getElement('input[name=Other_Flow_Generator_Model_1]');
	otherModelInput.setProperty('value', '');
	otherModelInput.setProperty('id', 'Other_Flow_Generator_Model_' + fgCount);	
	otherModelInput.setProperty('name', 'Other_Flow_Generator_Model_' + fgCount);
	otherModelInput.setProperty('tabindex', lastFGTabIndex++);
	validator.addFieldToList(otherModelInput);

	var serialNoSleepDeviceInput = newFGTable.getElement('input[name=Serial_Number_Sleep_Device_1]');
	serialNoSleepDeviceInput.setProperty('value', '');
	serialNoSleepDeviceInput.setProperty('id', 'Serial_Number_Sleep_Device_' + fgCount);	
	serialNoSleepDeviceInput.setProperty('name', 'Serial_Number_Sleep_Device_' + fgCount);
	serialNoSleepDeviceInput.setProperty('tabindex', lastFGTabIndex++);    
	validator.addFieldToList(serialNoSleepDeviceInput);
	
	var datePurchaseLoanInput = newFGTable.getElement('select[name=Date_Purchase_Rented_1]');
	datePurchaseLoanInput.setProperty('selectedIndex', 0);
	datePurchaseLoanInput.setProperty('id', 'Date_Purchase_Rented_' + fgCount);	
	datePurchaseLoanInput.setProperty('name', 'Date_Purchase_Rented_' + fgCount);
	datePurchaseLoanInput.setProperty('tabindex', lastFGTabIndex++);
	validator.addFieldToList(datePurchaseLoanInput);
	
	var monthPurchaseLoanInput = newFGTable.getElement('select[name=Month_Purchase_Rented_1]');	
	monthPurchaseLoanInput.setProperty('selectedIndex', 0);
	monthPurchaseLoanInput.setProperty('id', 'Month_Purchase_Rented_' + fgCount);	
	monthPurchaseLoanInput.setProperty('name', 'Month_Purchase_Rented_' + fgCount);
	monthPurchaseLoanInput.setProperty('tabindex', lastFGTabIndex++);
	validator.addFieldToList(monthPurchaseLoanInput);	

	var yearPurchaseLoanInput = newFGTable.getElement('select[name=Year_Purchase_Rented_1]');	
	yearPurchaseLoanInput.setProperty('selectedIndex', 0);
	yearPurchaseLoanInput.setProperty('id', 'Year_Purchase_Rented_' + fgCount);	
	yearPurchaseLoanInput.setProperty('name', 'Year_Purchase_Rented_' + fgCount);
	yearPurchaseLoanInput.setProperty('tabindex', lastFGTabIndex++);
	validator.addFieldToList(yearPurchaseLoanInput);

	var SleepDeviceOutletTextArea = newFGTable.getElement('textarea[name=SleepDevice_Outlet_Supplier_by_1]');
	SleepDeviceOutletTextArea.setProperty('id', 'SleepDevice_Outlet_Supplier_by_' + fgCount);	
	SleepDeviceOutletTextArea.setProperty('name', 'SleepDevice_Outlet_Supplier_by_' + fgCount);
	SleepDeviceOutletTextArea.setProperty('tabindex', lastFGTabIndex++);
	validator.addFieldToList(SleepDeviceOutletTextArea);
		
	newFGTable.injectAfter('flowgentable_' + (fgCount-1));
	
	modelSelect.focus();
}

