<!--
window.addEvent('domready',function() {
	var menuWidthTotal = 0;
	var width = $('mainNav').getStyle('width').toInt();
	var menuItems = $$('#mainNav li');
	for(var i = 0; i < menuItems.length; i++) {
		menuWidthTotal += menuItems[i].getStyle('width').toInt();
	}
	var itemMargin = Math.floor((width - menuWidthTotal) / menuItems.length);
	if(itemMargin < 0) {
		itemMargin = 20;
	}
	for(var i = 0; i < menuItems.length - 1; i++) {
		menuItems[i].setStyle('margin-right', (itemMargin - 2) + 'px');	
	}
});

var xmlHttp = new Array();

function showHideProductInfo(id) {
	var el = document.getElementById('product' + id);
	if(el) {
		if(el.style.display == 'none') {
			el.style.display = 'block';			
		} else {
			el.style.display = 'none';	
		}
	}
	return false;
}

function startSearch() {
	var val = document.getElementById('searchVal').value;
	if(val != '') {
		document.getElementById('searchForm').submit();
	} else {
		alert('U heeft nog geen zoekwaarde opgegeven.');	
	}
	return false;
}

function confirmDelete(quotationId, productId, confirmText) {
	var href = location.href.replace(/http:\/\//gi,'');
	href = href.split('/');
	if(confirm(confirmText)) {
		xmlHttp[1] = GetXmlHttpObject();
		var url = 'includes/ajax.asp?script=deletequotationproduct&quotationId=' + quotationId + '&productId=' + productId + '&rid=' + Math.random();
		xmlHttp[1].open('GET', url, true);
		xmlHttp[1].onreadystatechange = function() {
			if (xmlHttp[1].readyState == 4) {
				if(xmlHttp[1].responseText == 'success') {
					var el = document.getElementById('productTable' + productId);
					if(el) {
						el.parentNode.removeChild(el);
						location.href = 'http://' + href[0] + '/' + href[1] + '/';
					}
				} else {
					alert('Er is een fout opgetreden bij het verwijderen van het product uit de offerte.');	
				}
			}
		}
		xmlHttp[1].send(null);
	}
	return false;
}

function addText(str, addStr) {
	return str + addStr	
}
function addError(el) {
	el.className = el.className + ' error';	
}
function removeError(el) {
	el.className = el.className.replace(/ error/gi,'');	
}

function saveField(fieldId, val, id) {
	if(fieldId == 'hasPrint') {
		if(parseInt(val) == 1) {
			document.getElementById('pmsColorsRow').style.display = '';	
		} else {
			document.getElementById('pmsColorsRow').style.display = 'none';
		}
	}
	var tmp = encodeURI(val);
	xmlHttp[1] = GetXmlHttpObject();
	var url = 'includes/ajax.asp?script=savefield&field=' + fieldId + '&quotationId=' + id + '&value=' + tmp + '&rid=' + Math.random();
	xmlHttp[1].open('GET', url, true);
	xmlHttp[1].onreadystatechange = function() {
		if (xmlHttp[1].readyState == 4) {

		}
	}
	xmlHttp[1].send(null);
	return false;
}

function checkQuotationForm(id) {
	var foundErrors = '';
	var fieldsChecked = 0;
	var fields = 'title|aanhef|radio|Male|Female,contactName|naam|text,email|e-mailadres|email,phone|telefoonnummer|text,hasPrint|bedrukking|radio|Yes|No,pmsColors|aantal (pms) kleuren bedrukking|radio|1|2|3|4|5,dataDuplication|dataduplicatie|radio|Yes|No,dataSecurity|databeveiliging|radio|Yes|No';
	fields = fields.split(',');
	var addressFields = 'address|adres|text,zipcode|postcode|text,city|plaats|text';
	addressFields = addressFields.split(',');
	foundErrors = checkFields(fields, null);
	var capacityFields = document.getElementById('capacityFields').getElementsByTagName('input');
	for(var i = 0; i < capacityFields.length; i++) {
		if(capacityFields[i].checked == true) {
			fieldsChecked++;	
		}
	}
	if(fieldsChecked == 0) {
		foundErrors = foundErrors + 'U bent vergeten de capaciteit(en) aan te vinken.\n';	
	}
	if(checkQuantity(id) == 0) {
		foundErrors = foundErrors + 'U bent vergeten een of meerdere aantallen aan te vinken.\n';	
		foundErrors = foundErrors + 'Wanneer u gebruik maakt van het Anders-veld, moet u minimaal een aantal van 10 opgeven.\n';
	} else if(getMaxQuantity() >= 300) {
		foundErrors = foundErrors + checkPMS();
	} else if(getMaxQuantity() < 10) {
		foundErrors = foundErrors + 'Wanneer u gebruik maakt van het Anders-veld, moet u minimaal een aantal van 10 opgeven.\n';
	}
	if(checkSamples() > 0) {
		foundErrors = foundErrors + checkFields(addressFields, null);
	} else {
		for(var i = 0; i < addressFields.length; i++) {
			var tmp = addressFields[i].split('|');
			document.getElementById(tmp[0]).value = '';	
		}
	}
	if(foundErrors == '') {
		//var popup = document.getElementById('samplePopup');
		//if(popup) {
		//	popup.style.display = 'block';
		//} else {
			submitQuotationForm();
		//}
		return false;	
	} else {
		alert(foundErrors);
		return false;	
	}
}

function checkSamples(id) {
	var ids = '';
	var counter = 0;
	if(document.getElementById('sampleFields')) {
		var sampleFields = document.getElementById('sampleFields').getElementsByTagName('input');
		for(var i = 0; i < sampleFields.length; i++) {
			if(sampleFields[i].checked == true) {
				ids = ids + '#' + sampleFields[i].value + '#'
				counter++;	
			}
		}
		ids = ids.replace(/##/gi,',');
		ids = ids.replace(/#/gi,'');
		saveField('sampleIds', ids, id);
		if(counter > 0) {
			document.getElementById('addressFields').style.display = '';	
		} else {
			document.getElementById('addressFields').style.display = 'none';
			var aFields = document.getElementById('addressFields').getElementsByTagName('input');
			for(var i = 0; i < aFields.length; i++) {
				aFields[i].value = '';	
			}
		}
	}
	return counter;
}

function checkSampleForm() {
	var fields = document.getElementById('sampleProducts').getElementsByTagName('input');
	var checked = 0;
	for(var i = 0; i < fields.length; i++) {
		if(fields[i].type.match(/checkbox/gi)) {
			if(fields[i].checked == true) {
				checked++;	
			}
		}
	}
	if(checked == 0) {
		if(confirm('U heeft aangegeven geen samples te willen ontvangen.\nKlik op OK om door te gaan of op Annuleren om alsnog uw samples op te geven.')) {
			submitQuotationForm();
		}
	} else {
		submitQuotationForm();
	}
}

function countSamples(el) {
	var fields = document.getElementById('sampleProducts').getElementsByTagName('input');
	var checked = 0;
	for(var i = 0; i < fields.length; i++) {
		if(fields[i].type.match(/checkbox/gi)) {
			if(fields[i].checked == true) {
				checked++;	
			}
		}
	}
	if(checked > 2) {
		alert('U kunt maximaal 2 samples selecteren.');
		return false;
	} else {
		return true;
	}
}

function submitQuotationForm() {
	document.getElementById('quotation').submit();	
}

function checkFields(fields, fieldId) {
	var foundErrors = '';
	var el;
	var field;
	var id;
	for(var i = 0; i < fields.length; i++) {
		field = fields[i].split('|');
		id = field[0];
		if(fieldId && field[2] != 'radio') {
			id = id + fieldId;
		}
		switch (field[2]) {
			case 'text':
				el = document.getElementById(id);
				if(el.value == '') {
					if(fieldId) {
						foundErrors = addText(foundErrors,'U bent vergeten uw ' + field[1] + ' in te vullen voor ' + document.getElementById('productTitle' + fieldId).innerHTML + '.\n');
					} else {
						foundErrors = addText(foundErrors,'U bent vergeten uw ' + field[1] + ' in te vullen.\n');
					}
				}
				break;
			case 'radio':
				var checked = 0;
				
				if(id == 'pmsColors') {
					if(document.getElementById('hasPrintYes').checked == false) {
						break;	
					} else {
						for(var k = 3; k < field.length; k++) {
							if(fieldId) {
								el = document.getElementById(id + field[k] + fieldId);					
							} else {
								el = document.getElementById(id + field[k]);
							}
							if(el.checked == true) {
								checked++;	
							}							
						}
					}
				} else {
					if(fieldId) {
						el = document.getElementById(id + field[3] + fieldId);					
					} else {
						el = document.getElementById(id + field[3]);
					}
					if(el.checked == true) {
						checked++;	
					}
					if(fieldId) {
						el = document.getElementById(id + field[4] + fieldId);					
					} else {
						el = document.getElementById(id + field[4]);
					}
					if(el.checked == true) {
						checked++;	
					}
				}
				if(checked != 1) {
					if(fieldId) {
						foundErrors = addText(foundErrors,'U bent vergeten uw ' + field[1] + ' te selecteren voor ' + document.getElementById('productTitle' + fieldId).innerHTML + '.\n');
					} else {
						foundErrors = addText(foundErrors,'U bent vergeten uw ' + field[1] + ' in te vullen.\n');
					}
				}
				break;
			case 'email':
				el = document.getElementById(id);
				if(el.value == '') {
					if(fieldId) {
						foundErrors = addText(foundErrors,'U bent vergeten uw ' + field[1] + ' in te vullen voor ' + document.getElementById('productTitle' + fieldId).innerHTML + '.\n');
					} else {
						foundErrors = addText(foundErrors,'U bent vergeten uw ' + field[1] + ' in te vullen.\n');
					}
				} else {
					if(emailCheck(id, 1) == false) {
						if(fieldId) {
							foundErrors = addText(foundErrors,'U heeft een ongeldig e-mailadres voor ' + document.getElementById('productTitle' + fieldId).innerHTML + ' opgegeven.\n');
						} else {
							foundErrors = addText(foundErrors,'U bent vergeten uw ' + field[1] + ' in te vullen.\n');
						}
					}
				}
				break;
			case 'numeric':
				el = document.getElementById(id);
				if(el.value == '') {
					if(fieldId) {
						foundErrors = addText(foundErrors,'U bent vergeten uw ' + field[1] + ' in te vullen voor ' + document.getElementById('productTitle' + fieldId).innerHTML + '.\n');
					} else {
						foundErrors = addText(foundErrors,'U bent vergeten uw ' + field[1] + ' in te vullen.\n');
					}
				} else if(!el.value.match(/(\d+)/gi)) {
					if(fieldId) {
						foundErrors = addText(foundErrors,'U heeft een ongeldig ' + field[1] + ' voor ' + document.getElementById('productTitle' + fieldId).innerHTML + ' opgegeven.\n');
					} else {
						foundErrors = addText(foundErrors,'U heeft een ongeldig ' + field[1] + ' opgegeven.\n');
					}
				}
				break;
			case 'date':
				el = document.getElementById(id);
				if(el.value == '') {
					foundErrors = addText(foundErrors,'U bent vergeten uw ' + field[1] + ' in te vullen.\n');
				}
				break;
			default:
				alert('unknown field: ' + id);
				break;
		}
		if(field[0] == 'dataDuplication') {
			if(document.getElementById(field[0] + 'Yes').checked == true) {
				var dataAmount = parseInt('0' + document.getElementById('dataAmount').value,10);
				if(dataAmount < 1) {
					foundErrors = foundErrors + 'U bent vergeten de hoeveelheid data op te geven.\n';	
				}
			}
		}
	}
	return foundErrors;
}

function GetXmlHttpObject() {
	var HttpObj = null;
	try	{
		HttpObj = new XMLHttpRequest();
	} catch (e) {
		try {
			HttpObj = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch (e) {
			HttpObj = new ActiveXObject('Microsoft.XMLHTTP');
		}
	}
	return HttpObj;
}

function startPage(doGoogle) {
	Cufon.replace('#container #menutop a', {fontFamily: 'Gotham',hover: true});
	document.getElementById('menutop').style.visibility = 'visible';
	if (doGoogle) gmInitialize();
}

function keyStroke(e, validate) {
	var key = window.event ? e.keyCode : e.which;
	if (key == 8 || key == 0) return true;
	if (key == 124 ) return false;
	if (validate == 1 && key == 32) return false;
	if (validate == 4 || validate == 3 || validate == 2) {
		if (key < 48 || key > 57) {
			if (key != 44 && key != 45) {
				return false;
			} else {
				if (validate == 2 && key == 44) {
					return false;
				} else if(validate == 4 && (key == 44 || key == 45)) {
					return false;
				}
			}
		}
	}
}

function checkCapacity(id) {
	var amounts = '';
	var checkCount = 0;
	var fields = document.getElementById('capacityFields').getElementsByTagName('input');
	for(var i = 0; i < fields.length; i++) {
		if(fields[i].checked == true) {
			checkCount++;
			if(amounts.length > 0) {
				amounts = amounts + ',';	
			}
			amounts = amounts + fields[i].value;
		}
	}
	saveField('capacityIds', amounts, id);	
}

function checkQuantity(id) {
	var amounts = '';
	var sufficientAmount = false;
	var checkCount = 0;
	var minAmount = 300;
	var fields = document.getElementById('quantityFields').getElementsByTagName('input');
	for(var i = 0; i < fields.length; i++) {
		if(fields[i].type.match(/text/gi)) {
			if(parseInt(fields[i].value,10) >= 10) {
				checkCount++;
				if(amounts.length > 0) {
					amounts = amounts + ',';	
				}
				amounts = amounts + fields[i].value;
			}
			if(parseInt(fields[i].value,10) >= minAmount) {
				sufficientAmount = true;
			}
		} else if(fields[i].checked == true) {
			checkCount++;
			if(fields[i].value == 'Anders') {
				checkCount--;
			} else if(parseInt(fields[i].value,10) >= minAmount) {
				sufficientAmount = true;
			}
			if(fields[i].value + '' != 'Anders') {
				if(amounts.length > 0) {
					amounts = amounts + ',';	
				}
				amounts = amounts + fields[i].value;
			}
		}
	}
	saveField('quantities', amounts, id);
	if(sufficientAmount == true) {
		document.getElementById('pms').style.display = '';
	} else {
		document.getElementById('pms').style.display = 'none';
	}
	return checkCount;
}

function getMaxQuantity() {
	var tmpAmount
	var maxAmount = 0;
	var fields = document.getElementById('quantityFields').getElementsByTagName('input');
	for(var i = 0; i < fields.length; i++) {
		if(fields[i].type.match(/text/gi)) {
			if(fields[i].value.match(/\d+/)) {
				tmpAmount = parseInt(fields[i].value,10);
				if(tmpAmount > maxAmount) {
					maxAmount = tmpAmount;	
				}
			}			
		} else if(fields[i].checked == true) {
			if(fields[i].value.match(/\d+/)) {
				tmpAmount = parseInt(fields[i].value,10);
				if(tmpAmount > maxAmount) {
					maxAmount = tmpAmount;	
				}
			}
		}
	}
	return maxAmount;
}

function checkPMS() {
	var foundErrors = '';
	var field = ['ownPMSColor|behuizing eigen PMS|radio|1|0'];
	foundErrors = checkFields(field,null);
	return foundErrors;
}

function checkForm(block_id) {
	var formitem = document.getElementById('checkfields' + block_id).value.split('#**#');
	for (var x = 0; x < formitem.length; x++) {
		var formproperties = formitem[x].split('#|#');
		var formfield = formproperties[0];
		var fieldname = formproperties[1];
		var validate = parseInt(formproperties[2]);
		var compulsary = parseInt(formproperties[3]);
		var unique = parseInt(formproperties[4]);
		if (validate != 2) {
			if (document.getElementById(formfield)) {
				var a = doTrim(document.getElementById(formfield));
				document.getElementById(formfield).value = a;
			}
		}
		if (validate == 2) {
			var found = false;
			for (var i = 0; i < document.getElementsByName(formfield).length; i++) {
				if (document.getElementsByName(formfield)[i].checked) found = true;
			}
			if (found == false) {
				translate(2, '', ': ' + fieldname);
				return false;
			}
		}
		if (compulsary == 1) {
			if (document.getElementById(formfield).value == '') {
				if (document.getElementById(formfield).style.visibility != 'hidden') {
					translate(3, '', ': ' + fieldname);
					document.getElementById(formfield).focus();
					return false;
				}
			}
		}
		if (validate == 1) {
			if (emailCheck(formfield, compulsary) == false) {
				translate(4, '', '');
				document.getElementById(formfield).focus();
				return false;
			}
		}
		if (unique == 1) {
			var result = parseInt(document.getElementById(formfield + '_unique').value);
			if (result == 0) {
				translate(5, '', ': ' + fieldname);
				document.getElementById(formfield).focus();
				return false;
			}
		}
	}
	return;
}

function checkUnique(dbtable, dbfield, id, formfield) {
	xmlHttp[1] = GetXmlHttpObject();
	var url = '/includes/ajax.asp?script=checkunique&dbtable=' + dbtable + '&dbfield=' + dbfield + '&dbvalue=' + escape(document.getElementById(formfield).value) + '&id=' + id + '&sid=' + Math.random();
	xmlHttp[1].open('GET', url, true);
	xmlHttp[1].onreadystatechange = function() {
		if (xmlHttp[1].readyState == 4) {
			result = xmlHttp[1].responseText;
			document.getElementById(formfield + '_unique').value = result;
		}
	}
	xmlHttp[1].send(null);
}

function emailCheck(formfield, compulsary) {
	var emailStr=document.getElementById(formfield).value
	if (compulsary == 0 && emailStr == '') return true;
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) return false;
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i = 0; i < user.length; i++) {
		if (user.charCodeAt(i) > 127) return false;
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i) > 127) return false;
	}
	if (user.match(userPat)==null) return false;
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i = 1;i <= 4; i++) {
			if (IPArray[i] > 255) return false;
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i = 0; i < len; i++) {
		if (domArr[i].search(atomPat) == -1) return false;
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) return false;
	if (len < 2) return false;
	return true;
}

