﻿// JScript File
/*******************************************
Language block
********************************************/
function setLanguage (value)
{
    // Nuon cookie
	var expdate = new Date();
	setCookie ("NuonLanguage", escape(value));
	
	if (document.location.toString().indexOf('Index.aspx') != - 1)
	{
	    document.location.href = "index.aspx";
	}
	else if (document.location.toString().indexOf('index.aspx') != - 1)
	{
	    document.location.href = "index.aspx";
	}
	else
	{
	document.location.reload(true);
    }
}

var urlList = new Array();
function SetLanguage (value)
{
    // Nuon cookie
	var expdate = new Date();
	setCookie ("NuonLanguage", escape(value));
	
	if (eval("urlList["+value+"]") != "" && eval("urlList["+value+"]") != "undefined"  && eval("urlList["+value+"]") != undefined)
	{   
	    //window.alert('1');
	    //document.location.reload(true);
	    //window.alert(eval("urlList["+value+"]"));
		document.location = eval("urlList["+value+"]");
	}
	else{
	    //window.alert('2');
		document.location.reload(true);
	}
}

function setCookie (name, value, expires) 
{
	if (!expires) 
	{
		document.cookie = name + "=" + escape (value) + "; path=/"; 			
	}
	else
	{
		document.cookie = name + "=" + escape (value) + "; expires=" + expires.toGMTString() + "; path=/"; 
	}
} 
/*******************************************
jumpMenu (deprecated?)
********************************************/
function jumpMenu(targ,selObj,restore){
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

/*******************************************
Scrolling of the meta content
********************************************/
function getWindowHeight()
{
	var w = 0;
	var h = 0;
	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return h;
}

function ScrollRight(){
	//20090714: GC: Added check for small screens. With many rightpane items, scroll was not functioning properly.
	var windowHeight = getWindowHeight();
	//document.title = windowHeight; //debug
	if(windowHeight>=800){
		var top = 0;
		if (document.getElementById('ctl00_divRightNarrow') != null){
			if (document.body.scrollTop != 0){
				top = document.body.scrollTop;
			}else if (document.documentElement.scrollTop != 0){
				top = document.documentElement.scrollTop;
			}
			if (top >= 200){
				top = top - 200;
			} else{
				top=0;
			}
			var topPx = top + 'px';
			document.getElementById('ctl00_divRightNarrow').style.marginTop=topPx;
		}
		if (document.getElementById('ctl00_ctl00_divRightNarrow') != null){
			if (document.body.scrollTop != 0){
				top = document.body.scrollTop;
			}else if (document.documentElement.scrollTop != 0){
				top = document.documentElement.scrollTop;
			}
			if (top >= 200){
				top = top - 200;
			} else{
				top=0;
			}
			var topPx = top + 'px';
			document.getElementById('ctl00_ctl00_divRightNarrow').style.marginTop=topPx;
		}
	}
}

window.onscroll = function(){
	ScrollRight();
};
window.setInterval(function(){ScrollRight();}, 250);


/*******************************************
Not sure where and why these functions are used
********************************************/
function getScrollY() 
{ 
    var scrOfY = 0; 
    if( typeof( window.pageYOffset ) == 'number' ) 
    { //Netscape compliant 
        scrOfY = window.pageYOffset; 
    } 
    else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
    { //DOM compliant 
        scrOfY = document.body.scrollTop; 
    } 
    else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
    { //IE6 standards compliant mode 
    scrOfY = document.documentElement.scrollTop ; 
    } 
    scrOfY= scrOfY + ( GetHeightSize()/2 ); 
    return [ scrOfY ]; 
} 

function GetHeightSize() 
{   var myHeight = 0; 
    if( typeof( window.innerWidth ) == 'number' ) 
    { //Non-IE 
    myHeight = window.innerHeight; 
    } 
    else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
    { //IE 6+ in 'standards compliant mode' 
    myHeight = document.documentElement.clientHeight; 
    } 
    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
    { //IE 4 compatible 
    myHeight = document.body.clientHeight; 
    } 
    return [ myHeight ]; 
}

function Anthem_PreCallBack() 
{  
    var loading = document.createElement("div"); 
    loading.id = "loading"; 
    loading.style.paddingLeft = "5px"; 
    loading.style.paddingRight = "5px"; 
    loading.style.position = "absolute"; 
    loading.style.right = window.screen.availWidth/2 + "px";
    loading.style.top = getScrollY() + "px";  
    loading.style.zIndex = "99999999"; 
    var oImgElem = document.createElement("img"); 
    oImgElem.src = "/images/precallback.gif"; 
    oImgElem.Title = "running"; 
    loading.appendChild(oImgElem); 
    document.body.appendChild(loading); 
}

function Anthem_PostCallBack() 
{ 
    var loading = document.getElementById("loading"); 
    document.body.removeChild(loading); 
}

function OpenHelp(contentId) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open('help.aspx?id=" + contentId + "', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=570,height=555,left = 350,top = 282');");
}

function Fill(field, val)
{
    document.getElementById(field).value = val;
}

function TestNumeric(c)
{
    
    if (c.value.indexOf(' ') != -1)
    {
        c.value = c.value.replace(' ', '');
    }
    
    if (isNaN(c.value))
    {
        var tmpstring;
        
        tmpstring = c.value;
        
        for (i=0; i < c.value.length; i++)
        {
            if (isNaN(c.value.charAt(i)))
            {
                tmpstring = tmpstring.replace(c.value.charAt(i), '');
            }
        }
        
        c.value = tmpstring;
    
        //c.value=c.value.substring(0,c.value.length - 1);
    }
    else 
    {
        if (c.value.indexOf('.') != -1)
        {
	        c.value = c.value.replace('.',''); 
	    }
	        
        if (c.value.indexOf(',') != -1)
        {
	        c.value = c.value.replace(',',''); 
	    }
    }
}

function TestInternationalPhoneNumber(c)
{
    if (isNaN(c.value))
    {
        if(c.value.substring(0,1) == '+')
        {
            //window.alert(phonenumber.substring(1,phonenumber.length - 1));
            c.value = c.value.substring(1,c.value.length - 1);
        }
        else
        {
            c.value = c.value.substring(0,phonenumber.length - 1);
        } 
    }
    else 
    {
	    c.value = c.value.replace('.',''); 
    }
}

var previousLength = 0;

function SetPreviousLength(field)
{
    previousLength = field.value.length;
}

function autojump(field, nextfield)
{
    if (field.maxLength == previousLength)
    {
        
    }
    else if (field.maxLength == field.value.length)
    {
        var myNextField = document.getElementById(nextfield);
        myNextField.focus();
    }
}

/***************************
OLR Date Replace functions
***************************/
function getFirstElementByAttribute(oElm, strTagName, strAttributeName, strAttributeValue)
{    
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)", "i") : null;
    var oCurrent;
    var oAttribute;
    for(var i=0; i<arrElements.length; i++)
    {
        oCurrent = arrElements[i];
        oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
        if(typeof oAttribute == "string" && oAttribute.length > 0){
            if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute)))
            {
                arrReturnElements.push(oCurrent);
            }        
        }
    }    
    return arrReturnElements[0];
}

