/*	++++++++++++++++++++++++
	COMMON VARS
	++++++++++++++++++++++*/

	var rootPath = "http://www.trulypvc.com/";
	//var rootPath = "http://www.trulypvc.com/proof/";
	//var rootPath = "http://192.168.0.1/trulypvc/";
	//var rootPath = "http://localhost/trulypvc/";
	var imgPath = rootPath + "images/";

	var hideInputs = false;
	var strHighlightCol = "#fdba30";



/*	++++++++++++++++++++++++
	LOAD EVENTS
	++++++++++++++++++++++*/
	
// http://simonwillison.net/2004/May/26/addLoadEvent/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() { if (oldonload) { oldonload(); } func(); } }
}


function initHideNonJSEles() {
	/* Elements with 'onlyJs' class - only to be visible where is JS (default display=none)
	Elements with 'nonJs' class - only to be visible where is no JS (default display=[assume block]) */
	
	arrNonJsEles = document.getElementsByClassName("nonJs");	
	for(i = 0; i < arrNonJsEles.length; i++) {
    	arrNonJsEles[i].style.display = "none"; }
		
	arrOnlyJsEles = document.getElementsByClassName("onlyJs");	
	for(i = 0; i < arrOnlyJsEles.length; i++) {
		if (arrOnlyJsEles[i].nodeName.toLowerCase() == "tr") {
			if (navigator.appName=="Microsoft Internet Explorer" && parseInt(navigator.appVersion)<7) { arrOnlyJsEles[i].style.display = "block"; } else { arrOnlyJsEles[i].style.display = "table-row"; }
		} else { arrOnlyJsEles[i].style.display = "block"; }
	}
		
		
}

/*	++++++++++++++++++++++++
	PAGE TOOLS & BOOKMARKING
	++++++++++++++++++++++*/

// class,href,title,onclick,text

var arrBookmark = new Array("bookmark","#","Bookmark this page","newBookmark();","Bookmark page");
var arrPrint = new Array("print","#","Print this page","window.print();","Print page");
var arrTools = new Array(arrPrint,arrBookmark);

function initPageTools() {
	if (document.getElementById("page-tools")) {
		var thisPTL = document.getElementById('page-tools');
		for(var i=0; i<arrTools.length; i++) {
			thisCl = arrTools[i][0]; thisHr = arrTools[i][1]; thisTi = arrTools[i][2]; thisOc = arrTools[i][3]; thisTxt = arrTools[i][4];
			var newLi = document.createElement('li');
			newLi.className = thisCl;
			newLi.innerHTML = '<a href=\'' + thisHr + '\' title=\'' + thisTi + '\' onclick=\'' + thisOc + '\'>' + thisTxt + '</a>';
			thisPTL.appendChild(newLi);
		}
	}
}

function newBookmark() { addBookmark(document.title,location.href); }

function addBookmark(title,url) {
	if (window.sidebar) {
		window.sidebar.addPanel(title, url,"");
	} else if( document.all ) {
		window.external.AddFavorite( url, title);
	} else if( window.opera && window.print ) {
		return true;
	}
}


function pageLink(linkID) {
	thisLoc = "#" + linkID;
	location = thisLoc; }


	
function initExternalLinks() {
	arrExternalLinks = document.getElementsByClassName("external");	
	for(i = 0; i < arrExternalLinks.length; i++) {
		arrExternalLinks[i].title += ' (opens in new window)';
		arrExternalLinks[i].onclick = openInNewWin; }
}

function openInNewWin() {
	var newWin = window.open(this.getAttribute('href'), 'sck-external');
	newWin.focus();
	return false;
}






/*	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	TOOLTIP THINGS
	++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */

