// JavaScript Document

function clearText(field,color){
    field.value = field.value == field.defaultValue ? '' : field.value;
	field.style.color = field.value == field.defaultValue ? color : field.style.color;
}
function returnText(field,color){
	
	field.value = field.value == '' ?  field.defaultValue : field.value;
	field.style.color = field.value == '' ? color : field.style.color;
}

function _open()
{
	var popup = document.getElementById('popup');
	popup.style.display= 'block';
	var width = popup.offsetWidth;
	var height = popup.offsetHeight;
	var maxH = 165;
	var maxW = 300;
	var opacity = 1;
	document.getElementById('grey').style.display = 'block';
	var Iwidth = setInterval(function(){
								if(width < maxW)
								{
									popup.style.width =  width+"px";
									width+=5;
								}
								else
								{
									popup.style.width = "300px";
									clearInterval(Iwidth);
								}
							},1); 
	var Iheight = setInterval(function(){
								if(height < maxH)
								{
									popup.style.height =  height+"px";
									height+=3;
								}
								else
								{
									popup.style.height = "165px";
									clearInterval(Iheight);
								}
							},1); 
}
function _close()
{
	var popup = document.getElementById('popup');
	popup.style.display= 'block';
	var width = popup.offsetWidth;
	var height = popup.offsetHeight;
	var Iwidth = setInterval(function(){
								if(width > 0)
								{
									popup.style.width =  width+"px";
									width-=5;
								}
								else
								{
									popup.style.width = "0px";
									clearInterval(Iwidth);
								}
							},1); 
	var Iheight = setInterval(function(){
								if(height > 0)
								{
									popup.style.height =  height+"px";
									height-=3;
								}
								else
								{
									popup.style.display = "none";
									clearInterval(Iheight);
									document.getElementById('grey').style.display = 'none';
								}
							},1); 
}

/* Form check */

function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9")) && c != "-") return false;
    }
    // All characters are numbers.
    return true;
}

