//////////////////////////////////////////////////////////////////////////////////////////////////
// File            : commmonFunctions.js
// Description     : common javascript functions
// Version         : 1.0
// Created         :
// Author (s)      : Sachin Shakya <sachin.shakya@procit.com>/<sachin_shakya@msn.com>
// History	   : 
//					 20050809 : Created by Sachin Shakya
//					 20050818 : Added DateToProcitDate() to convert Ext Date to Procit Date		
// Copyright PROCIT B.V., The Netherlands, 2002
// Merelhoven 283
// 2902 KE, Capelle ad IJssel
// The Netherlands
// info@ Procit.com
// +31(0)871901794

// This program is the intellectual property of Procit B.V., The Netherlands ("Procit").
// The program may be used, modified and/or copied only with the written permission of
// Procit or in accordance with the terms and conditions stipulated in the
// agreement/contract under which the program has been supplied.
/////////////////////////////////////////////////////////////////////////////////////////////////

//Java script right trim function
function rTrim(strText)
{
	while(''+ strText.charAt(strText.length-1)==' ')
		strText=strText.substring(0,strText.length-1);
	return strText;
}

//Java script left trim function
function lTrim(strText)
{
	while(''+strText.charAt ==' ')
		strText=strText.substring(1,strText.length);
	return strText;
}

//Java script right+left trim function
function Trim(strText)
{
	strText = rTrim(strText);	
	return lTrim(strText);
}

//funcion to submit forms(0)
function fnDoSubmit()
{
	document.forms(0).submit();
}

//funcion to submit forms(0) on Enter key press
function fnFilterSubmit()
{
	if(event.keyCode == 13)
	{
		document.forms(0).submit();
	}
}

// function to get current clock time in standard format
function getClockTime()
{
   var now    = new Date();
   var hour   = now.getHours();
   var minute = now.getMinutes();
   var second = now.getSeconds();
   
   if (hour   > 12) { hour = hour - 12;      }
   if (hour   == 0) { hour = 12;             }
   if (hour   < 10) { hour   = "0" + hour;   }
   if (minute < 10) { minute = "0" + minute; }
   if (second < 10) { second = "0" + second; }
   var timeString = hour + ':' + minute + ':' + second;
   return timeString;
}

function DateObject(strValue)
{
this.value = strValue;
}

//funcion to display calendar popup
function fnCalPopUp(strTextBox, objCalImage, event)
{
 
	var iTop, iLeft, iWidth, iHeight;
	var fldname;
	fldname = strTextBox;
	
	iHeight = 206; iWidth = 162;
	iTop = event.clientX;
	iLeft = event.clientY;
		
	var strTextBox = new CalendarPopup();
	
	strTextBox.offsetX = iLeft ;
	strTextBox.top = iTop
	strTextBox.showYearNavigation();
	strTextBox.showYearNavigationInput();

	strTextBox.select(document.all(fldname),objCalImage.id,'dd-MM-yyyy');
}

//funciton to set focus on the html object
function SetFocus(objStr)
{
	var obj;
	try
	{
	obj = document.all(objStr);
	obj.focus();
	}
	catch(e){}
}

//funciton to flip the DIV
function fnFlipDivision(strDivName)
{
	var objFrame = document.all(strDivName);
	if (objFrame.style.display=='')
		objFrame.style.display = 'none';
	else
		objFrame.style.display = '';
}	

//funciton to make sting two digit
function MakeTwoDigit(valNumber)
{
	var temp;
	if(valNumber.length<2)
		temp='0' + valNumber;
	else
		temp=valNumber;
	return temp;
}

//this function returns current date in supplied format
//strLocale: should be 1033 for EN, 1043 for NL
//strSeparator: if empty strSeparator="-"
function GetCurrentDate(strLocale, strSeparator)
{
	var now = new Date();
	var yy = now.getYear();
	var mm = now.getMonth();
	var dd = now.getDate();
	var retVal;
	
	mm = parseInt(mm) + 1;
	mm = MakeTwoDigit(String(mm));
	dd = MakeTwoDigit(String(dd));
	
	if(strSeparator=="")
		strSeparator = "-";
		
	strLocale = Trim(String(strLocale));
	
	if(strLocale=="1043")
	{
		retVal = dd + strSeparator + mm + strSeparator + yy;							
	}
	
	if(strLocale=="1033")
	{
		retVal = mm + strSeparator + dd + strSeparator + yy;							
	}

	//alert(retVal);
	return (retVal);
}

