/****
 * JS for praganda
 * Author: Mario Diaz @ mario-diaz.com
 * Created: 2011-07-21
 ****/


// some problems with de and com for google maps key (PHP is not able to find difference). No other solution than redirecting from de to com
sTmp = document.URL;
if (sTmp.indexOf('praganda.de') != -1) {
	aTmp = sTmp.split('praganda.de');
	location.href = aTmp.join('praganda.com');
}


var bMsie = (document.all) ? true : false;
var bIE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
var bIE7 = (navigator.appVersion.indexOf("MSIE 7.") == -1) ? false : true;
var sUserAgent = navigator.userAgent.toLowerCase();
var is_iphone = (sUserAgent.indexOf('iphone') != -1 || sUserAgent.indexOf('ipad') != -1/* || 2 == 2*/);
var bSafari = sUserAgent.indexOf('safari') != -1;


//for preloading images:
var aTmpPreload = new Array();


var iScrollWidth = 12;

var layoutSettings_InnerAccounts = {
		 
        fxName: "none"       

	}; 


// *** startup actions ************************************************************************************************
$(document).ready(function(){
	
	// init header auto change:
	if (iHeaderItems > 0) {
		setTimeout("changeHeaderTextAuto()", iHeaderAutoMilliSecs);
		if (!bHeaderAutomatic && iStartWithHeaderText > 0) {
			showHeaderText(iStartWithHeaderText, true);
		}
	}
	
	if (!is_iphone && !bIE7) { 
		setTimeout("initMasonry()", (!bSafari && !bMsie ? 500 : 1500));
	} else {
		setTimeout("initMasonry()", 2000);
	}
	
	// scroll elements:
	if (document.getElementById('scroll_text_1')) {
		$('#scroll_text_1').jScrollPane({showArrows:true,scrollbarWidth:iScrollWidth});
	}
	if (document.getElementById('scroll_text_2')) {
		$('#scroll_text_2').jScrollPane({showArrows:true,scrollbarWidth:iScrollWidth});
	}
	
	
	if (document.getElementById('footerSlideContent')) {
		$("#footerSlideContent").mouseleave(function(){
			toggleFooterBottomDisplay();
		});
	}
	
	if (is_iphone) {
		iCounter = 11;
		while (document.getElementById('box_' + iCounter)) {
			document.getElementById('box_' + iCounter).style.visibility = 'visible';
			iCounter++;
		}
	}
	
	correctIphoneFooter();
	
	// all alpha layers need mouseleave:
	iCounter = 0;
	while (document.getElementById('box_' + iCounter)) {
		/*
		$("#alpha_layer_" + iCounter).mouseleave(function(){
			hideAlphaNum(iCounter);
		});
		*/
		aAlphasShown[iCounter] = false;
		iCounter++;
	}
	
	
	for (i = 0; i <= iCounter; i++) {
		aSlidingActive[i] = false;
	}
	
	
	/* not wanted so far
	  $(function() {
		    
			//cache selector
		  sAlphaLayers = '';
		  for (i = 0; i <= iCounter; i++) {
			  if (i > 0) {
				  sAlphaLayers += ',';
			  }
			  sAlphaLayers += 'alpha_layer_' + i;
		  }
			var images = $(".alpha_layer"),
			  title = $("title").text() || document.title;
			
		    //make images draggable
		    images.draggable({
			  //create draggable helper
			  helper: function() {
			    return $("<div>").attr("id", "helper").html("<span>" + title + "</span><img id='thumb' src='" + $(this).attr("src") + "'>").appendTo("body");
			  },
			  cursor: "pointer",
			  cursorAt: { left: -10, top: 20 },
			  zIndex: 99999,
			  //show overlay and targets
			  start: function() {
			    $("<div>").attr("id", "overlay").css("opacity", 0.7).appendTo("body");
				$("#tip").remove();
				$(this).unbind("mouseenter");
				$("#targets").css("left", ($("body").width() / 2) - $("#targets").width() / 2).slideDown();
			  },
			  //remove targets and overlay
			  stop: function() {
			    $("#targets").slideUp();
				$(".share", "#targets").remove();
			    $("#overlay").remove();
				$(this).bind("mouseenter", createTip);
			  }
			});
			
			//make targets droppable
			$("#targets li").droppable({
			  tolerance: "pointer",
			  //show info when over target
			  over: function() {
			    $(".share", "#targets").remove();
			    $("<span>").addClass("share").text("Share on " + $(this).attr("id")).addClass("active").appendTo($(this)).fadeIn();
			  },
			  drop: function() {
			    var id = $(this).attr("id"),
				  currentUrl = window.location.href,
				  baseUrl = $(this).find("a").attr("href");

				if (id.indexOf("twitter") != -1) {
				  window.location.href = baseUrl + "/home?status=" + title + ": " + currentUrl;
				} else if (id.indexOf("delicious") != -1) {
				  window.location.href = baseUrl + "/save?url=" + currentUrl + "&title=" + title;
				} else if (id.indexOf("facebook") != -1) {
				  window.location.href = baseUrl + "/sharer.php?u=" + currentUrl + "&t=" + title;
				}
			  }		  
			});
		  
		    var createTip = function(e) {
			  //create tool tip if it doesn't exist
			  ($("#tip").length === 0) ? $("<div>").html("<span>Drag this image to share the page<\/span><span class='arrow'><\/span>").attr("id", "tip").css({ left:e.pageX + 30, top:e.pageY - 16 }).appendTo("body").fadeIn(2000) : null;
			};
			
			images.bind("mouseenter", createTip);
			
			images.mousemove(function(e) {
			
			  //move tooltip
	          $("#tip").css({ left:e.pageX + 30, top:e.pageY - 16 });
	        });
		  
		    images.mouseleave(function() {
			
			  //remove tooltip
			  $("#tip").remove();
		    });
		  });
	*/
	
});



