﻿/////////////////////////////
//      Antoine ROUX       //
//  Copyright 2007 - 2009  //
/////////////////////////////

/*Dialogue image*/
function openImageDialog(grpName) {
	ret = window.showModalDialog("?pid=95", null, "dialogWidth:1000px; dialogHeight:600px; center:yes; resizable:yes");
	if (ret != null) {
		grpChamp = document.getElementsByName(grpName);
		for (i = 0; i < grpChamp.length; i++) {
			var Champ = grpChamp.item(i);
			Champ.value = ret;
			if (ret != "") {
				Champ.src = ret;
			} else {
				Champ.src = "Images/NotFound.png";
			}
		}
	}
}

/*Outils*/
function FindPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
        return [curleft, curtop];
    }
}
function FindSize(obj) {
    return [obj.offsetWidth, obj.offsetHeight];
}

function SetCookie(name, value, expires, path, domain, secure) {
    szCookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
        document.cookie = szCookie;
}

function GetCookie(name) {
    if (document.cookie) {
        index = document.cookie.indexOf(name);
        if (index != -1) {
            nDeb = (document.cookie.indexOf( "=", index) + 1);
            nFin = document.cookie.indexOf( ";", index);
            if (nFin == -1) {nFin = document.cookie.length;}
            return unescape(document.cookie.substring(nDeb, nFin));
        } 
    }
    return null;
}

function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    } else {
        return false;
    }
}

/*Modification de prix*/
function EditPrice(e, obj, id, paramname, target) {
    var key;
    if (window.event) key = event.keyCode;
    else if (e) key = e.which;

    if (obj.defaultValue != obj.value) {
        obj.style.backgroundColor = "#FFCCAA";
    } else {
        obj.style.backgroundColor = "";
    }
    
    // Enter
    if (key == 13) {
        ValidPrice(obj, id, paramname, target);
    }
}

function ValidPrice(obj, id, paramname, target) {    
    var xhr_object = null;
    if (window.XMLHttpRequest) // Firefox
        xhr_object = new XMLHttpRequest();
    else if (window.ActiveXObject) // Internet Explorer
        xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    else { // XMLHttpRequest non supporté par le navigateur
        alert("Votre navigateur ne supporte pas XMLHttpRequest, veuillez utiliser un navigateur compatible");
        return;
    }

    xhr_object.open("POST", target, true);
    xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    xhr_object.onreadystatechange = function() {
        if (xhr_object.readyState == 4) {
            var rep = xhr_object.responseText;
			var prices = rep.split("|");
			for (var i=0; i<prices.length; i++) {
				var price = prices[i].split("=");
				if (price[0] == paramname) {
					obj.value = price[1];
				}
			}
            obj.defaultValue = obj.value;
            obj.style.backgroundColor = "";
        }
    }

    xhr_object.send("ID=" + id + "&" + paramname + "=" + obj.value);
}
