/*  .-=====================================================-.  */
/*  |-|  Time include v1.11 for support contact gmccartn  |-|  */
/*  |-|  Created: [14/03/05]   Last Modified: [25/04/05]  |-|  */
/*  |-|  v1.1 to v1.11 =================================  |-|  */
/*  |-|  Added: _?? numeric values to gmt for easier js   |-|  */
/*  |-|  v1.0 to v1.1 ==================================  |-|  */
/*  |-|  Added: Automatic summertime adjustment           |-|  */
/*  |-|  stripZero, findLastSunday and isSummer functions |-|  */
/*  |-|  Requires: =====================================  |-|  */
/*  |-|  [gmt] time object as created with timestamp.     |-|  */
/*  '-=====================================================-'  */

var sDNames=['ddd','day','mmm','month','dd','dom','hh24','hh12','doy','moy','mm','ampm','ss','dow','woy','yy','yyyy','zone'];
/* gmt.UKT is UK time (automatically adjusted for GMT/BST) as a 24Hr decimal format (eg: 07:30=7.5 and 23:15=23.25)          */
var notOpen=[,,,,,,];
var normalOpen=[,'4,13','4,13','4,13','4,13','4,13',];
//var saturdayOpen=[,'7,18','7,18','7,18','7,18','7,18','7,13'];
//var lunchOpen=[,'7,13,14,18','7,13,14,18','7,13,14,18','7,13,14,18','7,13,14,18',];
var normalClosed=[,,,,,,,,,,,];

var defaultValues=[['talk',normalOpen,normalClosed],['chat',normalOpen,normalClosed],['phone',normalOpen,normalClosed]];
/* if a country is not listed in exceptions it follows defaultValues */
var exceptions=[
[['XX_xx'],[['talk',notOpen,normalClosed],['chat',notOpen,normalClosed],['phone',notOpen,normalClosed]]]
];

function serverDate(names,values)
{
	if(names&&values)
	{
		for(var i=0;i<names.length;i++)
		{
			this[names[i]]=(values[i])?values[i]:'';
		}
		this._moy=parseInt(stripZero(this.moy))-1;//return 0-based moy for js
		this._dow=parseInt(this.dow);//standard 0-based dow for js
		this._dom=parseInt(stripZero(this.dom));//standard 1-based dom
		this._hh24=parseInt(stripZero(this.hh24));//numeric 24hr hour
		this._hh12=parseInt(stripZero(this.hh12));//numeric 12hr hour
		this._mm=parseInt(stripZero(this.mm));//numeric minute
		this.dt=(this._hh24+(this._mm/60));
		this.summertime=isSummer(this);
		this.UKT=(this.summertime)?this.dt+1:this.dt;
	}
	else
	{
		this.UKT=0;
		this._dom=-1;
	}
}

function stripZero(str)
{
	return str.replace(/^0/g,'');
}

function findLastSunday(yyyy,moy)
{
	for(var i=31;i>20;i--)
	{
		var then=new Date(yyyy,moy,i);
		if(parseInt(then.getMonth())==moy&&then.getDay()==0)//getDay()==0==Sunday
		{
			return i;
		}
	}
}

function isSummer(date)
{
	var summertime=false;
	var moy=date._moy;
	if(moy>=2&&moy<=9)
	{
		summertime=true;
		if(isIn(date.mmm,['Mar','Oct']))
		{
			var ls=findLastSunday(date.yyyy,moy);
			if((date.mmm=='Mar'&&date._dom<=ls&&date._h24<1)||(date.mmm=='Oct'&&date._dom>=ls&&date._h24>=1)) summertime=false;
		}
	}
	return summertime;
}

function showWhen(what,when,truecode,othercode)
{
	addalert('countryLang='+countryLang);
	if(what=='help')
	{
		return '<br><strong>showWhen(\'help\')=This message</strong><br>showWhen(what,when,truecode,falsecode)<br>what=[\'chat\',\'talk\',\'phone\']<br>when=[\'open\',\'closed\']<br>truecode=return when true<br>falsecode (optional)=return when false<br>';
	}
	else
	{
		var falsecode=(othercode!=undef)?othercode:'';
		var openWhen;
		var closedWhen;
		var foundIt=false;
		var openClosed=defaultValues;
		for(var i=0;i<exceptions.length;i++)
		{
			for(var j=0;j<exceptions[i][0].length;j++)
			{
				if(countryLang==exceptions[i][0][j])
				{
					openClosed=exceptions[i][1];
					foundIt=true;
					break;
				}
			}
			if(foundIt==true) break;
		}
		addalert('foundIt='+foundIt);
		for(var k=0;k<openClosed.length;k++)
		{
			var set=openClosed[k];
			if(!isNaA(set)&&set[0]==what&&set.length==3)
			{
				openWhen=set[1];
				closedWhen=set[2];
				var dow=gmt._dow;
				var moy=gmt._moy;
				addalert('dow='+dow);
				addalert('moy='+moy);
				var o=(openWhen[dow]!=undef)?openWhen[dow]:'';
				var c=(closedWhen[moy]!=undef)?closedWhen[moy]:'';
				addalert('o='+o+'\nc='+c+'\nisOpen(o,c)='+isOpen(o,c));
				return ((isOpen(o,c)==when)?truecode:falsecode);
			}
		}
		return '<!--Nothing-->';
	}
}

function isOpen(openDay,closedMonth)
{
	var hours=openDay.split(',');
	var days=closedMonth.split(',');
	addalert('!isIn(gmt._dom,days)='+(!isIn(gmt._dom,days)))
	if(!isIn(gmt._dom,days))
	{
		for(var i=0;i<(hours.length-1);i+=2)
		{
			addalert(gmt.UKT+'>='+parseFloat(hours[i]));
			addalert(gmt.UKT+'<='+parseFloat(hours[i+1]));
			if(gmt.UKT>=parseFloat(hours[i])&&gmt.UKT<=parseFloat(hours[i+1])) return 'open';
		}
		return 'closed';
	}
	else
	{
		return 'closed';
	}
	return '';
}

function displayObj(theObj)
{
	var t=returnObj(theObj,'\n');
	alert(t);
}

function returnObj(theObj,nl)
{
	if(!nl) nl='\n';
	var t="";
	for(var i in theObj)
	{
		t+=i+'='+theObj[i]+nl;
	}
	return t;
}

function isIn(theValue,theList)
{
	if(isNaA(theValue))
	{
		return isIt(theValue,theList);
	}
	else
	{
		for(var i=0;i<theValue.length;i++)
		{
			var ret=isIt(theValue[i],theList);
			if(ret) return true;
		}
	}
	return false;
}

function isIt(theValue,theList)
{
	if(isNaA(theList))
	{
		if(theValue==theList) return true;
	}
	else
	{
		for(var i=0;i<theList.length;i++)
		{
			if(theValue==theList[i]) return true;
		}
	}
	return false;
}

function isNaA(a)
{
	return !((typeof(a)=='object')&&(!isNaN(a.length)));
}

function whereIs(v,l)
{
	if(!isNaA(l))
	{
		for(var i=0;i<l.length;i++)
		{
			if(v==l[i]) return i;
		}
	}
	else
	{
		errors+=l+' is not an array\n';
	}
	return -1;
}

var gmt=new serverDate(sDNames,GMTValues);