function correctIphoneFooter () {
	if (is_iphone && document.getElementById('footer')) {
		oFooter = document.getElementById('footer');
		if (document.getElementById('container').offsetHeight < 600) {
			setTimeout("correctIphoneFooter()", 500);
		} else {
			oFooter.style.top = (document.getElementById('container').offsetHeight - oFooter.offsetHeight - 300) + "px";
		}
	}
}



function initMasonry () {
	$('#wrap').masonry({
		columnWidth: 315,
		itemSelector: '.box'
	});
}



/* *** window resize actions *** */
$(window).bind('resize', function() {
	//handleAlphaPosAfterWinResize();
	//window.location.href = window.location.href;
});



//scrolling effect:
var iCurrentScrapHeight = 0;
var iBoxesVisible = 10;


/* *** if some elements are hidden and scroll position gets to bottom then show more elements *** */
$(window).scroll(function () {
	/*
	if ($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
		showMoreBoxes();
	}
	*/
	if (iCurrentScrapHeight != 0) {
		if ($(window).scrollTop() > iCurrentScrapHeight - $(window).height()/* + 400*/) {
			showMoreBoxes();
		}
	}
});


// 
function setScrollEffectHeight () {
	if (document.getElementById("box_10")) {
		aTmp = findPos(document.getElementById("box_10"));
		iCurrentScrapHeight = aTmp[1] + document.getElementById("box_10").offsetHeight;
	}
}

var bNewBoxes = false;

/**
 * shows more boxes on scrolling to the bottom
 * @return void
 */
function showMoreBoxes () {
	if (!bNewBoxes) {
		bNewBoxes = true;
		iCounter = iBoxesVisible + 1;
		
		iFadeTime = bMsie ? 10 : 100;
		
		if (document.getElementById("box_" + iCounter)) {
			document.getElementById("box_" + iCounter).style.display = 'none';
			document.getElementById("box_" + iCounter).style.visibility = 'visible';
			$("#box_" + iCounter).fadeIn(iFadeTime, function() {
				iCounter++;
				if (document.getElementById("box_" + iCounter)) {
					document.getElementById("box_" + iCounter).style.display = 'none';
					document.getElementById("box_" + iCounter).style.visibility = 'visible';
					$("#box_" + iCounter).fadeIn(iFadeTime, function() {
						iCounter++;
						if (document.getElementById("box_" + iCounter)) {
							document.getElementById("box_" + iCounter).style.display = 'none';
							document.getElementById("box_" + iCounter).style.visibility = 'visible';
							$("#box_" + iCounter).fadeIn(iFadeTime, function() {
								bNewBoxes = false;
								iBoxesVisible = iCounter;
							});
						}
					});
				}
			});
		}
	} else {
		setTimeout("showMoreBoxes()", 50);
	}
}


/**
 * preloads images:
 * @param aImgArr
 * @return void
 */
function preloadImages (aImgArr) {
	iStart = aTmpPreload.length;
	for (i = 0; i < aImgArr.length; i++) {
		aTmpPreload[iStart + i] = new Image();
		aTmpPreload[iStart + i].src = aImgArr[i];
	}
}


