


var toggle = 0;
function toggleImage()
{
	var image = arguments[0];
	var tmp = parseInt(image.src.slice(-5,-4));
	if (tmp == toggle)
	{
		toggle = 1 - toggle;
		if (image.name == "")
			image.src = image.src.slice(0,-5) + toggle + image.src.slice(-4);
		else
			for (var i = 0; i < document.images.length; i++)
				if (document.images[i].name == image.name)
					document.images[i].src = document.images[i].src.slice(0,-5) + toggle + document.images[i].src.slice(-4);
	}
}

function openHelpWindow(bookmark)
{
	var windowLocation = "/AvpPash/relay.do?dispatch=help" + bookmark;
	var windowTitle = "Aide";
	var optionWidth = 1000;
	var optionHeight = 700;	
	var optionScreenX = (screen.availWidth - optionWidth) / 2;
	var optionScreenY = (screen.availHeight - optionHeight) / 2;

	var optionList = "";
	optionList += (optionList == "" ? "" : ",") + "directories=no";
	optionList += (optionList == "" ? "" : ",") + "height=" + optionHeight;
	optionList += (optionList == "" ? "" : ",") + "location=no";
	optionList += (optionList == "" ? "" : ",") + "menubar=no";
	optionList += (optionList == "" ? "" : ",") + "resizable=yes";
	// Netscape Navigator: screenX, screenY
	optionList += (optionList == "" ? "" : ",") + "screenX=" + optionScreenX;
	optionList += (optionList == "" ? "" : ",") + "screenY=" + optionScreenY;
	// Internet Explorer: left, top
	optionList += (optionList == "" ? "" : ",") + "left=" + optionScreenX;
	optionList += (optionList == "" ? "" : ",") + "top=" + optionScreenY;
	optionList += (optionList == "" ? "" : ",") + "scrollbars=yes";
	optionList += (optionList == "" ? "" : ",") + "status=yes";
	optionList += (optionList == "" ? "" : ",") + "toolbar=no";
	optionList += (optionList == "" ? "" : ",") + "width=" + optionWidth;

	var newWindow = window.open(windowLocation, (windowTitle ? windowTitle : "window"), optionList);
//	newWindow.resizeTo(width,height);
	newWindow.focus();
	return newWindow;
}

function openDialog(windowLocation, windowTitle, width, height, scrollbars)
{
	var optionWidth = width;
	var optionHeight = height;
	
	var optionScreenX = (screen.availWidth - optionWidth) / 2;
	var optionScreenY = (screen.availHeight - optionHeight) / 2;

	var optionList = "";
	optionList += (optionList == "" ? "" : ",") + "directories=no";
	optionList += (optionList == "" ? "" : ",") + "height=" + optionHeight;
	optionList += (optionList == "" ? "" : ",") + "location=no";
	optionList += (optionList == "" ? "" : ",") + "menubar=no";
	optionList += (optionList == "" ? "" : ",") + "resizable=yes";
	// Netscape Navigator: screenX, screenY
	optionList += (optionList == "" ? "" : ",") + "screenX=" + optionScreenX;
	optionList += (optionList == "" ? "" : ",") + "screenY=" + optionScreenY;
	// Internet Explorer: left, top
	optionList += (optionList == "" ? "" : ",") + "left=" + optionScreenX;
	optionList += (optionList == "" ? "" : ",") + "top=" + optionScreenY;
	optionList += (optionList == "" ? "" : ",") + "scrollbars=" + scrollbars;
	optionList += (optionList == "" ? "" : ",") + "status=no";
	optionList += (optionList == "" ? "" : ",") + "toolbar=no";
	optionList += (optionList == "" ? "" : ",") + "width=" + optionWidth;

	var newWindow = window.open(windowLocation, (windowTitle ? windowTitle : "dialog"), optionList);
//	newWindow.resizeTo(width,height);
	newWindow.focus();
	return newWindow;
}


function openWindow(windowLocation, windowTitle, width, height, scrollbars, toolbar, menubar)
{
	var optionWidth = width;
	var optionHeight = height;	
	var optionScreenX = (screen.availWidth - optionWidth) / 2;
	var optionScreenY = (screen.availHeight - optionHeight) / 2;
	if (menubar==null)
	{
		menubar="no";
	}
	var optionList = "";
	optionList += (optionList == "" ? "" : ",") + "directories=no";
	optionList += (optionList == "" ? "" : ",") + "height=" + optionHeight;
	optionList += (optionList == "" ? "" : ",") + "location=no";
	optionList += (optionList == "" ? "" : ",") + "menubar=" + menubar;
	optionList += (optionList == "" ? "" : ",") + "resizable=yes";
	// Netscape Navigator: screenX, screenY
	optionList += (optionList == "" ? "" : ",") + "screenX=" + optionScreenX;
	optionList += (optionList == "" ? "" : ",") + "screenY=" + optionScreenY;
	// Internet Explorer: left, top
	optionList += (optionList == "" ? "" : ",") + "left=" + optionScreenX;
	optionList += (optionList == "" ? "" : ",") + "top=" + optionScreenY;
	optionList += (optionList == "" ? "" : ",") + "scrollbars=" + scrollbars;
	optionList += (optionList == "" ? "" : ",") + "status=yes";
	optionList += (optionList == "" ? "" : ",") + "toolbar=" + toolbar;
	optionList += (optionList == "" ? "" : ",") + "width=" + optionWidth;
	var newWindow = window.open(windowLocation, (windowTitle ? windowTitle : "window"), optionList);
//	newWindow.resizeTo(width,height);
	newWindow.focus();
	return newWindow;
}

var enabled  = true;
var disabled = false;

