var IE = document.all?true:false
var msg2 = ""

$(document).ready(function() {
	if(document.getElementById('location')){
		getDealers('location');
	}
})

//id van selectbox waarin items geplaatst moeten worden
function getDealers(mode){
	//haal (optioneel) geselecteerde city op
	var q = '';
		q = '?country=' + $('#country').val();
	if(mode=='dealer'){q += '&city=' + $('#location').val()}

	//haal xml op en plaats deze in selectbox
	//alert('dit');
	$.ajax({
		type: "GET",
		url: PROJECT_URL + 'xml/dealers.xml.asp' + q,
		dataType: "xml",
		success: function(xml){
			if(mode=='dealer'){
				FillDealers(xml)
			}else{
				FillCity(xml);
			}
		}
	});
}
function FillCity(xml) {
	var selectbox=document.getElementById("location");
	selectbox.options.length=1;
	$(xml).find("item").each(function() {
		selectbox.options[selectbox.options.length]=new Option($(this).find("city").text(),$(this).find("city").text());
	});
	var selectboxdealer=document.getElementById("dealer");
	selectboxdealer.options.length=1;
}
function FillDealers(xml) {
	var selectbox=document.getElementById("dealer");
	selectbox.options.length=1;
	$(xml).find("item").each(function() {
		selectbox.options[selectbox.options.length]=new Option($(this).find("name").text(),$(this).attr("id"));
	});
}

function goto(e, nr){
	if($('#code'+nr).val().length == 3) {
		if(nr<4){ $('#code'+(nr+1)).focus(); }
	}
}

function mOn(obj) {
	var ext = obj.src.slice(-3)
	if(obj.src.indexOf('_o.'+ ext)==-1){
		obj.src=obj.src.replace('.'+ ext,'_o.'+ ext)
	}
}
function mOut(obj) {
	var ext = obj.src.slice(-3)
	obj.src=obj.src.replace('_o.'+ext,'.'+ext)
}

//All fields with id filled in are required fields.
function cOn(td, color) {
    if(document.getElementById||(document.all && !(document.getElementById))) {
	  td.style.backgroundColor= color;
	  td.style.cursor = 'pointer';
	  td.style.color='#333333';
	}
}
function cOut(td,color) {
    if(document.getElementById||(document.all && !(document.getElementById))) {
	  td.style.backgroundColor=color;
	  td.style.color='';
	}
}

function nav(obj,show) {
	if(show){
		if(obj.src.indexOf('_o.gif')==-1){
			obj.src=obj.src.replace('.gif','_o.gif')
		}
	}else{
		obj.src=obj.src.replace('_o.gif','.gif')
	}
}

function display(id,arr) {
	if(arr!=undefined) {
		var a = arr.split(',')
		for(i=0;i<a.length;i++){
			document.getElementById(a[i]).style.display='none'
		}
	}
	var obj = document.getElementById(id).style
	if(obj.display=='none'){
		obj.display='block'
	}
	return false
}
function submit_register(){
	check_Username()
	setTimeout("checkform('frmRegister')",1000);
}

function submit_updateprofiel(){
	setTimeout("checkform2('frmRegister')",1000);
}

