var cookieName = 'zipValid';

function CheckForEnter(e)
{
    var keynum;

    if(window.event) // IE
    {
        keynum = e.keyCode
        e.keyCode = 0;
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which
    }   
       
    if (keynum == 13)
    {
        e.returnValue=false;
        e.cancel = true;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
        if (e.preventDefault) e.preventDefault();
     
        CheckAvail();
        return false;
    }   
}    

function CheckAvail()
{
    var zipinput = document.getElementById('zipinput'); 
    //WinderFarms.ZipAvail.IsZipAvail(zipinput.value,OnLookupComplete);
    WinderFarms.ZipAvail.ZipCodeLocation(zipinput.value,OnLookupComplete);
}

function ToggleVisible(el, show)
{
    if (show)
    {
        el.style['visibility'] = 'visible';
        el.style['display'] = 'block';
    }
    else
    {
        el.style['visibility'] = 'hidden';
        el.style['display'] = 'none';
    }
}

function getArgs() {
    var args = new Object();
    var query = location.search.substring(1);
    var pairs = query.split("&");
    
    for(var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
        if (pos == -1) continue;
        var argname = pairs[i].substring(0,pos);
        var value = pairs[i].substring(pos+1);
        args[argname] = unescape(value);
    }
    return args;
} 

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );
    expires = 365;

    /*
    if the expires variable is set, make the correct 
    expires time, the current script below will set 
    it for x number of days, to make it for hours, 
    delete * 24, for minutes, delete * 60 * 24
    */
    if ( expires )
    {
    expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
    ( ( path ) ? ";path=" + path : "" ) + 
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}
	
// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) &&
    ( name != document.cookie.substring( 0, name.length ) ) )
    {
    return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ";", len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
    if ( Get_Cookie( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}



	
// POPUP WINDOW
// (original without GoogleAnalytics)
//
function popWindow_Original (url,myWidth,myHeight,myScrollbars,myTop,myLeft,myMenubar,myResize,success) 
{
    if (success != null)
    {
        var cookie = Get_Cookie(cookieName);
        if (cookie == null)
        {    
            var child = window.open (url, "","width="+myWidth+",height="+myHeight+",scrollbars="+myScrollbars+",top="+myTop+",left="+myLeft+",menubar="+myMenubar+",resizable="+myResize+",bgcolor=#ffffff");
            child['success'] = success;
        }
        else if (cookie == 1) //Utah
        {
            window.location.href = success;  
        }
        else if (cookie == 2) //LV
        {
            if (success.toLowerCase().indexOf('http://') == 0)
            {
                window.location.href = success; 
            }
            else
            {
                window.location.href = 'http://www.winderfarmslasvegas.com' + success + '?type=' + cookie + '&url=' + escape(success);
            }                       
        }
        else if (cookie == 3 && success != 'http://signup.winderfarms.com') //Utah but not signup.
        {
            window.location.href = 'http://www.winderfarmslasvegas.com' + success + '?type=' + cookie + '&url=' + escape(success);
        }
        else
        {
            window.location.href = '/not_available.aspx?zip=' + cookie + '&url=' + escape(url); 
        }
    }
    else
    {
        var child = window.open (url, "","width="+myWidth+",height="+myHeight+",scrollbars="+myScrollbars+",top="+myTop+",left="+myLeft+",menubar="+myMenubar+",resizable="+myResize+",bgcolor=#ffffff");
    }
}

// popWindow with Google Analytics
//
function popWindow (url,myWidth,myHeight,myScrollbars,myTop,myLeft,myMenubar,myResize,success) 
{
    if (success != null)
    {
        var cookie = Get_Cookie(cookieName);
        if (cookie == null)
        {    
            var child = window.open (url, "","width="+myWidth+",height="+myHeight+",scrollbars="+myScrollbars+",top="+myTop+",left="+myLeft+",menubar="+myMenubar+",resizable="+myResize+",bgcolor=#ffffff");
            child['success'] = success;
        }
        else if (cookie == 1) //Utah
        {
            window.location.href = success;  
            //__utmLinker(success);
        }
        else if (cookie == 2) //LV
        {
            if (success.toLowerCase().indexOf('http://') == 0)
            {
                //window.location.href = success; 
                __utmLinker(success);
            }
            else
            {
                //window.location.href = 'http://www.winderfarmslasvegas.com' + success + '?type=' + cookie + '&url=' + escape(success);
                success = 'http://www.winderfarmslasvegas.com' + success + '?type=' + cookie + '&url=' + escape(success);
                __utmLinker(success);
            }                       
        }
        else if (cookie == 3 && success != 'http://signup.winderfarms.com') //Utah but not signup.
        {
            //window.location.href = 'http://www.winderfarmslasvegas.com' + success + '?type=' + cookie + '&url=' + escape(success);
            success = 'http://www.winderfarmslasvegas.com' + success + '?type=' + cookie + '&url=' + escape(success);
            __utmLinker(success);
        }
        else
        {
            //window.location.href = '/not_available.aspx?zip=' + cookie + '&url=' + escape(url); 
            success = '/not_available.aspx?zip=' + cookie + '&url=' + escape(url);
            __utmLinker(success);
        }
    }
    else
    {
        var child = window.open (url, "","width="+myWidth+",height="+myHeight+",scrollbars="+myScrollbars+",top="+myTop+",left="+myLeft+",menubar="+myMenubar+",resizable="+myResize+",bgcolor=#ffffff");
    }
}

	// BOTTOM NAVIGATION
	var bottomNav = '';
	bottomNav += '<a href="/AboutUs/" class="footer">About Us</a> | <a href="/AboutUs/Employment/" class="footer">Employment</a><br />';
	bottomNav += '	&nbsp; &copy;2007 Winder Farms.  All Rights Reserved<br />';
	
	
	// TOP NAVIGATION HOME
	var topNavHome = '';
		topNavHome += '		<li id="nav_home">';
		topNavHome += '			<a href="/default.aspx">Home</a>';
		topNavHome += '		</li>';
		
	
	// TOP NAVIGATION
	var topNav = '';
		topNav += '		<li id="nav_whywinder">';
		topNav += '			<a href="/why_winder.html">Why Winder</a>';
		topNav += '		</li>';
		topNav += '		<li id="nav_ourproducts">';
		topNav += '			<a href="javascript:popWindow(\'/pop_enterZip.aspx\',\'730\',\'415\',\'no\',\'50\',\'50\',\'no\',\'no\',\'/ourproducts/\');">Our Products</a>';
		topNav += '		</li>';
		topNav += '		<li id="nav_howitworks">';
		topNav += '			<a href="javascript:popWindow(\'/pop_enterZip.aspx\',\'730\',\'415\',\'no\',\'50\',\'50\',\'no\',\'no\',\'/how_it_works.html\');">How It Works</a>';
		topNav += '		</li>';
		topNav += '		<li id="nav_ourguarantee">';
		topNav += '			<a href="/our_guarantee.html">Our Guarantee</a>';
		topNav += '		</li>';
		
function Redirect()
{
    var args = getArgs();
    if (args['type'] && args['type'] != null)
    {        
        Set_Cookie(cookieName, args['type']);          
    }
    if (args['url'] && args['url'] != null)
    {
        window.location.href = unescape(args['url']);     
    }
}

if (window.attachEvent) {

window.attachEvent("onload",  Redirect);

} else {

window.addEventListener("DOMContentLoaded", Redirect, false);

}		


// 
//
function pop_window (url,myWidth,myHeight,myScrollbars,myTop,myLeft,myMenubar,myResize) {
	window.open (url, "","width="+myWidth+",height="+myHeight+",scrollbars="+myScrollbars+",top="+myTop+",left="+myLeft+",menubar="+myMenubar+",resizable="+myResize+",bgcolor=#ffffff");
}
