var sBrowserVersion = navigator.appVersion;

var IE = (document.all) ? true : false;
var IE4 = (IE && !document.getElementById) ? true : false;
var IE5 = (IE && document.getElementById) ? true : false;
var IE6 = IE5 ? (sBrowserVersion.match(/MSIE 5/) ? false : true) : false;

var NS4 = (document.layers) ? true : false;
var NS6 = (document.getElementById && !IE) ? true : false;

var gbAjaxProcessing = false;

//-----------------------------------------------------------------------------------------

function selectTab(loTab) {
    //loTab.className);
}

//-----------------------------------------------------------------------------------------

function IsIn(inArray, inItem)
{
	var iArrayIndex, bIsIn;
	bIsIn = false;
	for (iArrayIndex in inArray)
	{
		if (inArray[iArrayIndex] == inItem)
		{
			bIsIn = true;
			break;
		}
	}
	return(bIsIn);
}

//---------------------------------------------------------------------------

function ScrollToEnd()
{
	window.scrollTo(0,999999);
}

//---------------------------------------------------------------------------

/* use this to find width (& height) : javascript:alert(document.body.clientWidth) */

var winPopup;

function popupWindow(strUrl, intWidth, intHeight, strName, bNoScroll, bWindowFeatures, bPopunder)
{
	var sParams, iScreenX, iScreenY;
	if (!strName)
	{
		strName = 'winPopup';
	}

	if(window.screen)
	{
		intWidth = (intWidth ? Math.min(intWidth, window.screen.availWidth-45) : window.screen.availWidth-45);
		intHeight = (intHeight ? Math.min(intHeight, window.screen.availHeight-60) : window.screen.availHeight-60);

		iScreenX = (window.screen.availWidth - intWidth) /2
		iScreenY = (window.screen.availHeight - 20 - intHeight) /2
	}

	//alert(iScreenX + '::' + intWidth)
	//alert(iScreenY + '::' + intHeight)

	sParams = (bWindowFeatures ? 'toolbar=1,titlebar=1,status=1,' : 'toolbar=0,titlebar=0,status=0,') 
		+ 'scrollbars=' + (bNoScroll ? 0 : 1) + ',resizable=' + (bNoScroll ? 0 : 1) 
		+ (intWidth ? ',width=' + intWidth : '') + (intHeight ? ',height=' + intHeight : '')
		+ ",left=" + iScreenX + ",top=" + iScreenY + ",screenX=" + iScreenX + ",screenY=" + iScreenY ;
		
	winPopup = window.open(strUrl, strName, sParams);
	if (bPopunder && winPopup)
	{
		window.focus()
	}
	else if (winPopup)
	{
		winPopup.focus();
	}
	return (winPopup);
}

function popupNoScrollWindow(strUrl, intWidth, intHeight, strName, bPopunder)
{
	winPopup = popupWindow(strUrl, intWidth, intHeight, strName, true, false, bPopunder)
	return (winPopup);
}

function popupNormal(strUrl, strName)
{
	if (!strName)
	{
		strName = 'winPopup';
	}
	winPopup = window.open(strUrl, 'winPopup');
	return (winPopup);
}


//-----------------------------------------------------------------------------------------


function SelectAllCheckboxes(sCheckBoxName, bSelValue)
{
	var oCheckBoxes, j
	oCheckBoxes = document.getElementsByName(sCheckBoxName);
	for(j = 0; j < oCheckBoxes.length; j++) 
	{
		oCheckBoxes[j].checked = bSelValue
	}
}

//-----------------------------------------------------------------------------------------

function showHide( loLayer) {
    if (document.getElementById( loLayer).style.display == 'none')
        $('#'+loLayer).show('slow');
    else
        $('#'+loLayer).hide('slow');
}

function ToggleShowHide(loLayer) {
    
    if (document.getElementById( loLayer).style.display == 'none') {
        ToggleHidden(loLayer, true);
    }
    else {
        ToggleHidden(loLayer, false);
    }
}

//----------------------------------------------------------------------------


function Hide(loLayer) {
    ToggleHidden(loLayer, false);
}

