regularExpressionBR = new RegExp("<br/>", "gi");
/* swap image */
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/* end swap */

function gotoSearchPage()
{
	var productName = document.getElementById('productName').value ;
	if (productName.indexOf("\"") >= 0 || productName.indexOf("'") >= 0) {
		alert("' and \" are invalid characters.");
		return;
	}
	document.location.href="/searchproduct/" + productName  + "/";
}

function commonBuildPage()
{
    buildBaseFramework();
	addStandardContentFramework();
    commonResizeHandler();
    window.onresize = commonResizeHandler;

    // Stop weird graphic glitch
    // For Internet Explorer:
	document.onselectstart = function() { return false; }
	// For everyone else
	document.onmousedown = function() { return false; }
}

function commonBuildPageWithoutStandardContent()
{
    buildBaseFramework();
    commonResizeHandler();
    window.onresize = commonResizeHandler;
}

function buildBaseFramework()
{
    var tempBody = document.getElementsByTagName("body")[0];
	var tempDiv = document.createElement("div");
    var tempTable = document.createElement("table");
    var tempTbody = document.createElement("tbody");
    var tr, td, tempDiv, tempChildDiv, tempImg, tempSpan, tempA, tempHr;
    
	tempDiv.setAttribute("id", "mainTableDiv");
    tempTable.setAttribute("id", "mainTable");
    tempTbody.setAttribute("id", "mainTbody");
    tempTable.appendChild(tempTbody);
    tempBody.appendChild(tempDiv);
    tempDiv.appendChild(tempTable);
    
    tr = tempTbody.insertRow(tempTbody.rows.length);
    td = tr.insertCell(tr.cells.length);
    td.setAttribute("id", "mainTableTopLeftCell");
    td = tr.insertCell(tr.cells.length);
    td.setAttribute("id", "mainTableTopCenterCell");
    td = tr.insertCell(tr.cells.length);
    td.setAttribute("id", "mainTableTopRightCell");

    tr = tempTbody.insertRow(tempTbody.rows.length);
    td = tr.insertCell(tr.cells.length);
    td.setAttribute("id", "mainTableMiddleLeftCell");
    td = tr.insertCell(tr.cells.length);
    td.setAttribute("id", "mainTableMiddleCenterCell");
    td = tr.insertCell(tr.cells.length);
    td.setAttribute("id", "mainTableMiddleRightCell");

    tr = tempTbody.insertRow(tempTbody.rows.length);
    td = tr.insertCell(tr.cells.length);
    td.setAttribute("id", "mainTableBottomLeftCell");
    td = tr.insertCell(tr.cells.length);
    td.setAttribute("id", "mainTableBottomCenterCell");
    td = tr.insertCell(tr.cells.length);
    td.setAttribute("id", "mainTableBottomRightCell");

}

