/*******************************************************************************
Author : Roger D. Hosto Jr.
Version : Version 1.1 - Nov 17, 2006
*******************************************************************************s
Notes :
1.2 Added the Server Date Time
1.1 Added the ability to have multiples
*******************************************************************************
*******************************************************************************
Usage :
*******************************************************************************
*******************************************************************************/
/*
 * ONLY EDIT ANYTHING BELOW HERE IF YOU TRUELY KNOW WHAT YOU ARE DOING
 */
function ajaxCounter( name ){
	//Public Variables
	this.setname=name;
	this.divClass=null;
	this.divClassDate=null;
	this.timexml="/timexml?";
	this.CounterMessage="[counter]";
	//Private Variables
	var LongMonths = new Array( "January","February","March","April","May","June","July","August","September","October","November","December" );
	var ShortMonths = new Array( "Jan.","Feb.","Mar.","Apr.","May","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec." );
	var LongDays = new Array( "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" );
	var ShortDays = new Array( "Sun.","Mon.","Tue.","Wed.","Thu.","Fri.","Sat." );
	var Hours12 = new Array( "12","1","2","3","4","5","6","7","8","9","10","11","12","1","2","3","4","6","7","8","9","10","11" );
	var XMLFile = null;
	var StartDate = null;
	var xmlData = null;
	var Endtime = 0;
	var CurrentTime = 0;
	var that = this;
	//init();
	this.init = function() {
		if( StartDate == "24HOURS" ) {
			getXMLPage( this.timexml+Math.random() );
		}else if( StartDate ) {
			getXMLPage( this.timexml+StartDate+"&"+Math.random() );
		} else if( XMLFile ) {
			getXMLPage( XMLFile );
		}
	}//end init();
	//addStartXml
	this.addStartXML = function( file ) {
		if( ! StartDate ) {
			XMLFile = file;
		} else {
			alert( "Error: Your all ready a have a start date added" );
		}
	}// end addStartXml();
	//addDate( MM-DD-YEAR, Number )
	this.addDate = function( date, days ) {
		if( date && days ) {
			StartDate = date+","+days;
		} else if( date ) {
			StartDate = date
		}
	}// end addDate
	//countdown;
	this.countdown = function() {
		var dd=( new Date(EndTime) ) - ( new Date(CurrentTime) );
		var dday=Math.floor(dd/(60*60*1000*24)*1);
		var dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1);
		var dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
		var dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);
		if( dsec < 10 )
			dsec = "0"+dsec;
		if( dmin < 10 )
			dmin = "0"+dmin;
		if(dday<=0&&dhour<=0&&dmin<=0&&dsec<=1){
			window.location.reload();
			return;
		} else {
			var txt = "";
			if( dday > 0 ) {
				txt += dday+" days ";
			}
			if( dhour > 0 ) {
				if( dhour < 10 && dday > 0 ) {
					txt += "0"+dhour+" hours ";
				} else {
					txt += dhour+" hours ";
				}
			}
			txt += dmin+" minutes "+dsec+" seconds ";
			var regex = new RegExp( "\\[counter\\]", "g" );
			var textMessage = this.CounterMessage.replace( regex, txt );
			if( this.divClass ) {
				var divas = document.getElementsByTagName( 'div' );
				for( var x = 0; x < divas.length; x++ ){
					if( divas[x].className == this.divClass ) {
						divas[x].innerHTML = textMessage;
					}
				}
			} else {
				document.getElementById( "countdown" ).innerHTML = textMessage;
			}
		}
		CurrentTime = CurrentTime + 1000;
		this.TimerID=setTimeout( this.setname+".countdown("+CurrentTime+")",1000);
	}// end countdown();
	//DateTime();
	this.DateTime = function(){
		if( this.divClass ) {
			var date = new Date( CurrentTime );
			var divas = document.getElementsByTagName( 'div' );
			for( var x = 0; x < divas.length; x++ ){
				if( divas[x].className == this.divClassDate ) {
					var textMessage = divas[x].innerHTML;
					var regex = new RegExp( "<!--", "g" );
					textMessage = textMessage.replace( regex, "" );
					regex = new RegExp( "-->", "g" );
					textMessage = textMessage.replace( regex, "" );
					regex = new RegExp( "\\[YEAR\\]", "g" );
					textMessage = textMessage.replace( regex, ( date.getYear() < 1900 )?(date.getYear() + 1900):date.getYear() );
					regex = new RegExp( "\\[LONGMONTH\\]", "g" );
					textMessage = textMessage.replace( regex, LongMonths[ date.getMonth() ] );
					regex = new RegExp( "\\[SHORTMONTH\\]", "g" );
					textMessage = textMessage.replace( regex, ShortMonths[ date.getMonth() ] );
					regex = new RegExp( "\\[MONTHNUM\\]", "g" );
					textMessage = textMessage.replace( regex, ( date.getMonth() + 1 ) );
					regex = new RegExp( "\\[SHORTDAY\\]", "g" );
					textMessage = textMessage.replace( regex, ShortDays[ date.getDay() ] );
					regex = new RegExp( "\\[LONGDAY\\]", "g" );
					textMessage = textMessage.replace( regex, LongDays[ date.getDay() ] );
					regex = new RegExp( "\\[DAYNUM\\]", "g" );
					textMessage = textMessage.replace( regex, date.getDate() );
					regex = new RegExp( "\\[HOURS24\\]", "g" );
					textMessage = textMessage.replace( regex, date.getHours() );
					regex = new RegExp( "\\[HOURS12\\]", "g" );
					textMessage = textMessage.replace( regex, Hours12[ date.getHours() ] );
					regex = new RegExp( "\\[MINUTES\\]", "g" );
					textMessage = textMessage.replace( regex, ((date.getMinutes() < 10)?"0"+date.getMinutes():date.getMinutes()) );
					divas[x].innerHTML = textMessage;
				}
			}
		}
	}// end DateTime();
	//getXMLpage();
	function getXMLPage( url ) {
		//Try to on Browser that us the XMLHttpRequest Object ( eg Firefox, Opera )
		if ( window.XMLHttpRequest ) {
			try {
				xmlhttp= new XMLHttpRequest();
			} catch( e ) {
				xmlhttp = false;
				//alert( "Error " + e );
			}
		//Try to on Browser that us theActiveObject ( eg Internet Explorer )
		} else if ( window.ActiveXObject ) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch( e ) {
				try {
					xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				} catch( e ) {
					xmlhttp = false;
					//alert( "Error " + e );
				}
			}
		}
		if( xmlhttp ) {
			//Make sure that you set the onreadystatechange before you do the open send request
			//it needs this to finish the processes.
			xmlhttp.onreadystatechange = processReqChange;
			xmlhttp.open( "GET", url, true);
			xmlhttp.send(null);
		}
	}//end getXMLpage();
	//processReqChange()
	function processReqChange() {
		if( xmlhttp.readyState == 4 ) {
			var Data = xmlhttp.responseXML.childNodes;
			for( var i = 0; i < Data.length; i++ ) {
				for(var x = 0; x < Data[i].childNodes.length; x++ ) {
					if( Data[i].childNodes[x].nodeType != 1 ) continue;
					if( Data[i].childNodes[x].nodeName == "CurrentTime" ){
						CurrentTime = parseInt(Data[i].childNodes[x].firstChild.nodeValue)*1000;
					}
					if( Data[i].childNodes[x].nodeName == "EndTime" ){
						EndTime = parseInt(Data[i].childNodes[x].firstChild.nodeValue)*1000;
					}
					if( Data[i].childNodes[x].nodeName == "StartDate" ) {
						StartDate = Data[i].childNodes[x].firstChild.nodeValue;
					}
				}
			}
			if( CurrentTime && EndTime ) {
				var name = eval( that.setname );
				startProcessing();
			} else if( StartDate ) {
				getXMLPage( that.timexml+StartDate+"&"+Math.random() );
			}
		}
	}//end processReqChange()
	//startProcessing()
	function startProcessing() {
		var name = eval( that.setname );
		if( that.divClassDate ) {
			name.DateTime();
		}
		name.countdown();
	}//end startProcessing()
}