function Show(loLayer) {
    ToggleHidden(loLayer, true);
}


function ToggleHide(loLayer, lbShow) {
    if (lbShow)
        $('#'+loLayer).show('slow');
    else
        $('#'+loLayer).hide('slow');
}


function ToggleHidden(loLayer, lbShow) {
    
    if (lbShow)
        document.getElementById( loLayer).style.display = 'block';
    else
        document.getElementById( loLayer).style.display = 'none';
}

//----------------------------------------------------------------------------

function makeFloat( loSource) {
    if (loSource.value=='')
        loSource.value = '0';
    loSource.value = parseFloat(loSource.value).toFixed(2);
}

//----------------------------------------------------------------------------

function addVAT( loSource, loTarget) {
    checkFloat(loSource);
    if (loSource.value!='')
        loTarget.value = (parseFloat(loSource.value) * gfVAT).toFixed(2);
}

//----------------------------------------------------------------------------

function removeVAT( loSource, loTarget) {
    checkFloat(loSource);
    if (loSource.value!='')
        loTarget.value = (parseFloat(loSource.value) / gfVAT).toFixed(2);
}

//----------------------------------------------------------------------------

function checkFloat( lsFormElement) {
    var newVal = '';
    
    lsFormElement.value = lsFormElement.value.replace(',','');
    
    for (var i=0; i < lsFormElement.value.length; i++) { 
        if (lsFormElement.value.substr(i,1).match(/\d+/) != null || lsFormElement.value.substr(i,1) == '.' || lsFormElement.value.substr(i,1) == '-')
            newVal += lsFormElement.value.substr(i,1)
    }
    
    if (newVal != lsFormElement.value) 
        lsFormElement.value = newVal;
    
}

//----------------------------------------------------------------------------

function ajaxKeyUpPost(lsURL, loLayer) {
    clearTimeout(searchTimer);
    searchTimer = setTimeout("ajaxPost('"+lsURL+"', '"+loLayer+"');",300);
}

function ajaxPost(lsURL, loLayer, loFunction, lbNoTracking, lsPostData, lsLoadingHTML) {
    var xmlHttp = GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Your browser does not support AJAX!");
        return;
    } 
    if (lsURL!='') {
		//if (!lbNoTracking)
			gbAjaxProcessing =  true;
        
        if (!lsPostData)  {
			lsType = 'GET';
			lsURL += '&nc=' + (new Date()).getTime();
		} else {
		    lsType = 'POST';
		}

		if (lsLoadingHTML)
		    document.getElementById( loLayer).innerHTML = lsLoadingHTML;
	
        xmlHttp.onreadystatechange = stateChanged;
        xmlHttp.open( lsType, lsURL, true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.send(lsPostData);
    }
    
    function stateChanged() { 
        var lsResponseStr;
        if (xmlHttp.readyState==4){
            var lsResponseText = xmlHttp.responseText;
            
            if (loLayer) {
				//Do ajax post form processing
                if (lsResponseText.indexOf('#form#') != -1) {
					var laResponseArray = lsResponseText.split('#form#')
					
					lsResponseText = laResponseArray[0];
					if (laResponseArray.length>1)
						lsResponseText += laResponseArray[2];
					
					document.getElementById( loLayer).innerHTML = lsResponseText;
					
					eval(laResponseArray[1]);
                
                //No onload ajax to load
	            } else {
					document.getElementById( loLayer).innerHTML = lsResponseText;
				}
            }
            
            
            if (loFunction) {
                loFunction = loFunction.replace('{ajax}',lsResponseText);
                eval(loFunction);
            }
            if (loLayer) {
                if (lsResponseText!=''){
                    document.getElementById( loLayer).style.display = 'block';
                } else {
                    document.getElementById( loLayer).style.display = 'none';
                }
			    //ajaxSuggestions.init();
            }
            //if (!lbNoTracking)
				gbAjaxProcessing = false;
				
			//document.getElementById('searchbox').focus();
        }
    }
}

//----------------------------------------------------------------------------