//this function returns current date in YYYYMMDD format
function GetCurrentProcitDate()
{
	var now = new Date();
	var yy = now.getYear();
	var mm = now.getMonth();
	var dd = now.getDate();
	
	mm = parseInt(mm) + 1;
	mm = MakeTwoDigit(String(mm));
	dd = MakeTwoDigit(String(dd));
	
	var retVal = yy + mm + dd;
	//alert(retVal);
	return (retVal);
}

//this function returns current date in HHMMSS format
function GetCurrentProcitTime() 
{
	var tms = new Date();
	var Hr = tms.getHours();
	var Mn = tms.getMinutes()
	var Sc = tms.getSeconds();
	if (Hr > 12)
		Hr = Hr - 12;
	
	if (Hr == 0)
		Hr = 12;
	
	Hr = MakeTwoDigit(String(Hr));
	Mn = MakeTwoDigit(String(Mn));
	Sc = MakeTwoDigit(String(Sc));
	var retVal = Hr + Mn + Sc;
	//alert(retVal);
	return (retVal);
}

//set value of checkbox to integer (0/1) or boolean (true/false)
//strValType = 0:integer
//strValType = 1:boolean
function fnSetCheckboxVal(obj, strObjName, strValType)
{
	if(obj && strObjName!="")
	{
		if(obj.checked)
		{
			if(strValType == "0")
				document.all(strObjName).value = 1;
			else
				document.all(strObjName).value = "true";
		}
		else
		{
			if(strValType == "0")
				document.all(strObjName).value = 0;
			else
				document.all(strObjName).value = "false";
		}
	}
}

function DateToProcitDate(strDate, lngCurrLocale)
{
	//var dd,mm,yy;
	if(strDate!="" && lngCurrLocale!="")
	{
	    return(splitDate(strDate,lngCurrLocale));	
	}
}

function splitDate(strDate, lngCurrLocale)
{
// PURPOSE       : to split user-defined date into day, month and year
// IN            : date as string, Locale as string
// OUT           : array with spiltted date

    if( String(strDate).length >= 6 || String(strDate).length <= 10)
    {
        var strCDate,  strSeperator, arrDate;
        
        strSeperator = GetSeparator(strDate);
        
        arrDate = strDate.split(strSeperator);
        
		
        if (lngCurrLocale == 1043) //NL - dd-mm-yyyy
        {
            if (arrDate.length == 3)
                strCDate = MakeTwoDigit(arrDate[2]) + MakeTwoDigit(arrDate[1]) + MakeTwoDigit(arrDate[0]);
            else if (arrDate.length == 0) 
                strCDate = "Date format is invalid."
        }   
        else if (lngCurrLocale == 1033)  //EN - mm-dd-yyyy
        {
            if (arrDate.length == 3)
                strCDate = MakeTwoDigit(arrDate[2]) + MakeTwoDigit(arrDate[0]) + MakeTwoDigit(arrDate[1])
            else if (arrDate.length == 0) 
                strCDate = "Date format is invalid."
        }
        if (strCDate) 
			return(strCDate);
    }
}


function GetSeparator(str)
{
// PURPOSE       : to find out the date/time separator
// IN            : date as string
// OUT           : separator string

   var strSeparator;
    
    if (str.indexOf("-") > 0)
        strSeparator = "-";
    else if (str.indexOf("/") > 0)
        strSeparator = "/";
    else if (str.indexOf(":") > 0 ) //for time separator
        strSeparator = ":";
 
    if (strSeparator)
		return (strSeparator);
}


function fnCalPopUpTwoArgs(strDateBox, strTimeBox)
{
	var iTop, iLeft, iWidth, iHeight;
	var fldname;
	fldname = strDateBox
	
	iHeight = 206; iWidth = 162;
			
	iTop = event.clientX;
	iLeft = event.clientY;
		
	var strDateBox = new CalendarPopup();
	strDateBox.offsetX = iLeft ;
	
	strDateBox.top = iTop
	strDateBox.showYearNavigation();
	strDateBox.showYearNavigationInput();

	strDateBox.select(document.all(fldname),document.all(strTimeBox),fldname,'dd-MM-yyyy');
}