/**
 * finds position of element
 * @param object obj
 * @return int array
 */
function findPos (obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft, curtop];
}


/**
 * expands search field
 * @return void
 */
function expandSearch () {
	document.getElementById('search_field_left').style.backgroundImage = "url(fileadmin/templates/images/search_field_white.png)";
	document.getElementById('search_field_right').style.backgroundImage = "url(fileadmin/templates/images/search_field_white_right.png)";
	$("#search_field_left").animate({ width: "106px"}, 320 );
}


/**
 * minimizes search field
 * @return void
 */
function minimizeSearch () {
	$("#search_field_left").animate({
	    width: "44px"
	}, 320, function() {
		document.getElementById('search_field_left').style.backgroundImage = "url(fileadmin/templates/images/search_field.png)";
		document.getElementById('search_field_right').style.backgroundImage = "url(fileadmin/templates/images/search_field_right.png)";
	}
	);
}


/* *** header effects ****************************************************************************************** */
var iHeaderActive = 0;
var bHeaderAutomatic = true;	// automatic changing
var iHeaderItems = 0;
var iHeaderAutoMilliSecs = 20000;
var bHeaderAniActive = false;
var iStartWithHeaderText = -1;


/**
 * shows header text
 * @param int iNum
 * @param bool bClick (clicked by user)
 * @return void
 */
function showHeaderText (iNum, bClick) {
	if (bClick) {
		bHeaderAutomatic = false;
	}
	
	if (!bHeaderAniActive) {
		bHeaderAniActive = true;
		sLayerIdOld = 'header_item_' + iHeaderActive;
		sLayerIdNew = 'header_item_' + iNum;
		$('#' + sLayerIdOld).fadeOut(320, function() {
			if (document.getElementById('bubble_' + iHeaderActive)) {
				document.getElementById('bubble_' + iHeaderActive).className = 'bubble';
			}
			$('#' + sLayerIdNew).fadeIn(320, function() {
				// fading complete.
				iHeaderActive = iNum;
				if (document.getElementById('bubble_' + iHeaderActive)) {
					document.getElementById('bubble_' + iHeaderActive).className = 'bubble active';
				}
				bHeaderAniActive = false;
			});
		});
	} else {
		setTimeout(function(){showHeaderText(iNum, bClick);}, 250);
	}
}


/**
 * changes Header Automatic
 * @return void
 */
function changeHeaderTextAuto () {
	if (bHeaderAutomatic && iHeaderItems > 1) {
		iNum = iHeaderActive + 1;
		if (iNum == iHeaderItems) {
			iNum = 0;
		}
		showHeaderText(iNum, false);
		setTimeout("changeHeaderTextAuto()", iHeaderAutoMilliSecs);
	}
}



/* *** box effects ******************************************************************************************* */

var sCurrentBox = '';

var aMainContentItems = new Array();	// will be filled by another js file

var aSlidingActive = new Array();

var iCurrentBox = -1;

var aAlphasShown = new Array();


/**
 * starts gallery
 * @param iNum
 * @return void
 */
function startGallery (iNum) {
	if (document.getElementById('gallery_link_' + iNum)) {
		$("#gallery_link_" + iNum).fancybox().trigger('click');
	}
}


/**
 * shows Teaser info per number: Alpha effect, green border, init mouseout event
 * @param object oCaller (box)
 * @return void
 */
