/******************************************************************************
* trainingUtils.js                                                            *
* ----------------                                                            *
*                                                                             *
* UTILITY FUNCTIONS FOR COOKIES AND TRAINING CATALOG                          *
*                                                                             *
*******************************************************************************
*                                                                             *
* Copyright 2000-2002 Isotools, all right reserved                            *
*                                                                             *
******************************************************************************/

///////////////////////////////////////////////////////////////////////////////
// 
// COOKIE BASIC FUNCTIONS
//
function setCookie(CookieName, value, expires)
{
	document.cookie = CookieName + "=" + value + "; expires=" + expires.toGMTString() +  "; path=/";
}

function getCookie(CookieName)
{
	var search;
	search = CookieName + "=";
	offset = document.cookie.indexOf(search);
	if (offset != -1)
	{
		offset += search.length;
		end = document.cookie.indexOf(";", offset);
		if (end == -1)
			end = document.cookie.length;
		return document.cookie.substring(offset, end);
	}
	else
	{
		return "";
	}
}

function deleteCookie(CookieName)
{
	//cookieString=getCookie(CookieName);
	var exp = new Date();
	exp.setTime(exp.getTime() + (86400 * 1000 * 30));
	setCookie(CookieName,"", new Date(), exp);
}

function areCookiesEnabled()
{
	if (document.all) // if IE
	{
		if (window.navigator.cookieEnabled)
			return true;
	}
	else
	{
		var strTest = "#this is a test to know if cookie are enabled#";
		var exp = new Date();
		exp.setTime(exp.getTime() + (86400 * 1000 * 30));
		setCookie("testCookieEnabled",strTest,exp);
		var cookieTest = getCookie("testCookieEnabled");
		if (strTest == cookieTest)
			return true;
	}
	return false
}
//
///////////////////////////////////////////////////////////////////////////////

function deleteItem (itemName, itemID, cookieName, bReload)
{
	var confirmDelete = thesaurus.tc_confirmDelete;
	if ( confirm(confirmDelete) )
	{
		var cookie = getCookie(cookieName);
		var tmpCook = '';
		var items = cookie.split('#');
		var i = 0;
		while(i < items.length)
		{
			var tmp = items[i].split('$');
			if ( (tmp[0] != escape(itemID))  )
				tmpCook += items[i]+'#';
		
			i++;
		}
		var exp = new Date();
		exp.setTime(exp.getTime() + (86400 * 1000 * 30));
		setCookie(cookieName,tmpCook, exp);
		
		if (bReload == true)
			window.location.reload();
	}
}

function addItem(cookieName, item)
{
	//test if cookie option is enable
	if (areCookiesEnabled())
	{
		var cookie = getCookie(cookieName);
		var cookieString = item.dumpCookieString();
		cookie += cookieString;
		var exp = new Date();
		exp.setTime(exp.getTime() + (86400 * 1000 * 30));
		setCookie(cookieName,cookie,exp);
//		alert(thesaurus.tc_registerOk);
		return true;
	}
	else
	{
		alert(thesaurus.tc_cookieDisabled);
		return false;
	}
}

function getItemsNumber(cookieName)
{
	var cookie = getCookie(cookieName);
	if(cookie == "") return 0;

	var strItems = cookie.split('#');
	return strItems.length;
}

function getItems(cookieName)
{
	var cookie = getCookie(cookieName);
	var items = new Array();

	if(cookie == "")
		return;

	// get items from basket
	var strItems = cookie.split('#');
	var i;
	for (i=0 ; i<strItems.length ; i++)
	{
		var item = new TrainingRegister();
		if (strItems[i] == "" || strItems[i] == null)
			continue;
		item.makeFromCookieString(strItems[i]);
		items[items.length] = item;
	}

	return items;
}

///////////////////////////////////////////////////////////////////////////////
//
// UTILS FUNCTIONS
//
function isEmpty(s)
{   
	return ((s == null) || (s.length == 0));
}

function isNum(num)
{
	for (var i=0; i<num.length; i++)
	{
		var caractere = num.substring(i,i+1);
		if(caractere < "0" || caractere > "9")
		{
			if (caractere != "." ) return false;
		}
	}
	return true;
}

function round(Num)
{
	strNum = "" + Num;
	
	// round if needed
	if (strNum.indexOf('.') >= 0)
	{
		var parts = strNum.split('.');			
		
		var rightPart = parts[1];
		if (rightPart.length > 2)
		{
			strNum ="" + Math.round(Num*100)/100;
			
			if (strNum.indexOf('.') < 0)
			{
				strNum += ".00";
			}
			else
			{
				parts = strNum.split('.');			
				
				rightPart = parts[1];
				if (rightPart.length == 1)
					strNum += "0";
			}
		}
		else if (rightPart.length == 1)
			strNum += "0";
	}
	else
		strNum += ".00";

	return(strNum)
}

function replace (str, oldsubstr, newsubstr)
{
	newStr = new String(str);

	//var strRegExp = oldsubstr.replace(/[.?\\[\]*$+^(){}]/g, "\\$&");
	//var regExp = new RegExp(strRegExp, "g")
	//var newStr = str.replace(regExp, newsubstr);
	var regExp1 = new RegExp("[.?\\\\[\\]*$+^(){}]", "g");
	var strRegExp = oldsubstr.replace(regExp1, "\\$&");
	
	var regExp2 = new RegExp(strRegExp, "g");
	var strRes = newStr.replace(regExp2, newsubstr);
	
	return strRes;
}

function getPriceTTC(price, duty)
{
	var nPrice = parseFloat(price);
	var nDuty = parseFloat(duty);

	if ( isNaN(nPrice) || isNaN(nDuty))
		return -1;
	else
		return( nPrice*(1+(nDuty/100)) );
}

function getPriceHT(price, duty)
{
	var nPrice = parseFloat(price);
	var nDuty = parseFloat(duty);

	if ( isNaN(nPrice) || isNaN(nDuty))
		return -1;
	else
		return( nPrice/(1+(nDuty/100)) );
}

