/*
 *+------------------------------------------------------------------------+
 *| Licensed Materials - Property of IBM
 *| BI and PM: prmt
 *| (C) Copyright IBM Corp. 2002, 2013
 *|
 *| US Government Users Restricted Rights - Use, duplication or
 *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *|
 *+------------------------------------------------------------------------+
*/


/**
	This script is used to provide interactivity for the textBox prompt control
	@private
	@class
*/
cognos.Prompt.Control.SelectHTML5Date = cognos.Prompt.Control.f_extend({

	f_initialize: function( v_oProps ) {
		this[K_PRMT_LABEL_RANGE_LOWEST] = PMT_RNG_EARLIEST_DATE;
		this[K_PRMT_LABEL_RANGE_HIGHEST] = PMT_RNG_LATEST_DATE;

		this._type_ = "cognos.Prompt.Control.SelectHTML5Date";
		this.f_parent( v_oProps ); // call parent's initialize()

		this.m_bDisabled = false;

		this.f_initCompleted();
	},

	//validate the input into the control
	checkData: function()
	{
		// We keep a reference to the parent function
		// Calling this.m_oFrom functions seems to have a side effect on this.f_parent().
		var v_fnParent = this.f_parent;
		if ( ( this.isRequired() || !(this.m_bDisabled) ) && !(this.m_oDateHelper.f_checkDate( this.m_elPrompt.value )) ) {
			this.m_bValid = false;			
		} else {
			this.m_bValid = true;
		}
		this.f_parent = v_fnParent; // call parent's checkData using saved reference.
		this.f_parent(); // call parent's checkData using saved reference.
		return this.m_bValid;
	},

	clearValues: function() {
		this.f_parent();
		this.f_clear();
	},

	f_drawCompleted: function()	{
		var v_fnParent = this.f_parent;

		this.m_oInputCopy = $( this.f_getId( "_Date" ) );

		var v_iStartOfWeek = 0;
		switch( this["@startOfWeek"] )
		{
			case "Monday": iStartDay = 1; break;
			case "Tuesday": iStartDay = 2; break;
			case "Wednesday": iStartDay = 3; break;
			case "Thursday": iStartDay = 4; break;
			case "Friday": iStartDay = 5; break;
			case "Saturday": iStartDay = 6; break;
		}
		
		//oForm, oEditBox, sRef, sDefaultDate, iType, sInputOrder, iStartOfWeek, iDateTimeType, 
		// sFirstDate, sLastDate, bRequired, sCVId
		var v_oDateHelper = new PRMT_DateHelper (
				$( this.f_getId( "_Date" ) ), //oForm,
				$( this.f_getId( "txtDate" )), //oEditBox,
				this.f_getId(), //sRef,
				this["@defaultDate"], //sDefaultDate,
				(this["@calendar"] == "Imperial" ? 1 : 0), //iType,
				this["@inputOrder"], //sInputOrder,
				v_iStartOfWeek,
				(this["@DateTime"] ? 0 : 1), //iDateTimeType,
				this["@firstDate"], //sFirstDate,
				this["@lastDate"], //sLastDate,
				this.isRequired() //bRequired
			);

		this.m_oDateHelper = v_oDateHelper;
		var v_oInput = $( this.f_getId("txtDate") );
		v_oInput.m_oPrompt = this;
		this.m_elPrompt = v_oInput;
		
		this.m_oForm = $(this.f_getId("_Date"));
		
		//indicate whether the control has valid data
		this.m_bValid = false;
		
		var v_currentDate = v_oDateHelper.getCurrentDate();
		this.f_setObjectDate(v_currentDate);

		//PRMTUtils.f_addEvent( v_oInput, "input", this.checkData.bind(this) );
		PRMTUtils.f_addEvent( v_oInput, "blur", this.checkData.bind(this) );
		
		this.checkData();

		this.f_parent = v_fnParent; // call parent's f_drawCompleted using saved reference.
		this.f_parent(); // call parent's f_drawCompleted using saved reference.
	}
});

