// Variable Declaration Area
var val_ban_status = 'show';
var pend_status    = 1;

// This will be called initially once the page loaded
function init(val)
{
        // alert("Inside init::val " + val );
        hide('choices');
        hide('update-area');
        hide('action-area');
        hide('load-area');
}

// To hide a div
function hide(id)
{
        document.getElementById(id).style.display = "none";
}

// To show a div
function show(id)
{
        document.getElementById(id).style.display = "block";
}

// To Parse the input and return the Price Value only
function parse_price(str)
{
        var ret = "";
        for(i=0; i<str.length; ++i)
        {
                 if ((str.charAt(i)).match(/\d/))
                 {
                        ret += str.charAt(i);
                 }
        }
        return parseInt(ret);
}

// To Update Total Package Price
function update_new_sum(price)
{
        var voice = parse_price(document.getElementById('VOICE_price').innerHTML);
        var sms   = parse_price(document.getElementById('SMS_price').innerHTML);
        var mms   = parse_price(document.getElementById('MMS_price').innerHTML);
        var data  = parse_price(document.getElementById('GPRS_price').innerHTML);

        var new_sum = voice + sms + mms + data;
        document.getElementById('new_sum').innerHTML = new_sum + " kr./md.";
}

// To Update the New Price Value for Particular Package
function update_price(id,price)
{
        if ( price != 0 )
        {
                document.getElementById(id).innerHTML = "for " + price + " kr.";
        }
        else
        {
                document.getElementById(id).innerHTML = 0 + " kr.";
        }

        // Update Total Package Price
        update_new_sum(price);
}

// To Validate the BAN Md5 against the input Parameter
function val_ban(input,ban_md5)
{
        if ( ban_md5 != hex_md5(input) )
        {
                show("ban-img-cross");
                hide("ban-img-tick");
        }
        else
        {
                hide("ban-img-cross");
                show("ban-img-tick");
                document.getElementById('update_choices').disabled = false;
                val_ban_status = 'submit';
        }
}

// To Show the New Package division
function show_choices()
{
        hide('next');
        hide('update-button');
        show('choices');
        show('update-area');

        show('action-area');
        hide('ban-text');
        hide('ban');
        hide('ban-img-cross');
        hide('ban-img-tick');
        hide('ban-note');
}

// To submit or show the button
function show_ban_validator(frm)
{
        if ( val_ban_status == 'show' )
        {
				// alert("Inside show_ban_validator() :: " + val_ban_status );
                show('ban-text');
                show('ban');
                show('ban-img-cross');
                show('ban-note');
                document.getElementById('update_choices').disabled = true;
        }
        else
        {
                hide('choices');
                hide('update-area');
                hide('action-area');

                show('next');
                show('update-button');
                document.getElementById('show_choice').disabled = true;
                show('load-area');
                val_ban_status = 'show';
				//document.getElementById(frm).submit();
				update_services();
        }
}

// To Reload the page
function Reload(url)
{
        window.location.href = url;
}

// After submit
function post_submit()
{
        document.getElementById('show_choice').disabled = true;
        show('load-area');
        var t = setTimeout("Reload(unescape(window.location.pathname))",50000);
}

// Get Selected Value
function getSelectValue(name) 
{
	var sel = document.getElementsByName(name);
	for(i=0; i<sel.length; ++i)
	{
		if( sel[i].checked ) {
			return sel[i].value;
		}
	}
}

function get_status()
{
    var http_stat = new XMLHttpRequest();
    var url = "getpendingservices";
    var params = "";
    http_stat.open("GET", url, true);

    //Send the proper header information along with the request
    //http_stat.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    //http_stat.setRequestHeader("Content-length", params.length);
    //http_stat.setRequestHeader("Connection", "close");

    //Call a function when the state changes.
    http_stat.onreadystatechange = function() {
        if(http_stat.readyState == 4 ) {
            if ( http_stat.status == 200 ) {
				var status = http_stat.responseXML.getElementsByTagName("result")[0].getElementsByTagName("status")[0].firstChild.nodeValue;
				if ( ! status.match('pending') ) {
					pend_status = 0;
				}
            } else {
                alert("There was a problem retrieving status:\n" + http_stat.statusText);
            }
        }
    }
    http_stat.send(params);
}

// Ajax call for Update Services
function update_services()
{
	var tale = getSelectValue("S1544_VOICE");
	var sms  = getSelectValue("S1544_SMS");
	var mms  = getSelectValue("S1544_MMS");
	var data = getSelectValue("S1544_GPRS");
	var ban  = document.getElementsByName('S1544_ban')[0].value;

	var http = new XMLHttpRequest();
	var url = "updateservices";
	var params = "&VOICE=" + tale + "&SMS=" + sms + "&MMS=" + mms + "&GPRS=" + data + "&ban=" + ban + "&action=update";
	http.open("POST", url, true);

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	//Call a function when the state changes.
	var status = 0;
	http.onreadystatechange = function() {
		if(http.readyState == 4 ) {
			if ( http.status != 200 ) {
				alert("There was a problem retrieving the data:\n" + http.statusText);
				return;
			}
		} 
	}
	http.send(params);

	post_submit();
}