function showTeaserInfoNum (iNum) {
	
	if (!aSlidingActive[iNum]) {
		
		//if (iCurrentBox != -1 && iCurrentBox != iNum) {
		//	hideAlphaNum(iCurrentBox);
		//}
		iN = aAlphasShown.length;
		for (i = 0; i < iN; i++) {
			if (aAlphasShown[i] && i != iNum) {
				hideAlphaNum(i);
			}
		}
		
		iCurrentBox = iNum;
		aSlidingActive[iNum] = true;
		
		oCaller = document.getElementById('box_' + iNum);
		
		// if scrolling doesn't match then correct:
		//aPosRel = findPosRelativeToViewport(oCaller);
		aTmp = findPos(oCaller);
		//iWinH = $(window).height();
		if (1 == 2/* && aPosRel[1] + oCaller.offsetHeight > iWinH*/) {
			//window.scrollBy(0, aPosRel[1] + oCaller.offsetHeight - iWinH + 27);
			//setTimeout(function(){showTeaserInfoNum(iNum);}, 100);
		} else {

			oLayer = document.getElementById('alpha_layer_' + iNum); 
			
			// now green large border:
			showGreenBorder(oCaller, iNum);
			
			
			// get text and so on:
			
			// add padding for mouseleave:
			if (oCaller.offsetWidth > oLayer.offsetWidth) {
				oLayer.style.paddingRight = (oCaller.offsetWidth - 277) + 'px';
			}
			
			oLayer.style.visibility = 'hidden';
			oLayer.style.display = 'block';
			if (oCaller.offsetHeight > oLayer.offsetHeight) {
				oLayer.style.paddingTop = (oCaller.offsetHeight - oLayer.offsetHeight) + 'px';
			}
			oLayer.style.display = 'none';
			oLayer.style.visibility = 'visible';
			
			oCaller.style.borderColor = '#72BF44';
			// position alpha layer:
			oLayer.style.display = 'block';
			//alert(aTmp[0]);
			oLayer.style.left = (aTmp[0] + 3) + 'px';
			oLayer.style.top = (aTmp[1] + oCaller.offsetHeight - oLayer.offsetHeight - 3) + 'px';
			oLayer.style.display = 'none';
			aAlphasShown[iNum] = true;
			$("#alpha_layer_" + iNum).mouseleave(function(){ iCurrentBox = -1; hideAlphaNum(iNum); });
			
			$('#alpha_layer_' + iNum).show("slide", { direction: "down" }, 250, function(){ aSlidingActive[iNum] = false; });
		}
	} else {
		//setTimeout(function(){showTeaserInfoNum(iNum);}, 250);
	}
}


/**
 * shows green border around current box
 * @param object oCaller
 * @param int iNum
 * @return void
 */
function showGreenBorder (oCaller, iNum) {
	if (!bIE6 && !bIE7) {
		oBorderLayer = document.getElementById('green_border_' + iNum);
		oBorderLayer.style.width = (oCaller.offsetWidth - 6) + 'px';
		oBorderLayer.style.height = (oCaller.offsetHeight - 6) + 'px';
		oBorderLayer.style.left = (aTmp[0]) + 'px';
		oBorderLayer.style.top = (aTmp[1]) + 'px';
		oBorderLayer.style.visibility = 'visible';
	}
}


/**
 * hides alpha per number
 * @param int iNum
 * @return void
 */
function hideAlphaNum (iNum) {
	if (!aSlidingActive[iNum] && aAlphasShown[iNum]) {
		if (!bIE6 && !bIE7) {
			document.getElementById('green_border_' + iNum).style.visibility = 'hidden';
		}
		aSlidingActive[iNum] = true;
		aAlphasShown[iNum] = false;
		$('#alpha_layer_' + iNum).hide("slide", { direction: "down" }, 250, function(){ aSlidingActive[iNum] = false; document.getElementById('box_' + iNum).style.borderColor = '#999';  });
	} else if (aAlphasShown[iNum]) {
		setTimeout(function(){hideAlphaNum(iNum);}, 300);
	}
}


/**
 * handles positioning of alpha after window resize
 * @return void
 */
function handleAlphaPosAfterWinResize () {
	if (sCurrentBox != '') {
		oLayer = document.getElementById('alpha_layer');
		if (document.getElementById(sCurrentBox)) {
			oCaller = document.getElementById(sCurrentBox);
			aTmp = findPos(oCaller);
			oLayer.style.left = (aTmp[0] + 1) + 'px';
			oLayer.style.top = (aTmp[1] + oCaller.offsetHeight - oLayer.offsetHeight - 1) + 'px';
		}
	}
}


//getPageScroll() by quirksmode.org
//Finds the scroll position of a page
function getPageScroll () {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}
	return [xScroll,yScroll];
}


//Finds the position of an element relative to the viewport.
function findPosRelativeToViewport (obj) {
	var objPos = findPos(obj);
	var scroll = getPageScroll();
	return [ objPos[0]-scroll[0], objPos[1]-scroll[1] ];
}



