﻿// FONCTIONS JAVASCRIPT URBACOM




// ********** DÉTECTE SI UN UTILISATEUR ACCEPTE LES COOKIES **********

// cookie construct
function Cookie(document,name,hours,path,domain,secure) {
  // any VAR in "this" that does not start with a "$" will
  // be written into the cookie (read from also)
  this.$doc  = document
  this.$name = name
  if (hours)  this.$expiration=new Date((new Date()).getTime()+hours*3600000); else this.$expiration = null
  if (path)   this.$path   = path;                                             else this.$path       = null
  if (domain) this.$domain = domain;                                           else this.$domain     = null
  if (secure) this.$secure = true;                                             else this.$secure     = false
}

function CookieWrite() {
  var cookieval=""
  for(var prop in this) {
    if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function') || prop == '') continue
	if (cookieval != "") cookieval += '&'
	cookieval+=prop+":"+escape(this[prop])
  }
  var cookie=this.$name+"="+cookieval
  if (this.$expiration) cookie+='; expires=' + this.$expiration.toGMTString()
  if (this.$path)       cookie+='; path='    + this.$path
  if (this.$domain)     cookie+='; domain='  + this.$domain
  if (this.$secure)     cookie+='; secure'
  this.$doc.cookie=cookie
}

function CookieRead() {
  var allcookies=this.$doc.cookie
  if (allcookies=="") {
    return false
  }
  var start= allcookies.indexOf(this.$name+'=')
  if (start== -1) {
    return false
  }
  start += this.$name.length+1
  var end=allcookies.indexOf(';',start)
  if (end == -1) end=allcookies.length
  var cookieval = allcookies.substring(start,end)
  var a = cookieval.split('&')
  for (var i=0;i < a.length;i++) a[i]=a[i].split(':')
  for (var i=0;i < a.length;i++) this[a[i][0]]=unescape(a[i][1])
  return true
}

function CookieDelete() {
  var cookie = this.$name+'='
  if (this.$path)   cookie+='; path='+this.$path
  if (this.$domain) cookie+='; domain='+this.$domain
  cookie+='; expires=Fri, 02-Jan-1970 00:00:00 GMT'  // MAKE IT EXPIRE!
  this.$doc.cookie=cookie
}

/*new Cookie()
Cookie.prototype.write = CookieWrite
Cookie.prototype.del   = CookieDelete
Cookie.prototype.read  = CookieRead
// end cookie construct

var myCookie = new Cookie(document,"myName",240)
myCookie.test="test";
myCookie.write();
if (!myCookie.read() || myCookie.test!="test")
  document.write("Cookies not working!");
else
  document.write("Cookies are working!");*/
// ********** FIN DÉTECTE SI UN UTILISATEUR ACCEPTE LES COOKIES **********





// ********** DETECTION BROWSER **********

// Retourne strBrowser=NN si Netscape Navigator ou strBrowser=IE si MSIE
var strBrowser = "IE";
if (navigator.appName.substring(0,8) == "Netscape") strBrowser = "NN";
if (navigator.appName.substring(0,9) == "Microsoft" && navigator.appVersion.indexOf("Win") > 0 ) strBrowser = "IE";

// Detection browser
var browser;
var isIE4 = (document.all) ? true:false; 
if (isIE4) { 
	if (navigator.userAgent.indexOf('MSIE 6') > 0) { 
		browser = 'IE6';
	} else if (navigator.userAgent.indexOf('MSIE 5') > 0) { 
		browser = 'IE5';
	} else {
		browser = 'IE4';
	} 
} else {
	if (navigator.userAgent.indexOf('Netscape/7') > 0) { 
		browser = 'NN7';
	} else if (navigator.userAgent.indexOf('Netscape/6') > 0) { 
		browser = 'NN6';
	} else {
		browser = 'NN4';
	} 
} 

// Détection Mac
var OS;
if (navigator.appVersion.indexOf("Mac") != -1) {
	OS = 'mac'; 
} else {
	OS = 'win'; 
}
// ********** FIN DETECTION BROWSER **********





// ********** ROLLOVERS **********

function switchToOn(imgName, sourceName) {
	if (sourceName == null) {
		imgOn = eval(imgName + "ON.src");
	} else {
		imgOn = eval(sourceName + "ON.src");
	}
	document[imgName].src = imgOn;
}

function switchToOff(imgName, sourceName) {
	if (sourceName == null) {
		imgOff = eval(imgName + "OFF.src");
	} else {
		imgOff = eval(sourceName + "OFF.src");
	}
	document[imgName].src = imgOff;
}
// ********** FIN ROLLOVERS **********





// ********** COMPTEUR TEXTE **********

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	} else {
		countfield.value = maxlimit - field.value.length;
	}
}
// ********** FIN COMPTEUR TEXTE **********



// ********** VALIDATION NUM **********

