/*
 * DID selection Form JavaScript
 *
 * Written by Eddie Barwani @ COMSYS
 * Date:   28/11/2006
 * Client: K5
 */

$(document).ready(function() {
	var countryChange = function(jq_object) {
		//Disable elements
		//alert($("select#did_city").html());
		$("select#did_city").attr("disabled", "disabled");
		$("select#did_number").attr("disabled", "disabled");
		$("input[@name=did_purchase]").attr("disabled", "disabled");
		//show progress bar/icon
		$("img#city_progress_bar").show();

		//Load cities
		$.post("dids.php", { 
			countryCode : jq_object.val(),
			dspCities   : "1"
		},function(data){
			//Remove any other cities
			$("select#did_city > option").each(function(i) {
				if(i) {//the first value = 0,, i.e. select city
					$(this).remove();
				}
			});
			//append new data
			$("select#did_city").append(data);
			$("img#city_progress_bar").hide();
			$("select#did_city").removeAttr("disabled");
			//change to the default select
  			document.getElementById('did_city').selectedIndex = 0;
  		});
	}
	/**
	 * Country Combo Box change
	 *
	 * Query the server through AJAX to get the list of cities
	 */
	$("select#did_country").change(function() {
		countryChange($(this));
		/*
		//Disable elements
		$("select#did_city").attr("disabled", "disabled");
		$("select#did_number").attr("disabled", "disabled");
		$("input[@name=did_purchase]").attr("disabled", "disabled");
		//show progress bar/icon
		$("img#city_progress_bar").show();

		//Load cities
		$.post("dids.php", { 
			countryCode : $(this).val(),
			dspCities   : "1"
		},function(data){
			//Remove any other cities
			$("select#did_city > option[@value]").each(function(i) {
				if(i) {//the first value = 0,, i.e. select city
					$(this).remove();
				}
			});
			//append new data
			$("select#did_city").append(data);
			$("img#city_progress_bar").hide();
			$("select#did_city").removeAttr("disabled");
			//change to the default select
  			document.getElementById('did_city').selectedIndex = 0;
  		});*/
	});
	/*
	 * City Combo Box Change
	 *
	 * Query the server for list of dids
	 */
	$("select#did_city").change(function() {
		//Disable elements
		$("select#did_number").attr("disabled", "disabled");
		$("input[@name=did_add]").attr("disabled", "disabled");
		//show progress bar
		$("img#did_progress_bar").show();
		//Load cities
		$.post("dids.php", { 
			countryCode : $("select#did_country").val(),
			cityID      : $(this).val(),
			dspDIDs     : "1"
		},function(data){
			//Remove any other cities
			$("select#did_number > option").each(function(i) {
				if(i) {//the first value = 0,, i.e. select number
					$(this).remove();
				}
			});
			//append data
			$("select#did_number").append(data);
			$("img#did_progress_bar").hide();
			$("select#did_number").removeAttr("disabled");
			$("input[@name=did_add]").removeAttr("disabled");
			//change to the default select
  			document.getElementById('did_number').selectedIndex = 0;
  		});
	});
	/*
	 * Add Did button click event
	 *
	 * Add did to the table,, and appropriate hidden fields
	 */
	$("input[@name=did_add]").click(function() {
		/*
		 * Follwing will show different variables.
		 *  
		   alert("Country Code: "+$("select#did_country").val());
		   alert("Country Name: "+$("option[@value="+$("select#did_country").val()+"]").text());
		   alert("City Code: "+$("select#did_city").val());
		   alert("City Name: "+$("option[@value="+$("select#did_city").val()+"]").text());
		   alert("Phone did: "+$("select#did_number").val());
		   alert("Setup Cost: "+$("option[@value="+$("select#did_number").val()+"]").attr("setupcost"));
		   alert("Price: "+$("option[@value="+$("select#did_number").val()+"]").attr("price"));
		 *
		*/
		
		var did = $("select#did_number").val().split("#"); //did#isLocal
		//alert("did: "+did[0]);
		if(document.getElementById(did[0]) == null &&
		   document.getElementById('did_number').selectedIndex != 0) {
			//Get needed variable to add
			var countryCode = $("select#did_country").val();
			var countryName = $("option[@value="+countryCode+"]").text();
			var cityCode    = $("select#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
			$("table.selected_dids").show();
			$("#noDIDs").hide();
			//Append to the table of dids and make sure you add hidden variables
			$("table.selected_dids").append(
			   '<tr class="style72" id="'+did[0]+'">' +
			   '<td>'+countryName+'</td>' +
			   '<td>'+cityName+'</td>' +
			   '<td>'+did[0]+'</td>' +
			   '<td>'+setupCost+'</td>' +
			   '<td>'+price+'</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="0" />' +
			   '</td>' +
			   '</tr>'
			);
			//Remove Button click function binding
			$('a#remove_'+did[0]).click(function() {
			   $("#"+$(this).id().split('_')[1]).remove();
			   //$("tr#noDIDs"));
			   if($("table.selected_dids tr").size() < 3) { //3 # of raws
			      //alert($("table.selected_dids tr").size());
				 $("table.selected_dids").hide();// $("#noDIDs").show();
			   }
			});
		}
	});
	/*
	 * Remove Did from table, these will be dids filled by php not JavaScript
	 */
	$("a.remove_did").click(function () {
		//alert($(this).parent().parent().html());
		//removing the row, which has the link
		$(this).parent().parent().remove();
		if($("table.selected_dids tr").size() < 3) { //3 # of raws
			      //alert($("table.selected_dids tr").size());
				  $("table.selected_dids").hide();//$("#noDIDs").show();
		}
	});
	$("area").click(function () {
		var countryCode =$(this).attr("countryCode");
		$("option[@value="+countryCode+"]").attr("id",countryCode);
		document.getElementById('did_country').selectedIndex = document.getElementById(countryCode).index;
		
		//$("select#did_country").trigger("change");
		countryChange($(document.getElementById('did_country')));
	});
});