/**
@private
@param {C_PromptElement} v_el Container.
@return {void}
*/
cognos.Prompt.Control.SelectHTML5Date.prototype.f_drawInput = function( v_el )
{
	$CE("input", {"type": "hidden", "id": this.f_getId("_Date"), "value": this["@defaultDate"]}, v_el );
	
	var v_tbl = $CE( "table", { "border": K_PRMT_DEBUG_TABLEBORDER, "cellPadding": 0, "cellSpacing": 0, "role":K_PRMT_ARIA_ROLE_PRESENTATION}, v_el );
	var v_tbd = $CE( "tbody", {}, v_tbl );
	var v_tr = $CE( "tr", {}, v_tbd );
	var v_td = $CE( "td", {}, v_tr );
	
	// selectDateEditBoxStyle
	var v_oP = {
			"type": "date",
			"class": "clsHTML5Input",
			"id": this.f_getId("txtDate"),
			"value": K_PRMT_sEMPTY
	};	
	
	// title for WCAG 2.0
	if ( this["@title"] ) {
		v_oP.title = this["@title"];
	}
	
	if ( this.isRequired() ) {
		v_oP["aria-required"] = "true";
	}
	
	if ( this["@firstDate"] ) {
		v_oP["min"] = this["@firstDate"];
	}
	
	if ( this["@lastDate"] ) {
		v_oP["max"] = this["@lastDate"];
	}
	
	var v_oInput = $CE( "input", v_oP, v_td );
	
	return v_oInput;
};

/**
@private
@return {void}
*/
cognos.Prompt.Control.SelectHTML5Date.prototype.f_clear = function( )
{
	this.m_elPrompt.value = "";
};

/**
@private
@return {void}
*/
cognos.Prompt.Control.SelectHTML5Date.prototype.f_setDate = function(/* String */ v_sDate )
{
	this.f_setObjectDate(dParseDate(v_sDate, "YMD"));
};

/**
@private
@return {void}
*/
cognos.Prompt.Control.SelectHTML5Date.prototype.f_setObjectDate = function( v_sDate )
{
	var v_oInput = this.m_elPrompt;
	v_oInput.value = this.m_oDateHelper.f_formatDateYMD(v_sDate);
};

// aux methods for DateTime
cognos.Prompt.Control.SelectHTML5Date.prototype.getCalendarType = function()  { return this.m_oDateHelper.m_iDateTimeType; };
cognos.Prompt.Control.SelectHTML5Date.prototype.getInputOrder = function()  { return this.m_oDateHelper.m_sInputOrder; };

/**
 * Return as Date javascript object
 * 
 * @private
 * @return {cognos.Value[]}
 */
cognos.Prompt.Control.SelectHTML5Date.prototype.f_getValueAsDate = function()
{
	var result = dParseDate(this.m_elPrompt.value,"YMD");
	return result;
};

cognos.Prompt.Control.SelectHTML5Date.prototype.f_getUseValue = function()
{
	return this.m_elPrompt.value;
};

cognos.Prompt.Control.SelectHTML5Date.prototype.f_getDisplayValue = function()
{
	return this.m_oDateHelper.f_formatDateCurrentLocale(this.f_getValueAsDate());
};

/**
@private
@return {cognos.Value[]}
*/
cognos.Prompt.Control.SelectHTML5Date.prototype.f_getPV = function()
{
	var v_oPV = null;
	this.m_oDateHelper.f_checkDate(this.m_elPrompt.value);

	var v_oDate = this.f_getValueAsDate();
	
	var v_sUse = this.m_oDateHelper.f_formatSubmitDate(v_oDate);
	if ( this.m_bIsRangeEndControl && this["@DateTime"]) {
		v_sUse = v_sUse.replace( K_PRMT_reTIMEPART, K_PRMT_sTIME_MAX );
	}
	
	return (
		this.m_bDisabled || !v_sUse ?
			null :
			{ "use": v_sUse, "display": this.m_oDateHelper.f_formatDateCurrentLocale(v_oDate) }
	);
};

