﻿var debug = false;
function stopError()	{return true;}
if (!debug) window.onerror=stopError;

var error = new Array();
error[0] = "Bitte füllen Sie alle Muss-Felder aus.";
error[1] = "Diese E-Mail-Adresse scheint nicht korrekt zu sein.";


// create shortened document title
//var title = $("title").html();
//var Mint_SI_DocumentTitle = title.replace(/ELCO Heating Solutions \| /g, "");
//Mint_SI_DocumentTitle = title.substring(24);

function initKontaktForm()
{
	$('.required').after('<img src="../img/icons/required.gif" class="icon" alt="" />');	// append the "required" image to all fields with the class "required"
	$('.required').each(function(){this.onblur = validateField;});				// validation handler to all required fields
	$("#ContactForm").submit(function(){return validateForm();});				// event handler on form submit
}

function validateForm()
{
	// --------------------------------------------------------------------------------------------------------------
	// Check for empty fields
	// --------------------------------------------------------------------------------------------------------------
	var firstError = false;														// field where the first error occured
	var n = $('.required').length;											// number of required fields

	$('.required').each(function(i)											// check all required fields if they're not empty
	{
		if ($.trim(this.value) != '' && this.value != 0)
			n--;
		else
		{
			if (!firstError)														// did we already find an error?
				firstError = this;												// if not, remember this one to focus
		}
	});

	if (n != 0)																		// found empty fields?
	{
		alert("Bitte füllen Sie alle Muss-Felder aus.");
		if (firstError)
		{
			firstError.focus();
		}
		return false;
	}

	// --------------------------------------------------------------------------------------------------------------
	// Check for correct e-mail address
	// --------------------------------------------------------------------------------------------------------------
	var obj = $('#Email');														// get object of email field
	var value = $.trim(obj.attr('value'));									// get content of email field
	if (!/^[\w.\-&']+@([\w\-]+\.)+[A-Z]{2,6}$/i.test(value))			// check email address
	{
		alert("Diese E-Mail-Adresse scheint nicht korrekt zu sein.");
		obj.focus();
		return false;
	}

	return true;
}

function validateField()
{
	var field = this.id;

	var obj = document.getElementById(field).nextSibling;
	var value = $.trim(this.value);

	if (value == '' || value == 0)
	{
		obj.src = '../img/icons/required.gif';
		return false;
	}
	else
	{
		if(field == 'Email' && (!/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/.test(value)))
		{
			obj.src = '../img/icons/required-error.png';
			return false;
		}
		obj.src = '../img/icons/required-ok.png';
	}
}


//-------------------------------------------------------------------------------------
// cookie object functions
//-------------------------------------------------------------------------------------
jQuery.cookie = function(name, value, options)
{
	// ---------------------------------
	// name and value given: set cookie
	// ---------------------------------
	if (typeof value != 'undefined')
	{
		options = options || {};
		if (value === null)
		{
			value = '';
			options.expires = -1;
		}
		var expires = '';
		if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString))
		{
			var date;
			if (typeof options.expires == 'number')
			{
				date = new Date();
				date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
			}
			else
			{
				date = options.expires;
			}
			expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
		}

		// CAUTION: Needed to parenthesize options.path and options.domain
		// in the following expressions, otherwise they evaluate to undefined
		// in the packed version for some reason...
		var path = options.path ? '; path=' + (options.path) : '';
		var domain = options.domain ? '; domain=' + (options.domain) : '';
		var secure = options.secure ? '; secure' : '';
		document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
	}
	// ---------------------------------
	// only name given: get cookie
	// ---------------------------------
	else
	{
		var cookieValue = null;
		if (document.cookie && document.cookie != '')
		{
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++)
			{
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '='))
				{
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
};


// --------------------------------------
// Highslide
// --------------------------------------
try
{
	hs.registerOverlay(
	{
		thumbnailId: null,
		overlayId: 'controlbar',
		position: 'top right',
		hideOnMouseOut: true
	});
	hs.graphicsDir = 'img/hs/';
	hs.outlineType = 'rounded-white';
	hs.outlineWhileAnimating = true;
	hs.allowSizeReduction = false;
	hs.preserveContent = false;
	hs.dimmingDuration = 0;
	hs.dimmingOpacity = 0.5;

	hs.align='center';
	hs.outlineType = 'drop-shadow';
	hs.align = 'center';
	hs.showCredits = false;

	hs.restoreTitle = 'Hier klicken um das Bild wieder zu schliessen.';

	//hs.captionId = 'the-caption';
	hs.captionSlideSpeed = 3;
	hs.allowSizeReduction = false;
	hs.loadingText = '';
	hs.closeText = '';
	hs.closeTitle = '';
	hs.captionEval = 'this.thumb.alt';
	hs.onSetClickEvent = function (sender,e)
	{
		e.element.onclick = function()
		{
			return hs.expand(this,{slideshowGroup: this.parentNode.className});
		}
		return false;
	}
}
catch(error)
{
	//alert(error);
}
// --------------------------------------


// -------------------------------------------------------------------------------------------------------------------
// When the DOM is ready...
// -------------------------------------------------------------------------------------------------------------------
$(document).ready(function()
{
	// -----------------------------------------------------------------------------
	// Handling of the contact form
	// -----------------------------------------------------------------------------
	if($("#ContactForm").length)
	{
		initKontaktForm();
	}
});
