var showPrices = true;


///////////////////////////////
// Ajax Functions
///////////////////////////////

function GetHTTP()
{
    if(document.all) return new ActiveXObject("Microsoft.XMLHTTP");
    else return new XMLHttpRequest();
}

function RunCGI(url)
{
    var request = GetHTTP();
    request.open('GET', url, false);
    request.send(null);
    return request.responseText;
}






///////////////////////////////
// Cart Functions
///////////////////////////////

function Order(SKU,Quantity,Price,Description,DesignString)
{
    if ( CheckForCookie() == false ) return false;
    
    var itemcount = GetCookie("ITEM_COUNT")*1;
    var itemstring = Description + "+PANEL_PARAM+" + DesignString + "+QUANTITY+" + Quantity + "+PRICE+" + Price + "+SKU+" + SKU;
    
    CreateCookie("ESTATE_MILLWORK_ITEM" + itemcount,itemstring);
    CreateCookie("ITEM_COUNT", ++itemcount);
    
    this.location="https://secure.estatemillwork.com/filtrexx.cgi?order_cart.frm";
    
    return (false);
}






///////////////////////////////
// Cookie Functions
///////////////////////////////

function GetCookie(name)
{
    var nameEQ = name + "=";
    var ca;
    if(document.cookie){
	ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
	    var c = ca[i];
	    while (c.charAt(0)==' ') c = c.substring(1,c.length);
	    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
    }
    return null;
}

function CreateCookie(Name,Value)
{
    var today=new Date();  
    var expiry=new Date(today.getTime() +  86400000);
    document.cookie=Name+"=" + Value + "; path=/; domain=.estatemillwork.com; expires="+expiry.toGMTString();
    
}

function DeleteCookie(Name,Value)
{
    CreateCookie(Name, '');
}


function CheckForCookie()
{
    var tmpcookie = new Date();
    chkcookie = (tmpcookie.getTime() + '');
    document.cookie = "chkcookie=" + chkcookie + "; path=/; domain=.estatemillwork.com;";
    if (document.cookie.indexOf(chkcookie,0) < 0)
    {
	alert("In order for our site to function properly, you must have cookies enabled in your browser, or call our staff for assistance at 1-866-322-0040");
	return false;
    }
    else
    {
	return true;	
    }
}

