function checkContactForm()
{
	if(
		( ($("contact_name").value = $("contact_name").value.trim()).length < 2 ) ||
		( $("contact_name").hasClass("inputinfo") )
	)
	{
		alert($("contact_name").getAttribute("error"));
		$("contact_name").focus();
		return false;
	}
	if(
		( ($("contact_email").value = $("contact_email").value.trim()).length < 5 ) ||
		( $("contact_email").value.indexOf(".") == -1 ) ||
		( $("contact_email").value.indexOf("@") == -1 ) ||
		( $("contact_email").hasClass("inputinfo") )
	)
	{
		alert($("contact_email").getAttribute("error"));
		$("contact_email").focus();
		return false;
	}
	if(
		( ($("contact_message").value = $("contact_message").value.trim()).length < 8 ) ||
		( $("contact_message").hasClass("inputinfo") )
	)
	{
		alert($("contact_message").getAttribute("error"));
		$("contact_message").focus();
		return false;
	}

	$("contact_send").value = "True";

	return true;
}

function checkMailingForm()
{
	$("mailing_name").value = $("mailing_name").value.trim();
	$("mailing_companyname").value = $("mailing_companyname").value.trim();
	
	if(
		( ($("mailing_email").value = $("mailing_email").value.trim()).length < 5 ) ||
		( $("mailing_email").value.indexOf(".") == -1 ) ||
		( $("mailing_email").value.indexOf("@") == -1 ) ||
		( $("mailing_email").hasClass("inputinfo") )
	)
	{
		alert($("mailing_email").getAttribute("error"));
		$("mailing_email").focus();
		return false;
	}

	if( $("mailing_name").hasClass("inputinfo") )
	{
		$("mailing_name").value = "";
	}

	if( $("mailing_companyname").hasClass("inputinfo") )
	{
		$("mailing_companyname").value = "";
	}
	
	$("mailing_submit").value = "True";

	return true;
}

function fixVideoPlayers()
{
	var videoPlayer = $$(".videoplayer");
	for( i = 0; i < videoPlayer.length; i++ )
	{
		if(
			( videoPlayer[i].getAttribute("movieurl") != null ) &&
			( videoPlayer[i].getAttribute("moviewidth") != null ) &&
			( videoPlayer[i].getAttribute("movieheight") != null )
		)
		{
			var width = parseInt(videoPlayer[i].getAttribute("moviewidth"));
			if( isNaN(width) )
			{
				width = 400;
			}
			var height = parseInt(videoPlayer[i].getAttribute("movieheight"));
			if( isNaN(height) )
			{
				height = 300;
			}

			if( videoPlayer[i].hasClass("small") == false )
			{
				videoPlayer[i].instance = new SWFObject("swf/fvp.swf","vp" + i,width,height,"9","#FFFFFF");
			}
			else
			{
				videoPlayer[i].instance = new SWFObject("swf/fvp-small.swf","vp" + i,width,height,"9","#FFFFFF");
			}
			videoPlayer[i].instance.addParam("flashvars","movieurl=" + escape(videoPlayer[i].getAttribute("movieurl")));
			videoPlayer[i].instance.write(videoPlayer[i]);
		}
	}
}

function fixFlashPlayers()
{
	var flashMovie = $$(".flashmovie");
	for( i = 0; i < flashMovie.length; i++ )
	{
		if(
			( flashMovie[i].getAttribute("movieurl") != null )
		)
		{
			width = parseInt(flashMovie[i].getAttribute("moviewidth"));
			if( isNaN(width) )
			{
				width = flashMovie[i].getParent().getWidth();
			}
			height = parseInt(flashMovie[i].getAttribute("movieheight"));
			if( isNaN(height) )
			{
				height = flashMovie[i].getParent().getHeight();
			}

			flashMovie[i].instance = new SWFObject(flashMovie[i].getAttribute("movieurl"),"fm" + i,width,height,"9","#FFFFFF");
			flashMovie[i].instance.write(flashMovie[i]);
		}
	}
}

function fixNewSolutionMediaPhotoListings()
{
    var listing = $$("div.newsolutionmedia div.photolisting");
    for( var i = 0; i < listing.length; i++ )
    {
        var prev = listing[i].getElement("div.prev");
        var next = listing[i].getElement("div.next");
        var photos = listing[i].getElement("div.photosholder dl.photos");
        var photo = listing[i].getElements("div.photosholder dl.photos dd");

        photos.currentFirstPhotoIndex = 0;
        photos.photo = photo;
        photos.fx = new Fx.Tween(photos,{wait:false});

        photos.setStyle("width",photo.length*115);

        if( photo.length < 5 )
        {
            prev.addClass("inactive");
            next.addClass("inactive");
        }
        else
        {
            prev.photosElement = photos;
            prev.addEvent("click",function(){
                var newIndex = this.photosElement.currentFirstPhotoIndex;
                newIndex--;
                if( newIndex < 0 )
                {
                    if( (newIndex = this.photosElement.photo.length-4) < 0 )
                    {
                        newIndex = 0;
                    }
                }

                var newLeft = newIndex * 113;

                this.photosElement.currentFirstPhotoIndex = newIndex;
                this.photosElement.fx.start("left",-newLeft);
            });

            next.photosElement = photos;
            next.addEvent("click",function(){
                var newIndex = this.photosElement.currentFirstPhotoIndex;
                newIndex++;
                if( newIndex > this.photosElement.photo.length-4 )
                {
                    newIndex = 0;
                }

                var newLeft = newIndex * 113;

                this.photosElement.currentFirstPhotoIndex = newIndex;
                this.photosElement.fx.start("left",-newLeft);
            });
        }
    }

}