function initToolTips() {

	//alert("ok");

	toolTipsArray = document.getElementsByTagName("span");

	for (var i=0; i < toolTipsArray.length; i++) {

		//if (toolTipsArray[i].className.match('hover')) {

		if (toolTipsArray[i].className.match(new RegExp('(\\s|^)hover(\\s|$)'))) {
		
			toolTipsArray[i].style.display = "none";
			
			var infoImgObj = document.createElement('img');
			infoImgObj.src = imgPath + "generic/icon_info.gif";
			infoImgObj.alt = "";
			infoImgObj.id = "hint_" + i;
			
			// set onclick event to show/hide hint
			
			infoImgObj.onmouseover = toggleHint;
			infoImgObj.onmouseout = toggleHint;
			infoImgObj.onfocus = toggleHint;
			infoImgObj.onblur = toggleHint;
			
			// insert node y as a child of node x just before node z.
			
			toolTipsArray[i].parentNode.insertBefore(infoImgObj,toolTipsArray[i]);
		
		}
		
		if (toolTipsArray[i].className.match('notehover')) {
			
			toolTipsArray[i].style.display = "none";
			
			//var infoHoverObj = getPreviousSibling(toolTipsArray[i]);
			
			var infoHoverObj = toolTipsArray[i].previousSibling;
			
			// set onclick event to show/hide hint
			
			infoHoverObj.onmouseover = toggleHint;
			infoHoverObj.onmouseout = toggleHint;
			infoHoverObj.onfocus = toggleHint;
			infoHoverObj.onblur = toggleHint;

		}
	}
}

function toggleHint() {
	//alert(this.id);
	hintObj = this.nextSibling;
	if(hintObj.style.display == "none") {
		hideHints();
		//alert("left=" + hintObj.offsetLeft + " top=" + hintObj.offsetTop);
		iPos = findPos(this);
		ix = iPos[0] +16;
		iy = iPos[1] +16;
		//alert("x=" + ix + " y=" + iy);
		hintObj.style.display = "block";
		hintObj.style.position = "absolute";
		//hintObj.style.marginLeft = "0.75em"; hintObj.style.marginTop = "0.5em";
		hintObj.style.top = iy +"px";
		hintObj.style.left = ix +"px";
		//hintObj.style.width = "20em"; hintObj.style.padding = "0.5em"; hintObj.style.border = "1px solid #B9CBE6"; hintObj.style.backgroundColor = "#E8EEF6";
		hintObj.style.zIndex = 99;
		//hintObj.onclick = hideHints; hintObj.title = "click to close";
		hintObj.className = "tooltip";
	} else {
		hintObj.style.display = "none";
	}
}

//hides all hints 

function hideHints() {
	if(document.getElementById) {
		//alert("OK");
		hintsArray = document.getElementsByTagName("span");
		for(var i=0; i<hintsArray.length; i++) {
			if(hintsArray[i].className.indexOf("hover") != -1) {
				hintsArray[i].style.display = "none";
			}
		}
	}
}

function findPos(obj) {
	//returns left,top of passed object
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft; curtop += obj.offsetTop;
		} while (obj = obj.offsetParent); return [curleft,curtop]; }
}



/*	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	REVEAL THINGS
	++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
	
var intReveals = 0;

function initReveal() {

	/* Used to hide panels that are only to be visible as default for nonJs browsers */
	
	var strShow = "Click to show options";
	var strHide = "Click to hide"
	var strImgSrc = imgPath + "generic/icon_show-hide.gif";
	
	arrRevealNodes = document.getElementsByClassName("reveal");

	for (var i=0; i<arrRevealNodes.length; i++) {
		
		intReveals = intReveals++;
		
		revealNode = arrRevealNodes[i];
		revealNode.style.display = "none";
		parentNode = revealNode.parentNode;
		//titleNode = revealNode.previous();

		// insert expand icon at end of title node

		var expandImageObj = document.createElement('img');
		expandImageObj.id = "show" + intReveals;
		expandImageObj.src = strImgSrc;
		expandImageObj.alt = "Show options";
		expandImageObj.setAttribute("class", 'expand'); //For Most Browsers
		expandImageObj.setAttribute("className", 'expand'); //For IE; harmless to other browsers.
		expandImageObj.title = strShow;
		expandImageObj.style.cursor = "pointer";
		expandImageObj.style.marginLeft = "0px";
		//titleNode.title = strShow;
		//titleNode.style.cursor = "pointer";
		//titleNode.style.display = "inline";

		parentNode.insertBefore(expandImageObj, revealNode);
		
		// add event handler things

		srcImg = expandImageObj.src;

		expandImageObj.onmouseover = function() {
			newImg = srcImg.replace(/.gif/,"_on.gif");
			doLosengeHover(this,newImg); };

		expandImageObj.onmouseout = function() {
			newImg = srcImg.replace(/_on.gif/,".gif");
			doLosengeHover(this,newImg); };

		expandImageObj.onclick = function() { doReveal(revealNode.id,this,strShow,strHide,newImg) };
		//titleNode.onclick = function() { doReveal(revealNode.id,this,strShow,strHide,newImg) };
		
	}
}

