// JavaScript Document

function clearTextLogin(text, name) {
	if (text.value == name) {
		text.value= "";
	}
}

function resetTextLogin(text, name) {
	if (text.value == "") {
		text.value= name;
	}
}


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;
}

Array.prototype.has=function(needle,strict)
{
	strict = strict === undefined ? strict : false;
	for(key in this)
		if(strict ? this[key] === needle : this[key] == needle)
			return true;
    return false;
}

/* 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 isEmail(email)
{
	return (/^[A-Za-z0-9]+(([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/).test(email);
}
function isPhone(num)
{
	return (/^((\d(-|\s|\.)?)?(\d{3}(-|\s|\.)?))?\d{3}(-|\s|\.)?\d{4}$/).test(num);
}

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


function checkForm(form)
{
	var totalValid = true;
	for(var i=0;i != form.elements.length; ++i)
	{
		var valid = true;
		var element = form[i];
		var className = element.className;
		
		if(className != undefined && className.match(/check/g) != null)
		{
			fieldType = /\[([a-z|])+\]/.exec(className);
			if(fieldType != null)
			{
				fieldTypes = fieldType[0].replace(/\[|\]/g,"").split("|");
				var required = fieldTypes.has('required'), empty = element.value == "";
				for(var j = 0; j<fieldTypes.length && (!empty || required);j++)
					switch(fieldTypes[j])
					{
						case "email" : valid = isEmail(element.value);break;	
						case "phone" : valid = isPhone(element.value);break;
						case "postal" : valid = isPostal(element.value);break;
						default : valid = (required && !empty);break;
					}
			}
			else
				valid = false;
			element.style.borderColor = valid ? "#362C27" : "#FF0000";
			totalValid = totalValid && valid;
		}
	}
	return totalValid;				
}
/* Fin form check */


/* Rotation Banner */
var banner_on = false;
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"+img_num)
		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 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'+current_banner;
	$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'+img_num;
	$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);
}

/* End rotation banner */