function isNumeric(strString) {
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	for (i = 0; i<strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}
// ********** FIN VALIDATION NUM **********




// ********** VALIDATION FORMULAIRES **********

// Clean du champ recherche
var isCleaned = false;
function  cleanRecherche(oChampRecherche) {
	if (!isCleaned) {
		oChampRecherche.value = '';
		isCleaned = true;
	}
}
// ********** FIN VALIDATION FORMULAIRES **********




// ********** POP-UPS **********
function popup(strPage, intWidth, intHeight, strOptions) {
	var intTop = (screen.height-intHeight)/2;
	var intLeft = (screen.width-intWidth)/2;
	window.open(strPage, "", "top=" + intTop + ",left=" + intLeft + ",width=" + intWidth + ",height=" + intHeight + "," + strOptions);
//  window.open(strPage, '', 'top=50, left=200, width=500, height=400, resizable=yes, toolbar=no, scrollbars=no, status=yes');
}
// ********** FIN POP-UPS **********




// ********** CLOSE WINDOW **********
function CloseWindow() { 
	opener=self; 
	self.close(); 
}
// ********** FIN CLOSE WINDOW **********



// ********** DROP SHADOWS ON IMAGES **********
function applyDropShadows(cssSelector,shadowStyle){
	if(document.getElementsByTagName && document.createElement){
		// get all elements that match the specified css selector
		var elements = document.getElementsBySelector(cssSelector);
	
		// loops through the list of matching elements
		for(i=0;i<elements.length;i++){
		
			var element = elements[i];
			
			// create the wrapper divs
			var wrap1 = document.createElement('div');
			var wrap2 = document.createElement('div');
			var wrap3 = document.createElement('div');
			wrap1.className = shadowStyle;
			
			// duplicate the image to be shadowed
			var newElement = element.cloneNode(true);
			
			// nest the new image and wrapper divs
			wrap3.appendChild(newElement);
			wrap2.appendChild(wrap3);
			wrap1.appendChild(wrap2);
			var shadowedElement = wrap1;
			
			// replace the old image with the new div-wrapped version
			element.parentNode.replaceChild(shadowedElement,element);
			}
		}
	}

function getAllChildren(e) {
  // Returns all children of element. Workaround required for IE5/Windows. Ugh.
  return e.all ? e.all : e.getElementsByTagName('*');
}

document.getElementsBySelector = function(selector) {
  // Attempt to fail gracefully in lesser browsers
  if (!document.getElementsByTagName) {
    return new Array();
  }
  // Split selector in to tokens
  var tokens = selector.split(' ');
  var currentContext = new Array(document);
  for (var i = 0; i < tokens.length; i++) {
    token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');;
    if (token.indexOf('#') > -1) {
      // Token is an ID selector
      var bits = token.split('#');
      var tagName = bits[0];
      var id = bits[1];
      var element = document.getElementById(id);
      if (tagName && element.nodeName.toLowerCase() != tagName) {
        // tag with that ID not found, return false
        return new Array();
      }
      // Set currentContext to contain just this element
      currentContext = new Array(element);
      continue; // Skip to next token
    }
    if (token.indexOf('.') > -1) {
      // Token contains a class selector
      var bits = token.split('.');
      var tagName = bits[0];
      var className = bits[1];
      if (!tagName) {
        tagName = '*';
      }
      // Get elements matching tag, filter them for class selector
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      continue; // Skip to next token
    }
    // Code to deal with attribute selectors
    if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) {
      var tagName = RegExp.$1;
      var attrName = RegExp.$2;
      var attrOperator = RegExp.$3;
      var attrValue = RegExp.$4;
      if (!tagName) {
        tagName = '*';
      }
      // Grab all of the tagName elements within current context
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      var checkFunction; // This function will be used to filter the elements
      switch (attrOperator) {
        case '=': // Equality
          checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };
          break;
        case '~': // Match one of space seperated words 
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
          break;
        case '|': // Match start with value followed by optional hyphen
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
          break;
        case '^': // Match starts with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };
          break;
        case '$': // Match ends with value - fails with "Warning" in Opera 7
          checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
          break;
        case '*': // Match ends with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
          break;
        default :
          // Just test for existence of attribute
          checkFunction = function(e) { return e.getAttribute(attrName); };
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (checkFunction(found[k])) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      // alert('Attribute Selector: '+tagName+' '+attrName+' '+attrOperator+' '+attrValue);
      continue; // Skip to next token
    }
    // If we get here, token is JUST an element (not a class or ID selector)
    tagName = token;
    var found = new Array;
    var foundCount = 0;
    for (var h = 0; h < currentContext.length; h++) {
      var elements = currentContext[h].getElementsByTagName(tagName);
      for (var j = 0; j < elements.length; j++) {
        found[foundCount++] = elements[j];
      }
    }
    currentContext = found;
  }
  return currentContext;
}
// ********** FIN DROP SHADOWS ON IMAGES **********