﻿function generateXML()
{
    if (document.implementation && document.implementation.createDocument)
    {
        xmlDoc = document.implementation.createDocument("", "", null);
    }
    else if (window.ActiveXObject)
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    }
    else
    {
        alert("Error: Your browser does not support this script's functionality");
        return;
    }
    
    return xmlDoc;
}



function getRequest() 
{
    var request; 
    
    try
    {
        request = new XMLHttpRequest();   
    }
    catch (trymicrosoft)
    {
        try
        {
            request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (othermicrosoft)
        {
            try
            {
                request = ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (failed)
            {
                request = false;
            }
        }
    }
    if (!request)
    {
        alert("Error initializing XMLHttpRequest");
        return false;
    }
    else
    {
        return request;
    }
}

 function SetHomePage()
{
    var k = document.getElementById('aSetAsHomePage');
    
    k.style.behavior='url(#default#homepage)';
    k.setHomePage('http://www.snutt.no');
    
    return false;
}

function SetFavorite()
{
    window.external.AddFavorite('http://www.snutt.no','Snutt.no - Det du lurer på!');
    return false;
}

function LoadRequestedMovie()
{
    var vQs = new Querystring();
    var vMovieRef = vQs.get("MovieRef");

    if(vMovieRef)
        location.href = 'Movie.aspx?MovieRef=' + vMovieRef;
}

function Querystring(qs) 
{ 
    this.params = new Object();
    this.get = Querystring_get;
	
    if (qs == null)
        qs=location.search.substring(1,location.search.length);

    if (qs.length == 0) 
        return;

    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&');
	
    for (var i=0;i<args.length;i++) 
    {
        var value;
        var pair = args[i].split('=');
        var name = unescape(pair[0]);

        if (pair.length == 2)
	        value = unescape(pair[1]);
        else
	        value = name;
		
        this.params[name] = value;
    }
}

function Querystring_get(key, default_) 
{
    if (default_ == null) 
        default_ = null;
	
    var value = this.params[key];
	
    if (value==null) 
        value=default_;
	
    return value;
}

function frmRequest_KeyDown(e)
{
    var numCharCode;
    var elTarget;
    var strType;
    
    if (!e) var e = window.event;
    if (e.keyCode) numCharCode = e.keyCode;
    else if (e.which) numCharCode = e.which;
    
    if (e.target) elTarget = e.target;
    else if (e.srcElement) elTarget = e.srcElement;

    if ( elTarget.tagName.toLowerCase() == 'input' )
    {
        strType = elTarget.getAttribute('type').toLowerCase();
        
        switch ( strType )
        {
            case 'text' :
            if ( numCharCode == 13 )
            {
                if (e.keyCode)
                    e.keyCode = 9;
                else if 
                    (e.which) e.which = 9;

                SearchMovies();
            }
            break;
        }
    }
    return true;
}

function frmRequest_KeyPress(e)
{
    var numCharCode;
    var elTarget;
    var strType;
    if (!e) var e = window.event;
    
    if (e.keyCode) numCharCode = e.keyCode;
    else if (e.which) numCharCode = e.which;
    
    if (e.target) elTarget = e.target;
    else if (e.srcElement) elTarget = e.srcElement;

    if ( elTarget.tagName.toLowerCase() == 'input' )
    {
        strType = elTarget.getAttribute('type').toLowerCase();
        switch ( strType )
        {
            case 'text' :
            if ( numCharCode == 13 )
            {
                SearchMovies();
                return false;
            }
            break;
        }
    }
    return true;
}

function SearchOnFocus()
{
    if(document.getElementById('searchtext').value == "Søk i filmer og kategorier")
        document.getElementById('searchtext').value = "";
}

function SearchMovies()
{
    if(document.getElementById('searchtext') == null)
        return false;
    
    if(document.getElementById('searchtext').value == "")
        return false;
        
    if(document.getElementById('searchtext').value == "Søk i filmer og kategorier")
        return false;
        
    location.href = "SearchPage.aspx?SearchText=" + document.getElementById('searchtext').value;
}

function ShowOrHide(elementName, callingElementName)
{
    var k = document.getElementById(elementName);
    
    if(k == null)
        return;
        
    if(k.style.display == 'none')
    {
        k.style.display = 'inline';
            
        if(callingElementName != null || callingElementName != '')
            document.getElementById(callingElementName).style.display = 'none';
    }
    else
    {
        k.style.display = 'none';
        
        if(callingElementName != null || callingElementName != '')
            document.getElementById(callingElementName).style.display = 'inline';
    }
}


function testIsCookies() 
{ 
    var exp = new Date(); 
    exp.setTime(exp.getTime() + 1800000); 
    
    // first write a test cookie 
    setCookie("cookies", "cookies", exp, false, false, false); 
    
    if (document.cookie.indexOf('cookies') != -1) 
    { 
        return true; //Is cookies
    } 
    else { 
        return false; //NO !! cookies
    } 
    
    // now delete the test cookie 
    exp = new Date(); 
    exp.setTime(exp.getTime() - 1800000); 
    setCookie("cookies", "cookies", exp, false, false, false); 
}
	
function setCookie(name, value, expires, path, domain, secure) 
{ 
 var curCookie = name + "=" + escape(value) + 
	((expires) ? "; expires=" + expires.toGMTString() : "") + 
	((path) ? "; path=" + path : "") + 
	((domain) ? "; domain=" + domain : "") + 
	((secure) ? "; secure" : ""); 
 document.cookie = curCookie; 
}

function Search(where)
{
    var features = 'resizable=yes,scrollbars=yes,status=yes,toolbar=yes,menubar=yes,location=yes,title=yes'
    var c;
    
    if(where == "Google")
    {
        c = document.getElementById('txtGoogle');
        if(c != null && c != "")
            window.open('http://www.google.no/search?hl=no&q=' + c.value,null,features);
    }
    
    if(where == "Yr")
    {
        c = document.getElementById('txtYr');
        if(c != null && c != "")
            window.open('http://www.yr.no/soek.aspx?sted=' + c.value, null, features);
    }
    
    if(where=="SNL")
    {
       c = document.getElementById('txtSNL');
        if(c != null && c != "")
            window.open('http://www.snl.no/.search?query=' + c.value, null, features);
    }
    
    if(where=="Wiki")
    {
       c = document.getElementById('txtWiki');
        if(c != null && c != "")
            window.open('http://no.wikipedia.org/wiki/' + c.value,null,features);
    }
    
    if(where == "Gulesider")
    {
       c = document.getElementById('txtGulesider');
        if(c != null && c != "")
            window.open('http://www.gulesider.no/gs/categoryList.c?q=' + c.value,null,features);
    }
}
   
   
function Search_KeyDown(e, sWhere)
{
    var numCharCode;
    var elTarget;
    var strType;
    
    if (!e) var e = window.event;
    if (e.keyCode) numCharCode = e.keyCode;
    else if (e.which) numCharCode = e.which;
    
    if (e.target) elTarget = e.target;
    else if (e.srcElement) elTarget = e.srcElement;

    if ( elTarget.tagName.toLowerCase() == 'input' )
    {
        strType = elTarget.getAttribute('type').toLowerCase();
        
        switch ( strType )
        {
            case 'text' :
            if ( numCharCode == 13 )
            {
                if (e.keyCode)
                    e.keyCode = 9;
                else if 
                    (e.which) e.which = 9;
                  
                Search(sWhere);
            }
            break;
        }
    }
    return true;
}

function Search_KeyPress(e, sWhere)
{
    var numCharCode;
    var elTarget;
    var strType;
    if (!e) var e = window.event;
    
    if (e.keyCode) numCharCode = e.keyCode;
    else if (e.which) numCharCode = e.which;
    
    if (e.target) elTarget = e.target;
    else if (e.srcElement) elTarget = e.srcElement;

    if ( elTarget.tagName.toLowerCase() == 'input' )
    {
        strType = elTarget.getAttribute('type').toLowerCase();
        switch ( strType )
        {
            case 'text' :
            if ( numCharCode == 13 )
            {
                Search(sWhere);
                return false;
            }
            break;
        }
    }
    return true;
}
