// functies gebruikt voor de Search functionalteit
//

// returns true if string value is a correct date (e.g. 1-2-2008) and if date is in the future. Else return false
function isCorrectDate(dateValue)
{
    if (dateValue==null) return false;
    if (!IsValidDate(dateValue)) return false;

	var splittedString = dateValue.split("-");	
	if (splittedString.length != 3) return false;

    today = new Date();
	d = splittedString[0];
	m = splittedString[1]-1; // m 0-11
	y = splittedString[2];
    if (y.length==2) y = "20" + y;
	dateVal = new Date(y, m, d);
    return (today < dateVal);
}

function correctPeriod(dateFromString, dateToString)
{
    if (!isCorrectDate(dateFromString)) return false;
    if (!isCorrectDate(dateToString)) return false;

	var dateFromSplitted = dateFromString.split("-");	
	if (dateFromSplitted.length != 3) return false;

	d = dateFromSplitted[0];
    d++;
	m = dateFromSplitted[1]-1; // m 0-11
	y = dateFromSplitted[2];
    if (y.length==2) y = "20" + y;
	dateFrom = new Date(y, m, d);

	var dateToSplitted = dateToString.split("-");	
	if (dateToSplitted.length != 3) return false;

	d = dateToSplitted[0];
	m = dateToSplitted[1]-1; // m 0-11
	y = dateToSplitted[2];
    if (y.length==2) y = "20" + y;
	dateTo = new Date(y, m, d);

    return (dateFrom <= dateTo);
}


// returns true if string value is a correct date (e.g. 1-2-2008) else return false
function IsValidDate(valueString)
{ 
    if (valueString==null) return false;	
	var splittedString = valueString.split("-");
	
	if (splittedString.length == 3)
	{
		d = splittedString[0];
		m = splittedString[1]-1; // m 0-11
		y = splittedString[2];
        if (y.length==2) y = "20" + y;
		with (new Date(y, m, d))
    		return ((getDate()==d) && (getMonth()==m)) 
	}
	return false;
}


function XmlDateIsBefore(xmlDate1, xmlDate2)
{
    if (xmlDate1.GetDateTime('#yyyy#MM#dd') < xmlDate2.GetDateTime('#yyyy#MM#dd'))
    {
        return true;
    }
    else
    {
        return false;
    }
}

// return true if a numeric value is passed between 0 and 9999 else return false
function isCorrectNumberOfDays(s){
    if (s==null) return false;

    var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    if (s > -1 && s < 9999)
    {
        return true;
    }
    else
    {
        return false;
    }
}

// return a date value if a correct date string is passed (e.g. '31-12-2007') else return null
function ReturnDateValue(dateString)
{
    if (dateString==null) return null;	
	var splittedString = dateString.split("-");
	if (splittedString.length != 3) return null;

	d = splittedString[0];
	m = splittedString[1]-1; // m 0-11
	y = splittedString[2];
    if (y.length==2) y = "20" + y;

    var dateValue = new Date(y, m, d);
    if (dateValue.getDate()==d && dateValue.getMonth()==m) 
        return dateValue;
    else
        return null;
}

// add a number of days to a Date value and return the new Date value
function AddDays(date, numberofdays)
{
    var millisecs = date.valueOf();
	return new Date(millisecs + (86400000 * numberofdays));
}



// show the hidden surcharge details div if mouseover Total Surcharge value
function showSurchageDiv(surchageTotalObject, surchargesId)
{
	var objDiv = document.getElementById(surchargesId);
	if (objDiv==null)  return;

	if (surchageTotalObject!=null) 
    {
        var x = findPosX(surchageTotalObject);
        var y = findPosY(surchageTotalObject);
        
	    setStyle(surchargesId, "left", x + "px");
	    setStyle(surchargesId, "top", y + "px");
    }
    else
    {
	    setStyle(surchargesId, "left", "400px");
	    setStyle(surchargesId, "top", "400px");
    }

	objDiv.className = 'surchargeVisible';

}

// hide the surcharge details div if mouseover Total Surcharge value
function hideSurchageDiv(divId)
{
	var objDiv	= document.getElementById(divId);	
	objDiv.className			= 'surchargeInvisible';

}

// show the hidden price details div if mouseover Price, Surcharge or TotalPrice value
function showPriceDiv(surchageTotalObject, controlId)
{
	var objDiv = document.getElementById(controlId);
	if (objDiv==null)  return;

	if (surchageTotalObject!=null) 
    {
        var x = findPosX(surchageTotalObject);
        var y = findPosY(surchageTotalObject);
        
	    setStyle(controlId, "left", x + "px");
	    setStyle(controlId, "top", y + "px");
    }
    else
    {
	    setStyle(controlId, "left", "400px");
	    setStyle(controlId, "top", "400px");
    }
	objDiv.className = 'priceVisible';
}

// hide the price details div if mouseover Price, Surcharge or TotalPrice value
function hidePriceDiv(divId)
{
	var objDiv	= document.getElementById(divId);	
	objDiv.className			= 'priceInvisible';
}

// triggered if user clicks an searchresult column. Submits form again with the new sorting details
function sortbyfield(frm, order, ordertype)
{
	document.forms[frm].order.value = order;
	document.forms[frm].ordertype.value = ordertype;
	document.forms[frm].sortaction.value = '1';
	document.forms[frm].submit();
}
