function redirect(param){
	var elem_package = document.getElementsByName('Package'),
        package = '',
        elem = document.getElementsByName('plantype'),
        plantype = '';
	
	

	for(i = 0 ; i<= elem_package.length-1; i++)
	{
		//alert(elem[i].checked);
		if(elem_package[i].checked == true)
		{
			package = elem_package[i].value;
			 break;
		}
	}

	for(i = 0 ; i<= elem.length-1; i++)
	{
		//alert(elem[i].checked);
		if(elem[i].checked == true)
		{
			plantype = elem[i].value;
			break;
		}
	}
	
	

	if(package=='308N_1' || package=='308N')
	{
		package='';
	}

	if (package=='HOMEPHONE' || param=='HOMEPHONE'){
		//document.location.href="https://cyberstore.tpg.com.au/register/homephone/index.php?Package=" + package+"&plantype="+plantype+"&unlilocal="+elem_unlilocal+"&superchat="+elem_superchat;
		var elem_unlilocal = document.getElementById('unlilocal').checked
		if(elem_unlilocal){elem_unlilocal = 1};
		var elem_superchat = document.getElementById('superchat').checked
		if(elem_superchat){elem_superchat = 1};
		document.location.href="https://cyberstore.tpg.com.au/register/homephone/index.php?Package=" + package+"&plantype="+plantype+"&unlilocal="+elem_unlilocal+"&superchat="+elem_superchat;
	}
	else{
		document.location.href="https://cyberstore.tpg.com.au/register/adsl.php?Package="+package+"&plantype="+plantype;
	}
	//return false;
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autotab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode;
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		var formindex = (getIndex(input)+1) % input.form.length;
		input.form.elements[formindex].focus();
	}
}

function onlyNumbers(e)
{
	var keynum;
	var keychar;
	var numcheck;

	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}

	if(e.keyCode == 8 || e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39  || e.keyCode == 40 || e.keyCode == 46 || e.keyCode == 9 || e.keyCode == 116 )
	{
		return true;
	}
	else
	{
		keychar = String.fromCharCode(keynum);
	}
	numcheck = /\d/;
	return numcheck.test(keychar);
} // function onlyNumbers(e)

function containsElement(arr, ele) {
	var found = false, index = 0;
	while(!found && index < arr.length)
	if(arr[index] == ele)
		found = true;
	else
		index++;
	return found;
}

function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
	if (input.form[i] == input)index = i;
	else i++;
	return index;
}

function compute(){
	var homephone = 0;
	var product = 0;
	var pack = 0;

	if ($('#homephone').is(':checked') == false || parseInt($('#extoffnet').val()) == 0){
		homephone = 0;
	}else{
        homephone = 30;
        //Check for local unlimited
        if($("#unlilocal").is(":checked")){
            homephone = 30 + 5;
        }
        if($("#superchat").is(":checked")){
            homephone = 30 + 10;
        }
	}

	if ($('#heavy').is(':checked') == true){
		product = 29.99;
	}
	if ($('#medium').is(':checked') == true){
		product = 14.99;
	}
	if ($('#capsaver').is(':checked') == true){
		product = 14.99;
	}

	if ($('#adsl10gb').is(':checked') == true){
		pack = 9.99;
	}
	else if ($('#adsl100gb').is(':checked') == true){
		pack = 19.99;
	}
	else if ($('#adslunli').is(':checked') == true){
		pack = 29.99;
	}
	else if ($('#adsl500gb').is(':checked') == true){
		pack = 29.99;
	}
	else if ($('#adsl15gb').is(':checked') == true){
		pack = 29.99;
	}
	else if ($('#adsl60gb').is(':checked') == true){
		pack = 39.99;
	}
	else if ($('#adsl200gb').is(':checked') == true){
		pack = 49.99;
	}
	else if ($('#adsl500gb_2').is(':checked') == true){
		pack = 59.99;
	}
	else if ($('#adsl250gb').is(':checked') == true){
		pack = 69.99;
	}
	else if ($('#adslunli_2').is(':checked') == true){
		pack = 59.99;
	}
	else if ($('#adslvoip').is(':checked') == true){
		pack = 49.99;
	}
	//Broadband Off-net
	else if ($('#ext100gb').is(':checked') == true){
		pack = 39.99;
	}
	else if ($('#ext8mb50gb').is(':checked') == true){
		pack = 49.99;
	}		
	else if ($('#ext8mb200gb').is(':checked') == true){
		pack = 59.99;
	}			
	else if ($('#extunli').is(':checked') == true){
		pack = 69.99;
	}
	else if ($('#ext50gb').is(':checked') == true){
		pack = 49.99;
	}
	else if ($('#ext200gb').is(':checked') == true){
		pack = 59.99;
	}	
	else if ($('#ext300gb').is(':checked') == true){
		pack = 79.99;
	}
	else if ($('#ext500gb').is(':checked') == true){
		pack = 99.99;
	}
	else if ($('#ext2unli').is(':checked') == true){
		pack = 79.99;
	}
	else if ($('#extvoip').is(':checked') == true){
		pack = 59.99;
	}
	else if ($('#extvoipunli').is(':checked') == true){
		pack = 69.99;
	}

	total = homephone + pack + product;
	$('#calculator').html("<div class='total_text'><strong>Total Monthly Charge: $<span id='total'>"+total.toFixed(2)+"</span></strong></div><br />");
}