//All fields with id filled in are required fields.
function checkform2(obj) {
	if(obj== 'frmRegister'){
		check_Username()
	}
	if(obj== 'frmProfile'){
		check_Dealer()
	}
	var form=document.getElementById(obj);
	var el = form.getElementsByTagName('label');

	for(i=0;i<form.elements.length;i++){
		if(form.elements[i].id!='' && form.elements[i].id.indexOf('pass')==-1 && form.elements[i].id.indexOf('geboortedatum')==-1){
			if(form.elements[i].value==''){
				msg2 = msg2 + "- " + form.elements[i].title + "\n";
				form.elements[i].style.backgroundColor = "#CCCCCC";
				form.elements[i].style.color = "#333333";
		    }
			else {
				form.elements[i].style.backgroundColor = "#FFFFFF";
				form.elements[i].style.color = "#333333";
			}			
		}
	}
	if(obj == 'frmRegister' || obj == 'frmProfile') {
		if(document.getElementById('password').value!=''){
			if(document.getElementById('email').value!=document.getElementById('emailBis').value){
				msg2 = msg2 + "- " + 'emailadressen zijn niet aan elkaar gelijk' + "\n";
				document.getElementById('email').style.backgroundColor = "#CCCCCC";
				document.getElementById('email').style.color = "#333333";
				document.getElementById('emailBis').style.backgroundColor = "#CCCCCC";
				document.getElementById('emailBis').style.color = "#333333";
			}else{
				document.getElementById('email').style.backgroundColor = "#FFFFFF";
				document.getElementById('email').style.color = "#333333";
				document.getElementById('emailBis').style.backgroundColor = "#FFFFFF";
				document.getElementById('emailBis').style.color = "#333333";
			}
		}else{
			if(obj == 'frmRegister'){
				msg2 = msg2 + "- " + 'Wachtwoord' + "\n";
				document.getElementById('password').style.backgroundColor = "#CCCCCC";
				document.getElementById('password').style.color = "#333333";
			}
		}
	}
	if(obj == 'frmRegister' || obj == 'frmProfile') { 
		if(document.getElementById('email').value!=''){
			if(!isEmail(document.getElementById('email').value)) {
				msg2 = msg2 + "- " + 'Ongeldig emailadres' + "\n";
				document.getElementById('email').style.backgroundColor = "#CCCCCC";
				document.getElementById('email').style.color = "#333333";
			}else{
				document.getElementById('email').style.backgroundColor = "#FFFFFF";
				document.getElementById('email').style.color = "#333333";
			}
		}
	}
	if(obj == 'frmRegister'){
		MakeDate()
		//check op radio in frmRegister
		for (var e=0; e<el.length; e++) { //check for radio/checkboxes
			var objLabel = el[e];
			var domLabel = document.getElementById(objLabel.getAttribute('id'));
			var domLabelText = document.getElementById(objLabel.getAttribute('id')).innerHTML;
			var id_1 = domLabel.id.replace('lbl','')
	
			var spanObj = document.getElementById(id_1+'_group');
			var radio_checked1 = false;
			var radio_result = '';
	
			var allInputTags = spanObj.getElementsByTagName('INPUT');
			for (var c=0; c<allInputTags.length; c++) {
				if (allInputTags[c].checked) {
					radio_checked1 = true;
					radio_result = allInputTags[c].value;
					break;
				}
			}
			if(!radio_checked1) {
				msg2=msg2 + '- '+allInputTags[0].title +'\n';
			}	
		}
	}
	if(msg2!='') {
		alert("\n" + msg2);
		msg2 = ""
		return false
	}
	else {
		if(obj == 'frmRegister') {
			form.submit()
		}
		return true;
	}
}

//All fields with id filled in are required fields.
function checkform(obj) {
	if(obj== 'frmRegister'){
		check_Username()
	}
	if(obj== 'frmProfile'){
		check_Dealer()
	}
	var form=document.getElementById(obj);
	var el = form.getElementsByTagName('label');

	for(i=0;i<form.elements.length;i++){
		if(form.elements[i].id!='' && form.elements[i].id.indexOf('pass')==-1 && form.elements[i].id.indexOf('geboortedatum')==-1){
			if(form.elements[i].value==''){
				msg2 = msg2 + "- " + form.elements[i].title + "\n";
				form.elements[i].style.backgroundColor = "#CCCCCC";
				form.elements[i].style.color = "#333333";
		    }
			else {
				form.elements[i].style.backgroundColor = "#FFFFFF";
				form.elements[i].style.color = "#333333";
			}			
		}
	}
	if(obj == 'frmRegister' || obj == 'frmProfile') {
		if(document.getElementById('password').value!=''){
			if(document.getElementById('password').value!=document.getElementById('passwordBis').value){
				msg2 = msg2 + "- " + 'Wachtwoorden zijn niet aan elkaar gelijk' + "\n";
				document.getElementById('password').style.backgroundColor = "#CCCCCC";
				document.getElementById('password').style.color = "#333333";
				document.getElementById('passwordBis').style.backgroundColor = "#CCCCCC";
				document.getElementById('passwordBis').style.color = "#333333";
			}else{
				document.getElementById('password').style.backgroundColor = "#FFFFFF";
				document.getElementById('password').style.color = "#333333";
				document.getElementById('passwordBis').style.backgroundColor = "#FFFFFF";
				document.getElementById('passwordBis').style.color = "#333333";
			}
		}else{
			if(obj == 'frmRegister'){
				msg2 = msg2 + "- " + 'Wachtwoord' + "\n";
				document.getElementById('password').style.backgroundColor = "#CCCCCC";
				document.getElementById('password').style.color = "#333333";
			}
		}
	}
	if(obj == 'frmRegister' || obj == 'frmProfile') { 
		if(document.getElementById('email').value!=''){
			if(!isEmail(document.getElementById('email').value)) {
				msg2 = msg2 + "- " + 'Ongeldig emailadres' + "\n";
				document.getElementById('email').style.backgroundColor = "#CCCCCC";
				document.getElementById('email').style.color = "#333333";
			}else{
				document.getElementById('email').style.backgroundColor = "#FFFFFF";
				document.getElementById('email').style.color = "#333333";
			}
		}
	}
	if(obj == 'frmRegister'){
		MakeDate()
		//check op radio in frmRegister
		for (var e=0; e<el.length; e++) { //check for radio/checkboxes
			var objLabel = el[e];
			var domLabel = document.getElementById(objLabel.getAttribute('id'));
			var domLabelText = document.getElementById(objLabel.getAttribute('id')).innerHTML;
			var id_1 = domLabel.id.replace('lbl','')
	
			var spanObj = document.getElementById(id_1+'_group');
			var radio_checked1 = false;
			var radio_result = '';
	
			var allInputTags = spanObj.getElementsByTagName('INPUT');
			for (var c=0; c<allInputTags.length; c++) {
				if (allInputTags[c].checked) {
					radio_checked1 = true;
					radio_result = allInputTags[c].value;
					break;
				}
			}
			if(!radio_checked1) {
				msg2=msg2 + '- '+allInputTags[0].title +'\n';
			}	
		}
	}
	if(msg2!='') {
		alert(tr_emailadresincorrect + ":\n" + msg2);
		msg2 = ""
		return false
	}
	else {
		if(obj == 'frmRegister') {
			form.submit()
		}
		return true;
	}
}