function translate(id, before, after) {
	xmlHttp[2] = GetXmlHttpObject();
	var url = 'includes/ajax.asp?script=translate&id=' + id + '&rid=' + Math.random();
	xmlHttp[2].open('GET', url, true);
	xmlHttp[2].onreadystatechange = function() {
		if (xmlHttp[2].readyState == 4) {
			response = before + xmlHttp[2].responseText + after;
			alert(response);
		}
	}
	xmlHttp[2].send(null);
}

function tagReaction(reaction_id) {
	xmlHttp[1] = GetXmlHttpObject();
	var url = 'includes/ajax.asp?script=tagreaction&reaction_id=' + reaction_id + '&tagged=1&rid=' + Math.random();
	xmlHttp[1].open('GET', url, true);
	xmlHttp[1].onreadystatechange = function() {
		if (xmlHttp[1].readyState == 4) {
			document.getElementById('ok' + reaction_id).innerHTML = document.getElementById('tagMessage').value;
		}
	}
	xmlHttp[1].send(null);
}

function showAdvanced() {
	if (document.getElementById('advanced').style.display == 'none') {
		document.getElementById('advanced').style.display = 'block';
	} else {
		document.getElementById('advanced').style.display = 'none';
	}
}

function doTrim(obj) {
	var a = trim(obj.value);
	obj.value = a;
	return a;
}

function trim(strValue) {
	if (strValue.length < 1) return '';
	strValue = rTrim(strValue);
	strValue = lTrim(strValue);
	return strValue;
}

function rTrim(strValue) {
	var w_space = String.fromCharCode(32);
	var v_length = strValue.length;
	var strTemp = '';
	if (v_length < 0) return '';
	var iTemp = v_length - 1;
	while(iTemp > -1) {
		if (strValue.charAt(iTemp) != w_space) {
			strTemp = strValue.substring(0, iTemp + 1);
			break;
		}
		iTemp = iTemp - 1;
	}
	return strTemp;
}

function lTrim(strValue) {
	var w_space = String.fromCharCode(32);
	if (v_length < 1) return '';
	var v_length = strValue.length;
	var strTemp = '';
	var iTemp = 0;
	while (iTemp < v_length) {
		if (strValue.charAt(iTemp) != w_space) {
			strTemp = strValue.substring(iTemp, v_length);
			break;
		}
		iTemp = iTemp + 1;
	}
	return strTemp;
}


//-->

