// SVN $Id: forms.js 33585 2008-10-15 12:12:56Z dev2003\aschiessl $
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
 
function setDealerPrivate(whichToSet){
	var onlyCheckBox = document.forms['searchform'].elements['adtype'];
	var onlyDealerCheckBoxState = onlyCheckBox[0].checked;
	var onlyPrivateCheckBoxState = onlyCheckBox[1].checked;

	onlyCheckBox[0].checked = false;
	onlyCheckBox[1].checked = false;

	if (whichToSet == 'D'){
		onlyCheckBox[0].checked = onlyDealerCheckBoxState;
	}
	else{
		onlyCheckBox[1].checked = onlyPrivateCheckBoxState;
	}
}

function deselectRadios(radioName)
{
	var radios = document.getElementsByName(radioName);
	if (radios.value == -1) return;
	if (radios != null)
	{
		for (var i = 0; i < radios.length; i++)
		{
			radios[i].checked = false;
		}
	}
}

var lastSelectedRadio=null;
function deselectIfSelected(radio)
{
    if (lastSelectedRadio == null)
    {
        lastSelectedRadio = radio;
        return;
    }
    if (lastSelectedRadio == radio)
    {
        radio.checked = false;
        lastSelectedRadio = null;
    }
    else
    {
        lastSelectedRadio = radio;
    }
}


function selectFinanceSorting(selectName, boxControl)
{
    if (boxControl.selectedIndex > 0)
    {
        var listBox = document.getElementById(selectName);
        
        if (listBox != null)
        {
            listBox.selectedIndex = 1;
        }
    }
}

/* Deselect "all europe" checkbox if one of the  
	country checkboxes is unchecked */
function countryCheckBoxClicked(cb, europeId, otherCountriesId)
{
	if (cb.checked==false)
	{
		gE(europeId).checked=false;
	}
	
	checkRadiusSearchPossible(europeId, otherCountriesId)
}

function europeCheckBoxClicked(parentId, europe, defaultCbId)
{
	var children = gE(parentId).getElementsByTagName("input");
	
	if (europe.checked==true)
	{
		enableCountryCheckboxes(true, europe, children);
		disableRadiusSearchAvailability(true);
	}
	else
	{
		enableCountryCheckboxes(false, europe, children);
		var defaultCb = gE(defaultCbId);
		if (defaultCb != null)
		{
			defaultCb.checked = true;
			disableRadiusSearchAvailability(false);
		}
		else
		{
			disableRadiusSearchAvailability(true);
		}
	}
}

function enableCountryCheckboxes(enable, europe, children)
{
	for (var i = 0; i < children.length;  i++)
		{
			if (children[i].type=='checkbox')
			{
				if (children[i] != europe)
				{
					children[i].checked = enable;
				}
			}
		}
}

/* Enables or disables the radius search input controls */
function disableRadiusSearchAvailability(disabled)
{
		var textBox = gE('radiusDiv').getElementsByTagName('input');
		var dropDown = gE('radiusDiv').getElementsByTagName('select');
		
		dropDown[0].selectedIndex = 0;
		if (disabled == true)
		{
		    textBox[0].value = textBox[0].defaultText;
		    //textBox[0].blur();
		    
		}
		textBox[0].disabled = disabled;
		dropDown[0].disabled = disabled;
		
		if(dropDown[0].disabled)
		{
		    AddCssClassToItem(inputDisabled, textBox[0]);
		    AddCssClassToItem(selectDisabled, dropDown[0]);
		}
		else
		{
		    RemoveCssClassFromItem(inputDisabled, textBox[0]);
		    RemoveCssClassFromItem(selectDisabled, dropDown[0]);		
		}
}

function checkRadiusSearchPossible(europeId, otherCountriesId)
{
	var countryCheckboxes = gE('countryTable').getElementsByTagName("input");
	var otherCountries = gE(otherCountriesId);
	var europe = gE(europeId)
	var checkedOnes = 0;
	if (otherCountries.checked==true || europe.checked==true)
	{
		disableRadiusSearchAvailability(true);
	}
	else
	{
		for (var i = 0; i < countryCheckboxes.length; i++)
		{
			if (countryCheckboxes[i].id != europeId && countryCheckboxes[i].id != otherCountriesId)
			{
				if (countryCheckboxes[i].checked)
				{
					checkedOnes++;
				}
			}
		}
		(checkedOnes == 1) ? disableRadiusSearchAvailability(false) : disableRadiusSearchAvailability(true);
	}
}

// Input Type:
// 1 = Textbox
// 2 = Multiline Textbox
// 3 = Button
// 4 = CheckBox
// 5 = Dropdown
function FormElement(id, inputType)
{
	this.Id = id;
	this.InputType = inputType;
}

function hideModalLayer()
{
	gE('modalLayer').style.display='none';
}

function showModalLayer()
{
	gE('modalLayer').style.display='block';
}