function doLosengeHover(thisNode,newImg) { thisNode.src = newImg; }


function doReveal(nodeId,thisNode,showTxt,hideTxt,strNewImg) {
	
	// if node type img then reveal is next, else reveal is next next
	
	if (strNewImg == "") {
		strHideImg = imgPath + "generic/icon_show-hide_on.gif";
		strShowImg = imgPath + "generic/icon_show-hide.gif";
	} else { strHideImg = newImg; strShowImg = newImg; }
	
	if (thisNode.tagName.toLowerCase() == "img") {
		thisRevNode = thisNode.nextSibling;
		thisTitleNode = thisNode.previousSibling;
		thisImgNode = thisNode;
	} else {
		thisRevNode = thisNode.nextSibling.nextSibling;
		thisTitleNode = thisNode;
		thisImgNode = thisNode.next();
	}
	
	var thisRevNodeId = thisRevNode.id;
	if (thisRevNode.style.display == "none") {
		Effect.BlindDown(thisRevNodeId, { duration: 1.0 });
		thisImgNode.title = hideTxt;
		thisTitleNode.title = hideTxt;
		thisImgNode.src = strHideImg;
	} else {
		thisRevNode.hide();
		thisImgNode.title = showTxt;
		thisTitleNode.title = showTxt;
		thisImgNode.src = strShowImg;
	}
}















/*	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	QUOTE REQUEST
	++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
	
	
function initThumbGroup() {
	
	var arrInputBlocks = $$('span.thumb-group span span');
	
	for(var i=0; i<arrInputBlocks.length; i++){
		
		inputBlock = arrInputBlocks[i]; // span holding the radio button
		
		if (inputBlock.className.match(new RegExp('(\\s|^)hover(\\s|$)'))) {
			// exclude any child spans (hover) that may have been selected
		} else {
			
			if (hideInputs) { inputBlock.style.display = "none"; }
			
			var groupContainEle = inputBlock.parentNode.parentNode; // span containing all items (thumb-group)
			
			optionNa = inputBlock.innerHTML;
			optionNa = returnStringFromSubString(optionNa,">",1); // strips input tag leaving name and anything following
			optionNa = returnStringFromSubString(optionNa,"<",0); // strips any following hover img/span elements
			
			imgEle = inputBlock.previous();
			imgEle.title = "Select" + optionNa;
			imgEle.style.cursor = "pointer";
			imgEle.onclick = function() { selectRadios(this,groupContainEle) } ;
			
			inputEle = inputBlock.firstDescendant();
			inputEle.onclick = function() {	// de-highlight images and highlight our image
				clearImages(this.parentNode.parentNode.parentNode);
				highlightImage(this);
			} ;
			
			if (inputEle.checked == true) { imgEle.style.backgroundColor = strHighlightCol; }
			
		}
	}
}

function highlightImage(passedInput) {
	inputBlock = passedInput.parentNode;
	imgEle = inputBlock.previousSibling;
	imgEle.style.backgroundColor = strHighlightCol; }





function selectRadios(passedImg,passedGrpCont) {
	
	// called by clicking a thumbnail in group (see hideThumbGroupNames)
	
	var nameBlock = passedImg.next();
	var inputElement = nameBlock.firstDescendant();
	
	passedGrpCont = passedImg.parentNode.parentNode; // using this rather than passed as caused probs
	
	var inputNa = inputElement.name;
	var inputVal = inputElement.value;
	
	// clear any existing radio selections and image highlights
	clearButtons(passedGrpCont);
	clearImages(passedGrpCont);
	
	// select radio and highlight image
	arrRadios = $(passedGrpCont).getElementsBySelector('input');
	for (var r=0; r<arrRadios.length; r++) {
		if (arrRadios[r].value == inputVal) {
			arrRadios[r].checked = true; } }
	
	passedImg.style.backgroundColor = strHighlightCol;

}
	
function clearButtons(buttonGrp) {
	//alert("DEBUG: buttonGrp=" + buttonGrp);
	arrRadios = $(buttonGrp).getElementsBySelector('input');
	for (c=0; c < arrRadios.length; c++) {
		thisRadio = arrRadios[c];
		//alert("DEBUG: thisRadio(value)=" + thisRadio.value);
		if (thisRadio.checked == true) { thisRadio.checked = false; }
	}
} 


function clearImages(imgGrp) {
	arrImgs = $(imgGrp).getElementsBySelector('img');
	for (img=0; img < arrImgs.length; img++) {
		arrImgs[img].style.backgroundColor = "#fff";
	}
}









/*	++++++++++++++++++++++++
	COMMON FUNCTIONS
	++++++++++++++++++++++*/

	
