function replaceAll(text, strA, strB)
{
    return text.replace( new RegExp(strA,"g"), strB );    
}

function GetObj(objectId)
{
	return document.getElementById(objectId);
}

function isObj(obj)
{
	if(obj) return true;
	return false;
}

function checkInput(obj, value)
{
	if(obj)
	{
		setMessage("");
		
		if(obj.value == value)
		{
			obj.value = "";
		}
		else if(obj.value == "")
		{
			obj.value = value;
		}
	}
}

function isEmpty(obj)
{
	if(obj.value == "") return true;
	return false;
}

function isEmail(obj)
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (filter.test(obj.value)) return true;
	return false;
}

function isPostcode(obj)
{
	var filter = /^[1-9]{1}[0-9]{3}[a-zA-Z]{2}$/;
	
	if (filter.test(obj.value)) return true;
	return false;
}

function isSameAsTitle(obj)
{
	if(obj.value == obj.title) return true;
	return false;
}

function focusObject(obj)
{
	obj.focus();
}

function setMessageById(value, messageId)
{
	objMessage = document.getElementById(messageId);
	
	if(objMessage)
	{
		objMessage.innerHTML = value;
	}
}

function setMessage(value, objectId)
{
	objMessage = document.getElementById(objectId);
		
	if(objMessage)
	{
		objMessage.innerHTML = value;
	}
	else
	{
		objMessage = document.getElementById('message');
		
		if(objMessage)
		{
			objMessage.innerHTML = value;
		}
	}
}

// T Leon

function SendBookingRequest()
{
	objCaptcha 	= document.getElementById('captcha');
	
	if(objCaptcha)
	{
		if(isEmpty(objCaptcha) || isSameAsTitle(objCaptcha))
		{
			focusObject(objCaptcha);
			setMessage("Please, enter the <strong>captcha</strong> code!");
			return;
		}
		
		document.BookingForm.submit();
	}
}

function SendSearch()
{
	objName		= document.getElementById('name');
	objAddress	= document.getElementById('address');
	objCity		= document.getElementById('city');
	objPhone	= document.getElementById('phone');
	objEmail	= document.getElementById('email');
	objBrand	= document.getElementById('brand');
	objModel	= document.getElementById('model');
	objPrice	= document.getElementById('price');
	objComment	= document.getElementById('comment');
	
	
	if(objName && objAddress && objCity && objPhone && objEmail && objBrand && objModel && objPrice && objComment)
	{
		if(isEmpty(objName) || isSameAsTitle(objName))
		{
			focusObject(objName);
			setMessage("U bent vergeten uw <strong>naam</strong> in te vullen!");
			return;
		}
		
		if(isEmpty(objAddress) || isSameAsTitle(objAddress))
		{
			focusObject(objAddress);
			setMessage("U bent vergeten uw <strong>adresgegevens</strong> in te vullen!");
			return;
		}
		
		if(isEmpty(objCity) || isSameAsTitle(objCity))
		{
			focusObject(objCity);
			setMessage("U bent vergeten uw <strong>postcode of plaats</strong> in te vullen!");
			return;
		}
		
		if(isEmpty(objPhone) || isSameAsTitle(objPhone))
		{
			focusObject(objPhone);
			setMessage("U bent vergeten uw <strong>telefoonnummer</strong> in te vullen!");
			return;
		}
		
		if(isEmpty(objEmail) || isSameAsTitle(objEmail))
		{
			focusObject(objEmail);
			setMessage("U bent vergeten uw <strong>e-mailadres</strong> in te vullen!");
			return;
		}
		
		if(!isEmail(objEmail))
		{
			focusObject(objEmail);
			setMessage("Het opgegeven <strong>e-mailadres</strong> is onjuist!");
			return;
		}
		
		if(isEmpty(objBrand) || isSameAsTitle(objBrand))
		{
			focusObject(objBrand);
			setMessage("U bent vergeten het <strong>merk</strong> in te vullen!");
			return;
		}
		
		if(isEmpty(objModel) || isSameAsTitle(objModel))
		{
			focusObject(objModel);
			setMessage("U bent vergeten het <strong>model</strong> in te vullen!");
			return;
		}
		
		if(isEmpty(objPrice) || isSameAsTitle(objPrice))
		{
			focusObject(objPrice);
			setMessage("U bent vergeten de <strong>prijsklasse</strong> in te vullen!");
			return;
		}
		
		if(isEmpty(objComment) || isSameAsTitle(objComment))
		{
			focusObject(objComment);
			setMessage("U bent vergeten uw <strong>opmerking</strong> in te vullen!");
			return;
		}
		
		document.searchForm.submit();
	}
}