/**
@private
@return {cognos.Value[]}
*/
cognos.Prompt.Control.SelectHTML5Date.prototype.f_setPV = function(v_oPV)
{
	var v_sValue = v_oPV["use"];
	if ( v_sValue )
	{
		var v_aValues = v_sValue.split(new RegExp("[T\\s]"));

		if ( v_aValues.length > 2 )
		{
			for ( var v_idx = 1; v_idx < v_aValues.length - 1; v_idx++ )
			{
				// add all Date parts to the first element.
				v_aValues[0] += K_PRMT_sSP + (v_aValues[v_idx]).f_trim();
			}
			v_aValues[0] = v_aValues[0].f_trim();

			// move Time part (last value) in slot 1
			v_aValues[1] = v_aValues[ v_aValues.length - 1];
		}
		if ( v_aValues.length >= 1 )
		{
			this.f_setObjectDate( dParseDate(v_aValues[0], "YMD") );
		}
	}
};

/**
	Compare values. See {@link cognos.Prompt.Control#f_compare} for details.
	@see cognos.Prompt.Control#f_compare
	@private
	@return {Integer}
*/
cognos.Prompt.Control.SelectHTML5Date.prototype.f_compare = function( v_oValue )
{
	var v_iRetval = 1;
	if ( v_oValue )
	{
		// Get Date objects
		var v_dThis = this.f_getValueAsDate();
		var v_dCompareTo = v_oValue.f_getValueAsDate();

		var v_iThis = v_dThis.getTime();
		var v_iCompareTo = v_dCompareTo.getTime();

		if ( v_iCompareTo > v_iThis )
		{
			v_iRetval = -1;
		}
		else if ( v_iCompareTo == v_iThis )
		{
			v_iRetval = 0;
		}
	}
	return v_iRetval;
};


/**
	Update UI when control is losing the focus.
	@private
	@return {void}
*/
cognos.Prompt.Control.SelectHTML5Date.prototype.f_lostFocus = function()
{
	this.checkData();
};

/**
	@private
	@return {void}
*/
cognos.Prompt.Control.SelectHTML5Date.prototype.f_setEnabled = function( v_bState )
{
	this.m_bDisabled = !v_bState;
	if (v_bState) {
		this.m_elPrompt.removeAttribute("disabled");
	} else {
		this.m_elPrompt.setAttribute("disabled",true);
	}
};

/**
Update UI when control is losing the focus.
@private
@return {void}
*/
cognos.Prompt.Control.SelectHTML5Date.prototype.f_toggleDisable = function()
{
	var v_bState = this.m_bDisabled;
	this.f_setEnabled( v_bState );
	this.m_bDisabled = !v_bState;
};



/**
	Check if is a reprompt(this.c exists) and in case of a reprompt if the reprompt has data(a date)
	When a date control is optional, has a Calendar UI the user can decide not to submit the date, in
	that case the reprompt won't contain data.
	@private
	@return {boolean}
*/
cognos.Prompt.Control.SelectHTML5Date.prototype.isEmptyReprompt = function()
{
	var result =  false;
	if ((typeof this.c) != K_PRMT_sUNDEFINED) {
		// it's a reprompt
		var v_c = this.c;
		// [{n:'selectChoices',c:[{n:'selectOption',"@useValue":"2010-01-14","@displayValue":"Jan 14, 2010"}]}]  => not empty(value.n =='selectOption' || [{n:'selectChoices'}] => empty(value.n == 'selectChoices')
		if ( v_c.length && v_c[0].n && v_c[0].n == "selectChoices") {
			var v_c_value = cognos.Value.getValue( this.c );
			if (v_c_value.n && v_c_value.n == "selectChoices") result = true;
		}
	}
	return result;
};	

/**
@private
@return {boolean}
*/
cognos.Prompt.Control.SelectHTML5Date.prototype.hasValue = function()
{
	return (this.m_bValid);
};