function init()
{
	initMenu();

	try {
		initScroll();
	}
	catch(e) {}
}

function initScroll()
{
	var __scrollX = document.forms[0].__scrollX.value;
	var __scrollY = document.forms[0].__scrollY.value;
	
	if (__scrollX != "" || __scrollY != "")
	{
		if (document.all)
			window.scrollTo(__scrollX, __scrollY);
		else
			window.scroll(__scrollX, __scrollY);

		document.forms[0].__scrollX.value = "";
		document.forms[0].__scrollY.value = "";
	}
}

function saveScroll()
{
  document.forms[0].__scrollX.value = (document.all)?document.body.scrollLeft:window.pageXOffset;
  document.forms[0].__scrollY.value = (document.all)?document.body.scrollTop:window.pageYOffset; 
}


function open_genericPopup(theUrl,xtraParams)
{
	var genericPopup = '';
	genericPopup = window.open(theUrl,'genericPopup',xtraParams)
	if (!genericPopup.opener)
	        genericPopup.opener = self; 
}

function SetNavigateActionAsPopup(theForm, navigateAction)
{
	ClearFieldGroups();
	
	var targetname = "myPopup";
		
	document[theForm].__NAVIGATEACTION.value = navigateAction;
	document[theForm].action = "PopupPage.aspx";
	document[theForm].target = targetname;	
	var targetwin = window.open("", targetname, "resizable=yes, scrollbars=yes, width=710, height=450");	
	document[theForm].submit();
	document[theForm].action = document.location;
	document[theForm].target = "";
	targetwin.focus(); 
}

function loadXml(xmlString)
{
	var xmlDoc;
	
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		oParser = new DOMParser()
		xmlDoc = oParser.parseFromString(xmlString, "text/xml");
		xmlDoc.async = "false";
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.loadXML(xmlString);
		xmlDoc.async = "false";
 	}
	else
	{
		return;
	}
	return xmlDoc;
}

function NA_autoTab(input,len, e) 
{				
	var isNN = (navigator.appName.indexOf("Netscape") !=-1);				
	
	var keyCode = (isNN) ? e.which : e.keyCode; 				
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46]; 	
	
	if(input.value.length >= len && !NA_containsElement(filter,keyCode)) 
	{	
		input.value = input.value.slice(0, len);			
		
		// Als het volgende veld hidden is, dan trad er een error op bij het focussen.
		if (input.form[(NA_getIndex(input)+1) % input.form.length].type != "hidden") {
			input.form[(NA_getIndex(input)+1) % input.form.length].focus();
			input.form[(NA_getIndex(input)+1) % input.form.length].select();
		}
	
	};
			
	function NA_containsElement(arr, ele) {			
		var found = false, index = 0;
		while(!found && index < arr.length) {
			if(arr[index] == ele) {
				found = true;	
			} else {
				index++;
			};
		};		
		return found;
	};
	

	function NA_getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1) {
			if (input.form[i] == input) {
				index = i;
			} else {
				i++;
			};
		};
		return index;
	};
	

	return true;
	
};

//Sujesh 24/8/04 FT0466 - Postcode aanpassen aan standaard
function BewerkPostcode(postcode)
{		
	if (postcode.value.length == 0)
	{
		return true;
	}
			
	//spatie tussen numerieke waarden en alfanumerieke waarden gooien
	//alleen doen indien geldige postcode is toegevoegd
	var reg = /\d{4} ?[a-zA-Z]{2}/;
	if (reg.test(postcode.value)) 
	{ // if syntax is valid
	
		//karakters naar uppercase wijzigen
		postcode.value = postcode.value.toUpperCase();        
		if (postcode.value.length == 6)
		{
			postcode.value = postcode.value.substring(0,4) + " " + postcode.value.substring(4,6);
		}			
		return true;
	} 
	else 
	{
		return;
	}	
}

function SetCustomListAction(productCode, serviceName, itemId)
{
	//redirect naar de servicepagina
	window.location =	'ServicePage.aspx' +
						'?ProductCode=' + productCode + 
						'&serviceName=' + serviceName +
						'&transId=' + itemId;
}

