var OS = 'Win';
var version = "n";
browserVer=navigator.appVersion;
App = navigator.appName;
var Version=browserVer.substring(0,1);
var posOS = navigator.appVersion.indexOf('Mac');
var posOS2 = navigator.appVersion.indexOf('Win');
if (posOS >= 0) OS = 'Mac';
if ((posOS < 0) && (posOS2 >= 0)) OS = 'Win';
if (App == "Netscape" && Version >= 3) version = "n3";

/////////////////////
// gestion des pop-up
/////////////////////
var popupList=new Array();
function openwin(name,myname,mywidth,myheight,mytool,mydir,mystatus,myscroll,myresize,mymenu) {
	mytop=Math.floor((screen.height/2) - (myheight/2));
	myleft=Math.floor((screen.width/2) - (mywidth/2));
	var debute;
	if (version != "n3" && OS != 'Mac') {
		debute = window.open(name,myname,"toolbar="+mytool+",width="+mywidth+",height="+myheight+",directories="+mydir+",status="+mystatus+",scrollbars="+myscroll+",top="+mytop+",left="+myleft+",resizable="+myresize+",menubar="+mymenu)
	}
	if (OS == 'Mac') debute = window.open(name,myname,"toolbar="+mytool+",width="+mywidth+",height="+myheight+",directories="+mydir+",status="+mystatus+",scrollbars="+myscroll+",top="+mytop+",left="+myleft+",resizable="+myresize+",menubar="+mymenu)
	if (version == "n3") {
		debute = window.open(name,myname,"toolbar="+mytool+",width="+mywidth+",height="+myheight+",directories="+mydir+",status="+mystatus+",scrollbars="+myscroll+",top="+mytop+",left="+myleft+",resizable="+myresize+",menubar="+mymenu)
		debute.focus();
	}
	popupList.push(debute);
}

function closewindow(){
//close window open with openwin function
for(var i=0;i < popupList.length;i++){
if(popupList[i] && popupList[i].open)popupList[i].close();
}
}

function popup(myurl,mywidth,myheight) {

	if (arguments.length>3) {
		var myscroll = arguments[3];
	}else{
		var myscroll = 1;
	}
	var ran_number=Math.round(Math.random()*10); 
	
	debute = openwin(myurl,"popup" + ran_number,mywidth,myheight,0,0,1,1,1,0);
}

function launch(myobject,mywidth,myheight) {
	extension = myobject.substring(myobject.length - 3, myobject.length);
	if (extension == 'swf') {
		popup('/include/popup_flash.cfm?file='+myobject+'&width='+mywidth+'&height='+myheight,mywidth,myheight);
	}else if (extension == 'mov') {
		popup('/include/popup_quicktime.cfm?file='+myobject+'&width='+mywidth+'&height='+myheight,mywidth,myheight);
	}else {
		popup(myobject,mywidth,myheight);
	}
}

/////////////////////
// fonctions de base
/////////////////////
function check_email(value) {
    email_type = /^[a-z0-9][a-z0-9_\.-]*@[a-z0-9][a-z0-9_\.-]*\.([a-z]{2}|com|org|net|mil|gov|edu|biz|info|name)$/i;
    if (!email_type.test(value)) {
        return false;
    } else {
        return true;
    } 
}

function check_alpha(value) {
	if (value == "") {
		return false;
	} else {
		return true;
	}
}


function charInString (c, s){
	for (i = 0; i < s.length; i++){
		if (s.charAt(i) == c) return true;
	}
	return false
}

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"))) return false;
	}
	// All characters are numbers.
	return true;
}

function isIntegerInRange (s, a, b){
	if (!isInteger(s)) return false;
	var num = parseInt (s);
	return ((num >= a) && (num <= b));
}

function isYear (s){
	if (!isInteger(s)) return false;
	if (s.length == 4 && (s>1900 || s<2100)) return true;
	if (s.length == 2) return isIntegerInRange (s, 0, 99);
}