window.addEvent("domready",function(){

	var inputInfo = $$(".inputinfo");
	for( var i = 0; i < inputInfo.length; i++ )
	{
		inputInfo[i].setAttribute("inputinfovalue",inputInfo[i].value);
		inputInfo[i].addEvent("focus",function(){
			if( this.value == this.getAttribute("inputinfovalue") )
			{
				this.value = "";
			}
			if( this.hasClass("inputinfo") )
			{
				this.removeClass("inputinfo");
			}
		});
		inputInfo[i].addEvent("blur",function(){
			if( this.value == "" )
			{
				this.value = this.getAttribute("inputinfovalue");
				if( !this.hasClass("inputinfo") )
				{
					this.addClass("inputinfo");
				}
			}
		});
	}	

	setTimeout(fixVideoPlayers,1000);
	setTimeout(fixFlashPlayers,100);
    fixNewSolutionMediaPhotoListings();
	
	var milkboxOpener = $$(".milkboxopener");
	for( i = 0; i < milkboxOpener.length; i++ )
	{
		if( milkboxOpener[i].getAttribute("milkbox") != null )
		{
			milkboxOpener[i].addEvent("click",function(e){
				e = new Event(e);
				e.preventDefault();

                var parent = this.getParent();
                var anchor = parent.getElement("a");
                anchor.fireEvent("click");
			});
		}

	}

	if( $("callme_button") != null )
	{
		$("callme_button").addEvent("click",function(){

			if(
				( ($("callme_name").value = $("callme_name").value.trim()).length < 2 ) ||
				( $("callme_name").value == "Uw naam..." )
			)
			{
				alert($("callme_name").getAttribute("error"));
				$("callme_name").focus();
				return;
			}

			if(
				( ($("callme_telephone").value = $("callme_telephone").value.trim()).length < 10 ) ||
				( $("callme_telephone").hasClass("inputinfo") )
			)
			{
				alert($("callme_telephone").getAttribute("error"));
				$("callme_telephone").focus();
				return;
			}

			$("callme_form").setStyle("display","none");
			$("callme_wait").setStyle("display","block");

			var request = new Request.JSON({
				url: "/widgets/callme",
				onSuccess: function(jsonData){
					if( jsonData.sent == true )
					{
						$("callme_sent").setStyle("display","block");
					}
					else
					{
						alert("Could not send. Please try again (onSuccess): " + jsonData.sent);
						$("callme_form").setStyle("display","block");
					}
				},
				onFailure: function(instance){
					alert("Could not send. Please try again. (onFailure)");
					$("callme_form").setStyle("display","block");
				},
				onException: function(headerName,value){
					alert("Could not send. Please try again. (onException)");
					$("callme_form").setStyle("display","block");
				},
				onComplete: function(){
					$("callme_wait").setStyle("display","none");
				}
			});

			var sendArg = new Object();
			sendArg.widgetId = $("callme_form").getAttribute("widgetid");
			sendArg.name = $("callme_name").value;
			sendArg.telephone = $("callme_telephone").value;
			request.send("JSONData=" + JSON.encode(sendArg));

		});
	}

	if(( $("w_newsletter_submit") != null ) && ( $("w_newsletter_emailaddress") != null ))
	{
		$("w_newsletter_submit").addEvent("click",function(){
			location.href = $("w_newsletter_form").getAttribute("action") + "/email/" + $("w_newsletter_emailaddress").value;
		});
	}

	if( $("mailing_subscribe") != null )
	{
		$("mailing_subscribe").addEvent("click",function(){
			$("mailing_type").value = "subscribe";
		});
	}

	if( $("mailing_unsubscribe") != null )
	{
		$("mailing_unsubscribe").addEvent("click",function(){
			$("mailing_type").value = "unsubscribe";
		});
	}

	var item = $$(".openShowSiblings");
	for( var i = 0; i < item.length; i++ )
	{
		item[i].addEvent("click",function(e){
			e = new Event(e);
			e.stop();
			
			var parent = this.getParent();
			var images = parent.getChildren("img");
			
			var displayImages = Array();
			for( var i = 0; i < images.length; i++ )
			{
				displayImages[displayImages.length] = Array(images[i].getAttribute("largesrc"),images[i].getAttribute("alt"));
			}

			Slimbox.open(displayImages,0);

		});
	}
});