
var vgutils_isAjaxAvailable = false;

/*---------------------------------------------------------------------------* 
 * checks if string is empty																											 *
 *---------------------------------------------------------------------------*/
function fgutils_isEmptyString(str) {
	return str==null || str.length==0;
}

/*---------------------------------------------------------------------------* 
 * sends a request to the server via POST (core ajax functionality)																						 *
 *---------------------------------------------------------------------------*/
function fgutils_makePostRequest(url, parameters, readyStateChanged) {
	if (vgordr_clxAjaxHttpRequest) {
		vgordr_clxAjaxHttpRequest.onreadystatechange = readyStateChanged;
		vgordr_clxAjaxHttpRequest.open('POST', url, true);
		vgordr_clxAjaxHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		vgordr_clxAjaxHttpRequest.setRequestHeader("Content-length", parameters.length);
		vgordr_clxAjaxHttpRequest.setRequestHeader("Connection", "close");
		vgordr_clxAjaxHttpRequest.send(parameters);
		return true;
	}
	return false;
}
   
/*---------------------------------------------------------------------------* 
 * sends a request to the server via GET (core ajax functionality)																						 *
 *---------------------------------------------------------------------------*/
function fgutils_makeGetRequest(url, parameters, readyStateChanged) {
	if (vgordr_clxAjaxHttpRequest) {
		url += "?" + parameters + "&sid="+Math.random();
		vgordr_clxAjaxHttpRequest.onreadystatechange = readyStateChanged;
		vgordr_clxAjaxHttpRequest.open('GET', url, true);
		vgordr_clxAjaxHttpRequest.send(null);
		return true;
	}
	return false
}

/*---------------------------------------------------------------------------* 
 * constructs a string like 'param1=value1&param2=value2&param3=value3'	from the input fields of the form																									 *
 *---------------------------------------------------------------------------*/
function fgutils_getFormFieldsAsParams(form) {
	var str = "";
	var elements = form.elements;
	for (i=0; i<elements.length; i++) {
		if (elements[i].type == "checkbox") {
			if (elements[i].checked) {
				str += elements[i].name + "=" + elements[i].value + "&";
			} else {
				str += elements[i].name + "=&";
			}
		} else if (elements[i].type == "radio") {
			if (elements[i].checked) {
				str += elements[i].name + "=" + elements[i].value + "&";
			}
		} else {
			str += elements[i].name + "=" + elements[i].value + "&";
		}
	}
	/*
	elements = document.getElementsByTagName("select");
	for (i=0; i<elements.length; i++) {
		var sel = elements[i];
		str += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
	}
	*/
	str =  str.substring(0,str.length-1);
	return str;
}

/*---------------------------------------------------------------------------* 
 * extracts a params value from a string like 'param1=value1&param2=value2&param3=value3'																					 *
 *---------------------------------------------------------------------------*/
function fgutils_getValue(str,param)
{
	var value = "";
	var pos = str.indexOf(param+"=");
	if(pos!=-1) {
		var pos2 = str.indexOf(",",pos);
		if(pos2==-1) pos2 = str.length;
		value = str.substring(pos+param.length+1,pos2);
	}
	return value;
}


/* -----------------------------------------------------------------*
 * Returns value of any xml tag                                     * 
 * used in Price-object (fgtrans.js)																*
 * support "extend" xml tag like <sap:Header xmlsn...>..</sap:Header>*
 * support <TAG/> single xml tag																		*
 *------------------------------------------------------------------*/
function fgutils_getXMLValue (tag, x) {
	var y = null;
	var regexpTag = new RegExp ('<'+tag+'(>| [^>]+>)');	// <tag(>| [^>]+>)
	var start = x.search(regexpTag);
	if (start>-1)	start += RegExp.lastMatch.length;
	var end = x.indexOf('</'+tag+'>');
	if (start<end) {
		y = x.substring(start, end);
	} else {
		if (x.indexOf('<'+tag+'/>')!=-1)		// <tag/> ? -> return ''
			y = ''
		else
			y = null													// not found ? -> return null
	}
	return y;
}


