/**
* Ringtones US Leaf javascript functions
*/
var RingonesUs = function(handlerObject) {
	this.COOKIE_CARRIER = 'carrier';
	this.cookie_options = { path: '/', expires: 10 };

	this._constructor = function(handlerObject){
		this.handler = handlerObject;
	}

	this.setCarrierCookie = function(carrierID, leafID) {
		var nameCookie = this.COOKIE_CARRIER+leafID;
		g = escape(carrierID);
		$.cookie(nameCookie, g, this.cookie_options);
		return true;
	};

	this.getCarrierCookie = function(leafID) {
		var nameCookie = this.COOKIE_CARRIER+leafID;
		var carrierID = $.cookie(nameCookie);
		if ( carrierID ) {
			return carrierID;
		}
		return false;
	};

	this.isErrorInUrl = function(errorname) {
		var error_msisdn = jQuery.url.param(errorname);
		if (error_msisdn==1) {
			return true;
		} else {
			return false;
		}
	};

	this.updateStep = function(num) {
		var nameClass = "step_all";
		var nameId;
		var setId = false;
		if ($("."+nameClass).length>0) {
			$("."+nameClass).hide();
			$("."+nameClass).each(function(index) {
				nameId = $(this).attr('id');
				var myregexp = new RegExp('(([a-z0-9_]*'+num+'$)|([a-z0-9_]*'+num+'[^0-9]))',"i");
				if ( nameId.match(myregexp) ) {
					$(this).show();
					setId = true;
				}
			});
			if (!setId) {
				$("#step_other").show();
			}
		}
		return true;
	};

	this.updatePrice = function(carrierID) {
		var nameClass = "price_all";
		var nameId;
		var setId = false;
		
		if ($("."+nameClass).length>0 && carrierID!=0) {
			$("."+nameClass).hide();
			if(carrierID!=0){
				$("."+nameClass).each(function(index) {
					nameId = $(this).attr('id');
					var myregexp = new RegExp('(([a-z0-9_]*'+carrierID+'$)|([a-z0-9_]*'+carrierID+'[^0-9]))',"i");
					if ( nameId.match(myregexp) ) {
						$(this).show();
						setId = true;
					}
				});
				if (!setId) {
					$("#price_other").show();
				}
			}
		}
	};

	this.updateBanner = function(carrierID) {
		var nameClass = "banner_all";
		var nameId;
		var setId = false;
		
		if ($("."+nameClass).length>0) {
			$("."+nameClass).hide();
			if(carrierID!=0){
				$("."+nameClass).each(function(index) {
					nameId = $(this).attr('id');
					var myregexp = new RegExp('(([a-z0-9_]*'+carrierID+'$)|([a-z0-9_]*'+carrierID+'[^0-9]))',"i");
					if ( nameId.match(myregexp) ) {
						$(this).show();
						setId = true;
					}
				});

				
				if (!setId) {
					$("#banner_other").show();
				}
			}
		}
	};

	this.updateDisclaimer = function(carrierID) {
		var nameClass = "disclaimer_all";
		var nameId;
		var setId = false;

		if ($("."+nameClass).length>0) {
			$("."+nameClass).hide();
			$("."+nameClass).each(function(index) {
				nameId = $(this).attr('id');
				var myregexp = new RegExp('(([a-z0-9_]*'+carrierID+'$)|([a-z0-9_]*'+carrierID+'[^0-9]))',"i");
				if ( nameId.match(myregexp) ) {
					$(this).show();
					setId = true;
				} else {
					$(this).empty();
				}
			});

			if (!setId) {
				$("#disclaimer_other").show();
			}
		}
	};
	
	this.updateText = function(carrierID) {
		var nameClass = "cellnumber1_all";
		var nameId;
		var setId = false;

		if ($("."+nameClass).length>0) {
			$("."+nameClass).hide();
			if(carrierID!=0){
				$("."+nameClass).each(function(index) {
					nameId = $(this).attr('id');
					var myregexp = new RegExp('(([a-z0-9_]*'+carrierID+'$)|([a-z0-9_]*'+carrierID+'[^0-9]))',"i");
					if ( nameId.match(myregexp) ) {
						$(this).show();
						setId = true;
					}
				});
				if (!setId) {
					$("#cellnumber1_other").show();
				}
			}
		}
	};

	this.updateEnterpin = function(carrierID) {
		var nameClass = "enterpin_all";
		var nameId;
		var setId = false;

		if ($("."+nameClass).length>0) {
			$("."+nameClass).hide();
			$("."+nameClass).each(function(index) {
				nameId = $(this).attr('id');
				var myregexp = new RegExp('(([a-z0-9_]*'+carrierID+'$)|([a-z0-9_]*'+carrierID+'[^0-9]))',"i");
				if ( nameId.match(myregexp) ) {
					$(this).show();
					setId = true;
				}
			});
			if (!setId) {
				$("#enterpin_other").show();
			}
		}
	};

	this.updateCarrierList = function(carrierID) {
		var nameClass = "carrier_list_all";
		var nameId;
		var setId = false;

		if ($("."+nameClass).length>0) {
			$("."+nameClass).hide();
			if(carrierID!=0){
				$("."+nameClass).each(function(index) {
					nameId = $(this).attr('id');
					var myregexp = new RegExp('(([a-z0-9_]*'+carrierID+'$)|([a-z0-9_]*'+carrierID+'[^0-9]))',"i");
					if ( nameId.match(myregexp) ) {
						$(this).show();
						setId = true;
					}
				});
				if (!setId) {
					$("#carrier_list_other").show();
				}
			}
		}
	};

	this.updateFooter = function(carrierID, isPrelanding) {
		var nameClass = "footer_all";
		var nameId;
		var setId = false;

		if ($("."+nameClass).length>0) {
			$("."+nameClass).hide();
			if (isPrelanding) {
				$("#footer_jump").show();
			} else {
				$("."+nameClass).each(function(index) {
					nameId = $(this).attr('id');
					var myregexp = new RegExp('(([a-z0-9_]*'+carrierID+'$)|([a-z0-9_]*'+carrierID+'[^0-9]))',"i");
					if ( nameId.match(myregexp) ) {
						$(this).show();
						setId = true;
					}
				});
				if (!setId) {
					$("#footer_other").show();
				}
			}
		}
	};

	this.updateInputCarrier = function(carrierID) {
		$('#carrier_id').attr("value", carrierID);
	}

	this.updateRPC = function(carrierID) {
		var nameClass = "rpc_all";
		var nameId;
		var setId = false;

		if ($("."+nameClass).length>0) {
			$("."+nameClass).hide();
			$("."+nameClass).each(function(index) {
				nameId = $(this).attr('id');
				var myregexp = new RegExp('(([a-z0-9_]*'+carrierID+'$)|([a-z0-9_]*'+carrierID+'[^0-9]))',"i");
				if ( nameId.match(myregexp) ) {
					$(this).show();
					setId = true;
				} else {
					$(this).empty();
				}
			});
			if (!setId) {
				$("#rpc_other").show();
			}
		}
	};

	
	this.updateInfoCarrier = function(data) {
		
		if(data.carrier_id_be==0){
			$("#error_phone").show();
			$("#preload_operator").hide();
		}
		else
		{
			$("#error_phone").hide();
			$("#preload_operator").attr("src",data.logo_src);
			$("#preload_operator").show();
			
			this.updateStep("2");
		}
		
		this.updateBanner(data.carrier_id_be);
		this.updatePrice(data.carrier_id_be);
		this.updateText(data.carrier_id_be);
		this.updateCarrierList(data.carrier_id_be);
		this.updateFooter(data.carrier_id_be, false);
		this.updateInputCarrier(data.carrier_id_be);
		if (this.handler.leafID) {
			this.setCarrierCookie(data.carrier_id_be, this.handler.leafID);
		}
	}
	
	this.loadInfoCarrier = function(current_number){
		var tmpthis = this;
		    
		$.ajax({
		    type: "GET",
		    url: "/getOperator.php?phone_number="+current_number,
		    success: function(data){
			    tmpthis.updateInfoCarrier(data);
		    },
		    dataType:"json"
		});
		
	}
	
	this.redirectToPhoneChoice= function(carrierID, listPhoneSelection,url, delay){
		var tmpthis = this;
		
		var a_listPhoneSelection = listPhoneSelection.split(",");
		found=0;
		
		for (key in a_listPhoneSelection) {
			if (a_listPhoneSelection[key] === carrierID) {found=1;}
		}
		
		if(found){
			setTimeout("window.location = '"+url+"'", delay);
		}
	}
	
	this.workPage = function() {
	 	var tmpthis = this;

	 	if (this.isErrorInUrl("carrier_mismatch")) {
	 		//landing with error carrier_mismatch
			$("#insertCell").show();
			$("#carrierSelect").hide();
			if (this.handler.leafID) {
				var carrierID = this.getCarrierCookie(this.handler.leafID);
				this.updatePrice(carrierID);
				this.updateBanner(carrierID);
				this.updateCarrierList(carrierID);
				this.updateFooter(carrierID, false);
				this.updateStep("2");
			}
			$("#refresh").bind("click", function() {
				history.back();
			});

	 	} else if (this.isErrorInUrl("error_msisdn")) {
			//landing with error
			$("#insertCell").show();
			$("#carrierSelect").hide();
			if (this.handler.leafID) {
				var carrierID = this.getCarrierCookie(this.handler.leafID);
				this.updatePrice(carrierID);
				this.updateBanner(carrierID);
				this.updateCarrierList(carrierID);
				this.updateFooter(carrierID, false);
				this.updateStep("2");
			}
		} else {
			//prelanding
			$("#carrierSelect").hide();
			$("#insertCell").show();
			$("#jump_footer").hide();
			this.updateFooter(carrierID, true);
			this.updateStep("1");

			$(".availableCarriers").bind("click", function() {
				//landing
				var carrierArray = ($(this).attr("id")).split("_");
				var carrierID = carrierArray[1];
				$("#insertCell").show("slow");
				$("#carrierSelect").hide("slow");
				$("#step1").show();
				tmpthis.updateStep("2");
				tmpthis.updateBanner(carrierID);
				tmpthis.updatePrice(carrierID);
				tmpthis.updateText(carrierID);
				tmpthis.updateCarrierList(carrierID);
				tmpthis.updateFooter(carrierID, false);
				tmpthis.updateInputCarrier(carrierID);
				if (tmpthis.handler.leafID) {
					tmpthis.setCarrierCookie(carrierID, tmpthis.handler.leafID);
				}
			});
		}
	};

	this.confirmPage = function() {
		if (this.handler.confirmID!="") {
			var carrierID = this.getCarrierCookie(this.handler.confirmID);
			this.updateRPC(carrierID);
			this.updateFooter(carrierID, false);
			this.updateBanner(carrierID);
			this.updateDisclaimer(carrierID);
			this.updateEnterpin(carrierID);
			this.updateCarrierList(carrierID);
		}
	};

	this.thankyouPage = function() {
		if (this.handler.confirmID) {
			var carrierID = this.getCarrierCookie(this.handler.confirmID);
			this.updateFooter(carrierID, false);
			this.updateBanner(carrierID);
			this.updateCarrierList(carrierID);
			if (this.handler.listPhoneSelection!=null && this.handler.urlPhoneSelection!=null && this.handler.delayPhoneSelection!=null)
			{
				this.redirectToPhoneChoice(carrierID,this.handler.listPhoneSelection,this.handler.urlPhoneSelection,this.handler.delayPhoneSelection);
			}
		}
	};

	this._constructor(handlerObject);
}