function SetListAction(theForm, theList, theItemID, theAction)
{
	ClearListAction(theForm);
	
	document[theForm].__NA_listName.value = theList;
	document[theForm].__NA_listItemID.value = theItemID;
	document[theForm].__NA_listAction.value = theAction;
	document[theForm].submit();
}

function SetListActionAsPopup(theForm, theList, theItemID, theAction)
{
	ClearListAction(theForm);
	
	var targetname = "myPopup";
	
	document[theForm].__NA_listName.value = theList;
	document[theForm].__NA_listItemID.value = theItemID;
	document[theForm].__NA_listAction.value = theAction;
	
	document[theForm].action = document.location;
	document[theForm].target = targetname;
	
	var targetwin = window.open("", targetname, "resizable=yes, scrollbars=yes, width=300, height=200");
	document[theForm].submit();
		
	targetwin.focus();
}

function ClearListAction(theForm)
{
	if (document[theForm].__NAVIGATEACTION != null) 
	{
		document[theForm].__NAVIGATEACTION.value = '';
	}

	if (document[theForm].__NA_listName != null) 
	{
		document.forms[theForm].__NA_listName.value = '';
		document.forms[theForm].__NA_listItemID.value = '';
		document.forms[theForm].__NA_listAction.value = '';
	}
}

function GetObject(obj)
{
	var theObj;

	if (document.layers) 
	{
		if (typeof obj == 'string') 
		{
			return document.layers[obj];
		} else {
			return obj;
		}
	}

	if (document.all) 
	{
		if (typeof obj == 'string') 
		{
			return document.all(obj);
		} else {
			return obj;
		}
	}

	if (document.getElementById) 
	{
		if (typeof obj == 'string') 
		{
			return document.getElementById(obj);
		} else {
			return obj;
		}
	}

	return null;
}

function submitForm(form) 
{
	ClearFieldGroups();
	form.submit();
}

function ClearFieldGroups(groupName) 
{
	var fieldgroups = document.getElementsByTagName("div");
	
	for (i=0; i < fieldgroups.length; i++)
	{
		var id = fieldgroups[i].id;
	
		if (id != "") {
			id = id.substring(id.length - 6, id.length);
			 
			if (groupName == null && id == "_group") 
				fieldgroups[i].innerHTML = "";
			else if (fieldgroups[i].id == groupName + "_group")
				fieldgroups[i].innerHTML = "";
		}
	}		
}

function ShowAdditionalFields(id, mode)
{
	var sourceObj = GetObject(id + '_group');
	var targetObj = GetObject(id + '_placeholder');

	var Group = (sourceObj.length == null) ? sourceObj : sourceObj[0];
	var PlaceHolder = (targetObj.length == null) ? targetObj : targetObj[0];
	
	if (PlaceHolder && Group)
	{
		switch (mode)
		{
			case 'collapse':
				if (targetObj.innerHTML != '')
				{
					ClearAdditionalFields(Group);
					ClearAdditionalFields(PlaceHolder);
					PlaceHolder.innerHTML = "";
				}
				break;

			case 'expand':
				try 
				{
					if (Group.innerHTML != '')
					{
						PlaceHolder.innerHTML = Group.innerHTML;
					} 
				}
				catch(e)
				{
					alert(e.message);
				}
				break;

			default:
				break;
		}
	} 
}

function ClearAdditionalFields(ObjectContainer)	
{								
	var inputColl		= ObjectContainer.getElementsByTagName("select");				
	var inputColl_Input = ObjectContainer.getElementsByTagName("input");
	var inputColl_Textarea = ObjectContainer.getElementsByTagName("textarea");

	for (i=0;i<inputColl.length;i++)
	{
		inputColl[i].value= "";
	}

	for (i=0;i<inputColl_Input.length;i++)
	{
		if(inputColl_Input[i].type != 'radio' && inputColl_Input[i].type != 'checkbox')
        {
            inputColl_Input[i].value= "";           
        }
        else
        {
            inputColl_Input[i].checked = "";
        }      
	}	

	for (i=0;i<inputColl_Textarea.length;i++)
	{
		inputColl_Textarea[i].value= "";
	}
}

