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


/*
	CIntervalPickerIE5.js

	This script is used to provide interactivity for the selectTime prompt control
*/

//Constructor to create a selectTime component
//	oSubmit: the field used to submit values to the server
//	oForm: the field used to store processed user input
//	oDays: hours form control
//	oHours: hours form control
//	oMinutes: minutes form control
//	oSeconds: seconds form control, null if not shown
//	oMilliseconds: milliseconds form control, null if not shown
//	oImgTest: error feedback image object
//	sRef: the name of this object
//	sDefaultValue: the initial value for the control
//	bRequired: boolean, specify if user input is required
//	sSubmitType: 'default' will submit as a standard form
//				'XML' will convert the submission to XML and submit
//	bShowSeconds: true/false, determine whether to show seconds edit box
//	bShowMilliseconds: true/false, determine whether to show Milliseconds edit box
//	bAllowNegative: true/false, some intervals cannot be negative

function CIntervalPicker(oSubmit, oForm, oDays, oHours, oMinutes, oSeconds, oMilliseconds, oImgTest, sRef, sDefaultValue, bRequired, sSubmitType, bShowSeconds, bShowMilliseconds, bAllowNegative, sCVId)
{
	this.setCVId(sCVId);
	//get references to form controls
	this.m_oSubmit = oSubmit;
	this.m_oForm = oForm;
	this.m_oDays = oDays;
	this.m_oHours = oHours;
	this.m_oMinutes = oMinutes;
	this.m_bShowSeconds = bShowSeconds;
	this.m_bShowMilliseconds = bShowMilliseconds;

	//handle negative values
	this.m_bNegative = false;

	//create regular expressions for parsing with current locale
	initIntervalRegularExpressions();

	//bShowSeconds
	if ((this.m_bShowSeconds == true) || (this.m_bShowMilliseconds == true))
	{
		this.m_oSeconds = oSeconds;
	}
	else
	{
		this.m_oSeconds = null;
	}

	//bShowMilliseconds
	if (this.m_bShowMilliseconds == true)
	{
		this.m_oMilliseconds = oMilliseconds;
	}
	else
	{
		this.m_oMilliseconds = null;
	}

	//specify whether the user must enter a date or not
	this.m_bRequired = bRequired;

	this.m_bDisabled = false;

	//indicate whether the control has valid data
	this.m_bValid = false;

	//submit as XML or as a standard html form
	this.m_sSubmitType = sSubmitType;

	//can the user type a negative interval?
	this.m_bAllowNegative = bAllowNegative ? bAllowNegative : false;

	//define default interval
	var sDefaultTime = sDefaultValue ? sDefaultValue : K_PRMT_sEMPTY;

	//check to see if this is an XML schema duration data type
	if (sDefaultTime.indexOf ('P') != -1)
	{
		this.parseXSDIntervalValue(sDefaultTime);
	}
	//check to see if this is a UDA format interval
	else
	{
		//search for a negative value
		var z = sDefaultTime.search(rMinusSign);
		if (z == -1)
		{
			this.m_bNegative = false;
		}
		else
		{
			this.m_bNegative = true;
			//strip of the minus sign
			sDefaultTime = sDefaultTime.replace(rMinusSign, K_PRMT_sEMPTY);
		}

		var arDefaultTime = sDefaultTime.split(/[:.\s]/g);
		//do we have a valid date?

		switch (arDefaultTime.length)
		{
			case 5:
				//set default
				this.m_iDays = arDefaultTime[0];
				this.m_iHours = arDefaultTime[1];
				this.m_iMinutes = arDefaultTime[2];
				this.m_iSeconds = arDefaultTime[3];
				this.m_iMilliseconds = arDefaultTime[4];
				break;

			default:
				//no valid default set
				if (this.m_bRequired == true)
				{
					this.m_iDays = 0;
					this.m_iHours = 0;
					this.m_iMinutes = 0;
					this.m_iSeconds = 0;
					this.m_iMilliseconds = 0;
				}
				else
				{
					this.m_iDays = null;
					this.m_iHours = null;
					this.m_iMinutes = null;
					this.m_iSeconds = null;
					this.m_iMilliseconds = null;
				}
		}
	}

	//skin folder
	this.m_sSkin = (typeof getPromptSkin != K_PRMT_sUNDEFINED ? getPromptSkin() : K_PRMT_sDEFAULTSKIN);

	//images for handling errors
	this.m_oImgCheckDate = oImgTest;
	if (this.m_oImgCheckDate != null)
	{
		this.m_oImgErrorFalse= new Image();
		this.m_oImgErrorFalse.src = this.m_sSkin + "/prompting/images/error_timed_small_off.gif";
		this.m_oImgErrorTrue = new Image();
		this.m_oImgErrorTrue.src = this.m_sSkin + "/prompting/images/error_timed_small.gif";
	}

	//create a pointer to this function
	this.m_sRef = sRef;
	this.draw();
	this.updateFormField();
	this.checkInterval();
}