function hideFeedbackForm()
{
	hideModalLayer()
	gE('feedback-form').style.display='none';
}

var tabs;
function showHideForm(f,dis,pn,elemList)
{
	gE('modalLayer').style.display=dis;
	gE(f).style.display=dis;
	tabs=(dis=='none'?null:elemList);
	if (elemList)
	{
		if (dis!='none')
		{
			gE(elemList[0].Id).focus();
			// attach event listener
			if (document.addEventListener)
				document.addEventListener('keydown',handleKeyDown,false);
			else
				document.attachEvent('onkeydown',handleKeyDown);
		}
		else
		{
			// detach event listener
			if (document.removeEventListener)
				document.removeEventListener('keydown',handleKeyDown,false);
			else
				document.detachEvent('onkeydown',handleKeyDown);
		}
	}
	// omniture
	if (pn)
	{
		if (typeof(CheckPageName)=='function') pn=CheckPageName(pn);
		try
		{
			s.pageName=pn;
			s.events='';
			var s_code=s.t();
			if (s_code) document.write(s_code);
		}
		catch (e) {}
	}
	if (BrowserDetect.browser=='Explorer' && BrowserDetect.version<=6)
	{
		var sel=document.getElementsByTagName('select');
		for (var i=0;i<sel.length;i++) 
			if (sel[i].id.indexOf('insertionBugType')==-1) sel[i].style.display=(dis=='block'?'none':'');
	}
}

var formOpened=false;
function startupContact()
{
	if (formOpened) return;
	showHideForm('contact-form','block',s.pageName+'-Email',contactFormElements);
	formOpened=true;
}
function handleKeyDown(e)
{
	// get current event
	var evt=(window.event ? window.event : e);
	if (evt)
	{
		// get current key code
		var code=(evt.keyCode?evt.keyCode:evt.which);
		if (code==9 || code==13)
		{
			// get current sender and find it's id in the tabs collection
			var curId=(evt.srcElement?evt.srcElement.id:evt.target.id);
			var shift=evt.shiftKey;
			var curElement=-1;
			for (var i=0;i<tabs.length;i++)
			{
				if (tabs[i].Id==curId)
				{
					curElement=i;
					break;
				}
			}
			if (code==9)
			{
				// handle tab
				if (shift) 
				{
					// shift key is pressed
					curElement--; 
					if (curElement<0) curElement=tabs.length-1;
				}
				else
				{ 
					// only tab is pressed
					curElement++; 
					if (curElement>=tabs.length) curElement=0;
				}
				gE(tabs[curElement].Id).focus();
				// prevent event bubbling
				if (evt.preventDefault)
					evt.preventDefault();
				else
					evt.returnValue=false;
				return false;
			}
			if (code==13)
			{
				// handle enter
				if (tabs[curElement].InputType==3)
				{
					// submit on single line and button
					document.submit();
				}
				if (tabs[curElement].InputType!=2)
				{
					// prevent event bubbling when not an multiline box
					if (evt.preventDefault)
						evt.preventDefault();
					else
						evt.returnValue=false;
					return false;
				}
			}
		}
	}
}

function HidePhonePrefix(idCountry, idPrefix, idNumber)
{
	var elemCountry=gE(idCountry);
	var text=elemCountry.options[elemCountry.selectedIndex].text;
	if (text == '+33' || text == '+34' || text == '+352')
	{
		gE(idPrefix).value='';
		gE(idPrefix).style.display='none';
		gE(idPrefix).style.position='absolute';
		gE(idNumber).className='input-2col';
	}
	else
	{
		gE(idPrefix).style.display='';
		gE(idPrefix).style.position='';
		gE(idNumber).className='phone';
	}
}

function ValidateString(idInput, source, regEx)
{
	var ret;
	var elem = gE(idInput);
	var v = new RegExp(regEx);
	var m = v.exec(elem.value.trim());
	ret = (m != null && elem.value.trim() == m[0].trim());
	return ret;
}

function ValidateZip(idInput, idCountry, source, zipInfos)
{
	var ret;
	var countryElem = gE(idCountry);
	var zipCodeElem = gE(idInput);
	var countryVal = countryElem.value;
	var zipCodeVal = zipCodeElem.value;
	
	var zipRegEx = zipInfos[0].zipRegEx;
	
	for (var i = 1; i < zipInfos.length; i++)
	{
		if (countryVal == zipInfos[i].country && zipInfos[i].zipRegEx != undefined)
		{
			zipRegEx = zipInfos[i].zipRegEx;
			break;
		}
	} 

	var v = new RegExp(zipRegEx);
	var m = v.exec(zipCodeElem.value);
	ret = (m != null);
	
	return ret;
}