function addStandardContentFramework()
{
	var tempDiv;
	var tempChildDiv;
	var tempImg;
	var tempHr;
	var tempSpan;
	var td;
	
	td = document.getElementById("mainTableMiddleCenterCell");
	
	// Sanity Check
	if (td == null)
	{
		return;
	}
	
    tempDiv = document.createElement("div");
    tempDiv.setAttribute("id", "topMainBar");
    
    tempDiv.innerHTML += "<div id='topBarTextDiv'><table width='100%'><tr><td ><img src='images/allianceminerals3.png'></td><td align='right' valign='middle'><input type='text' size = '20' id='productName' tabindex='0' onkeypress='if(catchEnter(event)){javascript:gotoSearchPage();}'/></td><td align='left' valign='middle' style='padding-bottom:3px;padding-left:3px;padding-top:5px;'><a href='javascript:gotoSearchPage();'><img onMouseOut='MM_swapImgRestore()' onMouseOver='MM_swapImage(\"search\",\"\",\"images/searchhover.gif\",1)' src='images/search.gif'  name='search' width='81' height='20' border='0'></a></td></tr></table></div>" ;     
    tempDiv.innerHTML += "<div style='clear: both;'> </div>" ;
    tempDiv.innerHTML += "<div id='topBarDiv' ><hr id='topBarHr'/></div>" ;
    /*
        tempChildDiv = document.createElement("div");
        tempChildDiv.setAttribute("id", "topBarTextDiv");
        tempChildDiv.innerHTML += '<img src="images/allianceminerals3.png" alt="Alliance Minerals North America" />';
            tempHr = document.createElement("hr");
            tempHr.setAttribute("id", "topBarHr");
        tempChildDiv.appendChild(tempHr);
    tempDiv.appendChild(tempChildDiv);*/
    
    td.appendChild(tempDiv);
    
    tempDiv = document.createElement("div");
    tempDiv.setAttribute("id", "contentMainDiv");
	tempDiv.innerHTML += '<div id="homeMainLink" class="mainLinks" onmouseover="this.style.color = \'white\';" onmouseout="this.style.color = \'#BBBBBB\';"><a href="home" onmouseover="this.style.color = \'white\';" onmouseout="this.style.color = \'#BBBBBB\';"><b>Home</b></a></div>';
	tempDiv.innerHTML += '<div id="ourProductsMainLink" class="mainLinks" onmouseover="this.style.color = \'white\';" onmouseout="this.style.color = \'#BBBBBB\';" onclick="displayOurProductsPullDown(this, event);"><b>Our Products</b></div>';
	tempDiv.innerHTML += '<div id="factoryMainLink" class="mainLinks" onmouseover="this.style.color = \'white\';" onmouseout="this.style.color = \'#BBBBBB\';"><a href="javascript:openfactoryPage();"  onmouseover="this.style.color = \'white\';" onmouseout="this.style.color = \'#BBBBBB\';"><b>FACTORIES & QUARRIES</b></a></div>';
	tempDiv.innerHTML += '<div id="contactUsMainLink" class="mainLinks" onmouseover="this.style.color = \'white\';" onmouseout="this.style.color = \'#BBBBBB\';" onclick="displayContactUsPullDown(this, event);"><b>Contact Us</b></div>';	
    tempChildDiv = document.createElement("div");
    tempChildDiv.setAttribute("id", "contentActualContentDiv");
    tempDiv.appendChild(tempChildDiv);
    td.appendChild(tempDiv);
    
    tempDiv = document.createElement("div");
    tempDiv.setAttribute("id", "bottomMainBar");
        tempChildDiv = document.createElement("div");
        tempChildDiv.setAttribute("id", "bottomBarTextDiv");
            tempSpan = document.createElement("span");
            tempSpan.setAttribute("class", "mainLinks");
            tempSpan.style.paddingLeft = "0.5em";
            tempSpan.style.paddingRight = "0.5em";
            tempSpan.innerHTML = "&copy; Copyright 2008-2009 Alliance Minerals North America";
        tempChildDiv.appendChild(tempSpan);
    tempDiv.appendChild(tempChildDiv);
    td.appendChild(tempDiv);
    
}

function commonResizeHandler()
{
    var calculatedHeight;
	// var calculatedWidth;
	var remainingHeight;
	// var remainingWidth;
    
	if (window.innerHeight != null)
	{
		calculatedHeight = document.body.parentNode.clientHeight;
	}
	else
	{
		calculatedHeight = document.body.clientHeight;
	}
    
    remainingHeight = ((calculatedHeight - 450 - 46) < 0)?(0):(calculatedHeight - 450 - 46);
    //document.getElementById("mainTableDiv").style.marginTop = Math.floor(remainingHeight / 3) + "px";
    document.getElementById("mainTableDiv").style.marginTop = "10px";

}