CIntervalPicker.prototype = new CPromptControl();

function CIntervalPicker_draw()
{
	var sDayVal = (this.m_iDays != null) ? this.m_iDays : (this.m_bRequired == true) ? "0" : K_PRMT_sEMPTY;

	if (this.m_bNegative == true)
	{
		this.m_oDays.value = "-" + sDayVal;
	}
	else
	{
		this.m_oDays.value = sDayVal;
	}

	this.m_oHours.value = (this.m_iHours != null) ? this.m_iHours : (this.m_bRequired == true) ? "0" : K_PRMT_sEMPTY;
	this.m_oMinutes.value = (this.m_iMinutes != null) ? this.m_iMinutes : (this.m_bRequired == true) ? "0" : K_PRMT_sEMPTY;

	if ((this.m_bShowSeconds == true) || (this.m_bShowMilliseconds == true))
	{
		this.m_oSeconds.value = (this.m_iSeconds != null) ? this.m_iSeconds : (this.m_bRequired == true) ? "0" : K_PRMT_sEMPTY;
	}
	if (this.m_bShowMilliseconds == true)
	{
		this.m_oMilliseconds.value = (this.m_iMilliseconds != null) ? this.m_iMilliseconds : (this.m_bRequired == true) ? "0" : K_PRMT_sEMPTY;
	}
}

function CIntervalPicker_updateFormField()
{
	//return a valid interval data type
	//DDD HH:MT:SS.PPP, e.g. 2  10:20:30.888
	//hours
	var iDaysPart = 0;
	var iHoursPart = 0;
	var iMinutesPart = 0;
	var sDays = K_PRMT_sEMPTY;
	var sHours = K_PRMT_sEMPTY;
	var sMinutes = K_PRMT_sEMPTY;
	var sSeconds = K_PRMT_sEMPTY;
	var sMilliseconds = K_PRMT_sEMPTY;

	var sNegative = K_PRMT_sEMPTY;
	if (this.m_bNegative == true)
	{
		sNegative = '-';
	}

	if (this.m_iSeconds != null)
	{
		if (parseInt(this.m_iSeconds,10) >= 60)
		{
			iMinutesPart = 1;
			//Note the UI prevents this value from being > 2 characters
			sSeconds = (parseInt(this.m_iSeconds,10) - 60).toString();
		}
		else {
			sSeconds = this.m_iSeconds.toString();
		}
		if (sSeconds.length == 1)
		{
			sSeconds = '0' + sSeconds;
		}
	}
	if (this.m_iMinutes != null)
	{
		if (parseInt(this.m_iMinutes,10) >= 60)
		{
			iHoursPart = 1;
			//Note the UI prevents this value from being > 2 characters
			sMinutes = (parseInt(this.m_iMinutes,10) + iMinutesPart) - 60;
		}
		else {
			sMinutes = this.m_iMinutes.toString();
		}
		if (sMinutes.length == 1)
		{
			sMinutes = '0' + sMinutes;
		}
	}
	if (this.m_iHours > 24)
	{
		iDaysPart = (parseInt(this.m_iHours,10) + iHoursPart) / 24;
		iDaysPart = parseInt(iDaysPart, 10);
		iHoursPart += parseInt(this.m_iHours,10) % 24;
		if (this.m_iDays != null) {
			sDays = (parseInt(this.m_iDays,10) + iDaysPart).toString();
		}
		else {
			sDays = iDaysPart.toString();
		}
		sHours = iHoursPart.toString();
	}
	else
	{
		if (this.m_iDays != null)
		{
			sDays = this.m_iDays.toString();
		}
		else
		{
			sDays = '00';
		}

		if (this.m_iHours != null)
		{
			sHours = (parseInt(this.m_iHours,10) + iHoursPart).toString();
		}
		else if (iHoursPart > 0)
		{
			sHours = iHoursPart.toString();
		}
		else
		{
			sHours = '00';
		}
	}
	//days
	if (sDays.length == 2)
	{
		sDays = '0' + sDays;
	}
	else if (sDays.length == 1)
	{
		sDays = '00' + sDays;
	}

	//hours
	if (sHours.length == 1)
	{
		sHours = '0' + sHours;
	}

	//minutes
	if (iMinutesPart > 0)
	{
		sMinutes = iMinutesPart.toString();
		if (sMinutes.length == 1) {
			sMinutes = '0' + sMinutes;
		}
	}
	else if (sMinutes == K_PRMT_sEMPTY)
	{
		sMinutes = '00';
	}

	//seconds
	if (sSeconds == K_PRMT_sEMPTY)
	{
		sSeconds = '00';
	}


	//milliseconds
	if (this.m_iMilliseconds != null)
	{
		sMilliseconds = this.m_iMilliseconds.toString();
		if (sMilliseconds.length == 2)
		{
			sMilliseconds = '0' + sMilliseconds;
		}
		else if (sMilliseconds.length == 1)
		{
			sMilliseconds = '00' + sMilliseconds;
		}
	}
	else
	{
		sMilliseconds = '000';
	}

	this.m_oForm.value = sNegative + sDays + K_PRMT_sSP +sHours + K_PRMT_sCOLON + sMinutes + K_PRMT_sCOLON + sSeconds + K_PRMT_sDOT + sMilliseconds;
}

