var Profile = new ProfileControl()

function ProfileControl(  )
{
	this.initials = function ( oNode )
	{
		var sValue = oNode.value;
		oNode.value = sValue.replace( '.', '' );
	}
}


var Label = new LabelControl()

function LabelControl(  )
{
    var SelectedLabel     = '';
    var SelectPointer     = 'energielabel_pointer';
    var SelectPointerImg  = 'energie_pointer_';
    var Classhover        = 'pijl_hover';
    var Class             = 'pijl';

    this.mouseover = function ( oNode )
    {
        oNode.className = Classhover;
    }

    this.mouseout = function ( oNode )
    {
        if (oNode.id != SelectedLabel)
        {
            oNode.className = Class;
        }
        
    }
    
    this.reset = function ( )
    {
        for (var i=1; i<8; i++)
        {
            document.getElementById("energielabel_image_"+i).className = 'pijl';
            document.getElementById(SelectPointer).className = '';
            SelectedLabel = '';
        }   
    }

    this.click = function ( oNode )
    {
        if (oNode.id == SelectedLabel)
        {
            SelectedLabel = '';
        }
        else
        {
            if ( SelectedLabel != '' )
            {
                for (var i=1; i<8; i++)
                {
                    if (((oNode.id.substr(0, 19))+i) != oNode.id)
                    {   
                        document.getElementById((oNode.id.substr(0, 19))+i).className = Class;
                    }
                }   
            }
            var iLabel = oNode.id.substr(19, 20);
            if (iLabel == 1)
            {
              sLabel = "A";
            }
            if (iLabel == 2)
            {
              sLabel = "B";
            }
            if (iLabel == 3)
            {
              sLabel = "C";
            }
            if (iLabel == 4)
            {
              sLabel = "D";
            }
            if (iLabel == 5)
            {
              sLabel = "E";
            }
            if (iLabel == 6)
            {
              sLabel = "F";
            }
            if (iLabel == 7)
            {
              sLabel = "G";
            }
            
            document.getElementById("energielabel").value = sLabel;
            
            document.getElementById(SelectPointer).className = SelectPointerImg+(oNode.id.substr(19));
            SelectedLabel = oNode.id;
        }
    }
}

function finishSurvey()
{
	window.location='/uitslag';
}
function finishsurvey()
{
	window.location='/uitslag';
}
function showTerms()
{
    window.open('/voorwaarden.php', 'popup_algemene_voorwaarden', 'width=480, height=450, resizable=no, menubar=no, scrollbars=yes, status=yes, toolbar=no');
}

function showPrivacy()
{
    window.open('/privacy.php', 'popup_algemene_voorwaarden', 'width=480, height=450, resizable=no, menubar=no, scrollbars=yes, status=yes, toolbar=no');
}

function showEind()
{
    window.open('/bedankt.php', 'popup_eindtekst', 'width=454, height=325, resizable=no, menubar=no, scrollbars=no, status=no, toolbar=no');
}

function showPrizes()
{
    window.open('/toonprijzen.php', 'popup_toon_prijzen', 'width=456, height=539, resizable=no, menubar=no, scrollbars=no, status=no, toolbar=no');
}

function onRegistrationSuccess()
{
	window.location='/test';
}

function showTafMail()
{
	var name = document.getElementById('taf_name').value;
	var label = document.getElementById('energielabel').value;

    window.open('/taf-voorbeeld.php?label='+label+'&name='+name, 'popup_toon_prijzen', 'width=480, height=470, resizable=no, menubar=no, scrollbars=no, status=no, toolbar=no');
}

function redirect()
{
    setTimeout( "window.location = '/index'", 5000 );
}

function onLoginSuccess()
{
	setTimeout( "window.location = '/uitslag'", 2000 );
}

function updateSenders( sName, sEmail, sLabel )
{
    var str = "Jij denkt dat "+sName+" een "+sLabel+" scoort ("+sEmail+")";
    if (str.length >= 55)
    {
      str = str.substr(0,52)+'...';
    }
    sNewText = "<div class='taf_pijl'><img src='/media/image/TAF_pijl_"+sLabel+".png' alt='' width='364' height='20'><div class='invitercontent' >"+str+"</div></div>";
    document.getElementById( "tafsendercontent_new" ).innerHTML = document.getElementById( "tafsendercontent_new" ).innerHTML + sNewText;
    
    // Empty fields
    document.getElementById( "taf_name" ).value = '';
    document.getElementById( "taf_email" ).value = '';
    document.getElementById( "energielabel" ).value = '';

	//Show KLAAR button
	document.getElementById( "klaarbutton" ).style.display = 'block';
    Label.reset();
}

function checkAddress( )
{
	var sZipcode = document.getElementById( "postcodenum" ).value + document.getElementById( "postcodelet" ).value;
	var iHousenumber = document.getElementById( "huisnr" ).value;

	if ( sZipcode.length == 6 || iHousenumber != '' )
	{
		var oXML = new klib3.xml();
		oXML.onload = function()
		{
			onloadResultText( oXML.getData() );
		};
		oXML.request( "/rpc.php", "post", true, {command:"tntrpc",_format:"xml",zipcode:sZipcode,housenumber:iHousenumber} );
	}
}

function onloadResultText( oXML )
{
    var oStatus   = parseStatusResult( oXML );

	var oStraat = document.getElementById( "straat" );
	var oWoonplaats = document.getElementById( "woonplaats" );
	oStraat.value = '';
	oWoonplaats.value = '';

	var aAddress = new Array();

    if ( oStatus )
    {
		if ( oStatus.message.indexOf( '|||' ) > 0 )
		{
			aAddress = oStatus.message.split( "|||" );
			oStraat.value = aAddress[0];
			oWoonplaats.value = aAddress[1];
		}
    }
}

function getSiblingByName( oNode, sName )
{
    
    while ( ( oNode.nodeType != 1 || ( oNode.nodeName && oNode.nodeName.toLowerCase() != sName.toLowerCase() ) ) && oNode.nextSibling )
        oNode = oNode.nextSibling;

    if ( oNode.nodeType == 1 && ( oNode.nodeName && oNode.nodeName.toLowerCase() == sName.toLowerCase() ) )
    {   
        return oNode;
    }
    return false;
}

function getNodeValue( oNode )
{
    if ( oNode )
	{
		if ( oNode.nodeType == 3 || oNode.nodeType == 4 )
			return oNode.nodeValue;
		return arguments.callee( oNode.firstChild );
	}
}

function parseStatusResult( oDataXML )
{                    
    if ( oDataXML )
    {
        var oReply = getSiblingByName( oDataXML.firstChild, "reply" );
        if ( oReply )
        {
            var sStatus = "error";
            if ( typeof oReply.attributes[ 0 ] != "undefined" && oReply.attributes[ 0 ].name.toLowerCase() == "status" )
                sStatus = oReply.attributes[ 0 ].value;
                
                
            var sMessage = getNodeValue( getSiblingByName( oReply.firstChild, "message" ) );
            
            var sContent = getNodeValue( getSiblingByName( oReply.firstChild, "content" ) );
            
            var bStatus  = sStatus == "OK";
            
            return {
                success:bStatus,
                status:sStatus,
                message:unescape( sMessage ),
                content:sContent
            };
        }
    }
    
    return false;
}