/* $Id: script.js,v 1.6 2010-04-20 14:00:30 tmarciniak Exp $ */

/*=============================================================================
    PRELOAD IMAGES
==============================================================================*/

function preloadImages()
{
	var bgSerachToolTip = new Image();
	bgSerachToolTip.src = "/images/misc/search_form_tooltip_bg.gif";

	var sections = new Array();
	sections[0] = new Image();
	sections[0].src = "/images/sections/canopy_walkways_over.gif"
	sections[1] = new Image();
	sections[1].src = "/images/sections/observation_towers_over.gif"
	sections[2] = new Image();
	sections[2].src = "/images/sections/zip_line_tours_over.gif"
}

preloadImages();

/*=============================================================================
    FORM VALIDATION
==============================================================================*/

function validateForm(form)
{
	clearError();

	var check = checkInput("form_name","Please enter your Name") &&
	checkInput("form_email","Please enter your E-mail address") &&
	checkEmail("form_email","Please enter correct E-mail address") &&
	checkInput("form_phone","Please enter your Phone") &&
	checkPhoneNo("form_phone","Phone number length should be 10") &&
	checkInputRadio(form, "interest", "Please check your Primary Area of Interest") &&
	checkInputRadio(form, "budget", "Please check Project Budget") &&
	checkInputRadio(form, "timeframe", "Please check Project Timeframe");

	var check_textarea = true;

	if(document.getElementById("form_interest_5").checked)
	{
		check_textarea = checkTextarea("form_interest_other","Please specify your Area of Interest");
	}

	return check && check_textarea;
}

function checkPhoneNo(inputId, errorMessage)
{

	if(!document.getElementById(inputId))
	{
		alert( "Input " + inputId + " not found!");
		return false;
	}
	var input = document.getElementById(inputId);

	if (input.value == '' || input.value.replace(/[^0-9]/g,'').length != 10){
		return focusFailedInput(inputId,errorMessage);
	}
	return true;
}

function checkInput(inputId, errorMessage)
{
	if(!document.getElementById(inputId))
	{
		alert( "Input " + inputId + " not found!");
		return false;
	}

	var input = document.getElementById(inputId);

	if( input.value == "" )
    {
    	return focusFailedInput(inputId,errorMessage);
    }

    return true;
}

function checkTextarea(inputId, errorMessage)
{
	if(!document.getElementById(inputId))
	{
		alert( "Textarea " + inputId + " not found!");
		return false;
	}

	var input = document.getElementById(inputId);

	if( input.value.length <= 1 )
    {
    	return focusFailedInput(inputId,errorMessage);
    }

    return true;
}

function checkInputRadio(form, name, errorMessage)
{
	var inputs = form.getElementsByTagName("input");
	var labels = form.getElementsByTagName("label");
	//clear error class
	for( var i = 0; i < labels.length; i++ )
	{
		if(labels[i].htmlFor.indexOf("form_"+name) != -1 )
		{
			labels[i].className += labels[i].className.replace("error", "");
		}
	}

	for( var i = 0; i < inputs.length; i++ )
	{
		if(inputs[i].getAttribute("name") == name &&
			inputs[i].checked )
		return true;
	}
	alert(errorMessage);
	// set error class
	for( var i = 0; i < labels.length; i++ )
	{
		if(labels[i].htmlFor.indexOf("form_"+name) != -1 )
		{
			labels[i].className += " error";
		}
	}

	return false;
}

function checkEmail(inputId,errorMessage)
{
    if (!_checkEmail(document.getElementById(inputId).value))
    {
        focusFailedInput(inputId,errorMessage);
        return false;
    }
    return true;
}

function _checkEmail(email)
{
 	var template = /^[0-9a-z]+[0-9a-z._-]*\@[0-9a-z]+[0-9a-z._-]*\.[0-9a-z]{2,}$/i;
  	if (template.test(email) == false) return false;
	return true;
}


function focusFailedInput(inputId, errorMessage)
{
	var labels = document.getElementsByTagName("label");


	var tmplabel;
	// set error class to correct label and remove error class from others
	for(var i = 0; i < labels.length; i++ )
	{
		var label = labels[i];
		label.className = label.className.replace("error", "");
		if( label.htmlFor == inputId ) { label.className += " error"; }
	}

	document.getElementById(inputId).focus();
	alert(errorMessage);
	return false;
}