function reload(){
	var homephone = $('#homephone').is(':checked');

	if (homephone == true){
		window.location = "/products_services/adsl2plus_pricing.php?/pricing/homephone";
	}
	else{
		window.location = "/products_services/adsl2plus_pricing.php?/pricing/adsl2plus";
	}
}

function showMobileInfo(obj_contract_term,plan_id, selected,unlilocal){
    var obj_plan_type = {1:'capsaver',50:'medium',51:'heavy'},
        with_mobile = '';
    	    	
    	var superchat = $('#superchat').is(':checked'); 
    		
        if(unlilocal && selected){
            with_mobile = plan_id + '_unli_local';
        }
        
        if(superchat && selected){
            with_mobile = plan_id + '_superchat';
        }
                        
        $.each(obj_plan_type,function(index, value){
            $('#' + value).attr('checked','');
        })

        // contract term coding
        $.each(obj_contract_term,function(index,value){
            $('#' + value).hide();
        });

        if(selected){
            $('#' + obj_plan_type[plan_id]).attr('checked','checked');                  
            $('#mobile').css("display","block");
            $('#mobile_disclaimer').css('display','block');

            if(with_mobile.length > 0){                
                $('#' + obj_contract_term[with_mobile]).css("display","block");
            } else {                
                $('#' + obj_contract_term[plan_id]).css("display","block");
            }

        } else {
			$('#mobile').fadeOut('slow');
			$('#mobile_disclaimer').fadeOut('slow');

			// contract term coding
			if (superchat){				
				$('#superchat_ct').css("display","block");
			}
			else if(unlilocal){                
                $('#unli_local_ct').css("display","block");
            } else {                
                $('#nomobile_ct').css("display","block");
            }
        }
}