function isValidEmail(email)
{
	return email.search(/^[A-Za-z0-9]+(([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/) != -1;
}
function isValidPhoneNumber(num)
{
	if(num.length <= 11 && num.length >= 7 && isInteger(num)) return true;
	else if (num.length == 12 && num.search(/^\d{3}(-|\s)\d{3}(-|\s)\d{4}$/) != -1) return true;
	else if (num.length == 14 && num.search(/^\d{1}(-|\s)\d{3}(-|\s)\d{3}(-|\s)\d{4}$/) != -1) return true;
	else return false;
}

function isPostalCode(code)
{
	return code.search(/^\w{1}\d{1}\w{1}(-|\s)+?\d{1}\w{1}\d{1}$/) || code.search(/^\d{5}$/);
}

function checkForm(form)
{
	var valid = true;

	if(form.name.value == "")
	{
		form.name.style.borderColor = "#FF0000";
		valid = false;
	}
	else
		form.name.style.borderColor = "#065d8d";

	
	if(form.email.value == "")
	{
		form.email.style.borderColor = "#FF0000";
		valid = false;
	}
	else if(isValidEmail(form.email.value) == false)
	{
		form.email.style.borderColor = "#FF0000";
		valid = false;
	}
	else
		form.email.style.borderColor = "#065d8d";
	
	
	
	if(form.phone.value != "")
	{
		if(isValidPhoneNumber(form.phone.value) == false)
		{
			
			form.phone.style.borderColor = "#FF0000";
			valid = false;
		}
		else
			form.phone.style.borderColor = "#DCDCDB";
	}
	else
			form.phone.style.borderColor = "#DCDCDB";
			
	return valid;
}

/* Fin form check */

/* Rotation Banner */
var banner_on = true;
var current_banner = 1;
var t;
var btn = true;
var maxImg = 4;
var _timer;

function $F(id) {
var $F = document.getElementById(id);

$F.fadeIn = function(delay,callbk,out) {
    var _this = this;
	var filter = (navigator.appName == "Microsoft Internet Explorer") ? 50 : 100;
	if (out == undefined) _this.style.display = "block";
    for (i = 1; i <= 100; i++) {
      (function(j) {
            setTimeout(function() {  
                  if (out==true) j=100-j;
                  _this.style.opacity = j/100;
                  _this.style.MozOpacity = j/100;
                  _this.style.KhtmlOpacity = j/100;
                  _this.style.zoom = 1; // for ie, set haslayout
                   if (j==100&&callbk!=undefined) {callbk.call(_this);}
                   else if (out==true&&callbk!=undefined&&j==0) {callbk.call(_this);}
                  _this.style.filter = "alpha(opacity=" + j + ");";},j*delay/filter);         
        })(i);
    }
};

$F.fadeOut = function(delay,callbk) {
    $F.fadeIn(delay,callbk,true);
	};
return $F;
}

function clickBox(id,img_num)
{
	if(id.className.split(" ",1) == "flash_btn")
		rotate(img_num);	
}

function previousBox()
{
	if(current_banner-1 <= 0)
		rotate(maxImg);
	else
		rotate(current_banner-1);
}

function nextBox()
{
	if(current_banner + 1 > maxImg)
		rotate(1);
	else
		rotate(current_banner+1);
}

function openClose(id)
{
	closeAll(new Array("programme"),id);
	var div = document.getElementById(id);
	if(hasClass(div,"open"))
	{
		removeClass(div,"open");
		addClass(div,"close");
	}
	else if(hasClass(div,"close"))
	{
		removeClass(div,"close");
		addClass(div,"open");
	}
}

function closeAll(ids,id) {
	for(var i=0;i<ids.length;i++)
	{
		if(ids[i] != id)
		{
			var el = document.getElementById(ids[i]);
			removeClass(el,"open");
			addClass(el,"close");
		}
	}
}

function select_value(el,inputId)
{
	var value = el.innerHTML;
	document.getElementById(inputId).value = value;
	document.getElementById('current-selection-programme').innerHTML = value;
	openClose('programme');
}

function hasClass(ele,cls)
{
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}

function addClass(ele,cls)
{
	if (!this.hasClass(ele,cls)) 
		ele.className += " "+cls;
}

function removeClass(ele,cls) 
{
	if (hasClass(ele,cls)) 
	{
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

function init_rotate(btn_flag)
{
	if(btn_flag !== undefined)
		btn = btn_flag; 	
	for(var i = 2; i <= maxImg; i++)
	{
		$F("imgid"+i).fadeOut(1);
	}
	t = setTimeout(function(){rotate(2)},5000);
}

function init() 
{
  
  arguments.callee.done = true;
  if (_timer) clearInterval(_timer);
  init_rotate();
};

function rotate(img_num) 
{
	clearTimeout(t);
	var time;
	if(navigator.appName == "Microsoft Internet Explorer")
		time = 180;
	else
		time = 1000;
	if(btn)
		document.getElementById('btn_flash'+current_banner).className = 'flash_btn';
	$F("imgid"+current_banner).fadeOut(time);
	document.getElementById('flashlink').href = document.getElementById('imgid'+img_num).getAttribute('name');
	if(btn)
		document.getElementById('btn_flash'+img_num).className = 'flash_btn_active';
	$F("imgid"+img_num).fadeIn(time);
	current_banner = img_num;
	if(img_num >= maxImg || img_num <=0)
		img_num = 1;
	else
		img_num++;
	t = setTimeout(function(){rotate(img_num)},5000);
}

if(banner_on)
{

	/* for Mozilla/Opera9 */
	if (document.addEventListener) {
	  document.addEventListener("DOMContentLoaded", init, false);
	}

	/* for Internet Explorer */
	/*@cc_on @*/
	/*@if (@_win32)
	  document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	  var script = document.getElementById("__ie_onload");
	 _timer = function() {
		if (this.readyState == "complete") {
		  init(); // call the onload handler
		}
	  };
	/*@end @*/

	/* for Safari */
	if (/WebKit/i.test(navigator.userAgent)) { // sniff
	  var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
		  init(); // call the onload handler
		}
	  }, 10);
	}

	/* for other browsers */
	window.onload = init;
}

/* End rotation banner */
