/* * * * * * * * * * * * * * * * * * * * * * *
 *   C A R E N Z A  S C R I P T  F I L E     *
 *                                           *
 *   (c) Copyright 2008 - CARENZA            *
 *   http://www.carenza.nl/                  *
 *                                           *
 * * * * * * * * * * * * * * * * * * * * * * */

window.onload = function () {
	
	var submenu = document.getElementById("submenu");
	
	if(links = document.getElementById("menu").getElementsByTagName("a")) {
		loc1 = location.href.lastIndexOf("/");
		loc2 = location.href.lastIndexOf("/",loc1-1);
		foldername = location.href.substring(loc2+1,loc1);
		filename = location.href.substring(loc1+1);
		
		for (var i=0; i<links.length; i++) {
			if (i == 0 && links[i].href == location.href) { links[i].parentNode.className = "active"; break; }
			//if ( i == 0) alert(links[i].href.length+' '+links[i].href.lastIndexOf("/"));
			if ( i == 0 && links[i].href.lastIndexOf("/") == links[i].href.length - 2) { links[i].parentNode.className = "active"; }
			if (links[i].href.substring(links[i].href.lastIndexOf("/"),links[i].href.lastIndexOf("/",links[i].href.lastIndexOf("/")-1)+1) == foldername) {
				if(links[i].parentNode.parentNode.id == "menu") links[i].parentNode.className = "active";				
			}
			if (links[i].href.substring(links[i].href.lastIndexOf("/")+1) == filename) {
				if(links[i].parentNode.parentNode.parentNode.className == "active") links[i].parentNode.className = "activesub";
			}
		}
	}
	if(submenu) {
		links2 = submenu.getElementsByTagName("a");
		for (var i=0; i<links2.length; i++) {
			if (links2[i].href.substring(links2[i].href.lastIndexOf("/")+1) == filename) {
				links2[i].className = "active";
			}
		}
	}
	
	//$('#menu a,.image,#content .item,#navbar,#language,#wrapper').corner("4px");
	//$('h2').corner("top 4px");
	
	$("#language").hover(
	    function(){ $("#language").stop(); $("#language").animate({height:"155px" }, 500); }, 
	    function(){ $("#language").stop(); $("#language").animate({height: "25px" }, 500); }
	);
	
	$("#menu li").hover( function(){ $(this).addClass('hover'); }, function(){ $(this).removeClass('hover'); } );

	$("#gallery a").click( function(){
		img = $(this.firstChild).attr("src").replace("small", "large");
		$("#bigimg").attr("src",img);
		countRows = $("#iscr > a").size();
		pos = parseInt(this.id);
		scrollto(pos-2);
		return false;
	} );
	
	iscrw = $("#iscr > a").size() * 144;
	$("#iscr").css('width', iscrw );
	scrollto(0);	

	function scrollto(pos) {
		maxp = $("#iscr > a").size() - 5;
		
		if(pos<0) pos=0;
		if(pos>maxp) pos=maxp;
				
		if(pos==0)	$("#gallery #prev").removeClass('active'); else $("#gallery #prev").addClass('active');
		if(pos==maxp)	$("#gallery #next").removeClass('active'); else $("#gallery #next").addClass('active');
		
		leftval = -pos*144;
		current = parseInt($('#iscr').css("marginLeft"));
		speed = 3*Math.abs(leftval-current);
		$('#iscr').animate({ marginLeft:leftval+'px' }, speed);
	}
		
	$("#gallery #prev").click( function() {
		pos = -Math.round(parseInt(document.getElementById('iscr').style.marginLeft)/144);
		scrollto(pos-1);
	});
	
	$("#gallery #next").click( function() {
		pos = -Math.round(parseInt(document.getElementById('iscr').style.marginLeft)/144);
		scrollto(pos+1);
		//leftval = parseInt(document.getElementById('iscr').style.marginLeft)-144;
		//maxw = 720-parseInt($("#iscr").css('width'));
		//if(leftval<maxw) leftval=maxw;		
		//if(leftval==0) $("gallery #prev").removeClass('active'); else $("gallery #prev").addClass('active');
		//if(leftval==maxw) $("gallery #next").removeClass('active'); else $("gallery #next").addClass('active');
		//$('#iscr').animate({ marginLeft:leftval+'px' }, 500);
	});
	
        $("div.doektype").css("height","0").css("visibility","hidden");
        $("div.doekvorm").css("height","0").css("visibility","hidden");
          
        $("div."+$("form select#type_doek").val()).css("height","auto").css("visibility","visible");
        $("div."+$("form select#type_doek").val()+" div.inner").html($("div."+$("form select#type_doek").val()+" .default").html());
        //$("select#vorm_doek option").each(function () { if ($(this).hasClass($("form select#type_doek").val())) $(this).removeAttr("disabled"); else $(this).attr("disabled","disabled"); });
        $("select#vorm_doek").val('kies_doek');
        
	$("select#type_doek").change(function () {
          $("div.doektype").css({"height":"0", "visibility":"hidden"});
          $("div.doekvorm").css({"height":"0", "visibility":"hidden"});
          
          $("div.doektype."+$(this).val()).css({"height":"auto","visibility":"visible"});
          $("div.doektype."+$(this).val()+" div.inner").html($("div."+$(this).val()+" .default").html());
          //$("select#vorm_doek option").each(function () { if ($(this).hasClass($("form select#type_doek").val()) || $(this).hasClass("default")) $(this).removeAttr("disabled"); else $(this).attr("disabled","disabled"); });
          $("select#vorm_doek").val('kies_doek');
          //if($("select#type_doek").val() == 'harmonica') $("select#vorm_doek").val('harmonica_rechthoek');
          //if($("select#type_doek").val() == 'rolgordijn') $("select#vorm_doek").val('rolgordijn_rechthoek');
          $("div.doektype."+$("form select#type_doek").val()).css({"height":"auto", "visibility":"visible"});
        });

	$("select#vorm_doek").change(function () {
	  vorm = "div.doekvorm."+$("select#vorm_doek").val();
          $("div.doekvorm").height(0).css({"visibility":"hidden"});
          $(vorm).css({"height":"auto", "visibility":"visible"});
          $(vorm+" div.inner").html($(vorm+" .default").html());
          
          $("span.doekvorm").css("width","0").css("height","0").css("visibility","hidden");
          $("span."+$(this).val()).css("width","auto").css({"height":"auto", "visibility":"visible"});     
        });

        $(".maatw_afb a").click(function() { $("div."+$("form select#vorm_doek").val()+" div.inner").html($("#"+$(this).attr('rel')).html()); });

	//$("a.details").click( function() {  $("#details").css("height","450px"); return false; } );

	document.getElementById('menu').onmouseover = function() {
		var a = document.getElementById("menu").getElementsByTagName("li");
		for (var i=0; i<a.length; i++) { if(a[i].className == "active") { a[i].className="activ"; } }
	}
	
	document.getElementById('menu').onmouseout = function() {
		var a = document.getElementById("menu").getElementsByTagName("li");
		for (var i=0; i<a.length; i++) { if(a[i].className == "activ") { a[i].className="active"; } }
	}	
}