function isMonth (s){
	if (s.charAt(0)=="0" && s.length>1) s=s.substring(1);
	return isIntegerInRange (s, 1, 12);
}

function isDay (s){
	if (s.charAt(0)=="0" && s.length>1) s=s.substring(1);
	return isIntegerInRange (s, 1, 31);
}

function isDate (s){

	if (s.length == 0) return false;
	var selectedDate = s.split("/");
	var day = selectedDate[0];
	var month = selectedDate[1];
	var year = selectedDate[2];

	return (isDay(day)*isMonth(month)*isYear(year));	
}

function noenter() {
	return !(window.event && window.event.keyCode == 13); 
}

function confirmdelete(myurl) {
	userInput = confirm('Do you confirm \ndeleting this item?');
	if (userInput != '' && userInput != null) {
		document.location.href=myurl;
	}
}
function JSconfirm(myask, myurl) {
	userInput = confirm(myask);
	if (userInput != '' && userInput != null) {
		document.location.href=myurl;
	}
}


////////////////////
// Check formulaires
////////////////////
function CheckFormEvent()
{
	var message ="";
	en = check_alpha(document.myForm.event_name_en.value) && check_alpha(document.myForm.long_description_en.value);
	fr = check_alpha(document.myForm.event_name_fr.value) && check_alpha(document.myForm.long_description_fr.value);
	//if (!en && !fr) message+='- Provide at least EN or FR Title/Description\n';
	//if (!en && (check_alpha(document.myForm.event_name_en.value) || check_alpha(document.myForm.long_description_en.value))) message+='- Provide EN Title and Description or leave blank\n';
	//if (!fr && (check_alpha(document.myForm.event_name_fr.value) || check_alpha(document.myForm.long_description_fr.value))) message+='- Provide FR Title and Description or leave blank\n';


	if (check_alpha(document.myForm.contact_email.value) && !check_email(document.myForm.contact_email.value)) message+='- Email contact\n';
	if(message.length>0) {
		message='Please check the following fields :\n' + message;
		alert(message);
		return false;
	} else {
		return true;
		document.myForm.submit();
	}
}


function CheckFormReservation()
{
	var message ="";
	if (!isDate(document.myForm.start_date.value)) message+='- Start date\n';
	if (check_alpha(document.myForm.start_time1.value) || check_alpha(document.myForm.start_time2.value)) {
		if (!isIntegerInRange (document.myForm.start_time1.value, 0, 23) || !isIntegerInRange (document.myForm.start_time2.value, 0, 59)) {
			message+='- Start time\n';
		}
	}
	
	if (!isDate(document.myForm.end_date.value)) message+='- Stop date\n';
	if (check_alpha(document.myForm.end_time1.value) || check_alpha(document.myForm.end_time2.value)) {
		if (!isIntegerInRange (document.myForm.end_time1.value, 0, 23) || !isIntegerInRange (document.myForm.end_time2.value, 0, 59)) {
			message+='- Stop time\n';
		}
	}
	
	if (!check_alpha(document.myForm.event_title.value)) message+='- Title\n';
	if (check_alpha(document.myForm.contact_email.value) && !check_email(document.myForm.contact_email.value)) message+='- Email contact\n';
	if(message.length>0) {
		message='Please check the following fields :\n' + message;
		alert(message);
		return false;
	} else {
		return true;
		document.myForm.submit();
	}
}

function CheckFormUser()
{
	var message ="";
	document.myForm.submit();
}

////////////////////////////////////
// Manipulate multiple select combos
////////////////////////////////////

function Combo(myCombo,myArrItems,mySelectedItems){
	this.myCombo = myCombo;
	this.myArrItems = myArrItems;
	this.mySelectedItems = mySelectedItems;
	this.update=function(){
		clearCombo(this.myCombo);
		this.myArrSelectedItems = this.mySelectedItems.value.split(",");
		for (var i = 0; i < this.myArrSelectedItems.length; i ++){
			fillCombo(this.myCombo,this. myArrSelectedItems[i],this.myArrItems[this.myArrSelectedItems[i]]);
		}
	}
	this.update();
}