function setReferrerCookie()
{
    if (document.referrer && document.referrer != '' && (document.referrer.indexOf('estatemillwork.com') < 0 || document.referrer.indexOf('estatemillwork.com') > 15))
    {
	//Extract the keyword
	var keyword = 'none';
	var searchStart = 0; var searchEnd = 0;
	if(document.referrer.indexOf('q=') > 0) searchStart = document.referrer.indexOf('q=') + 2;  //google, bing
	else if(document.referrer.indexOf('p=') > 0) searchStart = document.referrer.indexOf('p=') + 2;  //yahoo
	else if(document.referrer.indexOf('query=') > 0) searchStart = document.referrer.indexOf('query=') + 6;  //aol
	
	if(searchStart > 0) {
	    if(document.referrer.substring(searchStart).indexOf("&") > 0) 
		searchEnd = searchStart + document.referrer.substring(searchStart).indexOf("&");
	    var tempSearch;
	    if(searchEnd > 0) tempSearch = document.referrer.substring(searchStart,searchEnd);
	    else tempSearch = document.referrer.substring(searchStart);
	    keyword = tempSearch.replace(/\+/g," ");
	}
	
	//Grab the domain
	var domain = 'unknown';
	var tmpDomain = document.referrer.replace('http://','');
	var firstSlash = tmpDomain.indexOf('/');
	if(firstSlash > 0) domain = tmpDomain.substring(0,firstSlash);
	else domain = tmpDomain;
	
	//Assume organic unless URL contains PPC flags
	var traffic_type = 'organic';

	if(location.href.indexOf('gclid=') > 0)
	{
	    traffic_type = 'ppc';  //google
	    domain = "www.google.com";
	}

	if(domain.indexOf('yahoo') >= 0 && location.href.indexOf('OVRAW=') > 0) traffic_type = 'ppc';  //yahoo
	
	//Get a date in SQL format
	var now = new Date();
	var timestamp = now.getFullYear() + '-' + (now.getMonth()+1) + '-' + now.getDate() + ' ' +
	    now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
	
	//Set expiry
	var trackingExpiry = new Date( (new Date()).getTime() +  1000 * 60 * 60 * 24 * 365 * 5);
	var kwExpiry = new Date( (new Date()).getTime() +  1000 * 60 * 60 * 24 * 30);
	
	if(GetCookie('tracking'))
	{
	    document.cookie = 'tracking=' + GetCookie('tracking') + '***' 
		+ domain + '!!!' 
		+ keyword + '!!!' 
		+ traffic_type + '!!!' 
		+ timestamp + "; path=/; domain=.estatemillwork.com; expires=" 
		+ trackingExpiry.toGMTString();
	}

	else
	{
	    document.cookie = 'tracking=' 
		+ domain + '!!!' 
		+ keyword + '!!!' 
		+ traffic_type + '!!!' 
		+ timestamp + "; path=/; domain=.estatemillwork.com; expires=" 
		+ trackingExpiry.toGMTString();
	}

	if(GetCookie('keyword') == 'none' || GetCookie('keyword') == '' || GetCookie('keyword') == null)
	{
	    document.cookie = "keyword=" + keyword + "; path=/; domain=.estatemillwork.com; expires="+kwExpiry.toGMTString();
	}

	document.cookie = "last_keyword=" + keyword + "; path=/; domain=.estatemillwork.com; expires="+kwExpiry.toGMTString();
    }
    
    //Set customer number
    if(GetCookie('cust_no'))
    { }
    else
    {
	var cust_no = RunCGI("/cust_no.cgi");
	var expiry = new Date( (new Date()).getTime() +  1000 * 60 * 60 * 24 * 365 * 5);
	document.cookie = 'cust_no=' + cust_no + "; path=/; domain=.estatemillwork.com; expires=" + expiry.toGMTString();
    }
}


function trackContact()
{
    //Set customer number
    var cust_no;

    if(GetCookie('cust_no'))
    {
        cust_no = GetCookie('cust_no');
    }
    else
    {
        cust_no = 'none';
    }

    if(GetCookie('tracking'))
    {	
	var cookies = GetCookie('tracking').split('***');
	
	for(var i = 0; i < cookies.length; i++)
	{
            var vals = cookies[i].split('!!!');
            var domain = vals[0];
            var keyword = vals[1];
            var type = vals[2];
            var timestamp = vals[3];
            var ordernum = '0';
	    
            RunCGI('em_tracking.cgi?' + ordernum + '+' + domain + '+' + keyword + '+' + type + '+' + timestamp + '+em_contact.cgi+' + cust_no);
	}
	
    }

    return true;
    
}




///////////////////////////////
// Misc Functions
///////////////////////////////

function getElementsByClassName(classname, node) 
{
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function StringFilter(input)
{
    s = input;
    AllowedValues = "1234567890.";
    var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {
	var c = s.charAt(i);
	if (AllowedValues.indexOf(c) > -1) returnString += c;
    }
    return returnString;
}

function SetText(Obj, Text)
{
    if(! Obj) return;
    Obj.innerHTML=Text;
    
}


function StripPrice(PriceAmount)
{
    return(PriceAmount.substring( PriceAmount.indexOf('$')+2, PriceAmount.indexOf(".")+3));
}


function showSiteMap(which)
{
    for(var i = 0; i < 6; i++)
    {
	document.getElementById("sm_" + i).style.height = "1px";
    }

    document.getElementById("sm_" + which).style.height = "100px";
}


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; 
    } 
}

function setOpacity(obj, value) {
    obj.style.opacity = value/10;
    obj.style.filter = 'alpha(opacity=' + value*10 + ')';
}