function getPhoneInfo(country)
{
	var ret = new phoneInfo(phoneInfos[0].country, phoneInfos[0].hasPrefix, phoneInfos[0].prefixRegEx, phoneInfos[0].numberRegEx, phoneInfos[0].prefixMobileRegEx, phoneInfos[0].numberMobileRegEx);
	for (var i = 1; i < phoneInfos.length; i++)
	{
		if (country == phoneInfos[i].country)
		{
			ret.country = phoneInfos[i].country;
			ret.hasPrefix = phoneInfos[i].hasPrefix;
			if (phoneInfos[i].prefixRegEx != '')
				ret.prefixRegEx = phoneInfos[i].prefixRegEx;
			if (phoneInfos[i].numberRegEx != '')
				ret.numberRegEx = phoneInfos[i].numberRegEx;
			if (phoneInfos[i].prefixMobileRegEx != '')
				ret.prefixMobileRegEx = phoneInfos[i].prefixMobileRegEx;
			if (phoneInfos[i].numberMobileRegEx != '')
				ret.numberMobileRegEx = phoneInfos[i].numberMobileRegEx;
			break;
		}
	} 
	return ret;
}

function ValidatePhonePrefix(idInput, idPhoneType, idCountry, source, phoneInfos, validateToTrueIfEmpty, validateToFalseIfHasNoPrefix)
{
	return ValidatePhonePrefixType(idInput, gE(idPhoneType).value, idCountry, source, phoneInfos, validateToTrueIfEmpty, validateToFalseIfHasNoPrefix);
}

function ValidatePhonePrefixType(idInput, phoneTypeVal, idCountry, source, phoneInfos, validateToTrueIfEmpty, validateToFalseIfHasNoPrefix)
{
	var ret = true;
	var regEx;
	var countryElem = gE(idCountry);
	var inputElem = gE(idInput);
	
	var countryVal = countryElem.value;
	var inputVal = inputElem.value;
	
	var phoneInfo = getPhoneInfo(countryVal)
	if (validateToFalseIfHasNoPrefix && !(phoneInfo.hasPrefix))
	{
		ret = false;
	}
	else
	{
		if (!(validateToTrueIfEmpty) || trim(inputVal) != '')	
		{
			if (phoneInfo.hasPrefix)
			{
				if (phoneTypeVal == 'M')
				{
					regEx = phoneInfo.prefixMobileRegEx;
				}
				else
				{
					regEx = phoneInfo.prefixRegEx;
				}
				var v = new RegExp(regEx);
				var m = v.exec(inputVal);
				ret = (m != null);
			}
		}
	}

	return ret;
}

function ValidatePhoneNumber(idInput, idPhoneType, idCountry, source, phoneInfos, validateToTrueIfEmpty)
{
	return ValidatePhoneNumberType(idInput, gE(idPhoneType).value, idCountry, source, phoneInfos, validateToTrueIfEmpty)
}

function ValidatePhoneNumberType(idInput, phoneTypeVal, idCountry, source, phoneInfos, validateToTrueIfEmpty)
{
	var ret = true;
	var regEx;
	var countryElem = gE(idCountry);
	var inputElem = gE(idInput);
	
	var countryVal = countryElem.value;
	var inputVal = inputElem.value;
	
	if (!(validateToTrueIfEmpty) || trim(inputVal) != '')	
	{
		var phoneInfo = getPhoneInfo(countryVal)
		if (phoneTypeVal == 'M')
		{
			regEx = phoneInfo.numberMobileRegEx;
		}
		else
		{
			regEx = phoneInfo.numberRegEx;
		}
		var v = new RegExp(regEx);
		var m = v.exec(inputVal);
		ret = (m != null);
	}
	return ret;
}

function CompareTextBoxEntries(id1, id2, source, required)
{
	var ret;
	var elem1 = gE(id1);
	var elem2 = gE(id2);
	var val1 = elem1.value.trim();
	var val2 = elem2.value.trim();
	if (val1 == '' && val2 == '')
	{
		ret =! required;
	}
	else
	{
		ret = (val1 == val2);
	}
	return ret;
}

function OpenFormAndHideMe(formId, meId, anchor)
{
	var f=gE(formId);
	f.style.display='block';
	gE(meId).style.display='none';
	var ot=f.offsetTop;
	if (BrowserDetect.browser=='Explorer') ot+=200;
	if (anchor)	window.scrollTo(0, ot);
	var inputs=f.getElementsByTagName('input');
	if (inputs.length>0)
	{
		for (var i=0;i<inputs.length;i++)
		{
			if (inputs[i].type=='text')
			{
				inputs[i].focus();
				break;
			}
		}
	}
}

function DisplayFurtherPhones()
{
	var pl;
	for (pl=2;pl<5;pl++)
	{	
		var elem=gE('phoneline'+pl);
		if (elem.style.display!='block')
		{
			elem.style.display='block';
			break;
		}
	}
	if (pl==4) gE('furtherPhonesText').style.display='none';
}