function SetStartDate(fieldDay, fieldMonth, fieldYear, fieldStartDate, fieldStartDateText, strCustomText)
{
    //effalert("SetStartDate");
    try {
        if ((document.getElementById(fieldDay) != null) && (document.getElementById(fieldMonth)) && (document.getElementById(fieldYear)))
        {
            if (isDate(document.getElementById(fieldMonth).value + '/' + document.getElementById(fieldDay).value + '/' + document.getElementById(fieldYear).value)) 
            {
                var calcDate = GetCalcStartDateMiddleOfMonth(document.getElementById(fieldDay).value, document.getElementById(fieldMonth).value, document.getElementById(fieldYear).value);            
            }
            else
            {
                var today = new Date();
                var calcDate = GetCalcStartDate(today.getDay(), today.getMonth() + 1, today.getFullYear());            
            }
            var strMonth = (calcDate.getMonth()+1).toString();
            if(strMonth.length<2){ strMonth = "0" + strMonth; }
            var newDate = calcDate.getDate() + "/" + strMonth + "/" + calcDate.getFullYear().toString();
            document.getElementById(fieldStartDate).value = newDate;                                 
            strCustomText = strCustomText.replace("<!--01/01/1999-->", newDate);
            document.getElementById(fieldStartDateText).innerHTML = strCustomText;
            if (fieldStartDateText.indexOf("Elek") != -1)
                var productType = "Elek";
            else
                var productType = "Gas";
                
            ShowInfo(fieldDay.value, fieldMonth.value, fieldYear.value, productType);                
        }
    }
    catch(err){
        alert("SetStartDate: " + err);
    }
}

