FORMAT_DATE_FR = "dmy"
FORMAT_DATE_US = "mdy";

function parseDateTime(stDate)
{

	if( stDate == '' ) return null;
	
	var index1 = stDate.indexOf("-");
	var stYear = stDate.substr(0, index1);
	var index2 = stDate.lastIndexOf("-");
	var stMonth = stDate.substring(index1+1, index2);
	
	var index3 = stDate.indexOf("T");
	var stDay = stDate.substring(index2+1, index3);
	
	var index4 = stDate.indexOf(":");
	var stHour = stDate.substring(index3+1, index4);
	var index5 = stDate.lastIndexOf(":");
	var stMinute = stDate.substring(index4+1, index5);
	var index6 = stDate.lastIndexOf(".");
	if( index6 == -1 ) index6 = stDate.length;
	var stSecond = stDate.substring(index5+1, index6);

	var d = new Date();
	d.setUTCDate(stDay);
	d.setUTCFullYear(stYear,(Number(stMonth) - 1),stDay);
	d.setUTCMonth((Number(stMonth) - 1));  
	d.setUTCDate(stDay);
	//
	d.setUTCHours(stHour);
	d.setUTCMinutes(stMinute);
	d.setUTCSeconds(stSecond);

	//return d.toLocaleString();

	return d;
}

function parseShortDateTimeFR(stDate)
{
	if( stDate == '' || stDate == null ) return '';
	var index1 = stDate.indexOf("/");
	var stDay = stDate.substr(0, index1);
	var index2 = stDate.lastIndexOf("/");
	var stMonth = stDate.substring(index1+1, index2);
	var stYear = stDate.substring(index2+1,stDate.length);
	
	var d = new Date();
	d.setDate(stDay);
	d.setMonth(stMonth-1);
	d.setYear(stYear);
	
	return d;
	
}

function formatDateToString(oDate,format)
{
	if( oDate == null ) return '';
	
	switch(format)
	{
		case FORMAT_DATE_FR:
			return oDate.getDate() + "/" + (oDate.getMonth()+1) + "/" + oDate.getFullYear();
		case FORMAT_DATE_US:
			return (oDate.getMonth()+1) + "/" + oDate.getDate() + "/" + oDate.getFullYear();
		default: return '';
	}
	
}

function DateConvert(val, format) 
{
    	function GetFullYear(year) 
    	{
        	return (year + parseInt(val.century)) - ((year < val.cutoffyear) ? 0 : 100);
    	}
    	
    	var num, cleanInput, m, exp;
	var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})\\s*$");
	
	m = val.match(yearFirstExp);
	
	var day, month, year;
	if (m != null && (m[2].length == 2 || m[2].length == 4 || format == "ymd")) 
	{
	    day = m[6];
	    month = m[5];
	    year = (m[2].length == 4 ) ? m[2] : GetFullYear(parseInt(m[3], 10))
	}
	else 
	{
	    if (format == "ymd") return null;		

	    var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
	    m = val.match(yearLastExp);
	    if (m == null) 
	        return null;

	    if (format == "mdy") 
	    {
	        day = m[3];
	        month = m[1];
	    }
	    else {
	    	
	        day = m[1];
	        month = m[3];
	    }
	    year = (m[5].length == 4 ) ? m[5] : (m[6].length == 2 ) ? Number('19' + m[6]) : GetFullYear(parseInt(m[6], 10))
	}
	month -= 1;
	var dt = new Date(year, month, day);
	return (typeof(dt) == "object" && year == dt.getFullYear() && month == dt.getMonth() && day == dt.getDate()) ? dt : null;
    
}