function initSIFR(type) {
	
	if(typeof sIFR == "function"){
		
		switch(type) {
			case 'popup':

				// header big, blue
				sIFR.replaceElement(named({sSelector:"h1",sFlashSrc:PROJECT_URL+"flash/h1.swf",sColor:"#4D4C3C",sWmode:"transparent",nPaddingTop:0,nPaddingBottom:0,sFlashVars:"textalign=left&offsetTop=0"}));
				sIFR.replaceElement(named({sSelector:"h2",sFlashSrc:PROJECT_URL+"flash/h1.swf",sColor:"#4D4C3C",sWmode:"transparent",nPaddingTop:0,nPaddingBottom:0,sFlashVars:"textalign=left&offsetTop=0"}));
				break
			default:

				// header big, blue
				sIFR.replaceElement(named( {
						sSelector:"h1", 
						sFlashSrc:PROJECT_URL + "flash/h1.swf", 
						sColor:"#ffffff", 
						sWmode:"transparent",
						nPaddingTop:0, 
						nPaddingBottom:0, 
						sFlashVars:"textalign=left&offsetTop=0"
				}));

				// header big, blue
				sIFR.replaceElement(named( {
						sSelector:"h2", 
						sFlashSrc:PROJECT_URL +"flash/h1.swf", 
						sColor:"#ffffff", 
						sWmode:"transparent",
						nPaddingTop:0, 
						nPaddingBottom:0, 
						sFlashVars:"textalign=left&offsetTop=0"
				}));

				// header bigger
				sIFR.replaceElement(named( {
						sSelector:"h3", 
						sFlashSrc:PROJECT_URL +"flash/h1.swf", 
						sColor:"#ffffff", 
						sWmode:"transparent",
						nPaddingTop:0, 
						nPaddingBottom:0, 
						sFlashVars:"textalign=center&offsetTop=0"
				}));

				break
			}
	
		
	}
	
}

function openOverlay(title,url,width,height) {
	GB_showCenter(title, url, /* optional */ height, width)
}

function del_onclick(id){
	if(confirm(tr_wishlist_verwijder)) {
		location.href='delWishlist.asp?wishlistid	='+ id
	}
}

function printme() {
		window.open('printwishlist.asp','Print','width=640px,height=480px,scrollbars=yes')
//	var objOut = document.getElementById("output");
//	var out = objOut.innerHTML;
//		
//	var wh = window.open("","wh","width=660px,height=480px,scrollbars=yes");
//	wh.document.open();
//	var output = '<link href="css/print.css" rel="stylesheet" type="text/css">';
//	output += '<'+'script type="text/javascript">';
//	output += 'window.print();';	
//	output += '</'+'script>';
//	output = '<html><head>'+output+'</head><body>'+out+'</body></html>';
//	wh.document.writeln(output);
//	wh.document.close();
}

function printFrame(url){
	var iframeCmd = document.getElementById('iframeCmd');
	iframeCmd.setAttribute('src',url);
	if(IE){
		iframeCmd.onreadystatechange = function() {
			if (iframeCmd.readyState == "complete") {
				window.frames['iframeCmd'].focus();
				window.frames['iframeCmd'].print();
			}
		}
	}else{//mozilla
		//window.frames['iframeCmd'].focus();
		//window.frames['iframeCmd'].print()
	}
}

function preloader() { 
  var args = preloader.arguments;
  document.imageArray = new Array(args.length);
  for(var i=0; i<args.length; i++) {
    document.imageArray[i] = new Image;
    document.imageArray[i].src = args[i];
  }
}

function checkSizeSelected(id){
	var v = '';
	v = $('#sizeId-'+id).val();
	//als maat geselecteerd, redirect naar wishlist
	if(v==''){
		display('warning-' + id,'wishlistbtn-' + id);
		setTimeout("display('wishlistbtn-"+ id +"','warning-"+ id +"')", 2000);
	}else{
		location.href='addToWishlist.asp?articleId='+ id + '&sizeid=' + v;
	}	
}