function TextBlink(objID, val) {
	return;
	if (val == true) { 
		eval ("document.getElementById('" + objID + "').style.color='FFFFFF';");
		setTimeout("TextBlink('" + objID + "', " + (0 + !val) + ");",400);
	} else {
		eval ("document.getElementById('" + objID + "').style.color='000000';");
		setTimeout("TextBlink('" + objID + "', " + (0 + !val) + ");",1000);
	}
	
}
function isEmail (email){
	return checkMail(email);
}

function RunFoo(swf, hauteur, largeur, couleur, nom) {
	document.write("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\""+hauteur+"\" height=\""+largeur+"\" id=\""+nom+"\" align=\"middle\">\n");
	document.write("<param name=\"allowScriptAccess\" value=\"sameDomain\" />\n");
	document.write("<param name=\"movie\" value=\""+swf+"\" /><param name=\"quality\" value=\"high\" /><param name=\"bgcolor\" value=\""+couleur+"\" /><embed src=\""+swf+"\" quality=\"high\" bgcolor=\""+couleur+"\" width=\""+hauteur+"\" height=\""+largeur+"\" name=\""+nom+"\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />\n");
	document.write("</object>\n");
}

function checkMail(email)
{
	var x = email;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
}
function isDepartement(valeur)
{
	var ret = false;
	var x = valeur;
	var filter  = /^([0-9]{2,3})+$/;
	if (filter.test(x)) {
		if (x!=0 && (x<96 || (x>970 && x<975))) ret = true;
	}
	
	return ret;
}

function isAlphaNum(inVal) {
	var x = inVal;
	var filter  = /^([a-zA-Z0-9])+$/;
	if (filter.test(x)) return true;
	else return false;
}
function isNumeric(inVal) {
	var x = inVal;
	var filter  = /^([0-9])+$/;
	if (filter.test(x)) return true;
	else return false;
}
function isMoney(inVal) {
	var x = inVal;
	var filter  = /^([0-9])+[\.,]?([0-9])*$/;
	if (filter.test(x)) return true;
	else return false;
}
// JavaScript Document
function Trim(TRIM_VALUE){
	return TRIM_VALUE.replace(/^\s+/,'').replace(/\s+$/,'');
} //End Function

function RTrim(VALUE){
	return VALUE.replace(/\s+$/,'');
} //End Function

function LTrim(VALUE){
	return VALUE.replace(/^\s+/,'');
} //End Function

function ShowContent(id_div) {
	if (id_div != '') action_div(id_div);
}
function ShowContent2(id_div, action) {
	if (id_div != '') action2_div(id_div, action);
}

function getDiv(id_div) {
	var item = null;
	if (document.all){
		item =  document.all[id_div];
	} else if (document.getElementById) {
		item =  document.getElementById(id_div);
	} else if (document.layers){
		item = document.layers[id_div];
	}
	
	return item;
}
function getElement(id_el) {
	var item = null;
	if (document.all){
		item =  document.all[id_el];
	} else if (document.getElementById) {
		item =  document.getElementById(id_el);
	}
	
	return item;
}

function action_div(id_div) {
	var item = null;
	if (document.getElementById) {
		item = document.getElementById(id_div);
	} else if (document.all){
		item = document.all[id_div];
	} else if (document.layers){
		item = document.layers[id_div];
	}
	
	if (!item) { // rien à faire
	} else if (item.style) {
		if (item.style.display == "none") {
			item.style.display = "";
		} else {
			item.style.display = "none";
		}
	} else {
		item.visibility = "show";
	}
}
function action2_div(id_div, action) {
	var item = null;
	if (document.getElementById) {
		item = document.getElementById(id_div);
	} else if (document.all){
		item = document.all[id_div];
	} else if (document.layers){
		item = document.layers[id_div];
	}
	
	if (!item) { // rien à faire
	} else if (action=='hide') {
		item.style.display = "none";
		item.visibility = "";
	} else if (action=='show') {
		item.style.display = "";
		item.visibility = "show";
	}
}


var http_request = false;
function InitAjax() {
	var success = false;
	if (!http_request) {
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
			
			}
			success =  true;			
		} else if (window.ActiveXObject) { // IE
			var MSXML_XMLHTTP_PROGIDS = new Array(
                'MSXML2.XMLHTTP.5.0',
                'MSXML2.XMLHTTP.4.0',
                'MSXML2.XMLHTTP.3.0',
                'MSXML2.XMLHTTP',
                'Microsoft.XMLHTTP'
            );
           
            for (var i=0;i < MSXML_XMLHTTP_PROGIDS.length && !success; i++) {
                try {
                    http_request = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]);
                    success = true;
                } catch (e) {}
            }
		}		
	}
	return success;
}

