/**
 * This file will take care of Plans Page aka Step3 javascript stuff
 *
 * Written by Eddie.Barwani@comsys.com.au
 * Date: 20/11/2006
 * Client: K5
 */
function countryChange() {
		   
		 
		//Disable elements

		$("select#def_did_city").attr("disabled", "disabled");
		$("select#def_did_number").attr("disabled", "disabled");
		$("input[@name=def_did_add]").attr("disabled", "disabled");
		//show progress bar/icon
		$("img#def_city_progress_bar").show();
		var str = $("select#def_did_country").val();
		//Show how many free dids for each country
		var current_option = $("#def_did_country > option[@value="+ str +"]");
		var current_country_didcount = current_option.attr("didcount");
		if($("#def_did_country").siblings().is("span.select_info")){
			$("#def_did_country").siblings().remove("span.select_info");
		}
		//current_option.parent().after(
			//"<span class=\"select_info\"> "+current_country_didcount +" free dids "+"</span>"
		//);

        var selectedCountry = document.getElementById("def_did_country").options[document.getElementById("def_did_country").selectedIndex].value;
		
		//Load cities
		$.post("dids.php", { 
			countryCode : selectedCountry,
			dspCities   : "1"
		},function(data){
			//Remove any other cities
			$("select#def_did_city > option").each(function(i) {
				if(i) {//the first value = 0,, i.e. select city
					$(this).remove();
				}
			});
			//append new data
			$("select#def_did_city").append(data);
			$("img#def_city_progress_bar").hide();
			$("select#def_did_city").removeAttr("disabled");
			
			//change to the default select
  			document.getElementById('def_did_city').selectedIndex = 0;
  		});
	}
	 /////////////////////////
	var cityChange = function() { 
	
 $("select#def_did_number").attr("disabled", "disabled");
		$("input[@name=def_did_add]").attr("disabled", "disabled");
		//show progress bar
		$("img#def_did_progress_bar").show();
		//Load cities
		
		var selectedCity = document.getElementById("def_did_city").options[document.getElementById("def_did_city").selectedIndex].value;
		
		$.post("dids.php", { 
			countryCode : $("select#def_did_country").val(),
			cityID      : selectedCity,
			dspDIDs     : "1"
		},function(data){
			//Remove any other cities
			$("select#def_did_number > option").each(function(i) {
				if(i) {//the first value = 0,, i.e. select number
					$(this).remove();
				}
			});
			//append data
			//alert(data);
			$("select#def_did_number").append(data);
			$("img#def_did_progress_bar").hide();
			$("select#def_did_number").removeAttr("disabled");
			$("input[@name=def_did_add]").removeAttr("disabled");
			//change to the default select
  			document.getElementById('def_did_number').selectedIndex = 0;
  		});
}	

/////////////////////////////ADD DID button
var addDidButoon = function() { 
		var did = $("select#def_did_number").val().split("#"); //did#isLocal
		//alert("did: "+did[0]);
		var current_option = $("#def_did_country > option[@value="+ $("select#def_did_country").val() +"]");
		var current_country_didcount = current_option.attr("didcount");
		current_country_didcount =Number(current_country_didcount)+Number(2);  
		
		if(document.getElementById('didsss').rows.length!=current_country_didcount){
		if(document.getElementById(did[0]) == null &&
		   document.getElementById('def_did_number').selectedIndex != 0) {
			//Get needed variable to add
			var countryCode = $("select#def_did_country").val();
			var countryName = $("option[@value="+countryCode+"]").text();
			var cityCode    = $("select#def_did_city").val();
			var cityName    = $("option[@value="+cityCode+"]").text();
			var setupCost   = $("option[@value="+did[0]+"#"+did[1]+"]").attr("setupcost");
			var price       = $("option[@value="+did[0]+"#"+did[1]+"]").attr("price");
			//Show table if hidden
			//$("div#buttonS").append("style=style='position:relative'");
			$("table.def_selected_dids").show();
			$("#def_noDIDs").hide();
			//Append to the table of dids and make sure you add hidden variables
			
			$("table.def_selected_dids").append(
			   '<tr class="style72" style="position:relative" id="'+did[0]+'">' +
			   '<td>'+countryName+'</td>' +
			   '<td>'+cityName+'</td>' +
			   '<td>'+did[0]+'</td>' +
			   '<td>free</td>' +
			   '<td>free</td>' +
			   '<td><a href="#" id="remove_'+did[0]+'">Remove</a>' +
			   '<input type="hidden" name="countryCode['+did[0]+']" value="'+countryCode+'" />' +
			   '<input type="hidden" name="countryName['+did[0]+']" value="'+countryName+'" />' +
			   '<input type="hidden" name="cityCode['+did[0]+']" value="'+cityCode+'" />' +
			   '<input type="hidden" name="cityName['+did[0]+']" value="'+cityName+'" />' +
			   //'<input type="hidden" name="did[]" value="" />' +
			   '<input type="hidden" name="isLocal['+did[0]+']" value="'+did[1]+'" />' +
			   '<input type="hidden" name="setupCost['+did[0]+']" value="'+setupCost+'" />' +
			   '<input type="hidden" name="price['+did[0]+']" value="'+price+'" />' +
			   '<input type="hidden" name="isDefaultDID['+did[0]+']" value="1" />' +
			   '</td>' +
			   '</tr>'
			);}
			//Remove Button click function binding
			$('a#remove_'+did[0]).click(function() {
			   $("#"+$(this).id().split('_')[1]).remove();
			   
			   //$("tr#noDIDs"));
			   if($("table.def_selected_dids tr").size() < 3) { //3 # of raws
			   //alert($("table.selected_dids tr").size());
				  //$("#def_noDIDs").show();
				  $("table.def_selected_dids").hide();
			   }
			});
		}
	}
 ///////////////
 var checked = 0;
