//-----------------------------------------------------------
// Public JS
//-----------------------------------------------------------
var mv = {cs:false};

mv.windowTop	= 0;
mv.windowLeft	= 0;

mv.setDays = function() {
	// Note, this function assumes the combo boxes for month/day have options Month/Day with values 00/00
	var y = dom.$('Year').options[dom.$('Year').selectedIndex].value;
	var m = dom.$('Month').options[dom.$('Month').selectedIndex].value;
	var d;

	// find number of days in current month
	if ( (m == 4) || (m == 6) || (m == 9) || (m == 11) ) {
		days = 30;
	} else if (m == 2) {
		// check for leapyear - Any year divisible by 4, except those divisible by 100 (but NOT 400)
		if ( (Math.floor(y/4) == (y/4)) && ((Math.floor(y/100) != (y/100)) || (Math.floor(y/400) == (y/400))) )
		  days = 29
		else
		  days = 28
	} else {
		days = 31;
	}

	// if (days in new month > current days) then we must add the extra days
	if (days + 1 > dom.$('Day').length) {
		for (i = dom.$('Day').length; i < days + 1; i++) {
			dom.$('Day').length = days + 1;
			dom.$('Day').options[i].text = i;
			dom.$('Day').options[i].value = i;
		}
	}
  
	// if (days in new month < current days) then we must delete the extra days
	if (days + 1 < dom.$('Day').length) {
		dom.$('Day').length = days + 1;
		if (dom.$('Day').selectedIndex == -1) 
			dom.$('Day').selectedIndex = days - 1;
	}

}

mv.CritterSelect = function(critterID,critterColor,itemID,itemColor) {
	dom.$('CritterID').value = critterID;
	dom.$('critterColor').value = avatarColor;
	dom.$('ItemID').value = itemID;
	dom.$('ItemColor').value = itemColor;
}

mv.CreditChore = function(theNode, userID, userChoreID, credit){
	if(confirm('Are you sure you want to apply ' + credit + '% credit for this chore?'))
		location.href='/parent_chore_list/?UserID=' + userID + '&FormAction=CreditChore&UserChoreID=' + userChoreID + '&Credit=' + credit;
	else
		theNode.selectedIndex = 0;
}

mv.ToggleChoreInput = function(theNode){
	dom.$('PostReturn').innerHTML = '';
	if(theNode.options[theNode.selectedIndex].innerHTML == 'Other')
		dom.$('ChoreInput').style.display = 'block';
	else
		dom.$('ChoreInput').style.display = 'none';
}

mv.UpdateField = function(theTable,theField,theID,thePK){
	var xhFile = 'table.update';
	var theValue;

	if(theField == 'Status'){
		theValue	= (dom.$('Status'+theID).innerHTML) == 'Active' ? 'Inactive' : 'Active';
	}else{
		if(theField == 'DateDue'){
			// format date to yyyy-mm-dd
			theValue = dom.$(theField+theID).value;
			var theDate = theValue.substring(6,10) + '-' + theValue.substring(0,2) + '-' + theValue.substring(3,5);
			if(!FutureDate(theDate,true)){
				alert('The date due cannot be a date in the past.');
				return(false);
			}
			theValue = theDate;
		}else{
			theValue	= trim(dom.$(theField+theID).value);
			if(theValue == '')
				theValue = ' ';
		}
	}

	var myVars	= { 
		'IDX'		: xhFile,
		'Table'		: theTable,
		'Field'		: theField,
		'Value'		: theValue,
		'ID'		: theID,
		'PK'		: thePK,
		'Action'	: 'Update'};

	$.post( '/', myVars, function( JSONReturn ) {
		if(JSONReturn.Msg == 'Updated'){
			if(JSONReturn.Field == 'Status'){
				if(dom.$('Status'+JSONReturn.ID).innerHTML == 'Active'){
					dom.$('Status'+JSONReturn.ID).innerHTML = 'Inactive';
				}else{
					dom.$('Status'+JSONReturn.ID).innerHTML = 'Active';
				}
			}else{
				if(trim(dom.$(JSONReturn.Field+JSONReturn.ID).value) == '')
					dom.$(JSONReturn.Field+'Disp'+JSONReturn.ID).innerHTML = '<span style="color: #c0c0c0;">click to add</span>';
				else
					dom.$(JSONReturn.Field+'Disp'+JSONReturn.ID).innerHTML = dom.$(JSONReturn.Field+JSONReturn.ID).value;

				mv.HideUpdateFields(JSONReturn.Field, JSONReturn.ID);
			}
			if(JSONReturn.Field = 'DateDue')
				dom.$('PostReturn').innerHTML = 'Due date updated.';
			else
				dom.$('PostReturn').innerHTML = JSONReturn.Field + ' updated.';
		}else{
			dom.$('PostReturn').innerHTML = JSONReturn.Msg;
		}		
	}, 'json' );
	return (false);
}

