// Javascript functions to check a form etc.

window.onload = function() {
	var sidenav = document.getElementById('sidenav');	
	var mainitems = sidenav.getElementsByTagName('li');
	var links = new Array();
	var children;
	var n = 0;
	for (var i=0;i<mainitems.length;i++) {
		if (mainitems[i].className == 'sidenav') {
			children = mainitems[i].getElementsByTagName('a');
			links[n] = children[0];	
			n++;
		}
	}
	for (var i=0;i<links.length;i++) {
		links[i].onclick = function() {
			var li = this.parentNode;
					
			if (!li.nextSibling) {
				return true;	
			}
			var next = li.nextSibling;
			var mozilla = false;
			if (!next.tagName || next.tagName.toLowerCase() != 'li') {
				if (!next.nextSibling) {
					return true;
				}
				mozilla = true;
				next = next.nextSibling;	
			}
			if (!next.className || next.className != 'sidenav_sub') {
				return true;	
			}
			var display = next.style.display == 'none'? 'block' : 'none';
			var arrow = next.style.display == 'none'? '/img/arrow_down.gif' : '/img/arrow_right.gif';
			var value = next.style.display == 'none'? 'true' : 'false';

			var thefile = '/ajax/setmenusessionvar.php';
			var xmlhttp = getxmlhttp();
			xmlhttp.open("GET", thefile + "?id=" + li.id + "&value=" + value + "&t="+((new Date()).valueOf()), true);   
			xmlhttp.onreadystatechange = function() {}
			xmlhttp.send(null);

			this.style.backgroundImage = 'url(\''+arrow+'\')';
			while (next && next.className && next.className == 'sidenav_sub') {
				next.style.display = display;
				if (!next.nextSibling) {
					return false;	
				}
				next = next.nextSibling;
				if (mozilla) {
					next = next.nextSibling;	
				}
			}
			return false;	
		}
	}
}

function getxmlhttp() {
    var xmlhttp = false;
    
    //Check for IE
    try {
        // If JavaScript version greater than 5
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e) {
        // If not, use older active x object
        try {
            // If IE
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(E) {
            // Browser non-ie
            xmlhttp = false;
        }
    }
    
    // If browser non-ie, create JavaScript instance of object
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}

function checkAccomodation(element) {
	if (element.options[element.selectedIndex].value == 'yes') {
		document.getElementById('accomodationtableno').style.display = 'none';
		document.getElementById('accomodationtableyes').style.display = '';
	}
	else {
		document.getElementById('accomodationtableno').style.display = '';
		document.getElementById('accomodationtableyes').style.display = 'none';
	}
}

function checkIAgreeBox(element) {
	if (element.checked) {
		document.getElementById('submitbutton').disabled = false;
	}
	else {
		document.getElementById('submitbutton').disabled = true;
	}
}

function checkForm() {
	var err = '';
	var foulChars = false;
	$(".required").each(function(elem) {
		var charsNotAllowed = getCharsNotAllowed($(this).attr('value'));
		
		if (($(this).attr('value') == '' || charsNotAllowed == true) &&  $(this).parents('table').css('display') != 'none' ) {
			err += 'x';
			if(charsNotAllowed == true) {
				foulChars = true;
				/*var msg = "Please do not use the following characters: \", «, » ´´, `, '\n\nPlease do not use nummered listings, eg.: 1. 2. etc.";
				if($('#formLanguage').text() == 'FR') {
					msg = "Merci de ne pas utiliser les caractères suivants.\n\nMerci de ne pas utiliser les signes numériques comme.";
				}

				alert(msg);*/
			}
			$(this).css({ backgroundColor: "#f3bcbc" });
		}
		else {
			$(this).css({ backgroundColor: "" });			
		}
	});
	
	$('.checkchars').each(function(elem) {
		var charsNotAllowed = getCharsNotAllowed($(this).attr('value'));
		if(charsNotAllowed == true &&  $(this).parents('table').css('display') != 'none') {
			err += 'x';
			foulChars = true;
			$(this).css({ backgroundColor: "#f3bcbc" });
		}
		else {
			$(this).css({ backgroundColor: "" });
		}
	});
	
	
	if(foulChars) {
		var msg = "Please do not use the following characters: \", «, » ´´, `, '\n\nPlease do not use nummered listings, eg.: 1. 2. etc.";
		if($('#formLanguage').text() == 'FR') {
			msg = "Merci de ne pas utiliser les caractères suivants.\n\nMerci de ne pas utiliser les signes numériques comme.";
		}
		alert(msg);
	}

	$(".requiredmailfr").each(function(elem) {
		if (checkMailFrench($(this).attr('value'))) {
			$(this).css({ backgroundColor: "" });
		}
		else {
			err += 'x';
			$(this).css({ backgroundColor: "#f3bcbc" });
		}
	});
	$(".requiredmail").each(function(elem) {
		if (checkMail($(this).attr('value'))) {
			$(this).css({ backgroundColor: "" });
		}
		else {
			err += 'x';
			$(this).css({ backgroundColor: "#f3bcbc" });
		}
	});
	

	if (err == '') {
		return true;
	}
	else {
		return false;
	}
}

function getCharsNotAllowed(value) {
	var reg = /"|«|`|»|´|´´|'|([0-9]*\.{1})/;
	return reg.test(value);
	
}
function checkMail(mail) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(mail) == false) {
      return false;
   }
   else {
	  var reg_yahoo = /yahoo/;
   	  if(reg_yahoo.test(mail) == true) {
		  alert('Attention!\nDue to technical problems we temporarily can\'t send email to certain accounts:\nsuch as terra.br.com, Yahoo or uol.com.br.\n\nPlease create a provisory personal account.');
		return false;
	  }
	  return true;
   }
}

function checkMailFrench(mail) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(mail) == false) {
      return false;
   }
   else {
	  var reg_yahoo = /yahoo/;
   	  if(reg_yahoo.test(mail) == true) {
		  alert('Attention:\nEn raison de difficultés techniques, nous ne pouvons temporairement envoyer de courriels à destinations de certains comptes: nnotamment Yahoo, uol.com.br et terra.br.com.\n\nMerci de créer un compte personnel provisoire.');
		return false;
	  }
	  return true;
   }
}


$(document).ready(function () {
    $("form").bind("submit", checkForm)
});

function showSub(li) {
	var mainnav_item = li.getElementsByTagName('a');
	mainnav_item = mainnav_item[0];
	var subnav = li.getElementsByTagName('ul');
	if (subnav.length > 0) {
		subnav = subnav[0];

		if (mainnav_item.className != 'stay_active') {
			mainnav_item.className = 'active';
		}
		subnav.style.display = 'block';
	}
}

function hideSub(li) {
	var mainnav_item = li.getElementsByTagName('a');
	mainnav_item = mainnav_item[0];
	var subnav = li.getElementsByTagName('ul');
	if (subnav.length > 0) {
		subnav = subnav[0];

		subnav.style.display = 'none';
		if (mainnav_item.className != 'stay_active') {
			mainnav_item.className = '';
		}
	}
}

function doShowIDHideClass(id, cls) {
	$('.'+cls).hide();
	$('#'+id).show();
}

function doYesNoID(val, id) {
	if (val == 'yes') {
		$('#'+id).show();
	}
	else {
		$('#'+id).hide();
	}
}