/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

/* 
	Eingaben im Formular überprüfen
 */
function checkForm()
{
	with (window.document.wohnungForm) {
		if (isEmpty(miete, 'Bitte einen Mietbetrag eingeben.')) {
			return;
		}
		else if (isEmpty(qm, 'Bitte die Quadratmeterzahl eingeben.')) {
			return;
		}
		else if (isEmpty(zimmer, 'Bitte die Zimmeranzahl eingeben (ohne Küche und WC).')) {
			return;
		}
		else if (isEmpty(strasse, 'Bitte eine Straße eintragen.')) {
			return;
		}
		else if (isEmpty(nr, 'Bitte eine Hausnummer eintragen.')) {
			return;
		}
		else if (!CheckDate(ab.value)) {
			alert('Bitte richtiges Datumsformat für den Einzugstermin verwenden.');
			ab.focus();
			return;
		}
		else if (!CheckDate(bis.value) && nachmiete.checked == false) {
			alert('Bitte richtiges Datumsformat für den Auszugstermin verwenden.');
			bis.focus();
			return;
		}
		else {
			submit();
		}
	}
}

function CheckDate(ChkDate)
{
	var Year=parseInt(ChkDate.substring(6,10), 10);
	var Month=parseInt(ChkDate.substring(3,5), 10);
	var Day=parseInt(ChkDate.substring(0,2), 10);

	if ((ChkDate.charAt(2)==".") && (ChkDate.charAt(5)=="."))
	{
		if ((Day<=31) && (Day>=1) && (Month>=1) && (Month<=12))
		{
			if((Month==1) || (Month==3) || (Month==5) || (Month==7) || (Month==8) || (Month==10) || (Month==12)){ return true; }
			else
			{
				if ((Day<=30) && (Day>=1))
				{
					if((Month==4) || (Month==6) || (Month==9) || (Month==11)) { return true; }
					else
					{
						if ((Day<=28) && (Day>=1)) { return true; }
						else
						{
							if(Day==29)
							{
								if ((Year%4)==0)
								{
									if ((Year%100)!=0) { return true; }
									else
									{
										if ((Year%400)==0) { return true; }
										else { return false; }
									}
								}
								else { return false; }
							}
							else { return false; }
						}
					}
				}
				else { return false; }
			}
		}
		else { return false; }
	}
	else { return false; }
}