mv.HideUpdateFields = function(theField, theID){
	dom.$(theField + 'Disp'+theID).style.display = 'inline';
	dom.$(theField + theID).style.display = 'none';
	dom.$('SaveBtn' + theField + theID ).style.display = 'none';
}

mv.Unload = function(){
	return(false);
/*	if(mv.u != null){
		var myVars={'IDX':'logout', 'UserID' : mv.u};
		xh.postResponse( '/', myVars, null );
		return (false);
	}
*/
}

mv.SetUserID = function(u){
	mv.u = u;
}

mv.CloseWindow = function(){
	window.close();
}

//-----------------------------------------------------------

var PopCal = function(){
	this.HoverClass = 'Hover';

	this.Create = function( Config ){
		var Error = '';

		if( !Config.ButtonImg )
			Error = 'ButtonImg not set.';
		else if( !Config.RelativeParent )
			Error = 'RelativeParent not set.';
		else if( !Config.InputID )
			Error = 'InputID not set.';
		else if( !Config.Callback )
			Error = 'Callback not set.';
		
		if( Error )
			alert( 'Error: '+ Error );
		else{
			var ExistingCalendar = document.getElementById( 'PopCal_'+ Config.InputID );

			if( ExistingCalendar )
				ExistingCalendar.parentNode.removeChild( ExistingCalendar );
			else{
				var CurrentValue = document.getElementById( Config.InputID ).value;
				var QueryString = new Array( 'InputID='+ Config.InputID, 'Value='+ CurrentValue, 'Callback='+ Config.Callback, 'FocusOnValue=TRUE' );

				if( Config.DateFormat )
					QueryString.push( 'DateFormat='+ Config.DateFormat );
				if( Config.DateStart )
					QueryString.push( 'DateStart='+ Config.DateStart );
				if( Config.DateEnd )
					QueryString.push( 'DateEnd='+ Config.DateEnd );
				if( Config.TheField )
					QueryString.push( 'TheField='+ Config.TheField );
				if( Config.TheID )
					QueryString.push( 'TheID='+ Config.TheID );
				var Calendar = document.createElement( 'iframe' );
					Calendar.frameBorder	= 0;
					Calendar.style.width	= ( Config.Width ? Config.Width : '200' ) + 'px';
					Calendar.style.height	= ( Config.Height ? Config.Width : '200' ) + 'px';
					Calendar.style.position	= 'absolute';
					Calendar.style.top		= ( Config.Top ? Config.Top : animation.getBounds( Config.ButtonImg ).top - animation.getBounds( Config.RelativeParent ).top ) +'px';
					Calendar.style.left		= ( Config.Left ? Config.Left : animation.getBounds( Config.ButtonImg ).left - animation.getBounds( Config.RelativeParent ).left ) +'px';
					Calendar.src			= '/pop.calendar/?'+ QueryString.join( '&' );
					Calendar.id				= 'PopCal_'+ Config.InputID;

				Config.ButtonImg.parentNode.appendChild( Calendar );
			}
		}
	}

	this.InsertDate = function( InputID, MyDate ){
		document.getElementById( InputID ).value = MyDate;
		this.Remove( InputID );

//		document.getElementById( 'HasDate_Yes' ).checked = 'selected';
	}

	this.HighlightDate = function( MyTD ){
		MyTD.className = MyTD.className +' '+ Cal.HoverClass;
	}

	this.UnHighlightDate = function( MyTD ){
		var DayClasses = MyTD.className.split( ' ' );

		var Tmp = new Array();
		for( var i = 0; i < DayClasses.length; i++ ){
			if( DayClasses[i] != Cal.HoverClass )
				Tmp.push( DayClasses[i] );
		}
		
		MyTD.className = Tmp.join( ' ' );
	}

	this.Remove = function( InputID ){
		var MyCalendar = document.getElementById( 'PopCal_'+ InputID );
		MyCalendar.parentNode.removeChild( MyCalendar );
	}
}

var Cal = new PopCal();

// Ad Overlay ------------------------------------------[TN]
var GoFish = function() {
	var This = {
		TogglePlayer : function() {
			var Display = ( arguments[0] == 'block' || arguments[0] == 'none' ) ? arguments[0] : ( ( $('.GoFish_Container').html() == '' ) ? 'block' : 'none' );
			if ( Display == 'none') {
				$('.GoFish_Container').empty();
			} else {
				var EmbedHTML = '<div class="GoFish_EmbedWrapper"><a href="#" onclick="GOFISH.TogglePlayer(\'none\'); return(false);" class="CloseGoFish"><span>close</span></a><iframe src="/gofish_embed/" width="1000" height="700" marginheight="0" marginwidth="0" frameborder="0" scrolling="no" allowTransparency="true"></iframe></div>';
				$('.GoFish_Container').append(EmbedHTML);
			}
		}
	};
	return This;
}
var GOFISH = new GoFish();
//------------------------------------------------------[TN]