function GetXmlHttpObject() {
    var xmlHttp=null;
    try{
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e) { 
        try {
            // Internet Explorer
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

//----------------------------------------------------------------------------

function makeArray() {
  var args = makeArray.arguments;
  for (var i = 0; i < args.length; i++) {
    this[i] = args[i];
  }
  this.length = args.length;
}

//----------------------------------------------------------------------------

function DateAdd(timeU,byMany,dateObj) {
	var millisecond=1;
	var second=millisecond*1000;
	var minute=second*60;
	var hour=minute*60;
	var day=hour*24;
	var year=day*365;
    
    
    if (!dateObj)
        dateObj = new Date();
    
	var newDate;
	var dVal=dateObj.valueOf();
	switch(timeU) {
		case "ms": newDate=new Date(dVal+millisecond*byMany); break;
		case "s": newDate=new Date(dVal+second*byMany); break;
		case "mi": newDate=new Date(dVal+minute*byMany); break;
		case "h": newDate=new Date(dVal+hour*byMany); break;
		case "d": newDate=new Date(dVal+day*byMany); break;
		case "y": newDate=new Date(dVal+year*byMany); break;
	}
	return newDate;
}

//----------------------------------------------------------------------------

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function dateToString(date) {
  //var months = new makeArray("January", "February","March", "April", "May", "June", "July", "August","September", "October", "November", "December");
  //return date.getDate() + "/" + (date.getMonth()+1) + "/" + ((date.getYear() < 100) ? "19" : "") + date.getYear();
  return date.getDate() + "/" + (date.getMonth()+1) + "/" + ((date.getUTCFullYear() < 100) ? "19" : "") + date.getUTCFullYear();
}

function getJSDateFromString(ldDate) {
    if (!ldDate)
        ldDate = dateToString(new Date());
    var laDate = ldDate.split("/");
    ldDate = laDate[2] + '/' + laDate[1] + '/' + laDate[0];
    return ldDate;
}


//----------------------------------------------------------------------------

function setEpochDate( loDateObject, ldDateValue, loFunction) {
    ldDateValue = getJSDateFromString(ldDateValue);
    var laDateArray = new Array(new Date(ldDateValue));
    loDateObject.removeDates( loDateObject.selectedDates, true);
    loDateObject.goToMonth( laDateArray[0].getFullYear(), laDateArray[0].getMonth());
    loDateObject.addDates( laDateArray, true);
    if (loFunction)
        loDateObject.onClick(loFunction);
        
}


//----------------------------------------------------------------------------

function getCheckboxString( loObject) {
    var lsResults='';
    try{
        
        if (loObject.length) {
            for (i=0;i<loObject.length;i++)
                if (loObject[i].checked)
                    lsResults += loObject[i].value + ',';
        } else if (loObject.checked) {
                lsResults = loObject.value;
        }
    } catch (err) {}
    
    return lsResults;
}

//----------------------------------------------------------------------------

function setHTML( loLayer, lsHTML) {
    document.getElementById( loLayer).innerHTML =lsHTML
}

function appendHTML( loLayer, lsHTML) {
    document.getElementById( loLayer).innerHTML += lsHTML;
}

function addRowHTML( loLayer, inputStr) {
    var elm = document.createElement('tr');
    var i=0;
	var j=0;
	while (i<inputStr.lastIndexOf("<")) {
		j=inputStr.indexOf("<td>",j)+1;
		i=inputStr.indexOf("<",j);
		var txtElm = document.createTextNode(inputStr.substring(j+3,i));
		var tdElm = document.createElement("td");
		tdElm.appendChild(txtElm);
		elm.appendChild(tdElm);
	}	
    document.getElementById( loLayer).appendChild(elm);
}

function addRow( loLayer, lsRow1, lsRow2) {
    var loTableRow = document.createElement('tr');
	var loTableCell1 = document.createElement("td");
	var loTableCell2 = document.createElement("td");
	loTableCell1.appendChild(document.createTextNode(lsRow1));
	loTableCell2.appendChild(document.createTextNode(lsRow2));
	loTableRow.appendChild(loTableCell1);
	loTableRow.appendChild(loTableCell2);
    document.getElementById( loLayer).appendChild(loTableRow);
}

//----------------------------------------------------------------------------

function ajaxService( lsServiceURL, lsServiceLayer, lsLoadingText, loFunction) {
    
    var xmlHttp=GetXmlHttpObject();
    
    if (xmlHttp!=null) {
        var lsServiceURL = lsServiceURL + "&nc=" + Math.random();
        xmlHttp.onreadystatechange = stateChanged;
        xmlHttp.open("GET",lsServiceURL,true);
        if (!lsLoadingText)
            lsLoadingText = 'Loading...';
        if (lsServiceLayer)
            document.getElementById(lsServiceLayer).innerHTML = lsLoadingText;
        xmlHttp.send(null);
    }
    
    function stateChanged() { 
        if (xmlHttp.readyState==4){
            if (lsServiceLayer) {
                document.getElementById(lsServiceLayer).style.display = 'block';
                document.getElementById(lsServiceLayer).innerHTML = xmlHttp.responseText;
            }
            if (loFunction)
                eval(loFunction);
        }
    } 
    
}

//----------------------------------------------------------------------------

function onBlurAjaxDropbox( lsIDName) {
	if(document.getElementById('_' + lsIDName).value=='') {
		document.getElementById(lsIDName).value='';
	} else {
		if (gsOriginalAjaxDropboxID==document.getElementById(lsIDName).value && gsOriginalAjaxDropboxName != document.getElementById('_' + lsIDName).value) {
				document.getElementById(lsIDName).value = gsOriginalAjaxDropboxID;
				document.getElementById('_' + lsIDName).value = gsOriginalAjaxDropboxName;
		}
	}
}

var gsOriginalAjaxDropboxName;
var gsOriginalAjaxDropboxID;
function onClickAjaxDropbox(lsIDName) {
	
	gsOriginalAjaxDropboxName = document.getElementById('_' + lsIDName).value;//loName.value;
	gsOriginalAjaxDropboxID = document.getElementById(lsIDName).value;//loID.value;
	
}


/*
function onBlurAjaxDropbox( loName, loID) {
	if(loName.value=='') {
		loID.value='';
	} else {
		if (gsOriginalAjaxDropboxID==loID.value && gsOriginalAjaxDropboxName != loName.value) {
				loID.value = gsOriginalAjaxDropboxID;
				loName.value = gsOriginalAjaxDropboxName;
		}
	}

}


var gsOriginalAjaxDropboxName;
var gsOriginalAjaxDropboxID;
function onClickAjaxDropbox(loName, loID) {
	gsOriginalAjaxDropboxName = loName.value;
	gsOriginalAjaxDropboxID = loID.value;
}
*/



//----------------------------------------------------------------------------


function updateDeadlineDate() {
	var lsDate = ''
	
	if (document.forms.form1.ActivityDeadline.value) {
		var ldDeadlineDate = new Date(getJSDateFromString(document.forms.form1.ActivityDeadline.value));
		var lsDate = dateFormat(ldDeadlineDate, "dddd, dS mmmm, yyyy");
		var liDays = DateDiff.inDays(new Date(), ldDeadlineDate) + 1;
		var liWeeks = DateDiff.inWeeks(new Date(), ldDeadlineDate);
		var liMonths = DateDiff.inMonths(new Date(), ldDeadlineDate);
		var liYears = DateDiff.inYears(new Date(), ldDeadlineDate);
	    
		lsDate += '<br /><span style="color:#c00;">';
		if (liDays<=0)
			lsDate += 'Deadline Passed';
		else if (liDays<=60)
			lsDate += 'Due In ' + liDays + ' Days';
		else if (liWeeks<=8)
			lsDate += 'Due In ' + liWeeks + ' Weeks';
		else if (liMonths<=23)
			lsDate += 'Due In ' + liMonths + ' Months';
		else
			lsDate += 'Due In ' + liYears + ' Years';
		lsDate += '</span>';
	}
    document.getElementById('deadlinedate_layer').innerHTML = lsDate;
}

//----------------------------------------------------------------------------