var tabs = new Object();
tabs.rechercheTab        = enabled;
tabs.resultatTab         = enabled;
tabs.proprietairesTab    = disabled;
tabs.parcellesArticleTab = disabled;
tabs.ficheParcelleTab    = disabled;
tabs.sigTab              = enabled;

function enableTab(tab)
{
	tabs[tab] = enabled;

	//
	// set image enabled
	//
}

function disableTab(tab)
{
	tabs[tab] = disabled;

	//
	// set image disabled
	//
}

function setButtonStatus(button,extention,status)
{
	var name;
	var pos;
	pos = button.src.lastIndexOf("." + extention); 
	name = button.src.substring(0,pos-1);
	if (status==false)
		button.src = name + "0." + extention;
	else
		button.src = name + "1." + extention;
}

function getObject(name)
{
	var object;
	if (document.all) // IE
	{
		object = document.all[name];
	}
	else // NN and Monzilla
	{
		if (document.getElementById) // Netscape
		{			
			object = document.getElementById (name);
		}
	}
	return object;
}

function getFormElement(formName,elementName)
{
	return document.forms[formName].elements[elementName]; // IE and NN
}

function checkEnterPressed(form,submitButton)
{
	retValue = true;
	if (document.all) // IE
	{
		if (event.keyCode && event.keyCode == 13)
		{
//	alert(getFormElement(form,submitButton).name);
			getFormElement(form,submitButton).click();
			retValue = false;
		}
	}
	else // NN and Monzilla
	{
	}
	return retValue;
}
function addThousandSeparator(number,separator) 
{  
	var returnValue = number;
	var iStart = returnValue.length;
	iStart -= 3;
	while (iStart >= 1)
	{
		returnValue = returnValue.substring(0,iStart) + separator + returnValue.substring(iStart,returnValue.length);
		iStart -= 3;
	}
	return returnValue;
}  

function trim(s)
{
	return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
}

function myValidateDate(field,datePattern)
{
	var bValid = true;
	var focusField = null;
	var i = 0;
	var value = field.value;
	var MONTH = "MM";
	var DAY = "dd";
	var YEAR = "yyyy";
	var orderMonth = datePattern.indexOf(MONTH);
	var orderDay = datePattern.indexOf(DAY);
	var orderYear = datePattern.indexOf(YEAR);
	if ((orderDay < orderYear && orderDay > orderMonth))
	{
		var iDelim1 = orderMonth + MONTH.length;
		var iDelim2 = orderDay + DAY.length;
		var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
		var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
		if (iDelim1 == orderDay && iDelim2 == orderYear)
		{
			dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})");
		}
		else if (iDelim1 == orderDay)
		{
			dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})");
		}
		else if (iDelim2 == orderYear)
		{
			dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})");
		}
		else
		{
			dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})");
		}
		var matched = dateRegexp.exec(value);
		if(matched != null)
		{
			if (!isValidDate(matched[2], matched[1], matched[3]))
			{
				if (i == 0)
				{
					focusField = field;
				}
				bValid = false;
			}
		}
		else
		{
			if (i == 0)
			{
				focusField = field;
			}
			bValid =  false;
		}
	}
	else if ((orderMonth < orderYear && orderMonth > orderDay))
	{
		var iDelim1 = orderDay + DAY.length;
		var iDelim2 = orderMonth + MONTH.length;
		var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
		var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
		if (iDelim1 == orderMonth && iDelim2 == orderYear)
		{
			dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})");
		}
		else if (iDelim1 == orderMonth)
		{
			dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})");
		}
		else if (iDelim2 == orderYear)
		{
			dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})");
		}
		else
		{
			dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})");
		}
		var matched = dateRegexp.exec(value);
		if(matched != null)
		{
			if (!isValidDate(matched[1], matched[2], matched[3]))
			{
				if (i == 0)
				{
					focusField = field;
				}
				bValid =  false;
			}
		}
		else
		{
			if (i == 0)
			{
				focusField = field;
			}
			bValid =  false;
		}
	}
	else if ((orderMonth > orderYear && orderMonth < orderDay))
	{
		var iDelim1 = orderYear + YEAR.length;
		var iDelim2 = orderMonth + MONTH.length;
		var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
		var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
		if (iDelim1 == orderMonth && iDelim2 == orderDay)
		{
			dateRegexp = new RegExp("^(\\d{4})(\\d{2})(\\d{2})");
		}
		else if (iDelim1 == orderMonth)
		{
			dateRegexp = new RegExp("^(\\d{4})(\\d{2})[" + delim2 + "](\\d{2})");
		}
		else if (iDelim2 == orderDay)
		{
			dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})(\\d{2})");
		}
		else
		{
			dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{2})");
		}
		var matched = dateRegexp.exec(value);
		if(matched != null)
		{
			if (!isValidDate(matched[3], matched[2], matched[1]))
			{
		    	if (i == 0)
		    	{
					focusField = field;
				}
				bValid =  false;
			}
		}
		else
		{
			if (i == 0)
			{
				focusField = field;
			}
			bValid =  false;
		}
	}
	else
	{
		if (i == 0)
		{
			focusField = field;
		}
		bValid =  false;
	}
    if (focusField!=null)
	{
    	focusField.focus();
	}
    return bValid;
}

function isValidDate(day, month, year)
{
	if (month < 1 || month > 12)
	{
        return false;
    }
    if (day < 1 || day > 31)
    {
        return false;
    }
    if ((month == 4 || month == 6 || month == 9 || month == 11) && (day == 31))
	{
        return false;
    }
    if (month == 2)
    {
        var leap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day>29 || (day == 29 && !leap))
		{
            return false;
        }
    }
    return true;
}