function ShowInfo(str, time) {
	ShowMessage("<p class=infoOK>" + str + "</p>",time);
}
function ShowAlert(str, time) {
	ShowMessage("<p class=infoBAD>" + str + "</p>",time);
}
function ShowMessage(str, time) {
	var infoDiv = getDiv('infoDiv');
	if (infoDiv) infoDiv.innerHTML = str;
	if (time>0) {
		setTimeout("getDiv('infoDiv').innerHTML='';",time);	
	}
}

function AjaxRequest(url, xml) {
		var infoDiv = getDiv('infoDiv');
		if (infoDiv) infoDiv.innerHTML = "Chargement en cours...";
	
        if (!InitAjax()) {
		   return false;
        }
		var ret = false;
        //http_request.onreadystatechange = alertContents;
        http_request.open('GET', url, false);
        http_request.send(null);
		if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                if (xml) ret = http_request.responseXML;
				else ret = http_request.responseText ;
            } else {
				ret = false;
			}
        }
		http_request = false;
		if (infoDiv) infoDiv.innerHTML = "";

		return ret;
}

function AjaxRequestAsync(url, eventProc) {
        if (!InitAjax()) {
		   return false;
        }
		var ret = false;
        http_request.onreadystatechange = eventProc;
        http_request.open('GET', url, true);
        http_request.send(null);
		
		//http_request = false;

		return ret;
}


function AjaxPostRequest(url, arrParams, xml) {
		var infoDiv = getDiv('infoDiv');
		if (infoDiv) infoDiv.innerHTML = "Chargement en cours...";
	
        if (!InitAjax()) {
		   return false;
        }
		var ret = false;
		var poststr = "";
		for (var i in arrParams) {
			if (poststr != "") poststr += "&";
			poststr = poststr + i + "=" + encodeURI(arrParams[i]);
		}
		
        http_request.open('POST', url, false);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      	http_request.setRequestHeader("Content-length", poststr.length);
      	http_request.setRequestHeader("Connection", "close");
      	http_request.send(poststr);

		if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                if (xml) ret = http_request.responseXML;
				else ret = http_request.responseText ;
            } else {
				ret = false;
			}
        }
		http_request = false;
		if (infoDiv) infoDiv.innerHTML = "";

		return ret;
}