function fnGetDateTime(obj)
{		
	eval("document.forms(0)." + obj).value = document.all(obj+"1").value + ' ' + document.all(obj+"2").value
}

function onpopupClick()
{
	//this line has nothing to do with application function	
	//but it must be there.
	var dummy = 1;
}
	
function openList(strForeignTableKeyField,strURL)
{
	var iTop,iLeft, iWidth, iHeight;
	iWidth = 900; iHeight = 400;
	iTop = (window.screen.height - iHeight)/2;
	iLeft = (window.screen.width - iWidth)/2;
			
	window.open(strURL,strForeignTableKeyField ,'top=' + iTop + ',left=' + iLeft + ',width=' + iWidth + ',height=' + iHeight + ',tltlebar=no,toolbar=no,location=no,menubar=no');
		
	try{
		eval("document.forms(0)." + strForeignTableKeyField).value = '';
		//document.all(strForeignTableKeyField).value = '';	
	}catch(err){}
}

function returnedToPage(strForeignTableKeyField,idfieldvalue, descfieldvalue,desc1,desc1value,desc2,desc2value,desc3,desc3value,des4,desc4value)
{
	eval("document.forms(0)."+strForeignTableKeyField).value = idfieldvalue;
	eval("document.forms(0).slListDesc"+strForeignTableKeyField).value = descfieldvalue;
	if (desc1!="")
	{
		try{eval("document.forms(0)."+desc1).value = desc1value;
			eval("document.forms(0)."+desc2).value = desc2value;
			eval("document.forms(0)."+desc3).value = desc3value;
			eval("document.forms(0)."+desc4).value = desc4value;
			}
		catch(e){}
	}
}
	
function returnedToEditable(strForeignTableKeyField,idfieldvalue, descfieldvalue)
{
	eval("document.forms(0).fld_"+strForeignTableKeyField+intId).value = idfieldvalue;
	eval("document.forms(0).slListDescfld_"+strForeignTableKeyField+intId).value = descfieldvalue;
}

function fnSwapCheckbox(obj, strObjName)
{
	if (strObjName=="")
		strObjName = "fldHiddenActive";
	
	if(obj)
	{
		if(obj.checked)
			document.all(strObjName).value = "true";
		else
		{
			document.all(strObjName).value = "false";
			try
			{
				document.all("o").value = "1";	
			}
			catch(e) {}
		}
		
		try{document.all("p").value	= "1";} catch(e){}	
		document.forms(0).submit();
	}
}

function fnSwapHistoryActive(objThis)
{		
	var objActive = document.all("fldHiddenActive");
	var objHistory = document.all("fldHiddenHistory");
	if (objThis == document.all("fldActive"))
	{	
		if(objThis.checked)
		{
			try
			{
				document.all("o").value = "1";	
			}
			catch(e) {}
			objActive.value = "true";
			objHistory.value = "false";
		}
		else
			objActive.value = "false";
	}
	else if (objThis == document.all("fldShowHistory"))
	{
		if(objThis.checked)
		{
			objHistory.value = "true";
			objActive.value = "false";
		}
		else
		{
			objHistory.value = "false";
			try
			{
				document.all("o").value = "1";	
			}
			catch(e) {}
		}	
	}
	try{document.all("p").value	= "1";} catch(e){}	
	document.forms(0).submit();
}
function textCounter(field, maxlimit) 
{
	if (field.value.length > maxlimit) // if too long...trim it!						
	field.value = field.value.substring(0, maxlimit);					
}

