/**************************************************************************************************
***************************************************************************************************

f_data.js

Written by Chris Harding and Joshua McKinney
© Copyright F1 Solutions Pty Ltd 2003

This file holds the data manipulation code used by all pages.

Production Version 2.0 - June 2007

***************************************************************************************************
**************************************************************************************************/

// do the check to see if we can
// save to the windows folder
CheckDataFolder();


/* Function: deleteData()                                  */
/* This function deletes both the cookies and data file    */
/* associated with the DGRR.                               */
/* INPUTS : none                                           */
/* OUTPUTS: none                                           */
function deleteData() {
	deleteCookie();
	deleteFile();
}



/* Function: CheckDataFolder()                             */
/* This function checks to see if we can open and write    */
/* some data to the same location that the data file is    */
/* saved to.                                               */
/* INPUTS : none                                           */
/* OUTPUTS: none                                           */
function CheckDataFolder() {
	// include a check as to whether cookies can be persisted on the client
}




/* Function: saveFile()                                    */
/* This function saves all of the data from the global     */
/* variables into the data file.                           */
/* INPUTS : none                                           */
/* OUTPUTS: none                                           */
function saveFile() {
	//saveFileNormal();
	saveCookies();
}

function saveCookies() {
	setCookie('dateTime', getDateTime());
	setCookie('ePremiseOccupier', escape(aPremiseData[ePremiseOccupier]));
	setCookie('ePremiseABN', escape(aPremiseData[ePremiseABN]));
	setCookie('ePremiseAddress', escape(aPremiseData[ePremiseAddress]));
	setCookie('ePremisePropertyLocation', escape(aPremiseData[ePremisePropertyLocation]));
	setCookie('ePremiseContactPersonAndPhone1', escape(aPremiseData[ePremiseContactPerson1]) + "," + escape(aPremiseData[ePremiseContactPhone1]));
	setCookie('ePremiseContactPersonAndPhone2', escape(aPremiseData[ePremiseContactPerson2]) + "," + escape(aPremiseData[ePremiseContactPhone2]));
	var aTempPremises = new Array(numPremises);
	for(storage_idx=0; storage_idx<numPremises; storage_idx++) {
		aTempPremises[storage_idx] = escape(aPremises[storage_idx]);
	}
	setCookie('aTempPremises', aTempPremises.join());		
	for(goods_idx=0; goods_idx < aGoods.length; goods_idx++) {
		if(aGoods[goods_idx] == null) {
			setCookie('aTempGood' + goods_idx, gstrNewGood);
		} else {
			var aTempGood = new Array(intGoodFC);
			aTempGood[eGAName] = escape(aGoods[goods_idx][eGAName]);
			aTempGood[eGAUN] = escape(aGoods[goods_idx][eGAUN]);
			aTempGood[eGAClass] = escape(aGoods[goods_idx][eGAClass]);
			aTempGood[eGASubrisk1] = escape(aGoods[goods_idx][eGASubrisk1]);
			aTempGood[eGASubrisk2] = escape(aGoods[goods_idx][eGASubrisk2]);
			aTempGood[eGAPackingGroup] = escape(aGoods[goods_idx][eGAPackingGroup]);
			aTempGood[eGAC1Isolated] = escape(aGoods[goods_idx][eGAC1Isolated]);
			aTempGood[eGAStorageArea] = escape(aGoods[goods_idx][eGAStorageArea]);
			aTempGood[eGAStorageType] = escape(aGoods[goods_idx][eGAStorageType]);
			aTempGood[eGAQuantity] = escape(aGoods[goods_idx][eGAQuantity]);
			aTempGood[eGAExempt] = escape(aGoods[goods_idx][eGAExempt]);
			aTempGood[eGACapacity] = escape(aGoods[goods_idx][eGACapacity]);
			aTempGood[eGAContainerID] = escape(aGoods[goods_idx][eGAContainerID]);
			aTempGood[eGAHazchem] = escape(aGoods[goods_idx][eGAHazchem]);
			aTempGood[eGAShippingName] = escape(aGoods[goods_idx][eGAShippingName]);
			setCookie('aTempGood' + goods_idx, aTempGood.join());
		}
	}
}


/* Function: loadFile()                                    */
/* This function loads all the data from the file into the */
/* global variable arrays.                                 */
/* INPUTS : none                                           */
/* OUTPUTS: none                                           */
function loadFile() {
	//loadFileNormal();
	loadFileFromCookies();
}

function loadFromCookie(key) {
	return render(unescape(getCookie(key)));
}

function render(value) {
	if (value == null || value == 'null' || value == 'undefined') {
		value = '';
	}
	return value;	
}