//catch the backspace key
//some browsers (IE5.5 don't capture this event)
function CIntervalPicker_keyPress(sKeyCode,sObj)
{
	if (sKeyCode=='8')
	{
		switch (sObj)
		{
			case 'checkDay':
				this.checkDay();
				break;
			case 'checkHour':
				this.checkHour();
				break;
			case 'checkMinute':
				this.checkMinute();
				break;
			case 'checkSecond':
				this.checkSecond();
				break;
			case 'checkMillisecond':
				this.checkMillisecond();
				break;

			default:
				this.checkInterval();
		}
	}
	return true;
}

//check for a valid interval
function CIntervalPicker_checkInterval()
{

	var bCheckFlag = false;
	var bCheckNegative = false;

	if (this.m_bRequired == true)
	{
		var bIntervalExists = true;
		if ((this.m_bShowMilliseconds == true) && (this.m_oMilliseconds.value == K_PRMT_sEMPTY))
		{
			bIntervalExists = false;
		}
		if ((bIntervalExists == true) && ((this.m_bShowSeconds == true) || (this.m_bShowMilliseconds == true)) && (this.m_oSeconds.value == K_PRMT_sEMPTY))
		{
			bIntervalExists = false;
		}
		if (bIntervalExists == true)
		{
			if ((this.m_oDays.value == K_PRMT_sEMPTY) || (this.m_oHours.value == K_PRMT_sEMPTY) || (this.m_oMinutes.value == K_PRMT_sEMPTY))
			{
				bIntervalExists = false;
			}
		}

		//if no values have been entered and this is a required prompt
		//then return an error
		if (bIntervalExists == false)
		{
			//show error animation
			this.checkIntervalFail();
			return;
		}
	}

	//are any of the controls negative
	//check days, hours and minutes
	if (this.bIsNegative(this.m_oDays.value)|| this.bIsNegative(this.m_oHours.value) || this.bIsNegative(this.m_oMinutes.value))
	{
		bCheckNegative = true;
	}

	//check seconds if seconds and milliseconds are enabled
	if ((this.m_bShowSeconds == true) || (this.m_bShowMilliseconds == true))
	{
		if (this.bIsNegative(this.m_oSeconds.value) == true)
		{
			bCheckNegative = true;
		}
	}

	//check milliseconds if milliseconds are enabled
	if (this.m_bShowMilliseconds == true)
	{
		if (this.bIsNegative(this.m_oMilliseconds.value) == true)
		{
			bCheckNegative = true;
		}
	}

	//check days, hours and minutes to see if they are correct
	if (this.bcheckNumber(this.m_oDays.value) && this.bcheckNumber(this.m_oHours.value) && this.bcheckNumber(this.m_oMinutes.value))
	{
		bCheckFlag = true;
	}
	//check seconds if seconds and milliseconds are enabled
	if ((this.m_bShowSeconds == true) || (this.m_bShowMilliseconds == true))
	{
		if (this.bcheckNumber(this.m_oSeconds.value) != true)
		{
			bCheckFlag = false;
		}
	}

	//check milliseconds if milliseconds are enabled
	if (this.m_bShowMilliseconds == true)
	{
		if (this.bcheckNumber(this.m_oMilliseconds.value) != true)
		{
			bCheckFlag = false;
		}
	}

	//handle negative values
	if (bCheckNegative == true)
	{
		this.m_bNegative = true;
		if (this.m_bAllowNegative==false)
		{
			bCheckFlag = false;
		}
	}
	else
	{
		this.m_bNegative = false;
	}

	//determine pass/fail
	if (bCheckFlag == true)
	{
		this.updateFormField();
		this.checkIntervalPass();
		this.m_bValid = true;
	}
	else
	{
		//show error animation
		this.checkIntervalFail();
		this.m_bValid = false;
	}
}