function displayOurProductsPullDown(parentDiv, evt)
{
	var numberOfLines;
	var pulldownHeight;
	
	ourProductsMenuChild = false;
	ourProductsMenuParent = true;

	// Let's calculate the proper length of the pulldown box - the one in CSS is just a guess
	numberOfLines = 0;
	for (var categoryIterator = 1; categoryIterator < productMap.length; categoryIterator++)
	{
		numberOfLines += productMap[categoryIterator].length;
	}
	
	// Height + linespace + padding
	pulldownHeight = (14 + 10) * numberOfLines;
	
	
	createDropDownBoxFadeIn(0, 0, 130, pulldownHeight, 0.1, 1.0, "productMenuPullDownDiv", parentDiv.getAttribute("id"), 50, 75, "displayOurProductsPullDownCleanup()", "", "", "relative");
	
	addEvent(parentDiv, "mouseover", commonMouseOverOurProductsParent, true);
	addEvent(parentDiv, "mouseout", commonMouseOutOurProductsParent, true);
	addEvent(document.getElementById("productMenuPullDownDiv"), "mouseover", commonMouseOverOurProductsChild, true);
	addEvent(document.getElementById("productMenuPullDownDiv"), "mouseout", commonMouseOutOurProductsChild, true);

}

function displayOurProductsPullDownCleanup()
{
	var productIterator;
	var categoryIterator;
	var tempString;
	var parentDiv = document.getElementById("productMenuPullDownDiv");
	
	parentDiv.innerHTML = "";
	
	for  (categoryIterator = 1; categoryIterator < productMap.length; categoryIterator++)
	{
		tempString = "";
		tempString += "<div class='commonPullDownMenuLinkDiv' onmouseover='this.style.backgroundColor = \"#565656\";' onmouseout='this.style.backgroundColor = \"\";' >";
		tempString += productMap[categoryIterator][0][0];
		tempString += "</div>";
		parentDiv.innerHTML += tempString;
		
		for (productIterator = 1; productIterator < productMap[categoryIterator].length; productIterator++)
		{
			// Because IE does not support :hover on things other than a elements
			// And because IE does not support programatic JS event addition with variables
			tempString = "";
			tempString += "<div class='commonPullDownMenuSubdivisionLinkDiv commonPullDownMenuLinkDiv' onmouseover='this.style.backgroundColor = \"#565656\";' onmouseout='this.style.backgroundColor = \"\";' onclick='window.location = \"/product/" + productMap[categoryIterator][0][0].toLowerCase() + "/" + productMap[categoryIterator][productIterator][0][0].toLowerCase() + "/\";'>";
			tempString += "<a href='/product/" + productMap[categoryIterator][0][0].toLowerCase() + "/" + productMap[categoryIterator][productIterator][0][0].toLowerCase() + "/' >";
			tempString += productMap[categoryIterator][productIterator][0][0];
			tempString += "</a></div>";
			parentDiv.innerHTML += tempString;
		}
	}
}

function commonMouseOverOurProductsParent()
{
	ourProductsMenuParent = true;
}

function commonMouseOutOurProductsParent()
{
	ourProductsMenuParent = false;
	setTimeout('removeOurProductsMenu();', 100);
}

function commonMouseOverOurProductsChild()
{
	ourProductsMenuChild = true;
}

function commonMouseOutOurProductsChild()
{
	ourProductsMenuChild = false;
	setTimeout('removeOurProductsMenu();', 100);
}

function removeOurProductsMenu()
{
	if ( (ourProductsMenuChild == false) && (ourProductsMenuParent == false) )
	{
		document.getElementById("ourProductsMainLink").innerHTML = "<b>Our Products</b>";
	}
}

function displayContactUsPullDown(parentDiv, evt)
{
	var numberOfLines;
	var pulldownHeight;
	
	contactUsMenuChild = false;
	contactUsMenuParent = true;

	// Let's calculate the proper length of the pulldown box - the one in CSS is just a guess
	numberOfLines = 3;
	
	// Height + linespace + padding
	pulldownHeight = (14 + 10) * numberOfLines;
	
	
	createDropDownBoxFadeIn(0, 0, 160, pulldownHeight, 0.1, 1.0, "contactUsMenuPullDownDiv", parentDiv.getAttribute("id"), 50, 75, "displayContactUsPullDownCleanup()", "", "", "relative");
	
	addEvent(parentDiv, "mouseover", commonMouseOverContactUsParent, true);
	addEvent(parentDiv, "mouseout", commonMouseOutContactUsParent, true);
	addEvent(document.getElementById("contactUsMenuPullDownDiv"), "mouseover", commonMouseOverContactUsChild, true);
	addEvent(document.getElementById("contactUsMenuPullDownDiv"), "mouseout", commonMouseOutContactUsChild, true);

}