function loadFileFromCookies() {	
	var fso, tf, strTemp = "", aTemp, aTempPremises, intGoodIndex=0;
	aPremiseData = new Array(iPremiseFC);
	aPremises = new Array();
	aGoods =  new Array();
	gstrLastUpdated = "Never";
	var test = loadFromCookie('ePremiseOccupier');//dateTime');// last updated
	if (test.length > 0) {
		gblnIsData = true; // we have data
		gstrLastUpdated = loadFromCookie('dateTime');		
		aPremiseData[ePremiseOccupier] = loadFromCookie('ePremiseOccupier');// occupier name
		aPremiseData[ePremiseABN] = loadFromCookie('ePremiseABN');// ABN
		aPremiseData[ePremiseAddress] = loadFromCookie('ePremiseAddress');// address
		aPremiseData[ePremisePropertyLocation] = loadFromCookie('ePremisePropertyLocation');// real property
		aTemp = getCookie('ePremiseContactPersonAndPhone1').split(",");// contact 1
		aPremiseData[ePremiseContactPerson1] = render(unescape(aTemp[0]));
		aPremiseData[ePremiseContactPhone1] = render(unescape(aTemp[1]));
		aTemp = getCookie('ePremiseContactPersonAndPhone2').split(",");// contact 2
		aPremiseData[ePremiseContactPerson2] = render(unescape(aTemp[0]));
		aPremiseData[ePremiseContactPhone2] = render(unescape(aTemp[1]));
		aTempPremises = loadFromCookie('aTempPremises');
		aTempPremises = aTempPremises.split(",");// storage areas
		for (storage_idx=0; storage_idx < aTempPremises.length; storage_idx++) {
			aPremises[storage_idx] = render(unescape(aTempPremises[storage_idx]));
		}
		var key = 'aTempGood' + intGoodIndex;
		strTemp = getCookie(key);// goods
		while (strTemp != null) {
			if (strTemp.length == 0) {
				aGoods[intGoodIndex] = null;
			} else {	
				aTempGood = new Array(intGoodFC);				
				aTempGood=strTemp.split(",");
				aTempGood[eGAName] = unescape(aTempGood[eGAName]);
				aTempGood[eGAUN] = unescape(aTempGood[eGAUN]);
				aTempGood[eGAClass] = unescape(aTempGood[eGAClass]);
				aTempGood[eGASubrisk1] = unescape(aTempGood[eGASubrisk1]);
				aTempGood[eGASubrisk2] = unescape(aTempGood[eGASubrisk2]);
				aTempGood[eGAPackingGroup] = unescape(aTempGood[eGAPackingGroup]);
				aTempGood[eGAC1Isolated] = unescape(aTempGood[eGAC1Isolated]);
				aTempGood[eGAStorageArea] = unescape(aTempGood[eGAStorageArea]);
				aTempGood[eGAStorageType] = unescape(aTempGood[eGAStorageType]);
				aTempGood[eGAQuantity] = unescape(aTempGood[eGAQuantity]);
				aTempGood[eGAExempt] = unescape(aTempGood[eGAExempt]);
				aTempGood[eGACapacity] = unescape(aTempGood[eGACapacity]);
				aTempGood[eGAContainerID] = unescape(aTempGood[eGAContainerID]);
				aTempGood[eGAHazchem] = unescape(aTempGood[eGAHazchem]);
				aTempGood[eGAShippingName] = unescape(aTempGood[eGAShippingName]);
				aGoods[intGoodIndex] = aTempGood;		
				intGoodIndex++;
				strTemp = getCookie('aTempGood' + intGoodIndex);
			}
		}
	} else {
		gstrLastUpdated="Never";
	}	
	numPremises = (aPremises.length==0 ? 1 : aPremises.length);
	if (intGoodIndex == 0) {
		aGoods[0] = null;
	}	
	numGoods = aGoods.length;
}


/* Function: GetPath(pstrSpecialFolderName)                */
/* This function returns the full path of the windows      */
/* special folder specified by pstrSpecialFolderName       */
/* INPUTS : string                                         */
/* OUTPUTS: string                                         */
function GetPath(pstrSpecialFolderName) {
	// old active X
}




/* Function: deleteFile()                                  */
/* This function deletes the data file from the DGRR.      */
/* INPUTS : none                                           */
/* OUTPUTS: none                                           */
function deleteFile() {
	// used to delete via active X
}




/* Function: isData()                                      */
/* This function returns true if there is data in the DGRR */
/* or false otherwise.                                     */
/* INPUTS : none                                           */
/* OUTPUTS: none                                           */
function isData() {
	return gblnIsData;
}



function saveFileNormal() {	
	// old active X
}


function loadFileNormal() {	
	// old active X	
}
