﻿var mCalendarFlag=0;

var mvelem = ""
var mvelem2 = ""
var afterEventFunc = "";

var sURLpage	 = ""
sURLpage = window.location.href


/////////////////////////////////////////////////////////////////////////////////////////
///
/// This Function will launch the Date selector Popup and let the user select a date.
/// If the User select a valid date ( did notclose the calender).
/// The Function will write to velem
///
/////////////////////////////////////////////////////////////////////////////////////////
	function SelectCalendarDate(txtElem, txtElem2, eventFuncName)
	{
		var sURL = ""
		var winFrame=null;
		var height = 179//280;
		var width  = 180;
		
		var left  = event.clientX - width;   //window.screen.width - width - 50;
		var top   = event.clientY + 4; //50 ;

		var curDate="" ;
		var retVal="" ;
		var pieces ;
		var flag, retDate
		var initVals;
		
		//
		if (txtElem.readOnly || txtElem.disabled)
				return;
		if (txtElem)
		{
			curDate = txtElem.value;
		}
		else
		{
			curDate =  "";  
		}
		//
		//
		winFrame = generateIFrameElem("calFrame", height, width, top, left)
		//
		var relativeDir= ""; 
		//
		
		sURL = relativeDir + "Calendar/Calendar.aspx?cc=" + rndNum() + "&initdate=" + curDate + "&velem=" + txtElem.id
		
		if (txtElem2!=null)
			sURL += "&velem2=" + txtElem2.id;
			
		if (eventFuncName!=null)
			sURL += "&eventname=" + eventFuncName;
		
		winFrame.src = sURL;
		//
		return;
	
	}
	/////////////////////////////////////////////////////////////////////////////////////////

	/////////////////////////////////////////////////////////////////////////////////////////
	function rndNum()
	{
	var jdate = new Date();
	var centdate = new Date(jdate.getFullYear(),0,1,1,1,1,1)

	SinceBeginning = Date.parse(jdate) - Date.parse(centdate);
	if((SinceBeginning % 1000) ==0)
	{
		SinceBeginning = SinceBeginning/1000;
	}	
	return SinceBeginning;
	
	}
/////////////////////////////////////////////////////////////////////////////////////////
///
/// This Function generates an IFRAME element within a document.
/// 
/////////////////////////////////////////////////////////////////////////////////////////
function generateIFrameElem(pElemID, pheight, pwidth, ptop, pleft){
	var genCtl =null

	if (document.all(pElemID)){
		genCtl = document.all(pElemID)
	}else{

		genCtl = document.createElement("Iframe")
	}
	genCtl.id = pElemID  
	genCtl.name= pElemID 
	genCtl.frameBorder=0
	genCtl.style.position = "absolute"
	
	genCtl.style.top= ptop ; //100
	
	genCtl.style.left=pleft; // 100
	genCtl.style.width= pwidth; //310
	genCtl.style.height= pheight ; //190
	//
	genCtl.scrolling="no"
	genCtl.style.zIndex = 100;
	
	genCtl.style.borderTop="0.4mm inset #C0C0C0"
	genCtl.style.borderBottom="0.4mm inset #C0C0C0"
	genCtl.style.borderLeft="0.4mm inset #C0C0C0"
	genCtl.style.borderRight="0.4mm inset #C0C0C0"
	
	document.body.appendChild(genCtl)   
	return document.all(pElemID)
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
///
///
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////
function window_onload() {
	if (mCalendarFlag==1){
		//pass selected Dates back up the parent window
		//
		window.parent.document.all(mvelem).value= frmCal.txtSelDate.value
		window.parent.document.all(mvelem).HValue= frmCal.txtSelDateM.value
		window.parent.document.all(mvelem).focus();
		if (window.parent.document.all("txtDirty")){
			window.parent.document.all("txtDirty").value="1";
		}
		//
		//
		//
		if (TrimStr(mvelem2)!=""){
			//
			if (TrimStr(window.parent.document.all(mvelem2).value)== ""){
				window.parent.document.all(mvelem2).value = frmCal.txtSelDate.value
				window.parent.document.all(mvelem2).HValue = frmCal.txtSelDateM.value
			}
		
		}
		//
		//
		//
		if (TrimStr(afterEventFunc)!=""){
			if (window.parent[afterEventFunc]!=null )
				window.parent.execScript(afterEventFunc + "('" + mvelem + "');");
		}
		// unload now---
		window.parent.document.all("calFrame").outerHTML = "";
		return;
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
///
///
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////
function cancelNow(){
	window.parent.document.all("calFrame").outerHTML = "";
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////
///
///
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////
function CalendarNotification()
{
	//footerMessage("Click here to launch the date selector","All date entered throughout the system should be of the format dd/mm/yyyy e.g. 29/6/2001.<BR>You can either type the date directly or select it from the popup calender.")
}

/////////////////////////////////////////////////////////////////////////////////////////
/// Function to validate date passed to it and reformat it to dd/mm/yyyy IF a second
/// parameter is passed then the date is formatted to dd/mmm/yyyy and placed in the field
/// this hidden date will be used on the server to force the server to read it in the correct
/// format
/////////////////////////////////////////////////////////////////////////////////////////

function dateFormatBD(elem)
{
    var indate = elem.value;
	var isgood = true;
	var today = new Date();
	
	
	if(indate != "")
	{
		indate = DateReformationBirthday(indate)
		if(isDate(indate))
		{
			elem.value = FormatDate(indate,"dd/mm/yyyy");
			elem.HValue = FormatDate(indate,"dd/mmm/yyyy");
			
		}
		else
		{
			elem.focus();
			elem.HValue = ""
			elem.value=""
			isgood = false;
		}
	}
	else
	{
		elem.HValue = ""
		
	}
	return isgood;
}

function dateFormat(elem)
{
	var indate = elem.value;
	var isgood = true;
	if(indate != "")
	{
		indate = DateReformation(indate)
		if(isDate(indate))
		{
			elem.value = FormatDate(indate,"dd/mm/yyyy");
			elem.HValue = FormatDate(indate,"dd/mmm/yyyy");
		}
		else
		{
			elem.focus();
			elem.HValue = ""
			elem.value=""
			isgood = false;
		}
	}
	else
	{
		elem.HValue = ""
		
	}
	return isgood;
}
/////////////////////////////////////////////////////////////////////////////////////////
// This is to initialize the the Date Textbox related to the one in focus.
/////////////////////////////////////////////////////////////////////////////////////////

function initRelatedField(pElem, pDateStr2){
	if (document.all(pDateStr2)){
	
		if (TrimStr(document.all(pDateStr2).value)==""){
			document.all(pDateStr2).value = pElem.value;
		}
	}

}