function displayContactUsPullDownCleanup()
{
	var productIterator;
	var categoryIterator;
	var tempString;
	var parentDiv = document.getElementById("contactUsMenuPullDownDiv");
	
	parentDiv.innerHTML = "";	

	tempString = "" ;
	tempString += "<div class='commonPullDownMenuLinkDiv' onmouseover='this.style.backgroundColor = \"#565656\";' onmouseout='this.style.backgroundColor = \"\";' onclick='window.location = \"/contact/NorthAmerica\";'>";
	tempString += "<a href='/contact/NorthAmerica'  >";
	tempString += "North America Area";
	tempString += "</a></div>";
	parentDiv.innerHTML += tempString;

	tempString = "" ;
	tempString += "<div class='commonPullDownMenuLinkDiv' onmouseover='this.style.backgroundColor = \"#565656\";' onmouseout='this.style.backgroundColor = \"\";' onclick='window.location = \"/contact/Asia\";'>";
	tempString += "<a href='/contact/Asia'  >";
	tempString += "Asia Area";
	tempString += "</a></div>";
	parentDiv.innerHTML += tempString;
	
	tempString = "" ;
	tempString += "<div class='commonPullDownMenuLinkDiv' onmouseover='this.style.backgroundColor = \"#565656\";' onmouseout='this.style.backgroundColor = \"\";' onclick='window.location = \"/dealer\";'>";
	tempString += "<a href='/dealer'  >";
	tempString += "Closest Resources";
	tempString += "</a></div>";
	parentDiv.innerHTML += tempString;
	

}

function commonMouseOverContactUsParent()
{
	contactUsMenuParent = true;
}

function commonMouseOutContactUsParent()
{
	contactUsMenuParent = false;
	setTimeout('removeContactUsMenu();', 100);
}

function commonMouseOverContactUsChild()
{
	contactUsMenuChild = true;
}

function commonMouseOutContactUsChild()
{
	contactUsMenuChild = false;
	setTimeout('removeContactUsMenu();', 100);
}

function removeContactUsMenu()
{
	if ( (contactUsMenuChild == false) && (contactUsMenuParent == false) )
	{
		document.getElementById("contactUsMainLink").innerHTML = "<b>Contact Us</b>";
	}
}

function openfactoryPage() {

	var winW = screen.availWidth -100;
	var winH = 760;
	var winX = (screen.availWidth - winW) / 2;    
	var winY = (screen.availHeight - winH) / 2;
	var url= window.location.protocol +"//" + window.location.host + "/factory" ;

	var features = 'left=' + winX + ',top=' + winY +  
	               ',height=' + winH + ',' + 'width=' + 
	               winW + ',status=no,' + 'toolbar=no,menubar=no,location=no';
	window.open(url, null, features);
	}

function openProjectsPage() {

	var winW = 820;
	var winH = 600;
	var winX = (screen.availWidth - winW) / 2;    
	var winY = (screen.availHeight - winH) / 3;
	var url= window.location.protocol +"//" + window.location.host + "/projects" ;

	var features = 'left=' + winX + ',top=' + winY +  
	               ',height=' + winH + ',' + 'width=' + 
	               winW + ',status=no,' + 'toolbar=no,menubar=no,location=no';
	window.open(url, null, features);
}

function openDiamondProjectsPage() {

	var winW = 700;
	var winH = 600;
	var winX = (screen.availWidth - winW) / 2;    
	var winY = (screen.availHeight - winH) / 2;
	var url= window.location.protocol +"//" + window.location.host + "/projects/diamond/1" ;
	var features = 'left=' + winX + ',top=' + winY +  
	               ',height=' + winH + ',' + 'width=' + 
	               winW + ',status=no,' + 'toolbar=no,menubar=no,location=no';
	window.open(url, null, features);
}