function clearCombo(myCombo){
	for (var i = myCombo.options.length - 1; i >= 0; i--){
		myCombo.options[i] = null;
	}
myCombo.selectedIndex = -1;
}

function fillCombo(myCombo, myValue, myName){
	if (myValue != "" && myName != ""){
		myCombo.options[myCombo.options.length] = new Option(myName, myValue);
	}
}

////////////////////////////////////
// Print Function
////////////////////////////////////

function printContent(idToPrint) 
{ 

if(document.getElementById(idToPrint)){
	   var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
       sOption+="scrollbars=yes,width=750,height=600,left=100,top=25"; 
   var sWinHTML = document.getElementById(idToPrint).innerHTML; 
   var winprint=window.open("","",sOption); 
       winprint.document.open(); 
       winprint.document.write('<html><head><title>World Heritage Centre</title><LINK href=/styles/style_fonts.cfm rel=Stylesheet><style>table {border:1px;}</style></head><body style="background-color:#ffffff;"><span class="main_page">'); 
       winprint.document.write(sWinHTML);          
       winprint.document.write('</span></body></html>'); 
       winprint.document.close(); 
       winprint.focus(); 
	   };
}
;


////////////////////////////////////
// Sets a DIV to visible or invisible
////////////////////////////////////
function VisibleId(baliseId,visible) 
  {
  if (document.getElementById && document.getElementById(baliseId) != null) 
    {
	var visibility = (!visible)? 'hidden' : 'visible';
	var display = (!visible)? 'none' : 'block';
    document.getElementById(baliseId).style.visibility = visibility;
	document.getElementById(baliseId).style.display = display;
    }
  }


/**
 * Append any supplied arguments to the end of the array, in the order given.
 * @return the new length of the array
 * @author <a href="http://hexmen.com/blog/">Ash Searle</a>
 * @license http://creativecommons.org/licenses/by/2.5/
 */
Array.prototype.push = function() {
    var n = this.length >>> 0;
    for (var i = 0; i < arguments.length; i++) {
	this[n] = arguments[i];
	n = n + 1 >>> 0;
    }
    this.length = n;
    return n;
};

/**
 * Remove the last element of the array and return it.
 * @return the element removed (if any), otherwise return <code>undefined</code>
 * @author <a href="http://hexmen.com/blog/">Ash Searle</a>
 * @license http://creativecommons.org/licenses/by/2.5/
 */
Array.prototype.pop = function() {
    var n = this.length >>> 0, value;
    if (n) {
	value = this[--n];
	delete this[n];
    }
    this.length = n;
    return value;
};

Array.prototype.clear = function() {
    var n = this.length  >>> 0;
	//alert(this.length);
	for (var i=0; i<n; i++){
	this[--this.length];
	delete this[i];	
	
	}
   return this;
};
/**
 * Display text into an input and removed it onselect and onsubmit
 * @return 
 * @author e.esquivel 2008
 * @updated in 2011-10-04
 * @license
 */	
 function cleanForm(form_name,inputfieldsList){
var valueArray = inputfieldsList.split(",");
var tempvalue=new Array();
var tempval = "";
for(var i=0; i<valueArray.length; i++)
	{
		
	document.getElementById(valueArray[i]).tempvalue=document.getElementById(valueArray[i]).value;
	document.getElementById(valueArray[i]).onfocus=function(){this.tempval=this.value;if(this.value==this.tempvalue){this.value="";}}
	document.getElementById(valueArray[i]).onblur=function(){if(this.value==""){this.value=this.tempvalue;
	}}
	}

	document.getElementById(form_name).onsubmit=function(){
		for(var i=0; i<valueArray.length; i++){
			if(document.getElementById(valueArray[i]).value==document.getElementById(valueArray[i]).tempvalue)
				{
				document.getElementById(valueArray[i]).value="";
				}
		}	
	}
}
