var SYSTEM_LOADING_OPENED = false;

var ifrlayer = {
        make:function(obj) {
            if(!obj) return; obj = (typeof(obj)=="string" ) ? document.getElementById(obj) : obj; if(!obj) return;
            if(document.all && window.print && !window.opera && document.getElementById && !obj.iframelayer ) {
                if(obj.parentNode && !obj.iframelayer) var ifr = obj.parentNode.insertBefore(document.createElement("iframe" ), obj);
                if(obj.currentStyle.zIndex != "" && parseInt(obj.currentStyle.zIndex)>1 ) {
                    ifr.style.zIndex = parseInt(obj.currentStyle.zIndex)-1;
                }
                ifr.src = "javascript:false";
                with(ifr.style) {
                    filter = "mask()";
                    position = "absolute";
                }
                obj.iframelayer = ifr;
            }
            if (obj.iframelayer) {
                with(obj.iframelayer.style) {
                    width = obj.offsetWidth+"px";
                    height = obj.offsetHeight+"px";
                    visibility = "visible";
                }
                ifrlayer.move(obj)
            }

        },
        hide:function(obj) {
            if(!obj) return; obj = typeof(obj)=="string" ? document.getElementById(obj) : obj; if (!obj) return;
            if(obj.iframelayer) {
                obj.iframelayer.style.visibility="hidden";
            }
        },
        move:function(obj) {
            if(obj && obj.iframelayer) {
                with(obj.iframelayer.style) {
                    top = obj.offsetTop+"px";
                    left = obj.offsetLeft+"px"
                }
            }
        }
    } 

//for flash resize
function setFlashWidth(divid, newW){
	document.getElementById(divid).style.width = newW+"px";
}
function setFlashHeight(divid, newH){
	document.getElementById(divid).style.height = newH+"px";		
}
function setFlashSize(divid, newW, newH){
	setFlashWidth(divid, newW);
	setFlashHeight(divid, newH);
}
function canResizeFlash(){
	var ua = navigator.userAgent.toLowerCase();
	var opera = ua.indexOf("opera");
	if( document.getElementById ){
		if(opera == -1) return true;
		else if(parseInt(ua.substr(opera+6, 1)) >= 7) return true;
	}
	return false;
}


// ROLLOVER: enter image id and image src
function rollover(img_id, img_src){
	try {
		$(img_id).src = "/tg-ecommerce/images/" + img_src + ".gif";
	} catch(e) {}
}

// insert mask
function insertIframe(id, width, height, opacity, beforePlace){
	if ($("maskIframe") == null) {
		var iFrame = beforePlace.parentNode.insertBefore(document.createElement("iframe" ), beforePlace);
		iFrame.id=id;
		iFrame.name=id; 
		iFrame.style.width = width + "px";
		iFrame.style.height = height + "px";
		iFrame.style.opacity = opacity;
		iFrame.style.display = "none";
	    with(iFrame.style) {
	    	background = "#ffffff";
	        position = "absolute";
	        top = "0px";
	        left = "0px";
	        margin = "0 0"; 
	        padding = "0 0 0 0";
	        filter = "alpha(opacity=50)";
	        border = "0px solid #666";
	        visibility = "hidden";
	    	zIndex = 1000;
	 	}
 	}
}

// parse XML functions
function xmlGetChildNodes(xml) {
	if (typeof(xml) == "string") {return new Array(xml);}
	var retArr = new Array();
	for (var i = 0; i<xml.childNodes.length; i++){
		var xNode = xml.childNodes[i];
		if (xNode.nodeType == 3 && xNode.nodeValue.trim() != "") {
			retArr.push(xNode.nodeValue.trim());
		}
		if (xNode.nodeType == 1) {
			retArr.push(xNode);
		}
	}
	return retArr;
}

function xmlGetFirstChild(xml){
	return xmlGetChildNodes(xml)[0];
}

function xmlGetChildLevel(xml, level){
	var i = 0;
	var xNode = xml;
	while (i < level) {
		xNode = xmlGetFirstChild(xNode);
		i++;
	}
	return xNode;
}

function getNodeValue(xml, nodeName){
	try {
		return xml.getElementsByTagName(nodeName)[0].firstChild.nodeValue || "";
	} catch(e){
		return "";
	}
}

// get query value from a string URL
function query(str, varName){
	var q = str.substr(str.indexOf("?")+1);
	if (q == "" || q.indexOf(varName+"=") == -1) {
		return "";
	}
	var p = q.split("&");
	for (var i = 0; i<p.length; i++) {
		var v = p[i].split("=");
		if (v[0] == varName) {
			return v[1];
		}
	}
	return "";
}

// get option selected value from a form
function getOptionValue(formId, optionName){
	var form = $(formId);
	if (!form.elements[optionName]) return "";
	for (var i = 0; i<form.elements[optionName].length; i++) {
		if (form.elements[optionName][i].checked == true) {
			return form.elements[optionName][i].value || i;
		}
	}
	return "";
}