function PrintDocs(form, formaction, windowheight, windowwidth) 
{
	var targetname = "defpopup";
	
	form.action	= formaction;
    form.target = targetname;
	form.hasprinted.value = "true";
		
	var targetwin = window.open("", targetname, "resizable=no, scrollbars=yes, width=" + windowwidth + ",height=" + windowheight)
	form.submit();


	form.action = "";
	form.target = "";
	form.__NAVIGATEACTION.value = "";
	return;
}

function checkPrintOptions(theForm) {
	var optionsOK = true;
	
	var numOptions = 0;
	var checkedOptions = 0;
	
	if (theForm.pr_mail_brief) 
	{
		numOptions++;
		if (theForm.pr_mail_brief.checked != false) checkedOptions++;
	}
	
	if (theForm.pr_mail_offerte) 
	{
		numOptions++;
		if (theForm.pr_mail_offerte.checked != false) checkedOptions++;
	}
	
	if (theForm.pr_mail_polisformulier) 
	{
		numOptions++;
		if (theForm.pr_mail_polisformulier.checked != false) checkedOptions++;
	}
	
	if (numOptions > 0) 
	{
		if (checkedOptions == 0) optionsOK = false;
	}
	
	return optionsOK;
}

function PrintDocsPDF(formName, targetName, formAction, windowHeight, windowWidth)
{
	var form = document.forms[formName];	
	
	form.action = formAction;
	form.target = targetName;
	
	var targetWindow = window.open("", targetName, "resizable=no, scrollbars=no, location=no, menubar=no, toolbar=no, width=" + windowWidth + ", height=" + windowHeight + "");
	
	form.submit();
	targetWindow.focus();
	
	form.action = "";
	form.target = "";
	
	// MW: Form niet direct submitten omdat anders je response is verdwenen. Hierdoor wordt het downloadwindow niet getoond.
	// form.submit();
}

//CHO:functie voor popup window opzegkaart
function MailOpzegkaart(path)
{
	var form = document.forms['Opzegkaart']; 
	
	if(OpzegkaartValidation(form))
	{
		var targetname = "defpopup";
		
		form.action	= path; //'http://ketenint2.turien-backoffice.nl/Download/MailForm.aspx?Type=1';//nl.Turien.Download.UI.ForceerDownload.aspx';
		form.target = targetname;
					
		var targetwin = window.open("", targetname, "resizable=no, scrollbars=no, width=300, height=200");
		form.submit();
		
		targetwin.focus();
		
		form.action = "";
		form.target = "";

		form.submit();
		
		form.__NAVIGATEACTION.value = "";
		return;	
	}	
	else
	{
		alert('Vul a.u.b. alle velden in.');
	}
}

function OpzegkaartValidation(form)
{
	result = true;
	
	var huidigeVerzekeraar	= form.ZE_MYAAND.value;	
	var huidigePolisnummer	= form.ZE_MYCODE.value;	
	var huidigeAdres		= form.huidigeverz_adres.value;	
	var huidigePostcode		= form.huidigeverz_pcode.value;	
	var huidigePlaats		= form.huidigeverz_plaats.value;
	var gezinsleden			= getCheckedValue(form.gezinsleden);
	var opzeggenGezinsleden = getCheckedValue(form.opzeggen_gezinsleden);

	if(	huidigeVerzekeraar == "" || 
		huidigePolisnummer == "" || 
		huidigeAdres == "" || 
		huidigePostcode == "" || 
		huidigePlaats == "" ||
		gezinsleden == "" || 
		opzeggenGezinsleden == "")	
		{
			result = false;
		}
	
	return result;
}