//render control in validated state
function CIntervalPicker_checkIntervalPass()
{
	if ((this.m_oImgCheckDate != null) && (this.m_oImgCheckDate.src != this.m_oImgErrorFalse.src))
	{
		this.m_oImgCheckDate.src = this.m_oImgErrorFalse.src;
	}
	this.m_bValid = true;
	this.notify(gPASS, this);
}

//render control in error state
function CIntervalPicker_checkIntervalFail()
{
	if (this.m_oImgCheckDate != null)
	{
		this.m_oImgCheckDate.src = this.m_oImgErrorTrue.src + '?' + Math.random();
	}
	this.m_bValid = false;
	this.notify(gFAIL, this);
}

function CIntervalPicker_checkDay()
{

	if (this.m_oDays.value != K_PRMT_sEMPTY)
	{
		var sDays = this.m_oDays.value;
		//check for a negative interval
		if (this.bcheckNumber(sDays) == true)
		{
			//update the variable
			sDays = this.sStripNegative(sDays);
			this.m_iDays = (sDays != K_PRMT_sEMPTY) ? sDays : null;
			this.m_oDays.className = "clsIntervalWidget";
		}
		else
		{
			//show error state
			this.m_iDays = null;
			this.m_oDays.className = "clsIntervalWidgetParseError";
		}
	}
	else
	{
		//no value, reset the variable
		this.m_iDays = null;
		if (this.m_bRequired == true)
		{
			this.m_oDays.className = "clsIntervalWidgetParseError";
		}
		else
		{
			this.m_oDays.className = "clsIntervalWidget";
		}
	}
	//update the form control
	this.checkInterval();
}

function CIntervalPicker_checkHour()
{

	if (this.m_oHours.value != K_PRMT_sEMPTY)
	{
		var sHours = this.m_oHours.value;
		if (this.bcheckNumber(sHours) == true)
		{
			//update the variable
			sHours = this.sStripNegative(sHours);
			this.m_iHours = (sHours != K_PRMT_sEMPTY) ? sHours : null;
			this.m_oHours.className = "clsIntervalWidget";
		}
		else
		{
			this.m_iHours = null;
			//show error state
			this.m_oHours.className = "clsIntervalWidgetParseError";
		}
	}
	else
	{
		//no value, reset the variable
		this.m_iHours = null;
		if (this.m_bRequired == true)
		{
			this.m_oHours.className = "clsIntervalWidgetParseError";
		}
		else
		{
			this.m_oHours.className = "clsIntervalWidget";
		}
	}
	//update the form control
	this.checkInterval();
}

function CIntervalPicker_checkMinute()
{
	if (this.m_oMinutes.value != K_PRMT_sEMPTY)
	{
		var sMinutes = this.m_oMinutes.value;
		if (this.bcheckNumber(this.m_oMinutes.value) == true)
		{
			//update the variable
			sMinutes = this.sStripNegative(sMinutes);
			this.m_iMinutes = (sMinutes != K_PRMT_sEMPTY) ? sMinutes : null;
			this.m_oMinutes.className = "clsIntervalWidget";
		}
		else
		{
			this.m_iMinutes = null;
			//show error state
			this.m_oMinutes.className = "clsIntervalWidgetParseError";
		}
	}
	else
	{
		//no value, reset the variable
		this.m_iMinutes = null;
		if (this.m_bRequired == true)
		{
			this.m_oMinutes.className = "clsIntervalWidgetParseError";
		}
		else
		{
			this.m_oMinutes.className = "clsIntervalWidget";
		}
	}
	//update the form control
	this.checkInterval();
}

function CIntervalPicker_checkSecond()
{
	if ((this.m_bShowSeconds == true) || (this.m_bShowMilliseconds == true))
	{
		if (this.m_oSeconds.value != K_PRMT_sEMPTY)
		{
			var sSeconds = this.m_oSeconds.value;
			if (this.bcheckNumber(sSeconds) == true)
			{
				//update the variable
				sSeconds = this.sStripNegative(sSeconds);
				this.m_iSeconds = (sSeconds != K_PRMT_sEMPTY) ? sSeconds : null;
				this.m_oSeconds.className = "clsIntervalWidget";
			}
			else
			{
				this.m_iSeconds = null;
				//show error state
				this.m_oSeconds.className = "clsIntervalWidgetParseError";
			}
		}
		else
		{
			//no value, reset the variable
			this.m_iSeconds = null;
			if (this.m_bRequired == true)
			{
				this.m_oSeconds.className = "clsIntervalWidgetParseError";
			}
			else
			{
				this.m_oSeconds.className = "clsIntervalWidget";
			}
		}
		//update the form control
		this.checkInterval();
	}
}

