//Global ISO date string
var ISOdate="";

//Global XMLHTTP Request object
var XmlHttp;

var AJAX_FILE_PATH="/ICON/CustomControls/DateControl/";

//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}

//validates the date
function validateDate(obj, format, msg, color, focus)  
{
	if(obj.value!='')
	{
		//this is needed to store in the cache a different page for every page call 
		//so that XML files do not get mixed up!
		var CurrentDateTime = document.lastModified;
		
		var CurrentValue = obj.value;
	
	    var lang="en";
	    var qs = new Querystring();
	    if(qs.get("lang")!=null)
	    {
	        lang = qs.get("lang");
	    }

		// URL to get conditions for a given base
		var requestUrl = AJAX_FILE_PATH+"AjaxServerCalendar.aspx" + "?CurrentValue=" + encodeURIComponent(CurrentValue)+
		"&lang="+lang + "&CurrentDateTime="+CurrentDateTime;
		
		CreateXmlHttp();
		
		// If browser supports XMLHTTPRequest object
		if(XmlHttp)
		{
			//Setting the event handler for the response
			XmlHttp.onreadystatechange = function(){
				// To make sure receiving response data from server is completed
				if(XmlHttp.readyState == 4)
				{
					// To make sure valid response is received from the server, 200 means response received is OK
					if(XmlHttp.status == 200)
					{
					    if(XmlHttp.responseText.substring(XmlHttp.responseText.indexOf(';')+1,XmlHttp.responseText.length)=="ERROR")
					    {
					        obj.style.color = color;
						    if (focus) obj.focus();
						    alert(msg);
						    return false;
					    }
					    else
					    {
						    //ctlToPlaceValue is a global variable in popupcalendar.js which is the current date field
						    obj.style.color = 'black';
    						obj.value = XmlHttp.responseText.substring(XmlHttp.responseText.indexOf(';')+1,XmlHttp.responseText.length);
					    }
						
					}
					else
					{
						obj.style.color = color;
						if (focus) obj.focus();
						alert(msg);
						return false;
					}
				}; 
			};
			
			//Initializes the request object with GET (METHOD of posting), 
			//Request URL and sets the request as synchronous.
			if(ie)
			{
			    XmlHttp.open("GET", requestUrl,  false);
			}
			else
			{
			    //Request URL and sets the request as asynchronous.
			    XmlHttp.open("GET", requestUrl,  true);
			}
			
			//Sends the request to server
			XmlHttp.send(null);		
		}
	}
}

//Gets the current date in correct format
function getCurrentDate(myDate) 
{
	//this is needed to store in the cache a different page for every page call 
	//so that XML files do not get mixed up!
	var CurrentDateTime = document.lastModified;
	
	var CurrentValue = myDate;
	
		 var lang="en";
	    var qs = new Querystring();
	    if(qs.get("lang")!=null)
	    {
	        lang = qs.get("lang");
	    }

		// URL to get conditions for a given base
		var requestUrl = AJAX_FILE_PATH+"AjaxServerCalendar.aspx" + "?CurrentValue=" + encodeURIComponent(CurrentValue)+
		"&lang="+lang + "&CurrentDateTime="+CurrentDateTime;
		
		CreateXmlHttp();//found in DateFunctions.js
		
		// If browser supports XMLHTTPRequest object
		if(XmlHttp)
		{
			//Setting the event handler for the response
			XmlHttp.onreadystatechange = function(){
				// To make sure receiving response data from server is completed
				if(XmlHttp.readyState == 4)
				{
					// To make sure valid response is received from the server, 200 means response received is OK
					if(XmlHttp.status == 200)
					{
					    if(XmlHttp.responseText.substring(XmlHttp.responseText.indexOf(';')+1,XmlHttp.responseText.length)=="ERROR")
					    {
					        alert("Unable to retrieve data from server.");
					    }
					    else
					    {
						    //ctlToPlaceValue is a global variable in popupcalendar.js which is the current date field
						    ctlToPlaceValue.value = XmlHttp.responseText.substring(XmlHttp.responseText.indexOf(';')+1,XmlHttp.responseText.length);
						    ISOdate = XmlHttp.responseText.substring(0,XmlHttp.responseText.indexOf(';'));
					    }
					}
					else
					{
						alert("Unable to retrieve data from server.");
					}
				}; 
			};
			
			//Initializes the request object with GET (METHOD of posting), 
			//Request URL and sets the request as synchronous.
			if(ie)
			{
			    XmlHttp.open("GET", requestUrl,  false);
			}
			else
			{
			    //Request URL and sets the request as asynchronous.
			    XmlHttp.open("GET", requestUrl,  true);
			}
			
			//Sends the request to server
			XmlHttp.send(null);		
		}
	
}

function getToday(obj, fld, format)
{
	var todaysdate = new Date();
	var date  = todaysdate.getDate();
	var day  = todaysdate.getDay() + 1;
	var month = todaysdate.getMonth() + 1;
	var yy = todaysdate.getYear();
	var year = (yy < 1000) ? yy + 1900 : yy;
	
	getCurrentDate(year + "-" + month + "-" + date);
	
	hideCalendar();
}