function getCheckedValue(radioObj) 
{
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
			
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function countTextareaChars(theForm, areaObject, countObject, maxLength)
{
	if (!e) var e = window.event;
	var controlKeyPressed = e.ctrlKey;
	var maximumReached = false;
	
	if (e.keyCode) keyPressed = e.keyCode;
	else if (e.which) keyPressed = e.which;

	if (!(controlKeyPressed && keyPressed == 65)) {
		if (areaObject.value.length >= maxLength) 
		{
			areaObject.value = areaObject.value.substring(0, maxLength);
			maximumReached = true;
		}

		countObject.value = maxLength - areaObject.value.length;
	} 
    
    return maximumReached;
}

function ShowGeboortedatumsKinderen() 
{
	var i;
	var n;
	var nr;
	var snr;
	var ctrl;
	
	var iss;
	var nrs

	if (document.forms[0] == null)
	{
		return;
	}
	
	ctrl = GetObject("st_aantjonger");

	if (ctrl.options == null)
	{
		snr = ctrl.value;
	}
	else
	{
		snr = ctrl.options[ctrl.selectedIndex].value;
	}

	nr = parseInt(snr);

	if (isNaN(nr))
	{
		nr = 0;
	}

	for (i=1; i < ctrl.options.length; i++) 
	{
		if (nr >= i) 
		{
			ShowAdditionalFields('fieldgroup_st_aantjonger' + i, 'expand');
		} 
		else 
		{
			ShowAdditionalFields('fieldgroup_st_aantjonger' + i, 'collapse');		
		}
	}
}

function ShowGekozenGeboortedatumBestuurder()
{	
	if (document.forms[0] == null)
	{
		return;
	}
	
	var ctrl = GetObject("vzDeBs");
		
	if (ctrl[0].checked)
	{
		ShowAdditionalFields('fieldgroup_bestuurder_ext_vp', 'expand');
	}
	else if(ctrl[1].checked)
	{
		ShowAdditionalFields('fieldgroup_bestuurder_ext_bs', 'expand');
	}
}

function selectNodes(xmlDoc, xpathQuery)
{
	if(window.XPathEvaluator)
	{
		var xpe = new XPathEvaluator();
		var nsResolver = xpe.createNSResolver(xmlDoc.ownerDocument == null ?
			xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
		var result = xpe.evaluate(xpathQuery, xmlDoc, nsResolver, 0, null);
		var found = [];
		var res;
		while (res = result.iterateNext())
			found.push(res);
		return found;		
	}
	else
	{
		return xmlDoc.selectNodes(xpathQuery);
	}
}	

function getInnerText(node) {
	if(node)
	{
		if (typeof node.textContent != 'undefined') {
			return node.textContent;
		}
		else if (typeof node.innerText != 'undefined') {
			return node.innerText;
		}
		else if (typeof node.text != 'undefined') {
			return node.text;
		}
		else {
			switch (node.nodeType) {
				case 3:
				case 4:
					return node.nodeValue;
				break;
				case 1:
				case 11:
					var innerText = '';
					for (var i = 0; i < node.childNodes.length; i++) {
						innerText += getInnerText(node.childNodes[i]);
					}
					return innerText;
					break;
				default:
					return '';
			}
		}
	}
}

function isArray(a) 
{
	if (!a) return false;

	var isobj = isObject(a);
	var hasconstrc = false;

	if (a.constructor) 
	{
		return isObject(a) && a.constructor == Array;
	} 
	else 
	{
		if (a.length)
		{
			return true;
		} 
		else 
		{
			return false;
		}
	}
}

function isBoolean(a) {
    return typeof a == 'boolean';
}

function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}

function isFunction(a) {
    return typeof a == 'function';
}

function isNull(a) {
    return typeof a == 'object' && !a;
}

function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isString(a) {
    return typeof a == 'string';
}

function isUndefined(a) {
    return typeof a == 'undefined';
} 

function WissenAutoGegevens() 
{
	var aj = new ajaxFunctions(bepaalFormnaam());
	aj.herstelFormulierVelden();
}

function KentekenOnbekend()
{
	document.forms[bepaalFormnaam()].elements['st_voertuiggegevens'].value = "1";
    
	ShowAdditionalFields('KentekenGegevens', 'collapse');
	ShowAdditionalFields('VoertuigGegevens', 'expand');

	ClearFieldGroups('VoertuigGegevens');
}

function InitialiseerKentekenOfVoertuigGegevens()
{
	if (document.forms[bepaalFormnaam()].elements['st_voertuiggegevens'].value == "1")
	{
		ShowAdditionalFields('VoertuigGegevens', 'expand');
		ClearFieldGroups('VoertuigGegevens');
	}
	else 
	{
		ShowAdditionalFields('KentekenGegevens', 'expand');
		ClearFieldGroups('KentekenGegevens');
	}
}

function OphalenAutogegevensViaKenteken() 
{
	var aj = new ajaxFunctions(bepaalFormnaam());
	aj.ajaxOphalenAutoGegevensOpKenteken('OB_KENTEKE');
}

function OphalenModellen() 
{
	var aj = new ajaxFunctions(bepaalFormnaam()); 
	aj.ajaxOphalenModellen('OB_MERK', 'OB_BOUWJR', 'OB_BOUWMND');
}

function OphalenTypes()
{
	var aj = new ajaxFunctions(bepaalFormnaam()); 
	aj.ajaxOphalenTypes('OB_MERK', 'OB_MODEL', 'OB_BOUWJR', 'OB_BOUWMND');
}

function OphalenAutoGegevens() 
{
	var aj = new ajaxFunctions(bepaalFormnaam()); 
	aj.ajaxOphalenAutoGegevens('OB_TYPE', 'OB_BOUWJR', 'OB_BOUWMND');
}

function isKenteken(waarde) 
{
	if (waarde == "")
	{
		return true;
	}
	
	var kentekenPatroon = /^(\d{2}|[A-Za-z]{2})-(\d{2}|[A-Za-z]{2}|[A-Za-z]{3})-(\d{2}|\d{1}|[A-Za-z]{2})$/;
	if (kentekenPatroon.test(waarde))
	{
		return(true)
	}
	else
	{
		return(false)
	}
}

// Functie die gebruikt kan worden voor het zetten van de navigateValue wanneer gesubmit wordt
// met de Enter-knop waardoor de navigateValue niet gezet wordt (bij bijvoorbeeld meerder 
// submit-knoppen)
// LET OP: de functie bepaalFormnaam() dient te zijn opgenomen in de View
function zetNavigateValueOnSubmit(navigateValue)
{
	var formName = bepaalFormnaam();
			
	if(document.forms[formName].__NAVIGATEACTION.value == '')
	{ 
		document.forms[formName].__NAVIGATEACTION.value = navigateValue;
	}
}

function validateMaetisForm()
{
	var validationResult = validateValuesSubmitted();
	
	if(validationResult == "")
	{
		validationResult += validateSubmittedValues();
	}
	
	if(validationResult == "")
	{
		submitForm(document.forms['OfferteAanvraagMaetis']);
	}
	else
	{
		alert(validationResult);
	}
}

function tip_it(which, elem, xOffset, yOffset)
{
	if(which)
	{
		var mouse_X = window.event.clientX + document.body.scrollLeft + 20 + xOffset;//Werkt alleen in Internet Explorer
		var mouse_Y = window.event.clientY + document.body.scrollTop + yOffset;//Werkt alleen in Internet Explorer
		
		if(mouse_X < 0){mouse_X = 0;}
		if(mouse_Y < 0){mouse_Y = 0;}
		
		var div = document.getElementById(elem);
		div.style.left = mouse_X;
		div.style.top  = mouse_Y;
		
/*		var iframe = document.createElement("IFRAME");
		
		iframe.style.width = div.clientWidth;
		iframe.style.height = div.clientHeight;
		iframe.style.left = div.style.left;
		iframe.style.top = div.style.top;
		iframe.style.zIndex = div.style.zIndex;
		iframe.id = "iframe";
		iframe.style.position = "absolute";
		iframe.src = "/EmptyMenuItem.htm";
		
		document.body.appendChild(iframe);
*/		

		hideSelect(true);
		
		div.style.visibility = "visible";
	}
	else
	{
		// document.body.removeChild(document.getElementById("iframe"));
		hideSelect(false);
		document.getElementById(elem).style.visibility = "hidden";
	}
}

function hideSelect(blnHide) 
{
	var agent = navigator.appVersion.toLowerCase();
	var version = parseFloat(agent.substr(agent.indexOf("msie")+5, 3));
		
	if(version < 7)
	{
		for(i=0;i<window.document.forms.length;i++) 
		{
			for(x=0;x<window.document.forms[i].elements.length;x++) 
			{ 
				if (window.document.forms[i].elements[x].tagName=="SELECT" && controleerHiddenDIV(window.document.forms[i].elements[x])) 
				{
					if (blnHide) 
					{
						window.document.forms[i].elements[x].style.visibility="hidden"; 						
					}
					else 
					{ 
						window.document.forms[i].elements[x].style.visibility="visible"; 						
					}
				}
			}
		}
	}
}

function controleerHiddenDIV(element)
{
	if(element.parentNode != null)
	{
		if(element.parentNode.tagName == "DIV")
		{
			if(element.parentNode.style.visibility == "hidden")
			{
				return false;
			}
		}	
		else
		{
			return controleerHiddenDIV(element.parentNode);
		}
	}
	else	
	{
		return true;
	}
}

function moveAdvies(which)
{
	if(which != "premium")
		hideAdvies("premium");
	if(which != "prima")
		hideAdvies("prima");
	if(which != "primair")
		hideAdvies("primair");
	
	var adviesBlok = document.getElementById(which+"_table");
	if(adviesBlok != null)
	{
		adviesBlok.className = "adviesVisible";
	}
}

function hideAdvies(which)
{
	var adviesBlok = document.getElementById(which+"_table");
	if (adviesBlok != null)
	{
		adviesBlok.className = "adviesHidden";
	}
}

function SwapImage(imageID, imagePath) {
  var Img = document.getElementById(imageID);
  Img.src=imagePath
}

function herberekenen(field)
{
	var aj = new ajaxAAMFunctions(field);
	aj.herberekenenAAM();
}

//CHO:functie voor popup window offerteaanvraagverzuimverzekering
function MailOfferteAanvraagVerzuimverzekering(formName, formAction)
{
	ClearFieldGroups();

	var form = document.forms[formName];	
	var targetname = "defpopup";
	var targetwin = window.open("", targetname, "resizable=no, scrollbars=no, width=300, height=200");
	
	form.__NAVIGATEACTION.value = "mailen";	
	form.action	= formAction;
	form.target = targetname;
	form.submit();
		
	targetwin.focus();

	form.__NAVIGATEACTION.value = "";	
	form.action	= "";
	form.target = "";
	
	return false;	
}

function AutoIFrame(iframe)
{
	var doc;
	var docHeight;
	
	try 
	{
    	if( iframe.contentDocument )
			// For NS6			
			doc = iframe.contentDocument; 			
		else if( iframe.contentWindow ) 
			// For IE5.5 and IE6
			doc = iframe.contentWindow.document;				
		else if( iframe.document )
			// For IE5
			doc = iframe.document;			
		else //other browser
			doc = iframe.document;			
			
		// 25 extra pixel voor Firefox compatibiliteit.
		docHeight = doc.body.scrollHeight + 25;
				
		if (iframe.frameElement)
			iframe.frameElement.style.height = docHeight + "px";
		else		
			iframe.style.height = docHeight + "px";
	}
	catch(e) 
	{
	}
}

function OpenPDF(uri)
{
	wd=screen.width;
	ht=screen.heigth;
	window.open(uri,"presentation","width="+wd+", heigth="+ht+"");
}