/* *** filter *** */
var bFiltersDisplayed = false;
function toggleFiltersDisplay () {
	oFilters = document.getElementById('filter_items');
	bFiltersDisplayed = !bFiltersDisplayed;
	if (bFiltersDisplayed) {
		$("#filter_items").show('slide', {direction: 'left'}, 400);
		document.getElementById('a_filter_label').style.backgroundImage = 'url(fileadmin/templates/images/arrow_grey_over.png)';
		$("#filters").mouseleave(function(){
			if (bFiltersDisplayed) {
				setTimeout("hideFilterDisplay()", 5000);
			}
		});
	} else {
		$("#filter_items").hide('slide', {direction: 'left'}, 640);
		document.getElementById('a_filter_label').style.backgroundImage = 'url(fileadmin/templates/images/arrow_green.png)';
		document.getElementById('img_filter_label').src = sFilterLabelImg;
	}
}


function showFilterDisplay () {
	if (!bFiltersDisplayed) {
		toggleFiltersDisplay();
	}
}


function hideFilterDisplay () {
	if (bFiltersDisplayed) {
		toggleFiltersDisplay();
	}
}



/* *** footer actions ******************************************************************************************* */
var bFooterAnimationActive = false;
var bFooterOpen = false;
function toggleFooterBottomDisplay () {
	if (!bFooterAnimationActive) {
		bFooterAnimationActive = true;
		if (bFooterOpen === false) {
			document.getElementById('footer').style.height = 'auto';
			$('#footerSlideContent').animate({ height: '250px'}, 320, function() { 
				bFooterOpen = true;
				bFooterAnimationActive = false;
				
				
				$("#container").click(function(){
					if (bFooterOpen) toggleFooterBottomDisplay();
				});
				
				if (is_iphone) {
					offsettop = $(document).height();
					window.scrollTo(0,offsettop);
				}
			});
			
			
		} else {
			$('#footerSlideContent').animate({ height: '0px'}, 320, function() {
				bFooterOpen = false;
				//document.getElementById('footer').style.height = '27px';
				bFooterAnimationActive = false;
			});
		}
	}
}



function showFooterBottom () {
	if (!bFooterOpen) {
		setTimeout("toggleFooterBottomDisplay()", 500);
	}
}


/* *** google maps ******************************************************************** */
/**
 * shows google maps address
 * @param string address
 * @param string info
 * @return void
 */
function showAddress (address, info) {
	geocoder.getLatLng(address,
						function (point) {
							if (!point) {
								alert("Could not find postcode " + address);
							} else {
								oMap.setCenter(point, 13);
								var marker = new GMarker(point);
								oMap.addOverlay(marker);
								
								marker.openInfoWindowHtml(info);
							}
						}
	);
}


/**
 * calls method for displaying route in google maps api
 * @param string fromAddress
 * @param string toAddress
 * @param string locale
 * @return void
 */
function setDirections (fromAddress, toAddress, locale) {
  	gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
}


/**
 * Use this function to access information about the latest load() results.
 * @return void
 */
function onGDirectionsLoad () { 
    
}


/**
 * handles error codes passed by google maps api
 * @return void
 */