function checkContactForm() {
	if(ccscheck('naam', '') + ccscheck('bedrijfsnaam', '') + ccscheck('mail','mail') == 0) return true;
	else return false;
}

function checkBrochureForm() {
	check = ccscheck('naam', '') + ccscheck('mail','mail');
	//alert(check);
	if(check == 0) return true; else return false;
}
function checkNewsForm() {
	if(ccscheck('naam', '') + ccscheck('mail','mail') == 0) return true; else return false; // mail, post, phone or other
}

function checkMaatwerkForm() {
	var standaard = ccscheck('naam', '') + ccscheck('adres','') + ccscheck('postc','') + ccscheck('plaats','')+ ccscheck('tel','') + ccscheck('mail','mail') + ccscheck('type_doek','dropd');
	if($("form select#type_doek").val() == 'dreamsail' || $("form select#type_doek").val() == 'coolfit') standaard += ccscheck('vorm_doek','dropd');
	$("form div."+$("select#vorm_doek").val()+" input.max2m").each(function() { standaard += ccscheck($(this).attr('id'),"max2m"); });
	$("form div."+$("select#vorm_doek").val()+" input.max3m").each(function() { standaard += ccscheck($(this).attr('id'),"max3m"); });
	$("form div."+$("select#vorm_doek").val()+" input.max4m").each(function() { standaard += ccscheck($(this).attr('id'),"max4m"); });
	$("form div."+$("select#vorm_doek").val()+" input.max6m").each(function() { standaard += ccscheck($(this).attr('id'),"max6m"); });
	$("form div."+$("select#vorm_doek").val()+" input.max8m").each(function() { standaard += ccscheck($(this).attr('id'),"max8m"); });

	$("form div."+$("select#type_doek").val()+" input.max2m").each(function() { standaard += ccscheck($(this).attr('id'),"max2m"); });
	$("form div."+$("select#type_doek").val()+" input.max3m").each(function() { standaard += ccscheck($(this).attr('id'),"max3m"); });
	$("form div."+$("select#type_doek").val()+" input.max4m").each(function() { standaard += ccscheck($(this).attr('id'),"max4m"); });
	$("form div."+$("select#type_doek").val()+" input.max6m").each(function() { standaard += ccscheck($(this).attr('id'),"max6m"); });
	$("form div."+$("select#type_doek").val()+" input.max8m").each(function() { standaard += ccscheck($(this).attr('id'),"max8m"); });

	if($("form select#type_doek").val() == 'dreamsail')
		$("form select#dreamsail_kleur").each(function()   { standaard += ccscheck($(this).attr('id'),"dropd"); });
	else	$("form select#coolfit_kleur").each(function()   { standaard += ccscheck($(this).attr('id'),"dropd"); });

	$("form select#"+$("select#type_doek").val()+"_kleur").each(function()    { standaard += ccscheck($(this).attr('id'),"dropd"); });
	if (standaard == 0) return true; else return false; // mail, post, phone or other
}