function clearError()
{
	var labels = document.getElementsByTagName("label");

	for(var i = 0; i < labels.length; i++ )
	{
		var label = labels[i];
		label.className = label.className.replace("error", "");
	}
	return true;
}

function setSelect(inputHidden, selectId)
{
	var value = selectId.options[selectId.selectedIndex].value;
	var text = selectId.options[selectId.selectedIndex].text;

	document.getElementById(inputHidden).value =  text + " (" + value + ")" ;
	return true;
}

function setRadio(inputHidden, radioId)
{
	var labelId;
	radioId = radioId.nextSibling;
	for (q=0; q<3; q++){
		if (radioId.tagName == "LABEL"){
			labelId = radioId;
			break;
		}
		radioId = radioId.nextSibling;
	}
	if (!labelId){
		alert("Label not found!");
		return false;
	}
	//var ns6=document.getElementById&&((!document.all && !window.opera) || (document.all && window.opera));
	//var labelValue= ns6 ? radioId.nextSibling.nextSibling : document.all[radioId.sourceIndex+1];
	//var labelValue = radioId.nextSibling.nextSibling;
	document.getElementById(inputHidden).value = labelId.innerHTML;
	return true;
}

function fieldStatus(field, status)
{
	document.getElementById(field).disabled=status;
	document.getElementById(field).className = (status == true) ? "disabled" : "";
	document.getElementById(field).title = (status == true) ? "Select 'Other' to write here" : "Write your primary area of interest here";
}

/*=============================================================================
    SEARCH TOOL TIP
==============================================================================*/

function searchToolTip()
{
	var input = document.getElementById("search-clause");
	input.onfocus = function() { toolTip(true) };
	input.onblur = function() { toolTip(false) };

}

function toolTip(status)
{
	var modifier = (status == true) ? 1 : -1;
	var toolTipStyle = document.getElementById("search_tooltip").style;
	toolTipStyle.display = (status == true) ? "block" : "none" ;
}

/*=============================================================================
    WINDOW GALLERY
==============================================================================*/

function makePopup(image)
{
	var url = image.getAttribute("href");
	var windowWidth = 600;
	var windowHeight = 600;
	var windowLeft = (screen.width / 2) - (windowWidth / 2)
	var windowTop = (screen.height / 2) - (windowHeight / 2)
	var newWindow = window.open(url, "okienko", "menubar=no, toolbar=no, location=no, scrollbars=no, resizable=no, status=no, width=" + windowWidth + ", height=" + windowHeight + ", left=" + windowLeft + ", top=" + windowTop)
	newWindow.focus()
	return false;
}


function generatePopups()
{
	var popups = document.getElementsByTagName("a");

	for( var i = 0; i < popups.length; i++ )
	{
		popup = popups[i];
		if(popup.className == "popup" || popup.className == "img popup")
		{
			popup.onclick = function() { return makePopup(this); };
		}
	}
}

/*=============================================================================
    KEYBOARD NAVIGATION
==============================================================================*/

function keyboardNavigation()
{
	makeTabOver("top_menu");
	makeTabOver("sections");
	makeTabOver("left_menu");
	makeTabOver("bottom_menu");
}

function makeTabOver(id)
{
	var nodes = document.getElementById(id).getElementsByTagName("a");
	for(var i = 0; i < nodes.length; i++)
	{
		nodes[i].onfocus = function() { this.className += " hover" }
		nodes[i].onblur = function() { this.className = this.className.replace("hover","") }
	}
}


function focusSearch()
{
	var input = document.getElementById("search-clause2");
	if(input)
	{
		input.select();
		input.focus();
	}
}

function setHeightsColumn(){
	colLeft = document.getElementById("left_column");
	colBody = document.getElementById("body");
	if (colLeft && colBody){
		if (colBody.offsetHeight < colLeft.offsetHeight ){
			colBody.style.height = colLeft.offsetHeight;
		}
	}
}

function fixPng() {
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	var shim='/js/x.gif';

	if ((version >= 5.5) && (document.body.filters))
	{
	   for(var i=0; i<document.images.length; i++)
	   {
	      var img = document.images[i]
	      var imgName = img.src.toUpperCase()
	      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	      {
      		var src = img.src;
			img.style.width = img.width + "px";
			img.style.height = img.height + "px";
			img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
			img.src = shim;
	         i = i-1
	      }
	   }
	}
}

function start()
{
//	keyboardNavigation();
	setHeightsColumn();
	generatePopups();
	focusSearch();
	fixPng();
}

window.onload = start;