function GlobalCheckForm3(theform, $arrForm) {
	var retValue = true;
	
	for (var i=0;i<$arrForm.length;i++) {
		
		
		var obj  = $arrForm[i];
		eval("var item = document." + theform.name + "." + obj.field);	
		// En dehors du switch
		var testCondition = true;
		if (obj.checkIfRadioConditionName) {
			eval("var radioObj = document." + theform.name + "." + obj.checkIfRadioConditionName);
			var isSelected = false;
			for (r=0;r<radioObj.length;r++) {
				if (radioObj[r].value==obj.checkIfRadioConditionValue && radioObj[r].checked) {
					isSelected = true;
					break;
				}
			}
			if (!isSelected ) testCondition = false;
			
		}
		
		if (testCondition) {
			
			switch(obj.type) {
				case 'TEXT' :
					if (obj.isOptionnal!=true || (obj.isOptionnal==true && Trim(item.value)!='')) {
						var a = Trim(item.value);
						if (a=='') {
							alert(obj.alertString);
							item.focus();
							TextBlink("document." + theform.name + "." + obj.field,0);
							retValue = false;
						}
						if (obj.minLength && obj.minLength>0 && a.length <= obj.minLength) {
							alert(obj.alertStringForLength);
							item.focus();
							TextBlink("document." + theform.name + "." + obj.field,0);
							retValue = false;
						}
		
						if (obj.isNumeric == true) {
							if (!isNumeric(a)) {
								alert(obj.alertStringForIsNumeric);
								item.focus();
								TextBlink("document." + theform.name + "." + obj.field,0);
							retValue = false;
							}
							if (obj.minVal && parseInt(a)<obj.minVal) {
								alert(obj.alertStringForNumericRange);
								item.focus();
								TextBlink("document." + theform.name + "." + obj.field,0);
							retValue = false;
							}
							if (obj.maxVal && parseInt(a)>obj.maxVal) {
								alert(obj.alertStringForNumericRange);
								item.focus();
								TextBlink("document." + theform.name + "." + obj.field,0);
							retValue = false;
							}
							
						}
						if (obj.isPrice == true) {
							a=a.replace(/ /, '');
							if (!isMoney(a)) {
								alert(obj.alertStringForPrice);
								item.focus();
								TextBlink("document." + theform.name + "." + obj.field,0);
								retValue = false;
							}
						}
					}
					break;
				case 'TEXTURL' :
					var a = Trim(item.value);
					var re =/([\w-]+\.)+[\w-]+([\w- .?%&=]*)/;
					if (!(re.test(a)) || a.length<=7) {
						alert(obj.alertString);
						item.focus();
						TextBlink("document." + theform.name + "." + obj.field,0);
						retValue = false;
					}
					break;
				case 'EMAIL' :
					eval("var item1 = document." + theform.name + "." + obj.field);
					eval("var item2 = document." + theform.name + "." + obj.fieldMatch);
					if (!checkMail(item1.value)) {
						alert(obj.alertString);
						item.focus();
						TextBlink("document." + theform.name + "." + obj.field,0);
						retValue = false;
					}
					if (!obj.dontMatch && item1.value != item2.value) {
						alert(obj.alertStringMatch);
						item2.focus();
						TextBlink("document." + theform.name + "." + obj.fieldMatch,0);
						retValue = false;
					}
					break;				
				case 'CHECKBOX' :
				
					var radioOK = false;
					if (item.length) {
						for (r=0;r<item.length;r++)  if (item[r].checked) radioOK = true;
						if (!radioOK) {
							alert(obj.alertString);
							retValue = false;
						}
					} else if (!item.checked) {
						alert(obj.alertString);
							retValue = false;
					}
					break;
				case 'RADIO' :
					var radioOK = false;
					for (r=0;r<item.length;r++) {
						if (item[r].checked) {
							radioOK = true;
							//alert('Checked');
						}
					}
					if (!radioOK) {
						alert(obj.alertString);
						//item.focus();
						retValue = false;
					}
					break;
				case 'SELECT' :
					if (Trim(item.value)=='') {
						alert(obj.alertString);
						item.focus();
						TextBlink("document." + theform.name + "." + obj.field,0);
							retValue = false;
					}
					if (typeof(obj.fieldValues)=='object') {
						var ok=false;
						for (var k=0;k<obj.fieldValues.length;k++) {
							if (obj.fieldValues[k]==item.value) {
								ok = true;
								break;
							}
						}
						if (!ok) {
							alert(obj.alertStringValues);
							item.focus();
							TextBlink("document." + theform.name + "." + obj.field,0);
							retValue = false;
						}
					}
					break;							
			}
		}
		
		if (!retValue) return false;
	}
	return true;
}