function SendContactRequest()
{
	objReaction	= document.getElementById('contact_reaction');
	objName 	= document.getElementById('contact_name');
	objEmail 	= document.getElementById('contact_email');
	objCaptcha 	= document.getElementById('captcha');
	
	if(objReaction && objName && objEmail && objCaptcha)
	{
		if(isEmpty(objName) || isSameAsTitle(objName))
		{
			focusObject(objName);
			setMessage("Please, fill out your <strong>name</strong>!");
			return;
		}
		
		if(isEmpty(objEmail) || isSameAsTitle(objEmail))
		{
			focusObject(objEmail);
			setMessage("Please, fill out your <strong>e-mail address</strong>!");
			return;
		}
		// hendrik
		if(!isEmail(objEmail))
		{
			focusObject(objEmail);
			setMessage("The given e-mail address is not valid!");
			return;
		}
		
		if(isEmpty(objReaction) || isSameAsTitle(objReaction))
		{
			focusObject(objReaction);
			setMessage("Please, fill out your <strong>reaction</strong>!");
			return;
		}
		
		if(isEmpty(objCaptcha) || isSameAsTitle(objCaptcha))
		{
			focusObject(objCaptcha);
			setMessage("Please, enter the <strong>captcha</strong> code!");
			return;
		}
		
		document.ContactForm.submit();
	}
}

function ReloadCaptcha()
{
	ImageObj = GetObj('captchaimage');
	
	if(ImageObj)
	{
		ImageObj.src = "";
		ImageObj.src = "captcha/dynamicimage.php";
	}
}

function Over(object, color)
{
	object.style.backgroundColor = color;
}

function Out(object)
{
	object.style.backgroundColor = 'transparent';
}

function ChangeSource(oImg)
{
	var oLargeImage = document.getElementById('g_foto');
	
	
	if(oImg && oLargeImage)
	{
		oLargeImage.src = oImg.src;
	}
}

function SendTaxatie()
{
	document.TaxatieForm.submit();
}

function SendAfhaalopdracht()
{
	var bSendForm = true;
	var aMessages = new Array(
					"U bent vergeten het <strong>kenteken</strong> in te vullen",
					"U bent vergeten de <strong>duplicaatcode</strong> in te vullen",
					"U bent vergeten de <strong>meldcode</strong> in te vullen",
					"U bent vergeten de <strong>kilometerstand</strong> in te vullen",
					"U bent vergeten de <strong>koopprijs</strong> in te vullen",
					"U bent vergeten de <strong>dealernaam</strong> in te vullen");
					
	var aFields = new Array("kenteken","duplicaatcode","meldcode","kmstand","koopprijs","dealernaam");
	
	for(var i = 0; i < aFields.length; i++)
	{
		var oTemp = document.getElementById(aFields[i]);
		
		if(isEmpty(oTemp) || isSameAsTitle(oTemp))
		{
			focusObject(oTemp);
			setMessage(aMessages[i]);
			return;
		}
	}
	
	alert("Verstuur form");
	
	document.AfhaalForm.submit();
}

function LoadCars(sId, sUrl)
{
	$('#' + sId).load(sUrl);
}

function LoadModels(oBrandSelect, sUrl)
{
	sUrl = replaceAll(sUrl,"main.php","buildselector.php");	
	
	if(oBrandSelect.value != '')
	{
		var sBrand = oBrandSelect.value;
		
		sUrl += '&merk=' + sBrand;
		
		sUrl = replaceAll(sUrl," ","%20");
		
		$('#ModelsHolder').load(sUrl);
		$('#TypesHolder').html('');
	}
	else
	{
		$('#ModelsHolder').html('');
		$('#TypesHolder').html('');
	}
}

function LoadTypes(oModelSelect, sUrl)
{
	oBrandSelect = document.getElementById('IdMerk');
	
	sUrl = replaceAll(sUrl,"main.php","buildselector.php");	
	
	if(oModelSelect.value != '')
	{
		var sBrand = oBrandSelect.value;
		sUrl += '&merk=' + sBrand;
		
		var sModel = oModelSelect.value;		
		sUrl += '&model=' + sModel;
		
		sUrl = replaceAll(sUrl," ","%20");
		
		$('#TypesHolder').load(sUrl);
	}
	else
	{
		$('#TypesHolder').html('');
	}
}

function search()
{
	formId = document.getElementById('SearchForm');
	oPage = document.getElementById('IdPage');
	
	if(formId)
	{
		oPage.value = ''; /*Clear the pager*/
		
		formId.submit();
	}
}

function Navigate(sPage)
{
	formId = document.getElementById('SearchForm');
	oPage = document.getElementById('IdPage');
	
	if(formId)
	{
		oPage.value = sPage;
		
		formId.submit();
	}
}

function Sort(sField, sType)
{
	formId = document.getElementById('SearchForm');
	oSort = document.getElementById('IdSort');
	oSortType = document.getElementById('IdSortType');
	
	if(formId)
	{
		oSort.value = sField;
		oSortType.value = sType;
		
		formId.submit();
	}
}

function clear()
{
	
}