function ccscheck(inputId, type) {
	$("#"+inputId).unbind('keyup').unbind('click');
	if(check(inputId,type)==1) {
		$("#"+inputId).removeClass('failCSS');
		return 0;
	} else {
		$("#"+inputId).addClass('failCSS');
		$("#"+inputId).keyup(function(event) { ccscheck(inputId, type); }).click(function() { ccscheck(inputId, type); });
		return 1;
	}
}

function check(inputId, type) {
	var id = document.getElementById(inputId);
	var v = id.value;
	switch (type) {
		
		case 'mail':
			var atIndex = v.indexOf('@');
			var dotIndex = v.lastIndexOf('.');
			if (atIndex < 1 || atIndex>=(v.length-2) || dotIndex < 1 || dotIndex>=(v.length-2) || atIndex > dotIndex - 1)
				return 0;
			else
				return 1;
			break
	
		case 'phone':
			var re = new RegExp('[^\\d]', "g");
			v = v.replace(re, "");
			if( parseInt(v,10) >= 100000000 && parseInt(v,10) <= 800000000 ) {
				v = '0' + parseInt(v,10);
				// id.value = v;
				return 1;
			} else return 0;
			break
		
		case 'postc':
			var re = new RegExp('(\\d{4})[\\s]*?([A-Z]{2}).*', "i");
			v = v.replace(re, "$1$2");
			v = v.toUpperCase();
			// id.value = v;
			if(v.length == 6 && re.test(v)) return 1; else return 0;
			break

		case 'dropd':
			if(v != 'kies_doek') return 1; else return 0;
			break

		case 'max2m':
			if(parseFloat(v.replace(",", ".")) <= 2.1 && parseFloat(v) > 0) return 1; else return 0;
			break

		case 'max3m':
			if(parseFloat(v.replace(",", ".")) <= 3 && parseFloat(v) > 0) return 1; else return 0;
			break

		case 'max4m':
			if(parseFloat(v.replace(",", ".")) <= 4 && parseFloat(v) > 0) return 1; else return 0;
			break

		case 'max6m':
			if(parseFloat(v.replace(",", ".")) <= 6 && parseFloat(v) > 0) return 1; else return 0;
			break

		case 'max8m':
			if(parseFloat(v.replace(",", ".")) <= 8 && parseFloat(v) > 0) return 1; else return 0;
			break
					
		default:
			var length = v.length;
			if(length >= 2) return 1; else return 0;
			break
	}

}