function handleErrors () {
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	   displayError("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	   displayError("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.<br /> Error code: " + gdir.getStatus().code);
   
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	   displayError("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.<br /> Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	   displayError("The given key is either invalid or does not match the domain for which it was given.<br /> Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	   displayError("A directions request could not be successfully parsed.<br /> Error code: " + gdir.getStatus().code);
    
   else displayError("An unknown error occurred.");
}


function UnCryptMailto(s) {
	var n=0;
	var r="";
	for(var i=0; i < s.length; i++) {
		n=s.charCodeAt(i);
		if (n>=8364) {n = 128;}
		r += String.fromCharCode(n-(1));
	}
	return r;
 }
 function linkTo_UnCryptMailto(s)	{
	location.href=UnCryptMailto(s);
 }
 
 
 

/* *** contact page ****************************************************************************************** */
function checkContactComplete () {
	bComplete = true;
	aTmp = new Array('message', 'name', 'email', 'captcha');
	for (i = 0; i < aTmp.length; i++) {
		
		sName = '';
		switch (aTmp[i]) {
		case 'message':
			sName = sContactTextMessage;
			break;
		case 'name':
			sName = sContactTextName;
			break;
		case 'email':
			sName = sContactTextEmail;
			break;
		case 'captcha':
			sName = sContactTextCaptcha;
			break;
		default:
			break;
		}
		
		oEl = document.getElementById(aTmp[i]);
		sValue = trimAll(oEl.value);
		if (sValue.length < 5 || sValue == sName) {
			bComplete = false;
			break;
		} else if (aTmp[i] == 'email') {
			if (!checkEmailAddress(sValue)) {
				bComplete = false;
				break;
			}
		}
	}
	if (bComplete) {
		oEl = document.getElementById('email');
		sValue = trimAll(oEl.value);
		if (sValue.indexOf('@') == -1) {
			bComplete = false;
		}
	}
	
	document.getElementById('submito_contact').style.backgroundColor = bComplete ? '#72BF44' : '#999';
	
	return bComplete;
} 


function trySubmit () {
	if (checkContactComplete()) {
		document.forms['contact_form'].submit();
	}
}


var aContactErrors = new Array('', '', '', '');




var sEmailErrorTmp = '';


function checkTextField (oEl) {
	sElementName = oEl.id;
	sName = '';
	iPosErrors = 0;
	switch (sElementName) {
	case 'message':
		sName = sContactTextMessage;
		break;
	case 'name':
		sName = sContactTextName;
		iPosErrors = 1;
		break;
	case 'email':
		sName = sContactTextEmail;
		iPosErrors = 2;
		break;
	case 'captcha':
		sName = sContactTextCaptcha;
		iPosErrors = 3;
		break;
	default:
		break;
	}
	
	sValue = trimAll(oEl.value);
	
	if (sValue.length < 5 || sValue == sName) {
		
		if (oEl.id != 'message')
			oEl.style.border = '1px solid #B00';
		else 
			document.getElementById('contact_top').style.border = '1px solid #B00';
		
		aContactErrors[iPosErrors] = sName;
		//document.getElementById('contact_message').innerHTML = '<span class="error">' + sContactErrorMissing + ' ' + sName + '</span>';
		outputContactErrors();
		return false;
	}
	aContactErrors[iPosErrors] = '';
	outputContactErrors();
	
	if (oEl.id != 'message')
		oEl.style.border = '1px solid #72BF44';
	else 
		document.getElementById('contact_top').style.border = '1px solid #72BF44';
	return true;
}


function outputContactErrors () {
	aErrors = new Array();
	for (i = 0; i < aContactErrors.length; i++) {
		if (aContactErrors[i] != '') {
			aErrors.push(aContactErrors[i]);
		}
	}
	bError = false;
	for (i = 0; i < aContactErrors.length; i++) {
		if (aContactErrors[i] != '') {
			bError = true;
			break;
		}
	}
	if (bError) {
		//document.getElementById('contact_message').innerHTML = '<span class="error">' + sContactErrorMissing + '<br />' + aErrors.join('<br />') + '</span>';
		document.getElementById('contact_message').innerHTML = '<span class="error">' + sContactError + '</span>';
	} else {
		document.getElementById('contact_message').innerHTML = '';
	}
	if (sEmailErrorTmp != '') {
		//document.getElementById('contact_message').innerHTML += (bError ? '<br /><br />' : '') + sEmailErrorTmp;
		document.getElementById('contact_message').innerHTML = '<span class="error">' + sContactError + '</span>';
		bError = true;
	}
	
	checkContactComplete();
}


function checkEmailField () {
	oEl = document.getElementById('email');
	sValue = trimAll(oEl.value);
	if (sValue.length < 4) {
		sEmailErrorTmp = '<span class="error">' + sContactErrorMissing + ' ' + sContactTextEmail + '</span>';
		oEl.style.border = '1px solid #B00';
	} else if (!checkEmailAddress(sValue)) {
		sEmailErrorTmp = '<span class="error">' + sContactErrorEmail + '</span>';
		oEl.style.border = '1px solid #B00';
	} else {
		sEmailErrorTmp = '';
		oEl.style.borderColor = '#72BF44';
	}
	
	outputContactErrors();
}


function checkEmailAddress (sValue) {
	if (sValue.indexOf('@') == -1 || sValue.lastIndexOf('.') == -1) {
		return false;
	}
	
	if (sValue.indexOf('@') > sValue.lastIndexOf('.')) {
		return false;
	}
	
	aTmp = sValue.split('@');
	sTmp1 = aTmp[0];
	if (aTmp.length < 2 || sTmp1.length < 2) {
		return false;
	}
	sTmp2 = aTmp[1];
	if (sTmp2.length < 3) {
		return false;
	}
	
	if (sValue.lastIndexOf('.') == sValue.length - 1) {
		return false;
	}
	
	return true;
}


/**
 * trim function for javascript
 * @param string sString
 * @return string sString
 */
function trimAll (sString) {
	while (sString.substring(0, 1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length - 1, sString.length) == ' ') {
		sString = sString.substring(0, sString.length - 1);
	}
	return sString;
}