function GlobalCheckForm2(theform, $arrForm) {
	
	for (var i=0;i<$arrForm.length;i++) {
		var obj  = $arrForm[i];
		eval("var item = document." + theform.name + "." + obj.field);
		switch(obj.type) {
			case 'TEXT' :
				if (obj.isOptionnal!=true || (obj.isOptionnal==true && Trim(item.value)!='')) {
					var a = Trim(item.value);
					if (a=='') {
						alert(obj.alertString);
						item.focus();
						TextBlink("document." + theform.name + "." + obj.field,0);
						return false;
					}
					if (obj.minLength && obj.minLength>0 && a.length <= obj.minLength) {
						alert(obj.alertStringForLength);
						item.focus();
						TextBlink("document." + theform.name + "." + obj.field,0);
						return false;							
					}
	
					if (obj.isNumeric == true) {
						a = a.replace(/ +/, '');
						if (!isNumeric(a)) {
							alert(obj.alertStringForIsNumeric);
							item.focus();
							TextBlink("document." + theform.name + "." + obj.field,0);
							return false;							
						}

						if (obj.minVal && a<obj.minVal) {
							alert(obj.alertStringForNumericRange);
							item.focus();
							TextBlink("document." + theform.name + "." + obj.field,0);
							return false;						
						}
						if (obj.maxVal && a>obj.maxVal) {
							alert(obj.alertStringForNumericRange);
							item.focus();
							TextBlink("document." + theform.name + "." + obj.field,0);
							return false;						
						}
						
					}
					if (obj.isPrice == true) {
						a=a.replace(/ /, '');
						if (!isMoney(a)) {
							alert(obj.alertStringForPrice);
							item.focus();
							TextBlink("document." + theform.name + "." + obj.field,0);
							return false;	
						}
					}
				}
				break;
			case 'TEXTURL' :
				var a = Trim(item.value);
				var re =/([\w-]+\.)+[\w-]+([\w- .?%&=]*)/;
				if (!(re.test(a)) || a.length<=7) {
					alert(obj.alertString);
					item.focus();
					TextBlink("document." + theform.name + "." + obj.field,0);
					return false;
				}
				break;
			case 'EMAIL' :
				eval("var item1 = document." + theform.name + "." + obj.field);
				eval("var item2 = document." + theform.name + "." + obj.fieldMatch);
				if (!checkMail(item1.value)) {
					alert(obj.alertString);
					item.focus();
					TextBlink("document." + theform.name + "." + obj.field,0);
					return false;
				}
				if (!obj.dontMatch && item1.value != item2.value) {
					alert(obj.alertStringMatch);
					item2.focus();
					TextBlink("document." + theform.name + "." + obj.fieldMatch,0);
					return false;
				}
				break;				
			case 'CHECKBOX' :
			
				var radioOK = false;
				if (item.length) {
					for (r=0;r<item.length;r++)  if (item[r].checked) radioOK = true;
					if (!radioOK) {
						alert(obj.alertString);
						return false;					
					}
				} else if (!item.checked) {
					alert(obj.alertString);
					return false;					
				}
				break;
			case 'RADIO' :
				var radioOK = false;
				for (r=0;r<item.length;r++) {
					if (item[r].checked) {
						radioOK = true;
						//alert('Checked');
					}
				}
				if (!radioOK) {
					alert(obj.alertString);
					//item.focus();
					return false;					
				}
				break;
			case 'SELECT' :
				if (Trim(item.value)=='') {
					alert(obj.alertString);
					item.focus();
					TextBlink("document." + theform.name + "." + obj.field,0);
					return false;
				}
				if (typeof(obj.fieldValues)=='object') {
					var ok=false;
					for (var k=0;k<obj.fieldValues.length;k++) {
						if (obj.fieldValues[k]==item.value) {
							ok = true;
							break;
						}
					}
					if (!ok) {
						alert(obj.alertStringValues);
						item.focus();
						TextBlink("document." + theform.name + "." + obj.field,0);
						return false;
					}
				}
				break;							
		}
	}
	return true;
}