function GetUTCDate(oDate)
{
	var str;
	if( oDate == null) return oDate;
	
	var strMonth;
	if(oDate.getUTCMonth()+1<10) strMonth = "0" + (oDate.getUTCMonth()+1);
	else strMonth = oDate.getUTCMonth()+1;

	var strDate;
	if(oDate.getUTCDate()<10) strDate = "0" + (oDate.getUTCDate());
	else strDate = oDate.getUTCDate();

	var strHours;
	if(oDate.getUTCHours()<10) strHours = "0" + oDate.getUTCHours();
	else strHours = oDate.getUTCHours();
	
	var strMinutes;
	if(oDate.getUTCMinutes()<10) strMinutes = "0" + oDate.getUTCMinutes();
	else strMinutes = oDate.getUTCMinutes();

	var strSeconds;
	if(oDate.getUTCSeconds()<10) strSeconds = "0" + oDate.getUTCSeconds();
	else strSeconds = oDate.getUTCSeconds();
	
	var strMilliseconds;
	if(oDate.getUTCSeconds()<10) strMilliseconds = "0" + oDate.getUTCMilliseconds();
	else strMilliseconds = oDate.getUTCMilliseconds();
		
	str = oDate.getUTCFullYear() + "-" + strMonth + "-" + strDate + "T" + strHours + ":" + strMinutes + ":" + strSeconds + "." + strMilliseconds;
	
	return str;
}


function GetUTCSmallDate(oDate)
{
	var str;
	if( oDate == null) return oDate;
	
	var strMonth;
	if(oDate.getUTCMonth()+1<10) strMonth = "0" + (oDate.getUTCMonth()+1);
	else strMonth = oDate.getUTCMonth()+1;

	var strDate;
	if(oDate.getUTCDate()<10) strDate = "0" + (oDate.getUTCDate());
	else strDate = oDate.getUTCDate();

	str = oDate.getUTCFullYear() + "-" + strMonth + "-" + strDate;
	return str;
}


function DisplayCalendar(dt)
{
	var oDate;
	
	
	if( dt != '' )
		oDate = parseShortDateTimeFR(dt);
	else
		oDate = new Date();

	oDate = showModalDialog( "Calendar.asp", oDate,"dialogWidth:110px; dialogHeight:189px; Scroll: No; help:No; resizable:No; status:No;" );
	
	if( oDate != null ) 
		oDate = new Date(oDate);
		
	return oDate;
		
}

function DisplayCalendarEx(btnName, fieldName, fieldUTCName)
{
		var vTopPos = 0;
		var vLeftPos = 0;
		var vScreenWidth;
		var vScreenHeight;
		var vPopupWidth = 112;
		var vPopupHeight = 185;
		var oDate;
		var vButtonFld = document.getElementById(btnName);
		var vFieldFld = document.getElementById(fieldName);
		var vFieldUTCFld = document.getElementById(fieldUTCName);
		
		vScreenWidth = screen.availWidth;
		vScreenHeight = screen.availHeight;
		vTopPos = vButtonFld.offsetTop - document.body.scrollTop + window.screenTop;
		vLeftPos = vButtonFld.offsetLeft - document.body.scrollLeft + window.screenLeft;
		vParent = vButtonFld.offsetParent;

		    while (vParent != null) {
		       vTopPos = vTopPos + vParent.offsetTop;
		       vLeftPos = vLeftPos + vParent.offsetLeft;
		       vParent = vParent.offsetParent;
		    }
		    
		    vTopPos = vTopPos - 75;
		    vLeftPos = vLeftPos + 0;
		    if (vLeftPos + vPopupWidth > vScreenWidth)
		       vLeftPos = vScreenWidth - vPopupWidth;
		    if (vTopPos + vPopupHeight > vScreenHeight)
		       vTopPos = vScreenHeight - vPopupHeight;

		  var vDateParam = '';
		  
		if( vFieldUTCFld.value != '' )
			oDate = parseDateTime(vFieldUTCFld.value);
		else
			oDate = new Date();

		oDate = showModalDialog("Calendar.asp" ,oDate ,'dialogTop:' + vTopPos + ';dialogLeft:' + vLeftPos + ';dialogWidth:110px; dialogHeight:189px; Scroll: No; help:No; resizable:No; status:No;');		  
		if( oDate != null ) 
		{
			oDate = new Date(oDate);		  
			vFieldFld.value = oDate.toString()
			vFieldUTCFld.value = GetUTCSmallDate(oDate);
		}
	
}