//Shortcut key functionality
function fnKeyPressAction(strListPage,blnList) 
{
     // <BODY onKeyPress="fnKeyPressAction()">
     //alert(event.keyCode)
     if (window.event.ctrlKey) 
     {
         //------------IE RESERVED KEYS-------------
         // a - Select All
         // b - Organise Favourites
         // c - Copy
         // d - Add to Favourites
         // e - searching
         // f - Find (on this page)
         // h - history
         // i - favourites
         // l - Open
         // n - New Window
         // o - Open
         // p - Print
         // r - Reload
         // s - Save
         // v - Paste
         // w - Close
         // x - Cut
         //------------IE RESERVED KEYS-------------
         
         if (window.event.keyCode == 17) //SAVE button (Ctrl and q)
         {
			doStandardAction('save',strListPage);
		 }
		 
		 if (window.event.keyCode == 20) //SAVE list (Ctrl and t)
		 {
			doStandardAction('savelist',strListPage);
		 }
		 
         if (window.event.keyCode == 7 ) //LIST (Ctrl and g)
         {
			doStandardAction('list',strListPage);
         }
         
        
		 if(blnList==1)
		 {
			 if (window.event.keyCode == 11) //NEW (Ctrl and k)
			{	
				eval(document.all("cmdNew").href);
			}
			if (window.event.keyCode == 10) //Cancel (Ctrl and j)
			{
				eval(document.all("cmdCancel").href);

			}	  
		 }
		 
		 else
		 {
			if (window.event.keyCode == 11) //NEW (Ctrl and k)
			{
				doStandardAction('new',strListPage);
			}
			if (window.event.keyCode == 10) //Cancel (Ctrl and j)
			{
				doStandardAction('cancel',strListPage);
			}
		}			
		if (window.event.keyCode == 13) //Search button (Ctrl and m)
		 {
		 	if(strListPage == 'Editable')
		 		doStandardAction('search');
		 	else	
		 		eval(document.all("cmdSearch").href);
		 }
		 	
		 /*			 				
		if (window.event.keyCode == 10) //Cancel (Ctrl and j)
		 {
		 	eval(document.all("cmdCancel").href);

		 }	   
		 if (window.event.keyCode == 11) //NEW (Ctrl and k)
		 {	
		 	eval(document.all("cmdNew").href);
		 }	
         if (window.event.keyCode == 21) alert('Ctrl and u pressed');
         if (window.event.keyCode == 25) alert('Ctrl and y pressed');
         if (window.event.keyCode == 26) alert('Ctrl and z pressed');
         */
     }
}

var strCurrentColoredRowClass;
var objCurrentColoredRow;

// Function to Set Browser type in Seesion
function fnSetBrowserSession()
{
    var xmlHttp;
    try // Firefox, Opera 8.0+, Safari
    {    
	    xmlHttp = new XMLHttpRequest();  
    }
    catch (e) // Internet Explorer  
    {
	    try
	    {    
		    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");    
	    }
	    catch (e)
	    {    
		    try
		    {      
			    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");      
		    }
		    catch (e)
		    {      
			    alert("Your browser does not support AJAX!");
		    }    
	    }  
    }
    xmlHttp.onreadystatechange = function()
    {
	    if(xmlHttp.readyState==4)
	    {}
    }
    var browserAppName;
    browserAppName = navigator.appName;
	
    if(browserAppName == "Microsoft Internet Explorer")
    {
	    browserAppName = "IE"
    }
    else
    {
	    browserAppName = "Mozilla"	
    }
    url = "";
    url+= "BrowserType.asp?"
    url+= "browserAppName=" + browserAppName;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}


function fnGetShortTariff(strFullTariff)
{
    var retShortTariff="''";
    
   	if (strFullTariff=="Zonder dans / zonder drank_INDOOR" || strFullTariff=="Zonder dans / zonder drank_OUTDOOR") 
		retShortTariff="'zonder drank','met drank','met dans'";
	else if  (strFullTariff=="Zonder dans / met drank_INDOOR" || strFullTariff=="Zonder dans / met drank_OUTDOOR") 
		retShortTariff= "'met drank','met dans'"
	else if  (strFullTariff=="Met dans / met drank_INDOOR" || strFullTariff=="Met dans / met drank_OUTDOOR") 
		retShortTariff= "'met dans'"
		
    return retShortTariff;
}


//=============================================================
// Remove comma and the number behind it from the surface field
//=============================================================
function fnRemoveComma(objform)
{
				
	for(i=0;i<objform.elements.length;i++)
	{
		obj=objform.elements[i];
					
					
		if(obj.getAttribute("extCType")=='surface')
		{	
			obj.value=obj.value.split(',')[0];
		}
	}
}

//=====================================================================