function CIntervalPicker_checkMillisecond()
{
	if (this.m_bShowMilliseconds == true)
	{
		if (this.m_oMilliseconds.value != K_PRMT_sEMPTY)
		{

			var sMilliseconds = this.m_oMilliseconds.value;

			if (this.bcheckNumber(sMilliseconds) == true)
			{
				//update the variable
				sMilliseconds = this.sStripNegative(sMilliseconds);
				this.m_iMilliseconds = (sMilliseconds != K_PRMT_sEMPTY) ? sMilliseconds : null;
				this.m_oMilliseconds.className = "clsIntervalWidget";
			}
			else
			{
				this.m_iMilliseconds = null;
				//show error state
				this.m_oMilliseconds.className = "clsIntervalWidgetParseError";
			}
		}
		else
		{
			//no value, reset the variable
			this.m_iMilliseconds = null;
			if (this.m_bRequired == true)
			{
				this.m_oMilliseconds.className = "clsIntervalWidgetParseError";
			}
			else
			{
				this.m_oMilliseconds.className = "clsIntervalWidget";
			}
		}
		//update the form control
		this.checkInterval();
	}
}

//set an interval in which the control will check for changed/valid data
function CIntervalPicker_startCheckDataInterval(field, interval)
{
	var functionString = this.m_sRef + ".checkValueChange('" + field + "')";

	if (field == "day")
	{
		this.m_sDaysCurrentValue = this.m_oDays.value;
		this.m_intervalIdDays = setInterval(functionString, interval);
	}
	else if (field == "hour")
	{
		this.m_sHoursCurrentValue = this.m_oHours.value;
		this.m_intervalIdHours = setInterval(functionString, interval);
	}
	else if (field == "minute")
	{
		this.m_sMinutesCurrentValue = this.m_oMinutes.value;
		this.m_intervalIdMinutes = setInterval(functionString, interval);
	}
	else if (field == "second" && this.m_oSeconds != null)
	{
		this.m_sSecondsCurrentValue = this.m_oSeconds.value;
		this.m_intervalIdSeconds = setInterval(functionString, interval);
	}
	else if (field == "millisecond" && this.m_oMilliseconds != null)
	{
		this.m_sMillisecondsCurrentValue = this.m_oMilliseconds.value;
		this.m_intervalIdMilliseconds = setInterval(functionString, interval);
	}
}

//stop the control from checking for changed/valid data
function CIntervalPicker_endCheckDataInterval(intervalId)
{
	if (typeof intervalId != K_PRMT_sUNDEFINED && intervalId !== K_PRMT_sEMPTY)
	{
		clearInterval(intervalId);
	}
}

//check if value has changed
function CIntervalPicker_checkValueChange(field)
{
	if (field == "day" && typeof this.m_sDaysCurrentValue != K_PRMT_sUNDEFINED)
	{
		if (this.m_oDays.value != this.m_sDaysCurrentValue)
		{
			this.endCheckDataInterval(this.m_intervalIdDays);
			this.checkDay();
		}
	}
	else if (field == "hour" && typeof this.m_sHoursCurrentValue != K_PRMT_sUNDEFINED)
	{
		if (this.m_oHours.value != this.m_sHoursCurrentValue)
		{
			this.endCheckDataInterval(this.m_intervalIdHours);
			this.checkHour();
		}
	}
	else if (field == "minute" && typeof this.m_sMinutesCurrentValue != K_PRMT_sUNDEFINED)
	{
		if (this.m_oMinutes.value != this.m_sMinutesCurrentValue)
		{
			this.endCheckDataInterval(this.m_intervalIdMinutes);
			this.checkMinute();
		}
	}
	else if (field == "second" && this.m_oSeconds != null && typeof this.m_sSecondsCurrentValue != K_PRMT_sUNDEFINED)
	{
		if (this.m_oSeconds.value != this.m_sSecondsCurrentValue)
		{
			this.endCheckDataInterval(this.m_intervalIdSeconds);
			this.checkSecond();
		}
	}
	else if (field == "millisecond" && this.m_oMilliseconds != null && typeof this.m_sMillisecondsCurrentValue != K_PRMT_sUNDEFINED)
	{
		if (this.m_oMilliseconds.value != this.m_sMillisecondsCurrentValue)
		{
			this.endCheckDataInterval(this.m_intervalIdMilliseconds);
			this.checkMillisecond();
		}
	}
}