function sdid(){									
			if($("input#dsp_dids").attr('checked')&&checked==0) {
				
  				checked=1;
  				$("div#dids  div.signup_block_header").show('slow');
				$("div#dids  div.signup_block_body").show('slow');
				
			}
			else {
					checked=0;
				$("div#dids  div.signup_block_header").hide();
				$("div#dids  div.signup_block_body").hide();
			}
}
function channel(){			
			if($("input#dsp_channels").attr('checked')) {
			
				$.post("channels.php", {
					channels     : "1",
					planSelected : $("#plan_title").val()
					},function(data){
					//alert(data);
						$("div#channels").append(data);
  					}
  				);
			}
			else {
				
				$("div#channels  div.signup_block_header").remove();
				$("div#channels  div.signup_block_body").remove();
			}
		}

$(document).ready(function() {
	

			
	$("div#dids div.info").append('<center><p style="position:relative" class="style72" id="para">Would you like to purchase international/extra dids?'+ 
			'<input  onclick="sdid()" type="checkbox" checked="checked" id="dsp_dids" name="dsp_dids" /></p></center>' 
			);
	
	
	     var DefaultChannels = $("option[@value="+$("#plan_title").attr("value")+"]").attr('DefaultChannels');//attr("DefaultChannels");
		if( $("#plan_title").attr("value")==1||$("#plan_title").attr("value")==2){
				
		if($("#plan_title").attr("value")==1)
		$("div#channels div.info").append(
			 '<center><p class="style72" id="para">Basic Plan'+
			' Offers '+ DefaultChannels +' channels by default, would you like to purchase more? '+
	'&nbsp; <input onclick="channel()" style="position:relative" type="checkbox" id="dsp_channels" name="dsp_channels" /></p></center>'
		);		
		else
		$("div#channels div.info").append('<center><p class="style72" id="para">Standard Plan'+
			' Offers '+ DefaultChannels +' channels by default, would you like to purchase more? '+
	'&nbsp; <input onclick="channel()" style="position:relative" type="checkbox" id="dsp_channels" name="dsp_channels" /></p></center>'
		);
		}
	
	
	
	if($("td#data").html()=='[def_dids.countryname.value;block=tr;magnet=tr;noerr]')
	{
		$("tr#rowD").remove();	
	}
	
	/**
	 * Plans Select Events
	 */ 
	
	
	 
	$("#plan_title").change(function() {
		//alert($(this).val());
		//Make sure all other plan details are hidden
		var selected= $("#plan_title").attr("value");
		//document.write(selected);
		//if(selected0)
		
		//var selectedOption = $("option#hello").attr("value");
		if($(this).val()==1||$(this).val()==2){
		//var allowedDids = $("option#hello").attr("value");
				
		$("div#channelDefault").remove();//hide irrelevant information defore loading....
		$("label#channels").remove();
		$("div#default").remove();
		$("p#para").hide(); 	
		checked=0;
		//document.write($(this).val());
		
		
		$("div.Registration_fee").hide();
		$("div.description").hide();
		//Show only selected plan
		$("#reg_fee_"+$(this).val()).show("slow");
		$("#desc_"+$(this).val()).show("slow");	
		if($(this).val()==1)
				{	
				$("div#default_dids").hide();
				$("table#did_text").hide();
				$("table#didsss").hide();
				$("table#didssss").hide();
				}
		
		//$("input#dsp_dids").show();
		
		$("h3").hide();
		
		
		
		
		}
		else 
		{
				$("table#did_text").hide();
				$("table#didsss").hide();
				$("table#didssss").hide();
			//if nothing selected remove everything from display screen....	
				$("div#channelDefault").remove();
				$("label#channels").remove();
				$("div#default").remove();
				
				$("input#dsp_dids").hide();
				$("p#para").remove();
				$("h3").hide();
				//hide all kind of plan information if no plan selected...
				$("#reg_fee_1").hide();
				$("#desc_1").hide();
				$("#reg_fee_2").hide();
				$("#desc_2").hide();
		}
		//alert($("option[@value="+$(this).val()+"]").attr('DefaultChannels'));
		//alert($("option[@value="+$(this).val()+"]").attr('allowedDids'));
		//var selectedOption = $("option#hell").attr("value");
		var allowedDids = $("option[@value="+$(this).val()+"]").attr('allowedDids');
					
		//Default DIDs Display
		if(allowedDids>0) {//document.write(allowedDids);  //loading of standard plan...
			
			/*$("table#dids").show();
			$("h3").show();
			$("div#default_dids div.info").append(
 				    
					   ' <p id="para">Standard Plan <br> Offers ' + allowedDids + ' DIDs in the following countries.</p>' 
					);*/
			$.post("dids.php", {
				
				dspDefaultDIDs : "1",
				plan_id        : $(this).val()
				},function(data){
					
					if(document.getElementById('didsss'))
					{
					
						$("table#did_text").show();
						$("table#didssss").show();
						if(document.getElementById('didsss').rows.length>2)
						$("table#didsss").show();
					}
					else
					{
					$("div#default_dids div.info").empty();
					$("div#default_dids div.info").append($("#plan_title option[@value="+$(this).val()+"]").text() + 
					   '<p class="style72" id="para">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Standard Plan Offers ' + allowedDids + ' DIDs in the following 					                        countries.</p>' + data);
					}
					
					//$("table.def_selected_dids").show();
					
					/*if(document.getElementById("didsss").rows.length>2){
						for(var count = 2;count<=$("table.def_selected_dids tr").size();i++)
						{
						alert($("tr#show").html());
						alert($("table.def_selected_dids tr").size());
						
						
						}
					}*/
	  			}
	  		);
			
			/*if($("table.def_selected_dids tr").size()>2)
			{
				$("table#didsss").show();	
			}*/
		}
		else if($(this).val()!=2){	
			
			$("table#dids").hide();
			
		}
		
		//Channels Display

		//$("div#formContent").append('<div id="channels"></div>');
		//var selectedOption = $("option#hello").attr("value");
		//if(selectedOption>0){
//loading of basic plan			
		
		var DefaultChannels = $("option[@value="+$(this).val()+"]").attr('DefaultChannels');//attr("DefaultChannels");
		if($(this).val()==1||$(this).val()==2){
		/*$("div#channels div.info").append(
			$( "#plan_title option[@value="+ $(this).val()+"]").text() +
			'Offers '+ DefaultChannels +' channels by default, would you like to purchase more? '+
			'<input type="checkbox" id="dsp_channels" name="dsp_channels" /><br />'
		);*/		
		if($(this).val()==1 )
		$("div#channels div.info").append(
			 '<p class="style72" id="para">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Basic Plan'+
			' Offers '+ DefaultChannels +' channels by default, would you like to purchase more? '+
		'&nbsp; <input onclick="channel()" style="position:relative" type="checkbox" id="dsp_channels" name="dsp_channels" /></p>'
		);		
		else
		$("div#channels div.info").append(
			 '<p class="style72" id="para">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Standard Plan'+
			' Offers '+ DefaultChannels +' channels by default, would you like to purchase more? '+
		'&nbsp; <input onclick="channel()" style="position:relative" type="checkbox" id="dsp_channels" name="dsp_channels" /></p>'
		);
		}
		//}
		//else()
		/**
		 * Check box clicked for additional channels
		 */
		
		/*
		$("input#dsp_channels").click(function () {
			if($(this).attr('checked')) {
			
				$.post("channels.php", {
					channels     : "1",
					planSelected : $("#plan_title").val()
					},function(data){
					//alert(data);
						$("div#channels").append(data);
  					}
  				);
			}
			else {
				
				$("div#channels  div.signup_block_header").remove();
				$("div#channels  div.signup_block_body").remove();
			}
		});*/


		//Internation DIDs Display
		//Check if the dids div exist
	//if($(this.val()==1)){
		if($(this).val()==1||$(this).val()==2)
		{
	
   $("div#dids div.info").append('<p  class="style72" id="para">'+
			 'Would you like to purchase international/extra dids?'+ 
			'<input onclick="sdid()" type="checkbox" id="dsp_dids" name="dsp_dids" /></p>' 
			);
	}
		$("div#dids").show('slow');
		$("div#dids  div.signup_block_header").hide();
		$("div#dids  div.signup_block_body").hide();
		
	});
});