function getOptionValue(formId, optionName){
	var form = $(formId);
	if (!form.elements[optionName]) return "";
	for (var i = 0; i<form.elements[optionName].length; i++) {
		if (form.elements[optionName][i].checked == true) {
			return form.elements[optionName][i].value || i;
		}
	}
	return "";
}

/*BEGIN TG SPECIFICS */
function getCompleterSoapEnvelope(value){

	var envelope = "";
	envelope += '<?xml version="1.0" encoding="utf-8"?>' + "\n";
	envelope += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + "\n";
	envelope += ' <soap:Body>' + "\n";
		envelope += " <getKeywords>";
		envelope += "<keyword>"+value+"</keyword>";
		envelope += "</getKeywords>\n";
	envelope += ' </soap:Body>' + "\n";
	envelope += '</soap:Envelope>';
	return envelope;
}
function getNavigatorSoapEnvelope(productCategoryId){

	var envelope = "";
	envelope += '<?xml version="1.0" encoding="utf-8"?>' + "\n";
	envelope += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + "\n";
	envelope += ' <soap:Body>' + "\n";
		envelope += "<getNavigations>";
		envelope+="<userLoginId>"+NAV_USERNAME+"</userLoginId>"
		if(productCategoryId!=null && productCategoryId!='undefined')envelope+="<productCategoryId>"+productCategoryId+"</productCategoryId>";
		envelope+="<date>"+NAV_DATE+"</date>"
		if(NAV_PUSH!=null && NAV_PUSH!='undefined')envelope+="<category>"+NAV_PUSH+"</category>";
		envelope += "</getNavigations>\n";
	envelope += ' </soap:Body>' + "\n";
	envelope += '</soap:Envelope>';
	return envelope;
}

function getAddToCartSoapEnvelope(productId,quantity){

	var envelope = "";
	envelope += '<?xml version="1.0" encoding="utf-8"?>' + "\n";
	envelope += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + "\n";
	envelope += ' <soap:Body>' + "\n";
		envelope += "<tgAddCartItem>\n";
			envelope +="<add_product_id>"+productId+"</add_product_id>\n";
			envelope +="<quantity>"+quantity+"</quantity>\n";
		envelope += "</tgAddCartItem>\n";
	envelope += ' </soap:Body>' + "\n";
	envelope += '</soap:Envelope>';
	return envelope;
}

function opencata(productCategoryId){
	var path="/tg-ecommerce/paf/"+productCategoryId+"/";
	window.open(path+"ecata.html","catalogue","width=1000, height=730, top=0, left=0,scrollbars=0,status=0,resizable=0");
}

function verifySimpleCharOnly(e){
	var keynum;
	var keychar;
	var numcheck;

	if(window.event){ // IE
	  keynum = e.keyCode;
	}
	else if(e.which){ // Netscape/Firefox/Opera
	  keynum = e.which;
	}
	if((keynum>47 && keynum<58) || (keynum==32) || (keynum==8) || (keynum==46) || (keynum>63 && keynum<91)|| (keynum>96 && keynum<123))
		return true;
    else 
    	return false;

	
}	    
/*END TG SPECIFICS */

// add/ subtract input value.
function subtract(obj) {
	try {
		var input = $(obj).getNext();
		var newVal = parseInt(input.value.trim())-1;
		input.value = newVal > 0 ? newVal : 0;
	} catch(e) {}
}

function add(obj) {
	try {
		var input = $(obj).getPrevious();
		var newVal = parseInt(input.value.trim())+1;
		input.value = newVal > 0 ? newVal : 0;
	} catch(e) {}
}

/* 
 * Lance un tracking analytics sur une action javascript prédéfini,
 * sans aucune autre action
*/
function saveGoogleTag(trackerId,analyticsTag) {
    try {
        var pageTracker = _gat._getTracker(trackerId);
        pageTracker._trackPageview(analyticsTag);
    } catch(err) {}    
}

/* 
 * Lance un tracking analytics sur une action javascript prédéfini,
 * en redirigeant l'utilisateur sur l'url passée en paramètre
*/
function saveGoogleTagWithRedirect(trackerId,analyticsTag,newUrl) {
    saveGoogleTag(trackerId,analyticsTag);
    document.location.url=newUrl;
}

// add JS files
new Asset.javascript("/tg-ecommerce/images/javascript/fixPNG.js");
new Asset.javascript("/tg-ecommerce/images/javascript/loading.js");
new Asset.javascript("/tg-ecommerce/images/javascript/error.js");
new Asset.javascript("/tg-ecommerce/images/javascript/navigator.js");
new Asset.javascript("/tg-ecommerce/images/javascript/autocompleter.js");
new Asset.javascript("/tg-ecommerce/images/javascript/scroller.js");
new Asset.javascript("/tg-ecommerce/images/javascript/contactTab.js");
new Asset.javascript("/tg-ecommerce/images/javascript/product.js");
new Asset.javascript("/tg-ecommerce/images/javascript/layer.js");
new Asset.javascript("/tg-ecommerce/images/javascript/tooltip.js");
//AJAX
new Asset.javascript("/tg-ecommerce/images/javascript/searchProduct.js");
new Asset.javascript("/tg-ecommerce/images/javascript/order.js");