function SetStartDateOlr(productType, strCustomText)
{
    //alert("SetStartDateOlr");
    //alert(productType);
    try {
        var fieldDay = getFirstElementByAttribute(document, 'input', 'origId', 'txt' + productType + 'EindeContractDay');
        var fieldMonth = getFirstElementByAttribute(document, 'input', 'origId', 'txt' + productType + 'EindeContractMonth');
        var fieldYear = getFirstElementByAttribute(document, 'input', 'origId', 'txt' + productType + 'EindeContractYear');
        var panel = getFirstElementByAttribute(document, 'div', 'origId', 'pnlStartDate' + productType);
        var fieldStartDate = getFirstElementByAttribute(document, 'input', 'origId', 'txtCalc' + productType + 'StartDate');
        var fieldStartDateText = document.getElementById('lblCalc' + productType + 'StartDate');       
                
        if ((fieldDay != null) && (fieldMonth != null) && (fieldYear != null))
        {
            if (isDate(fieldMonth.value + '/' + fieldDay.value + '/' + fieldYear.value)) {
                var calcDate = GetCalcStartDateMiddleOfMonth(fieldDay.value, fieldMonth.value, fieldYear.value)
                var strMonth = (calcDate.getMonth() + 1).toString();
                if (strMonth.length < 2) { strMonth = "0" + strMonth; }

                var newDate = calcDate.getDate() + "/" + strMonth + "/" + calcDate.getFullYear().toString();

                fieldStartDate.value = newDate;
                strCustomText = strCustomText.replace("<!--01/01/1999-->", newDate);
                fieldStartDateText.innerHTML = strCustomText;

                panel.style.display = '';
                ShowInfo(fieldDay.value, fieldMonth.value, fieldYear.value, productType);
            }
            else {
                panel.style.display = 'none';
                resetFieldInfo(productType);
            }
        }
        else {
            //Bug: Shown when changed option on step1. If no date should be calculate, don't show the panel.
            if(panel){
                panel.style.display = 'none';
            }
        }
    }
    catch(err)
    {
        alert("SetStartDateOlr: " + err);
    }
}

function resetFieldInfo(productType) {
    var fieldInfo = new Array(4);
    fieldInfo[0] = getFirstElementByAttribute(document, 'span', 'origId', 'lblInfo' + productType + '1');
    fieldInfo[1] = getFirstElementByAttribute(document, 'span', 'origId', 'lblInfo' + productType + '2');
    fieldInfo[2] = getFirstElementByAttribute(document, 'span', 'origId', 'lblInfo' + productType + '3');
    fieldInfo[3] = getFirstElementByAttribute(document, 'span', 'origId', 'lblInfo' + productType + '4');

    if (fieldInfo[0] != null) {
        for (var i = 0; i < 4; i++) {
            fieldInfo[i].style.display = 'none';
        }
    }
}

function SetStartDateFromToday(sender, productType, fieldStartDate, fieldStartDateText, strCustomText) {

    //alert("SetStartDateFromToday with params" + sender + "' " + productType + ", " + fieldStartDate + ", " + fieldStartDateText + ", " + strCustomText);    
    try
    {
        if (sender.checked) {
            var panel = getFirstElementByAttribute(document, 'div', 'origId', 'pnlStartDate' + productType);
        
            var currentTime = new Date();
            var month = currentTime.getMonth();
            var day = currentTime.getDate();
            var year = currentTime.getFullYear();

            var calcDate = GetCalcStartDate(day, month, year);
            
            var strMonth = (calcDate.getMonth()+1).toString();
            if(strMonth.length<2){ strMonth = "0" + strMonth; }
            
            var origDate = document.getElementById(fieldStartDate).value; //why?
            var newDate = "01/" + strMonth + "/" + calcDate.getFullYear().toString();
            document.getElementById(fieldStartDate).value = newDate;                                             
            strCustomText = strCustomText.replace("<!--01/01/1999-->", newDate);
            document.getElementById(fieldStartDateText).innerHTML = strCustomText;
            panel.style.display = '';

            var fieldInfo = new Array(4);
            fieldInfo[0] = getFirstElementByAttribute(document, 'span', 'origId', 'lblInfo' + productType + '1');
            fieldInfo[1] = getFirstElementByAttribute(document, 'span', 'origId', 'lblInfo' + productType + '2');
            fieldInfo[2] = getFirstElementByAttribute(document, 'span', 'origId', 'lblInfo' + productType + '3');
            fieldInfo[3] = getFirstElementByAttribute(document, 'span', 'origId', 'lblInfo' + productType + '4');

            if (fieldInfo[0] != null) {
                for (var i = 0; i < 4; i++) {
                    fieldInfo[i].style.display = 'none';
                }

                fieldInfo[2].style.display = '';
            }
        }
    }
    catch(err)
    {
        alert("SetStartDateFromToday: " + err);
    }
}