// INSERT TEXT AT THE CURSOR POS IN A TEXT AREA
// TextAreaInsertText(control textarea, textavantselection, textapresselection) {
function TextAreaInsertText(input, aTag, eTag) {
  //var input = document.forms['formular'].elements['eingabe'];
  input.focus();
  /* für Internet Explorer */
  if(typeof document.selection != 'undefined') {
    /* Einfügen des Formatierungscodes */
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = aTag + insText + eTag;
    /* Anpassen der Cursorposition */
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -eTag.length);
    } else {
      range.moveStart('character', aTag.length + insText.length + eTag.length);      
    }
    range.select();
  }
  /* für neuere auf Gecko basierende Browser */
  else if(typeof input.selectionStart != 'undefined')
  {
    /* Einfügen des Formatierungscodes */
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = input.value.substring(start, end);
    input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
    /* Anpassen der Cursorposition */
    var pos;
    if (insText.length == 0) {
      pos = start + aTag.length;
    } else {
      pos = start + aTag.length + insText.length + eTag.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
  /* für die übrigen Browser */
  else
  {
    /* Abfrage der Einfügeposition */
    var pos;
    var re = new RegExp('^[0-9]{0,3}$');
    while(!re.test(pos)) {
      pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
    }
    if(pos > input.value.length) {
      pos = input.value.length;
    }
    /* Einfügen des Formatierungscodes */
    var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
    input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
  }
}

function getWindowWidth()
{
 if (window.innerWidth) return window.innerWidth;
 else if (document.body && document.body.offsetWidth) return document.body.offsetWidth;
 else return 0;
}

function getWindowHeight()
{
 if (window.innerHeight) return window.innerHeight  ;
 else if (document.body && document.body.offsetHeight) return document.body.offsetHeight;
 else return 0;
}

// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. 
// If you wish to share this code with others, please just point them
// to the URL instead.
// Please DO NOT link directly to my .js files from your site. Copy
// the files to your server and use them there. Thank you.
// ===================================================================

function hasOptions(obj){if(obj!=null && obj.options!=null){return true;}return false;}
function selectUnselectMatchingOptions(obj,regex,which,only){if(window.RegExp){if(which == "select"){var selected1=true;var selected2=false;}else if(which == "unselect"){var selected1=false;var selected2=true;}else{return;}var re = new RegExp(regex);if(!hasOptions(obj)){return;}for(var i=0;i<obj.options.length;i++){if(re.test(obj.options[i].text)){obj.options[i].selected = selected1;}else{if(only == true){obj.options[i].selected = selected2;}}}}}
function selectMatchingOptions(obj,regex){selectUnselectMatchingOptions(obj,regex,"select",false);}
function selectOnlyMatchingOptions(obj,regex){selectUnselectMatchingOptions(obj,regex,"select",true);}
function unSelectMatchingOptions(obj,regex){selectUnselectMatchingOptions(obj,regex,"unselect",false);}
function sortSelect(obj){var o = new Array();if(!hasOptions(obj)){return;}for(var i=0;i<obj.options.length;i++){o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;}if(o.length==0){return;}o = o.sort(
function(a,b){if((a.text+"") <(b.text+"")){return -1;}if((a.text+"") >(b.text+"")){return 1;}return 0;});for(var i=0;i<o.length;i++){obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);}}
function selectAllOptions(obj){if(!hasOptions(obj)){return;}for(var i=0;i<obj.options.length;i++){obj.options[i].selected = true;}}
function moveSelectedOptions(from,to){if(arguments.length>3){var regex = arguments[3];if(regex != ""){unSelectMatchingOptions(from,regex);}}if(!hasOptions(from)){return;}for(var i=0;i<from.options.length;i++){var o = from.options[i];if(o.selected){if(!hasOptions(to)){var index = 0;}else{var index=to.options.length;}to.options[index] = new Option( o.text, o.value, false, false);}}for(var i=(from.options.length-1);i>=0;i--){var o = from.options[i];if(o.selected){from.options[i] = null;}}if((arguments.length<3) ||(arguments[2]==true)){sortSelect(from);sortSelect(to);}from.selectedIndex = -1;to.selectedIndex = -1;}
function copySelectedOptions(from,to){var options = new Object();if(hasOptions(to)){for(var i=0;i<to.options.length;i++){options[to.options[i].value] = to.options[i].text;}}if(!hasOptions(from)){return;}for(var i=0;i<from.options.length;i++){var o = from.options[i];if(o.selected){if(options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text){if(!hasOptions(to)){var index = 0;}else{var index=to.options.length;}to.options[index] = new Option( o.text, o.value, false, false);}}}if((arguments.length<3) ||(arguments[2]==true)){sortSelect(to);}from.selectedIndex = -1;to.selectedIndex = -1;}
function moveAllOptions(from,to){selectAllOptions(from);if(arguments.length==2){moveSelectedOptions(from,to);}else if(arguments.length==3){moveSelectedOptions(from,to,arguments[2]);}else if(arguments.length==4){moveSelectedOptions(from,to,arguments[2],arguments[3]);}}
function copyAllOptions(from,to){selectAllOptions(from);if(arguments.length==2){copySelectedOptions(from,to);}else if(arguments.length==3){copySelectedOptions(from,to,arguments[2]);}}
function swapOptions(obj,i,j){var o = obj.options;var i_selected = o[i].selected;var j_selected = o[j].selected;var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);o[i] = temp2;o[j] = temp;o[i].selected = j_selected;o[j].selected = i_selected;}
function moveOptionUp(obj){if(!hasOptions(obj)){return;}for(i=0;i<obj.options.length;i++){if(obj.options[i].selected){if(i != 0 && !obj.options[i-1].selected){swapOptions(obj,i,i-1);obj.options[i-1].selected = true;}}}}
function moveOptionDown(obj){if(!hasOptions(obj)){return;}for(i=obj.options.length-1;i>=0;i--){if(obj.options[i].selected){if(i !=(obj.options.length-1) && ! obj.options[i+1].selected){swapOptions(obj,i,i+1);obj.options[i+1].selected = true;}}}}
function removeSelectedOptions(from){if(!hasOptions(from)){return;}if(from.type=="select-one"){from.options[from.selectedIndex] = null;}else{for(var i=(from.options.length-1);i>=0;i--){var o=from.options[i];if(o.selected){from.options[i] = null;}}}from.selectedIndex = -1;}
function removeAllOptions(from){if(!hasOptions(from)){return;}for(var i=(from.options.length-1);i>=0;i--){from.options[i] = null;}from.selectedIndex = -1;}
function addOption(obj,text,value,selected){if(obj!=null && obj.options!=null){obj.options[obj.options.length] = new Option(text, value, false, selected);}}

function GetRadioValueForControl(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";

}