/* -----------------------------------------------------------------*
 * pEvent:			key code value of the key pressed										* 
 * pErrorText:	static text alert message														*
 * Purpose is to restrict key data entry to numeric data only				*
 * if pErrorText not populated, means MO has choosen not to					*
 *	utilize this restriction																				*
 *------------------------------------------------------------------*/
function fgutils_onKeyPressEvent(pEvent,pErrorText) {
	if (pErrorText==null || pErrorText=="") return;

	var vl_okay = true;
	if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 13) {
		// Numeric data and line return is allowed
		event.returnValue = false;
		vl_okay = false;
	}
	if (!vl_okay) alert(pErrorText);
}


/*---------------------------------------------------------------------------* 
 * Ajax Specific Code 																											 *
 *---------------------------------------------------------------------------*/

// ---------------------------------------------------------
// --- ajax in action (p 196, 236)--------------------------
// ---------------------------------------------------------
	/*
	url-loading object and a request queue built on top of it
	*/

	/* namespacing object */
	var vgutils_net=new Object();

	vgutils_net.READY_STATE_UNINITIALIZED=0;
	vgutils_net.READY_STATE_LOADING=1;
	vgutils_net.READY_STATE_LOADED=2;
	vgutils_net.READY_STATE_INTERACTIVE=3;
	vgutils_net.READY_STATE_COMPLETE=4;


	/*--- content loader object for cross-browser requests ---*/
	vgutils_net.ContentLoader=function(url,onload,onerror,method,params,contentType){
		this.timeStart = new Date();
		this.req=null;
		this.onload=onload;
		this.onerror=(onerror) ? onerror : this.defaultError;
		this.loadXMLDoc(url,method,params,contentType);
	}

	vgutils_net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){
		if (!method){
			method="GET";
		}
		if (!contentType && method=="POST"){
			contentType='application/x-www-form-urlencoded';
		}
		// the XMLHttpRequest section might be replaced by fgutils_initHttpRequest
		if (window.XMLHttpRequest) {					// Mozilla, Safari,...
			this.req=new XMLHttpRequest();
			if (this.req.overrideMimeType) {
				// set type accordingly to anticipated content type
				//vgordr_clxAjaxHttpRequest.overrideMimeType('text/xml');
				this.req.overrideMimeType('text/html');
			}
		} else if (window.ActiveXObject){
			try {
				this.req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					this.req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		if (this.req){
			try{
				var loader=this;
				this.req.onreadystatechange=function(){
					vgutils_net.ContentLoader.onReadyState.call(loader);
				}
				
				this.req.open(method,url,true);
				if (contentType){
					this.req.setRequestHeader('Content-Type', contentType);
				}
				if (method=="POST") {
					this.req.setRequestHeader("Content-length", params.length);
					this.req.setRequestHeader("Connection", "close");				
				}
				this.req.setRequestHeader("Cookie", document.cookie);							
				this.req.send(params);
			}catch (err){
				this.onerror.call(this);
			}
		}
	}


	vgutils_net.ContentLoader.onReadyState=function(){
		var req=this.req;
		var ready=req.readyState;

		if (ready==vgutils_net.READY_STATE_COMPLETE){

			var httpStatus=req.status;
			if (httpStatus==200 || httpStatus==0){
				this.onload.call(this);
				var objElement = document.getElementById('timeInfo')
				if(objElement!=null) {
					var timeStop = new Date();
					var timeToDisPrices = (timeStop-this.timeStart)/1000;
					objElement.innerHTML = objElement.innerHTML+'<br><strong>Time to load prices: '+timeToDisPrices+'</strong>';
				}
			}else{
				this.onerror.call(this);
			}
		}
	}

	vgutils_net.ContentLoader.prototype.defaultError=function(){
		alert("error fetching data!"
			+"\n\nreadyState:"+this.req.readyState
			+"\nstatus: "+this.req.status
			+"\nheaders: "+this.req.getAllResponseHeaders());
	}

// ---------------------------------------------------------
// --- ajax in action - end       --------------------------
// ---------------------------------------------------------





// ---------------------------------------------------------
// --- ajax CLX                   --------------------------
// ---------------------------------------------------------


/*---------------------------------------------------------------------------* 
 * tries to create a http request, returns if true on success 							 *
 * (core ajax functionality) used as AJAX detection													 *
 *---------------------------------------------------------------------------*/
function fgutils_initHttpRequest() {
	// return false - to simulate "non ajax"
	//return false;
	vgordr_clxAjaxHttpRequest = null;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		vgordr_clxAjaxHttpRequest = new XMLHttpRequest();
		if (vgordr_clxAjaxHttpRequest.overrideMimeType) {
			// set type accordingly to anticipated content type
			vgordr_clxAjaxHttpRequest.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			vgordr_clxAjaxHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				vgordr_clxAjaxHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	return vgordr_clxAjaxHttpRequest!=null;
}


/*---------------------------------------------------------------------------* 
 * initializes ajax functionality																						 *
 * sets global var for later ajax available? detection during submit form		 *
 *---------------------------------------------------------------------------*/
function fgutils_initAjax () {
	vgutils_isAjaxAvailable = fgutils_initHttpRequest();
}

/*---------------------------------------------------------------------------* 
 * evaluates x/y coords of an element relative to the viewport																												 *
 *---------------------------------------------------------------------------*/
function fgutils_getCoords(src){
	var posx = 0;
	var posy = 0;
	while(src.offsetParent && src.tagName != "body") {
		if(!isNaN(src.offsetLeft) && !isNaN(src.offsetTop)) {
			posx += src.offsetLeft;
			posy += src.offsetTop;
		}
		src = src.offsetParent;
	}
	return new Array(posx,posy);
}

/*---------------------------------------------------------------------------* 
 * positions the message layer in the center of the viewport																											 *
 *---------------------------------------------------------------------------*/
function fgutils_setLayerWindowCenter(id) {
	var msg_layer = document.getElementById(id);
	msg_layer.style.left = "50%";
	msg_layer.style.top = "50%";
	msg_layer.style.marginLeft = -msg_layer.offsetWidth/2+"px";
	msg_layer.style.marginTop = -msg_layer.offsetHeight/2+"px";
	msg_layer.style.visibility = "visible";
}


/*---------------------------------------------------------------------------* 
 * positions the message layer near to the clicked 'Add	to cart' button			*
 *---------------------------------------------------------------------------*/
function fgutils_setMessageLayer(obj) {
	var xy = fgutils_getCoords(obj);
	var msg_layer = document.getElementById("base_messageAsync");
	msg_layer.style.left = xy[0]-msg_layer.offsetWidth+obj.offsetWidth-5+"px";
	msg_layer.style.top = xy[1]+obj.offsetHeight+5+"px";
	msg_layer.style.margin = "0";
}


/*---------------------------------------------------------------------------* 
 * positions the message layer in the center of the viewport																											 *
 *---------------------------------------------------------------------------*/
function fgutils_setMessageLayerWindowCenter() {
	fgutils_setLayerWindowCenter("base_messageAsync");
}


/*---------------------------------------------------------------------------* 
 * hides the message layer																											 *
 *---------------------------------------------------------------------------*/
function fgutils_hideMessageLayer() {
	if (vgordr_clxAjaxThreadId==null) { //message loading... should not be closeable!
		var msg_layer = document.getElementById("base_messageAsync");
		msg_layer.style.visibility = "hidden";
	}
}

// ----------------------------------------------------
// -- info message -----

/*---------------------------------------------------------------------------* 
 * displays info messages
 *---------------------------------------------------------------------------*/
function fgutils_showInfoMsg(obj, info) {
	var xy = fgutils_getCoords(obj);
	var info_layer = document.getElementById('base_infoLayer');
	if(info_layer.className=='') {
		info_layer.className = "visible";
		var info_layer_txt = document.getElementById('base_infoLayerTxt');
		info_layer_txt.innerHTML = info;
		if(xy[0]+18+info_layer.offsetWidth>window.innerWidth) xy[0] = xy[0]-info_layer.offsetWidth;
		info_layer.style.left = xy[0]+18+"px";
		info_layer.style.top = xy[1]+"px";
		info_layer.style.visibility = "visible";
	} else {
		info_layer.className = '';
		info_layer.style.visibility = "hidden";
	}
}

/*---------------------------------------------------------------------------* 
 * hides info messages
 *---------------------------------------------------------------------------*/
function fgutils_hideInfoMsg() {
	var info_layer = document.getElementById('base_infoLayer');
	info_layer.className = '';
	info_layer.style.visibility = "hidden";
}


// ---------------------------------------------------------
// --- ajax CLX - end             --------------------------
// ---------------------------------------------------------

//----------------------------------------------------------
// --- DHTML Functions -------------------------------------
//----------------------------------------------------------

// Global variables for DHTML api to check the compatibility
var vgutils_isCSS, vgutils_isW3C, vgutils_isIE4, vgutils_isNN4, vgutils_isIE6CSS;

// This function shows the hidden layer and the progress bar with image and
// clock.
function fgutils_showPageLockProgressBar() {
	
	//initialize the dynamic html
	fgutils_initDHTMLAPI();

	//get css elements
	var obj = fgutils_getRawObject('pageLockBackgroundDiv');
	var obj1 = fgutils_getRawObject('pageLockProgressBarDiv');

	document.body.appendChild(obj);
	document.body.appendChild(obj1);

	obj.style.width = '752px';	//Hilti Online page width fixed
	obj.style.height = fgutils_getScrollHeight() + 'px';

	//position obj1 at the beginning of the document body
	var sWidth = fgutils_getScrollWidth();
	var left = parseInt((sWidth - 752)/2,10);
	obj.style.left=left+'px';

	//center the progress bar	   
	fgutils_centerOnWindow(obj1);

	//turn on the background layer and progress bar   
	fgutils_showObj(obj);
	fgutils_showObj(obj1);

	//reposition the lay and the progress bar in case of scrolling
	//and resizing
	window.onresize = fgutils_resizeShield;
	window.onscroll = fgutils_resizeShield;

}

// Relocate the pageLockProgressBarDiv at the Center of the window 
function fgutils_resizeShield() {
 var obj = fgutils_getRawObject('pageLockBackgroundDiv');
 var obj1 = fgutils_getRawObject('pageLockProgressBarDiv');

 if (obj.style.visibility == "visible") {
		var sWidth = fgutils_getScrollWidth();
		var left = parseInt((sWidth - 752)/2,10);
		obj.style.left=left+'px';
		obj.style.width = '752px';
 	obj.style.height = fgutils_getScrollHeight() + 'px';   
 	fgutils_centerOnWindow(obj1);
	}
}

// Initialize upon load to let all browsers establish content objects
function fgutils_initDHTMLAPI() {
	if (document.images) {
		vgutils_isCSS = (document.body && document.body.style) ? true : false;
		vgutils_isW3C = (vgutils_isCSS && document.getElementById) ? true : false;
		vgutils_isIE4 = (vgutils_isCSS && document.all) ? true : false;
		vgutils_isNN4 = (document.layers) ? true : false;
		vgutils_isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
	}
}

// Seek nested NN4 layer from string name
function fgutils_seekLayer(doc, name) {
	var theObj;
	for (var i = 0; i < doc.layers.length; i++) {
		if (doc.layers[i].name == name) {
			theObj = doc.layers[i];
			break;
		}
		// dive into nested layers if necessary
		if (doc.layers[i].document.layers.length > 0) {
			theObj = fgutils_seekLayer(document.layers[i].document, name);
		}
	}
	return theObj;
}

// Convert object name string or object reference
// into a valid element object reference
function fgutils_getRawObject(obj) {
	var theObj;
	if (typeof obj == "string") {
		if (vgutils_isW3C) {
			theObj = document.getElementById(obj);
		} else if (vgutils_isIE4) {
			theObj = document.all(obj);
		} else if (vgutils_isNN4) {
			theObj = fgutils_seekLayer(document, obj);
		}
	} else {
		// pass through object reference
		theObj = obj;
	}
	return theObj;
}

// Convert object name string or object reference
// into a valid style (or NN4 layer) reference
function fgutils_getObject(obj) {
	var theObj = fgutils_getRawObject(obj);
	if (theObj && vgutils_isCSS) {
		theObj = theObj.style;
	}
	return theObj;
}

// Position an object at a specific pixel coordinate
function fgutils_shiftTo(obj, x, y) {
	var theObj = fgutils_getObject(obj);
	if (theObj) {
		if (vgutils_isCSS) {
			// equalize incorrect numeric value type
			var units = (typeof theObj.left == "string") ? "px" : 0 
			theObj.left = x + units;
			theObj.top = y + units;
		} else if (vgutils_isNN4) {
			theObj.moveTo(x,y)
		}
	}
}

// Move an object by x and/or y pixels
function fgutils_shiftBy(obj, deltaX, deltaY) {
	var theObj = fgutils_getObject(obj);
	if (theObj) {
		if (vgutils_isCSS) {
			// equalize incorrect numeric value type
			var units = (typeof theObj.left == "string") ? "px" : 0 
			theObj.left = fgutils_getObjectLeft(obj) + deltaX + units;
			theObj.top = fgutils_getObjectTop(obj) + deltaY + units;
		} else if (vgutils_isNN4) {
			theObj.moveBy(deltaX, deltaY);
		}
	}
}

// Set the z-order of an object
function fgutils_setZIndex(obj, zOrder) {
	var theObj = fgutils_getObject(obj);
	if (theObj) {
		theObj.zIndex = zOrder;
	}
}

// Set the background color of an object
function fgutils_setBGColor(obj, color) {
	var theObj = fgutils_getObject(obj);
	if (theObj) {
		if (vgutils_isNN4) {
			theObj.bgColor = color;
		} else if (vgutils_isCSS) {
			theObj.backgroundColor = color;
		}
	}
}

// Set the visibility of an object to visible
function fgutils_showObj(obj) {
	var theObj = fgutils_getObject(obj);
	if (theObj) {
		theObj.visibility = "visible";
	}
}

// Set the visibility of an object to hidden
function fgutils_hideObj(obj) {
	var theObj = fgutils_getObject(obj);
	if (theObj) {
		theObj.visibility = "hidden";
	}
}

// Retrieve the x coordinate of a positionable object
function fgutils_getObjectLeft(obj)  {
	var elem = fgutils_getRawObject(obj);
	var result = 0;
	if (document.defaultView) {
		var style = document.defaultView;
		var cssDecl = style.getComputedStyle(elem, "");
		result = cssDecl.getPropertyValue("left");
	} else if (elem.currentStyle) {
		result = elem.currentStyle.left;
	} else if (elem.style) {
		result = elem.style.left;
	} else if (vgutils_isNN4) {
		result = elem.left;
	}
	return parseInt(result);
}

// Retrieve the y coordinate of a positionable object
function fgutils_getObjectTop(obj)  {
	var elem = fgutils_getRawObject(obj);
	var result = 0;
	if (document.defaultView) {
		var style = document.defaultView;
		var cssDecl = style.getComputedStyle(elem, "");
		result = cssDecl.getPropertyValue("top");
	} else if (elem.currentStyle) {
		result = elem.currentStyle.top;
	} else if (elem.style) {
		result = elem.style.top;
	} else if (vgutils_isNN4) {
		result = elem.top;
	}
	return parseInt(result);
}

// Retrieve the rendered width of an element
function fgutils_getObjectWidth(obj)  {
	var elem = fgutils_getRawObject(obj);
	var result = 0;
	if (elem.offsetWidth) {
		result = elem.offsetWidth;
	} else if (elem.clip && elem.clip.width) {
		result = elem.clip.width;
	} else if (elem.style && elem.style.pixelWidth) {
		result = elem.style.pixelWidth;
	}
	return parseInt(result);
}

// Retrieve the rendered height of an element
function fgutils_getObjectHeight(obj)  {
	var elem = fgutils_getRawObject(obj);
	var result = 0;
	if (elem.offsetHeight) {
		result = elem.offsetHeight;
	} else if (elem.clip && elem.clip.height) {
		result = elem.clip.height;
	} else if (elem.style && elem.style.pixelHeight) {
		result = elem.style.pixelHeight;
	}
	return parseInt(result);
}

// Return the available content width space in browser window
function fgutils_getInsideWindowWidth() {
	if (window.innerWidth) {
		return window.innerWidth;
	} else if (vgutils_isIE6CSS) {
		// measure the html element's clientWidth
		return document.body.parentElement.clientWidth
	} else if (document.body && document.body.clientWidth) {
		return document.body.clientWidth;
	}
	return 0;
}

// Return the available content height space in browser window
function fgutils_getInsideWindowHeight() {
	if (window.innerHeight) {
		return window.innerHeight;
	} else if (vgutils_isIE6CSS) {
		// measure the html element's clientHeight
		return document.body.parentElement.clientHeight
	} else if (document.body && document.body.clientHeight) {
		return document.body.clientHeight;
	}
	return 0;
}

function fgutils_centerOnWindow(elemID) {
	// 'obj' is the positionable object
	var obj = fgutils_getRawObject(elemID);
	// window scroll factors
	var scrollX = 0, scrollY = 0;
	if (document.body && typeof document.body.scrollTop != "undefined") {
		scrollX += document.body.scrollLeft;
		scrollY += document.body.scrollTop;
		if (document.body.parentNode && typeof document.body.parentNode.scrollTop != "undefined") {
			scrollX += document.body.parentNode.scrollLeft;
			scrollY += document.body.parentNode.scrollTop
		}
	} else if (typeof window.pageXOffset != "undefined") {
		scrollX += window.pageXOffset;
		scrollY += window.pageYOffset;
	}
	var x = Math.round((fgutils_getInsideWindowWidth()/2) - (fgutils_getObjectWidth(obj)/2)) + scrollX;
	var y = Math.round((fgutils_getInsideWindowHeight()/2) - (fgutils_getObjectHeight(obj)/2)) + scrollY;

	if (x < 0) x = 0;
	if (y < 0) y = 0;
	fgutils_shiftTo(obj, x, y);
}

function fgutils_maximizeWindow(elemID) {
	// 'obj' is the positionable object
	var obj = fgutils_getRawObject(elemID);

	fgutils_shiftTo(obj, 0, 0);
   
	obj.style.width = fgutils_getInsideWindowWidth();
	obj.style.height = fgutils_getInsideWindowHeight();
}

function fgutils_getScrollLeft() {
	var w = window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft;           
	return w ? w : 0;
} 

function fgutils_getScrollTop() {
	var h = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;           
	return h ? h : 0;
}

function fgutils_getScrollWidth() {
	var w = document.body.scrollWidth || document.documentElement.scrollWidth;   
	return w ? w : 0;
}

function fgutils_getScrollHeight() {
	var h = document.body.scrollHeight || document.documentElement.scrollHeight;   
	return h ? h : 0;
}

//----------------------------------------------------------
// --- DHTML Functions end----------------------------------
//----------------------------------------------------------