function isDate(dateStr) 
{
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        return false;
    }

    month = matchArray[1]; // p@rse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        return false;
    }

    if (day < 1 || day > 31) {
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            return false;
        }
    }
    return true; // date is valid
}

function GetCalcStartDate(strDay, strMonth, strYear)
{
    var startDate = new Date();
    try
    {
        var date = new Date();
        startDate = new Date(parseFloat(strYear), parseFloat(strMonth)-1, parseFloat(strDay));
        
        var one_day=1000*60*60*24;
        var difference = Math.ceil((startDate.getTime()-date.getTime()) / (one_day));

        if(difference <= 24)
        {
            date.setDate(date.getDate()+24);
            startDate = date;
        
            if (startDate.getDate() != 1)
                startDate = new Date(parseFloat(startDate.getFullYear()), parseFloat(startDate.getMonth())+1, 1);
        }
        else
        {
            if (startDate.getDate() != 1)
                startDate = new Date(parseFloat(startDate.getFullYear()), parseFloat(startDate.getMonth())+1, 1);
        }
    }
    catch(err)
    {
        alert('GetCalcStartDate: ' + err);
    }
    return startDate;
}


function GetCalcStartDateMiddleOfMonth(strDay, strMonth, strYear) {
    var startDate = new Date();
    try {
        var date = new Date();
        startDate = new Date(parseFloat(strYear), parseFloat(strMonth) - 1, parseFloat(strDay));

        var one_day = 1000 * 60 * 60 * 24;
        var difference = Math.ceil((startDate.getTime() - date.getTime()) / (one_day));

        if (difference < 24) {
            date.setDate(date.getDate() + 24);
            startDate = date;
        }
        else {
            startDate.setDate(startDate.getDate() + 1);
        }
    }
    catch (err) {
        alert('GetCalcStartDate: ' + err);
    }
    return startDate;
}

function ShowInfo(strDay, strMonth, strYear, productType)
{
    try
    {
        var fieldInfo = new Array(4);
        fieldInfo[0] = getFirstElementByAttribute(document, 'span', 'origId', 'lblInfo' + productType + '1');
        fieldInfo[1] = getFirstElementByAttribute(document, 'span', 'origId', 'lblInfo' + productType + '2');
        fieldInfo[2] = getFirstElementByAttribute(document, 'span', 'origId', 'lblInfo' + productType + '3');
        fieldInfo[3] = getFirstElementByAttribute(document, 'span', 'origId', 'lblInfo' + productType + '4');
        
        var fieldStartDateText = document.getElementById('lblCalc' + productType + 'StartDate');
        var endDate = new Date(parseFloat(strYear), parseFloat(strMonth)-1, parseFloat(strDay));
        var date = new Date();
        var one_day=1000*60*60*24;
        var difference = Math.ceil((endDate.getTime()-date.getTime()) / (one_day));

        // SET ALL FIELDS INVISIBLE //
        for (var i = 0; i < 4; i++)
        {
            if (fieldInfo[i] != null)
            {
                fieldInfo[i].style.display = 'none';
            }
        }
        
        var displayIndex = 1;
        
        if (difference < 0)
        {
            displayIndex = 0;
            fieldStartDateText.style.display = 'none'
        }
        else
        {
            if (difference < 24)
            {
                displayIndex = 1;
                fieldStartDateText.style.display = 'none'
            }
            else
            {
                if ((difference >= 24) && (difference <= 365))
                {
                    displayIndex = 2;
                    fieldStartDateText.style.display = ''
                }
                else if (difference > 365)
                {
                    displayIndex = 3;
                    fieldStartDateText.style.display = 'none'
                }
            }
        }
        if (fieldInfo[displayIndex] != null)
        {
            fieldInfo[displayIndex].style.display = '';            
        }
    }
    catch(err)
    {
        alert('ShowInfo: ' + err);
    }
}

function fireMyEvent(element, event) {
    try {
        if (document.createEventObject) {
            // dispatch for IE
            var evt = document.createEventObject();
            return element.fireEvent('on' + event, evt)
        }
        else {
            // dispatch for firefox + others
            var evt = document.createEvent("HTMLEvents");
            evt.initEvent(event, true, true); // event type,bubbling,cancelable
            return !element.dispatchEvent(evt);
        }
    }
    catch(err){
        alert("fireMyEvent: " + err);
    }
}

function SetUniqueRadioButton(nameregex, current) {
    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio') {
            if (re.test(elm.name)) {
                elm.checked = false;
            }
        }
    }
    current.checked = true;
}