$(document).ready(function() {
	var homephone = $('#homephone').is(':checked'),
		extoffnet = $('#extoffnet').val() == 0,
        obj_contract_term = {'def':'nomobile_ct','loc_unli':'unli_local_ct','51':'heavy_ct','50':'medium_ct','1':'capsaver_ct',
        '51_unli_local':'heavy_unli_local_ct','50_unli_local':'medium_unli_local_ct','1_unli_local':'capsaver_unli_local_ct',
        'superchat':'superchat_ct','50_superchat':'50_superchat_ct','1_superchat':'1_superchat_ct', '51_superchat':'51_superchat_ct'
        },
        with_unli_local = false,
        with_superchat = false,
        unli_local_with_mobile = false,
        mobile_plan = 0;
	
	if (homephone == true || extoffnet == true){
		$('#medium').attr('checked','');
		$('#heavy').attr('checked','');
		$('#capsaver').attr('checked','');
		compute();
	}
	else{
		$('#capsaver').attr('checked','');
		compute();
	}
	
    $('#unlilocal').change(function(){
        unli_local_with_mobile = $("input[name='plantype']:checked").length > 0;
        with_unli_local = $('#unlilocal').is(':checked');
				//alert("with_unli_local "+with_unli_local + "\nunli_local_with_mobile "+unli_local_with_mobile);

        if(unli_local_with_mobile){
            mobile_plan = $("input[name='plantype']:checked").val();
        }

        $.each(obj_contract_term,function(index,value){
            $('#' + value).hide();
        });
        
        if (with_unli_local){
        	$('#superchat').attr("checked", false);
        	$('#superchat_disclaimer').fadeOut(1000);
    	}	
        
        if ( with_unli_local && unli_local_with_mobile === false){                
            $('#unli_local_ct').css("display","block");
        }
		else{			
            if ( with_unli_local && unli_local_with_mobile ){							
                $('#'+ obj_contract_term[mobile_plan + '_unli_local']).css("display","block");
            }
            else
			{
                if ( with_unli_local === false && unli_local_with_mobile ){
                    $('#'+ obj_contract_term[mobile_plan]).css("display","block");
                } else {
                    $('#'+ obj_contract_term.def).css("display","block");
                }
			}
		}
        compute();        
    });     
    
    $('#superchat').change(function(){    	    	
    	superchat_with_mobile = $("input[name='plantype']:checked").length > 0;
    	with_superchat = $('#superchat').is(':checked');
    	mobile_plan = $("input[name='plantype']:checked").val();
    	    	
    	
    	$.each(obj_contract_term,function(index,value){
            $('#' + value).hide();
        });
    	
    	if(this.checked){
    		$('#unlilocal').attr('checked', false);    		    		     		   		 	        	      
	        $('#superchat_disclaimer').css("display","block");
	        
	        if(superchat_with_mobile){   		 		   		 	
   		 		$('#'+ obj_contract_term[mobile_plan + '_superchat']).css("display","block");
    	    }	        
	        else{	        
	        	$('#superchat_ct').css("display","block");
	        }
    	}   
    	else{
    		$('#superchat_disclaimer').fadeOut(1000);
    		
    		if (superchat_with_mobile ){    		
    			$('#'+ obj_contract_term[mobile_plan]).css("display", "block");
    		} 
    		else {    		
    			$('#'+ obj_contract_term.def).css("display", "block");
    		}    		
    	}
    	compute();
    })

	$('#homephone').change(function(){
		$('#calculator').html("");
		reload();
	});

    $('#heavy').change(function(){
        showMobileInfo(obj_contract_term,51,this.checked,with_unli_local);
        compute();
    })
    $('#medium').change(function(){
        showMobileInfo(obj_contract_term,50,this.checked,with_unli_local);
        compute();
    })
    $('#capsaver').change(function(){
        if (homephone == true || extoffnet == true){
            showMobileInfo(obj_contract_term,1,this.checked,with_unli_local);
        } else {
            if(this.checked){
				$('#mobile').fadeIn('slow');
				$('#mobile_disclaimer').fadeIn('slow');

                // contract term coding
				$('#adsl2plus_ct').fadeOut(1000,function(){
					$('#adsl2plus_mobile_ct').fadeIn(1000);
				});
            } else {
				$('#mobile').fadeOut('slow');
				$('#mobile_disclaimer').fadeOut('slow');

				// contract term coding
				$('#adsl2plus_mobile_ct').fadeOut(1000,function(){
					$('#adsl2plus_ct').fadeIn(1000);
				});
            }
        }
        compute();
    })
        
	$("table").delegate("input[name='Package']input:radio", "click", compute);        
    
    $('#adslunli').click(function(){
    	superchat();    	    
    	$("#superchat_div").css('display','block'); 	
    	$("#linerental_div").css('padding-bottom','5px');
    	$("#unlimited_div").css('padding-top','5px');
    });
    
    $('#adsl500gb').click(function(){superchat();});
    $('#adsl100gb').click(function(){superchat();});
    $('#adsl10gb').click(function(){superchat();});
    
    var with_superchat = $('#superchat').is(':checked');	
	if (with_superchat){		
		$('#superchat').change();
	}
    
    function superchat(){
    	with_mobile = $("input[name='plantype']:checked").length > 0;    	
    	mobile_plan = $("input[name='plantype']:checked").val();
    	
    	$("#superchat_div").css('display','none'); 	
        $("#linerental_div").css('padding-bottom','25px'); 	
        $("#unlimited_div").css('padding-top','25px');    	
    	
    	$("#superchat_disclaimer").css("display","none")
    	$("#superchat").attr('checked', false);

        
    	$.each(obj_contract_term,function(index,value){
            $('#' + value).hide();
        });
    	
    	if (with_mobile ){			    		
			$('#'+ obj_contract_term[mobile_plan]).css("display", "block");
		} 
		else {    					
			$('#'+ obj_contract_term.def).css("display","block");
		}    
    	    	    	    	       	    
    	compute();    	
    }
});