//test to see if the value is an integer
function CIntervalPicker_bcheckNumber(sTestString)
{
	//strip out the negative sign if it exists
	sTestString = sTestString.replace('-', K_PRMT_sEMPTY);

	//check for non-numeric characters
	var z = sTestString.search(/[^0-9]/);
	if (z == -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}


//return the time in SQL format
function CIntervalPicker_sGetInterval()
{
	this.updateFormField();
	var sValue = K_PRMT_sEMPTY;
	sValue = this.m_oForm.value;
	return sValue;
}

//return the time in SQL format
function CIntervalPicker_sGetFormatInterval()
{
	var sValue = K_PRMT_sEMPTY;
	if (this.m_bNegative == true)
	{
		sValue += g_minusSign;
	}

	var sDays = "0";
	if (this.m_iDays != null)
	{
		sDays = this.m_iDays.toString();
	}

	var sHours = "0";
	if (this.m_iHours != null)
	{
		sHours = this.m_iHours.toString();
	}

	var sMinutes = "00";
	if (this.m_iMinutes != null)
	{
		sMinutes = this.m_iMinutes.toString();
		if (sMinutes.length == 1)
		{
			sMinutes = '0' + sMinutes;
		}
	}

	sValue += sDays + K_PRMT_sSP + sHours + K_PRMT_sCOLON + sMinutes;

	if ((this.m_bShowSeconds == true) || (this.m_bShowMilliseconds == true))
	{
		var sSeconds = '00';
		if (this.m_iSeconds != null)
		{
			sSeconds = this.m_iSeconds.toString();
			if (sSeconds.length == 1)
			{
				sSeconds = '0' + sSeconds;
			}
		}
		sValue += K_PRMT_sCOLON + sSeconds;
	}

	if (this.m_bShowMilliseconds == true)
	{
		var sMilliseconds = '000';
		if (this.m_iMilliseconds != null)
		{
			sMilliseconds = this.m_iMilliseconds.toString();
			if (sMilliseconds.length == 2)
			{
				sMilliseconds = '0' + sMilliseconds;
			}
			else if (sMilliseconds.length == 1)
			{
				sMilliseconds = '00' + sMilliseconds;
			}
		}
		sValue += K_PRMT_sDOT + sMilliseconds;
	}

	return sValue;
}


//validate the input into the control
function CIntervalPicker_checkData()
{
	this.checkInterval();
	if ((this.m_bRequired == true) && (this.m_oForm.value==K_PRMT_sEMPTY))
	{
		this.m_bValid = false;
		this.checkIntervalFail();
		return false;
	}
	else
	{
		this.m_bValid = true;
		this.checkIntervalPass();
		return true;
	}
}


//perform any special processing for the server.
function CIntervalPicker_preProcess()
{
	if (this.m_sSubmitType == K_PRMT_sXML)
	{
		var sURLValues = K_PRMT_sEMPTY;
		if (this.hasValue() == true)
		{
			if ((this.m_oForm.value != K_PRMT_sEMPTY) && (this.m_bDisabled!=true))
			{
				sURLValues += '<selectOption';
				sURLValues += ' displayValue="' + this.sGetFormatInterval() +'"';
				sURLValues += ' useValue="' + this.sGetXSDValue() +'"';
				sURLValues += ' selected="true" />';
			}
		}
		addSelectChoices(this.m_oSubmit, sURLValues);
	}
	else
	{
		if (this.m_bDisabled != true)
		{
			this.m_oSubmit.value = this.sGetInterval();
		}
		else
		{
			//this control is disabled
			this.m_oSubmit.value = K_PRMT_sEMPTY;
		}
	}
}

//return interval data type
function CIntervalPicker_sGetValue()
{
	if (this.m_sSubmitType == K_PRMT_sXML)
	{
		return this.sGetXSDValue();
	}
	else
	{
		return this.sGetInterval();
	}
}

//return locale formatted value
function CIntervalPicker_sGetFormatValue()
{
	return this.sGetFormatInterval();
}

//return XML schema formatted value
//The value space of duration is a six-dimensional space where the coordinates designate the
//Gregorian year, month, day, hour, minute, and second components
//PnYnMnDTnHnMnS
//If the number of years, months, days, hours, minutes, or seconds in any expression equals zero,
//the number and its corresponding designator may be omitted.
//However, at least one number and its designator must be present.
//The seconds part �may� have a decimal fraction.
//The designator 'T' shall be absent if all of the time items are absent.
//The designator 'P' must always be present.
//An optional preceding minus sign ('-') is allowed, to indicate a negative duration.
//If the sign is omitted a positive duration is indicated.
function CIntervalPicker_sGetXSDValue()
{
	var sPeriod = K_PRMT_sEMPTY;

	//determine the number Days
	if (this.m_iDays != null)
	{
		sPeriod += this.sStripNegative(this.m_iDays.toString()) + "D";
	}

	//add the time designator
	if ((this.m_iHours != null) || (this.m_iMinutes != null) || (this.m_iSeconds != null) || (this.m_iMilliseconds != null))
	{
		sPeriod += "T";
		if (this.m_iHours != null)
		{
			sPeriod += this.sStripNegative(this.m_iHours.toString()) + "H";
		}
		if (this.m_iMinutes != null)
		{
			sPeriod += this.sStripNegative(this.m_iMinutes.toString()) + "M";
		}
		if (this.m_iSeconds != null)
		{
			sPeriod += this.sStripNegative(this.m_iSeconds.toString());
		}
		if (this.m_iMilliseconds != null)
		{
			sPeriod += K_PRMT_sDOT + this.sStripNegative(this.m_iMilliseconds.toString()) + "S";
		}
	}

	//has the user specified an interval?
	if (sPeriod != K_PRMT_sEMPTY)
	{
		sPeriod = "P" + sPeriod;

		//if the interval is negative, prepend with the minus sign
		if (this.m_bNegative == true)
		{
			sPeriod = g_minusSign + sPeriod;
		}
	}
	return sPeriod;
}


function CIntervalPicker_parseXSDIntervalValue(s)
{
	var parseString = s;

	var iYearVal = 0;
	var iMonthVal = 0;
	var iDayVal = 0;
	var iDays = 0;

	var iHourVal = 0;
	var iMinuteVal = 0;
	var iSecondVal = 0;
	var iMillisecondVal = 0;

	//is the negative symbol there?
	//search for a negative value
	var z = parseString.search(rMinusSign);
	if (z == -1)
	{
		this.m_bNegative = false;
	}
	else
	{
		this.m_bNegative = true;
		//strip of the minus sign
		parseString = parseString.replace(rMinusSign, K_PRMT_sEMPTY);
	}

	//is the prefix P there?
	var iPeriodSymbolPosition = parseString.indexOf('P');
	if (iPeriodSymbolPosition != -1)
	{
		//remove the P and a minus if necessary
		parseString = parseString.substring(iPeriodSymbolPosition + 1);
		var iTimeSymbolPosition = parseString.indexOf('T');
		//split into date and time portions
		var parseArray = parseString.split ('T');
		if (((parseArray.length > 0) && (iTimeSymbolPosition > 0)) || (iTimeSymbolPosition == -1))
		{
			//Deal with date
			//nYnMnD

			//convert years to days
			var iYearPos = parseArray[0].indexOf('Y');

			if (iYearPos != -1)
			{
				iYearVal = parseInt(parseArray[0].substring(currentPos, iYearPos),10);
				parseArray[0] = parseArray[0].substring(iYearPos+1);
			}

			//convert months to days
			var iMonthPos = parseArray[0].indexOf('M');
			if (iMonthPos != -1)
			{
				iMonthVal = parseInt(parseArray[0].substring(0, iMonthPos),10);
				parseArray[0] = parseArray[0].substring(iMonthPos+1);
			}

			//add days
			var iDayPos = parseArray[0].indexOf('D');
			if (iDayPos != -1)
			{
				iDayVal = parseInt(parseArray[0].substring(0, iDayPos),10);
			}

			//ToDo: get the correct conversion
			iDays = (iYearVal * 365) + (iMonthVal * 30) + iDayVal;
		}
		else
		{
			//set date to zero
			iDays = 0;
		}

		//Deal with time
		if ((parseArray.length == 2) || (iTimeSymbolPosition != -1))
		{
			var iArrayObj = parseArray.length - 1;

			//TnHnMnS

			//get hours
			//convert years to days
			var iHourPos = parseArray[iArrayObj].indexOf('H');
			if (iHourPos != -1)
			{
				iHourVal = parseInt(parseArray[iArrayObj].substring(0, iHourPos),10);
				parseArray[iArrayObj] = parseArray[iArrayObj].substring(iHourPos + 1);
			}

			//get minutes
			var iMinutePos = parseArray[iArrayObj].indexOf('M');
			if (iMinutePos != -1)
			{
				iMinuteVal = parseInt(parseArray[iArrayObj].substring(0, iMinutePos),10);
				parseArray[iArrayObj] = parseArray[iArrayObj].substring(iMinutePos +1);
			}

			//get seconds
			//get Milliseconds
			var iSecondPos = parseArray[iArrayObj].indexOf('S');
			if (iSecondPos != -1)
			{
				//split on . to separate seconds and milliseconds
				var decimalPosition = parseArray[iArrayObj].indexOf(K_PRMT_sDOT);
				var secondArray = parseArray[iArrayObj].substring(0, iSecondPos).split(K_PRMT_sDOT);
				if ((secondArray.length == 2) || (decimalPosition == -1))
				{
					iSecondVal = parseInt(secondArray[0],10);
				}

				if ((secondArray.length == 2) || (decimalPosition != -1))
				{
					iMillisecondVal = parseInt(secondArray[secondArray.length - 1],10);
				}
			}
		}

		//create the interval
		this.m_iDays = iDays;
		this.m_iHours = iHourVal;
		this.m_iMinutes = iMinuteVal;
		this.m_iSeconds = iSecondVal;
		this.m_iMilliseconds = iMillisecondVal;
	}
}

function CIntervalPicker_iConvertMonthToDays()
{
	//not entirely accurate, but without the month and year
	//it's impossible to tell
	return 30;
}

function CIntervalPicker_hasValue()
{

	if ((this.m_iDays != null) || (this.m_iHours != null) ||
		(this.m_iMinutes != null) || (this.m_iSeconds != null) ||
		(this.m_iMilliseconds != null))
	{
		return true;
	}

	return false;
}

//test to see if a string contains a negative
function CIntervalPicker_bIsNegative(s)
{
	if (s == null)
	{
		return false;
	}

	//search for a negative value
	var z = s.toString().search(rMinusSign);
	if (z == -1)
	{
		return false;
	}
	return true;
}

//return a string with the minus sign removed
function CIntervalPicker_sStripNegative(s)
{
	if (s != null)
	{
		return	s.toString().replace(rMinusSign, K_PRMT_sEMPTY);
	}
	return K_PRMT_sEMPTY;
}

//user has left a field in the interval control
function CIntervalPicker_lostFocus()
{

}

//Prototypes to assign methods to new instances of the object
CIntervalPicker.prototype.draw = CIntervalPicker_draw;
CIntervalPicker.prototype.updateFormField = CIntervalPicker_updateFormField;
CIntervalPicker.prototype.keyPress = CIntervalPicker_keyPress;
CIntervalPicker.prototype.checkInterval = CIntervalPicker_checkInterval;
CIntervalPicker.prototype.checkDay = CIntervalPicker_checkDay;
CIntervalPicker.prototype.checkHour = CIntervalPicker_checkHour;
CIntervalPicker.prototype.checkMinute = CIntervalPicker_checkMinute;
CIntervalPicker.prototype.checkSecond = CIntervalPicker_checkSecond;
CIntervalPicker.prototype.checkMillisecond = CIntervalPicker_checkMillisecond;
CIntervalPicker.prototype.bcheckNumber = CIntervalPicker_bcheckNumber;
CIntervalPicker.prototype.checkIntervalPass = CIntervalPicker_checkIntervalPass;
CIntervalPicker.prototype.checkIntervalFail = CIntervalPicker_checkIntervalFail;
CIntervalPicker.prototype.sGetInterval = CIntervalPicker_sGetInterval;
CIntervalPicker.prototype.sGetFormatInterval = CIntervalPicker_sGetFormatInterval;
CIntervalPicker.prototype.checkData= CIntervalPicker_checkData;
CIntervalPicker.prototype.preProcess = CIntervalPicker_preProcess;
CIntervalPicker.prototype.sGetFormatValue = CIntervalPicker_sGetFormatValue;
CIntervalPicker.prototype.sGetValue = CIntervalPicker_sGetValue;
CIntervalPicker.prototype.sGetXSDValue = CIntervalPicker_sGetXSDValue;
CIntervalPicker.prototype.parseXSDIntervalValue = CIntervalPicker_parseXSDIntervalValue;
CIntervalPicker.prototype.iConvertMonthToDays = CIntervalPicker_iConvertMonthToDays;
CIntervalPicker.prototype.hasValue = CIntervalPicker_hasValue;
CIntervalPicker.prototype.bIsNegative = CIntervalPicker_bIsNegative;
CIntervalPicker.prototype.sStripNegative = CIntervalPicker_sStripNegative;
CIntervalPicker.prototype.lostFocus = CIntervalPicker_lostFocus;
CIntervalPicker.prototype.startCheckDataInterval = CIntervalPicker_startCheckDataInterval;
CIntervalPicker.prototype.endCheckDataInterval = CIntervalPicker_endCheckDataInterval;
CIntervalPicker.prototype.checkValueChange = CIntervalPicker_checkValueChange;

//create regular expressions for parsing with current locale
function initIntervalRegularExpressions()
{
	rMinusSign = new RegExp(sEscapeRegularExpression(g_minusSign), K_PRMT_sG);
}

var g_minusSign = "-";
var rMinusSign = new RegExp(g_minusSign, K_PRMT_sG);