function toggleDisplay(pEle) {
	if (pEle.style.display == "none") { pEle.style.display = "block"; } else { pEle.style.display = "none"; } }

function getFirstChild(pNode) {
	thisNode = pNode.firstChild; while (thisNode.nodeType!=1) { thisNode=thisNode.nextSibling; } return thisNode; }
function getNextSibling(pNode) {
	thisNode = pNode.nextSibling; while (thisNode.nodeType!=1) { thisNode=thisNode.nextSibling; } return thisNode; }
function getParent(pNode) {
	thisNode = pNode.parentNode; while (thisNode.nodeType!=1) { thisNode=thisNode.parentNode; } return thisNode; }

function removeElementByID(pID) {
	//var thisEl = document.getElementById(pID); thisEl.parentNode.removeChild(thisEl);
	if (document.getElementById(pID)) { var thisEl = document.getElementById(pID); thisEl.parentNode.removeChild(thisEl); }
	

}
	
//http://javascript.internet.com/forms/currency-format.html
function formatCurrency(num,sym) {
	// second param toggles currency symbol (0=hide, 1=show)
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	if (sym==1) {
		return (((sign)?'':'-') + '&pound;' + num + '.' + cents);
	} else {
		return (((sign)?'':'-') + num + '.' + cents);
	}
}
	
function roundUp(num) {
	if (isNaN(num) == true) {
		return Math.round(num*100)/100;
	} else { return num; }
}

function indexInArray(theArray,subIndex,theValue) { // note: sub index used if matching against child index 
	var arrLen = theArray.length;
	for (var i=0; i<arrLen; i++) {
		if (subIndex == -1) {
			if (theArray[i] == theValue){ return i; } else { return -1; }
		} else {
			if (theArray[i][subIndex] == theValue){ return i; } else { return -1; }
		}
	}
}
	
	
	
function returnStringFromSubString(strSource,strInterest,intPartInterest) {
	var intStart = strSource.indexOf(strInterest);
	intLength = strSource.length;
	if (intPartInterest == 1) { // want bit after
		strNew = strSource.substring(intStart+strInterest.length,intLength);
	} else { // want bit before
		strNew = strSource.substring(0,intStart);
	}
	return strNew;
}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
function GetParentNodeWithTagName(objNode,strTagName) {
	strTagName = strTagName.toLowerCase();
	for (objNode = objNode.parentNode; 
		(objNode && ((objNode.tagName && (objNode.tagName.toLowerCase() != strTagName)) || (!objNode.tagName && (objNode.nodeType != 3))));
		objNode = objNode.parentNode){
	}
	return(objNode);
}


// This is our testing method.
function FindParent( objNode, strTagName ){
	var objParent = GetParentNodeWithTagName( objNode, strTagName );

	// Check to see if we found the parent.
	if (objParent != null){
		alert( "Found: " + objParent.tagName + " with ID '" + objParent.getAttribute( "id" ) + "'" );
	} else {
		alert( "No parent with the tagname " + strTagName + " was found." );
	}

}
	
	
function getElementsByName_iefix(topObj,tag,name) {
	if (topObj == null) {
		var elem = document.getElementsByTagName(tag);
	} else {
		var elem = topObj.getElementsByTagName(tag); }
	var arr = new Array();
	for(i = 0,iarr = 0; i < elem.length; i++) {
		att = elem[i].getAttribute("name");
		if (att == name) {
			arr[iarr] = elem[i];
			iarr++;
		}
	}
	return arr;
}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

//addLoadEvent(initHideNonJSEles);
//addLoadEvent(initPageTools);
//addLoadEvent(initExternalLinks);

addLoadEvent(initToolTips);
addLoadEvent(initReveal);
addLoadEvent(initThumbGroup);



