// SignUp Selector

function ccnconf(countryId) {
  
	var vfull = document.getElementById(countryId).value;
	var vCountryCodeUS = document.getElementById('countryCodeUS').value;
	if (vfull == vCountryCodeUS)
		{
			document.getElementById('state').style.display='block';
			document.getElementById('province').style.display='none';
		}  
		else
		{
			document.getElementById('state').style.display='none';
			document.getElementById('province').style.display='block';
		}  
	
}

function selectPaymentMethod(radioButton) {
	
	var pamentMethod = radioButton.value;

    if (pamentMethod == '1') {
        document.getElementById('payment_method_cc').style.display = 'block';
        document.getElementById('payment_method_paypal').style.display = 'none';

	}
	if (pamentMethod == '2') {
        document.getElementById('payment_method_cc').style.display = 'none';
        document.getElementById('payment_method_paypal').style.display = 'block';

	}  
}

function refill() {
  
	var fill = document.getElementById('inventoryItemCallingCardAutorecharge.automaticRechargeEnabled.checkbox').checked;
	if (fill)
		{
			document.getElementById('refill-level').style.display='block';
			document.getElementById('refill-amount').style.display='block';
			document.getElementById('inventoryItemCallingCardAutorecharge.automaticRechargeEnabled').value='true';
		}
	else
		{
			document.getElementById('refill-level').style.display='none';
			document.getElementById('refill-amount').style.display='none';
			document.getElementById('inventoryItemCallingCardAutorecharge.automaticRechargeEnabled').value='false';
		}  
	computeOrderAmounts();
}

/** 
 * Function called onload and onclick ( input viewDestinationMinutes class) 
 * that does the get request to our url and parses the result.
 * Receives the form as parameter.
 */
function displayProductMaxMinutes(form){//it uses a specific form because a field like sourceRegion.id could coexist with another one named the same attached to another form.
    var sourceRegionId = form.elements["sourceRegion.id"].value;
    var destinationRegionId = form.elements["destinationRegion.id"].value;
    var productId = form.elements["product.id"].value;
    var productAccessType = form.elements["productAccessType"].value;
    if (destinationRegionId > 0) {
    	$.ajax({
    		type: "GET",
    		url: "/eshop/productlist/viewProductMaxMinutesEmpty.htm",
    		data: ({ "sourceRegion.id": sourceRegionId, "destinationRegion.id": destinationRegionId, "product.id": productId,"productAccessType": productAccessType }),
    		success: function(data){
    			document.getElementById("productRateContainer").innerHTML=data;
    		},
    		error: function(data,options,error){
    			document.getElementById("productRateContainer").innerHTML='Error Code '+data.status;
    		}
    	});
    }
}

/** 
 * Function called for confirmation an action button. 
 */
function confirmActionButton(alertMessage) {
	//confirm popup
	var confirmed = confirm(alertMessage);
	if(!confirmed){
		return false;
	}
}

/** 
 * Function to calculate the order without account on first sigunp page. 
 * that does the get request to our url and parses the result.
 * Receives the form as parameter.
 */
function computeOrderAmounts(){//it uses a specific form because a field like sourceRegion.id could coexist with another one named the same attached to another form.
	var form = document.getElementById("signupForm");
	var productId = form.elements["product.id"].value;
    var denominationId = form.elements["denomination.id"].value;
    var autorechargeEnabled = form.elements["inventoryItemCallingCardAutorecharge.automaticRechargeEnabled"].value;
    var autorechargeThreshold = form.elements["inventoryItemCallingCardAutorecharge.rechargeThreshold.amount"].value;
    var autorechargeAmount = form.elements["inventoryItemCallingCardAutorecharge.rechargeAmount.amount"].value;
    var promotionalCode = '';
    if (document.getElementById("promotion-code") != null && document.getElementById("promotion-code").checked){
    	promotionalCode = jQuery.trim(form.elements["accountPaymentCredentials.promotionCode"].value);
    }
  
    if (productId > 0) {
    	$.ajax({
    		type: "GET",
    		url: "/eshop/order/computeOrderAmountsEmpty.htm",
    		data: ({ "product.id": productId, "denomination.id": denominationId, "autorechargeEnabled": autorechargeEnabled, "rechargeThreshold": autorechargeThreshold,
    					"rechargeAmount": autorechargeAmount, "promotionalCode": promotionalCode}),
    		success: function(data){
    			document.getElementById("computedOrderAmounts").innerHTML=data;
    		},
    		error: function(data,options,error){
    			document.getElementById("computedOrderAmounts").innerHTML='Error Code '+data.status	;
    		}
    	});
    }
}

/** 
 * Function called onChange on nobel:select with products 
 * that does the get request to our url and parses the result.
 */
function changeProductDenominations(){
	
    var product = document.getElementById("product.id").options[document.getElementById("product.id").selectedIndex].value;
    var company = document.getElementById("company.id").value;
    document.getElementById("denomination.id").value = 0;
    
	$.ajax({
		   type: "GET",
		   url: "/retail/pricing/viewProductDenominationsEmpty.htm",
		   data: ({ "product.id": product, "company.id": company}),
		   success: function(data){
				document.getElementById("productDenominationSpanId").innerHTML=data
		   		},
		   error: function(data,options,error){
				document.getElementById("productDenominationSpanId").innerHTML='Error Code '+data.status	
				}
		 });
	
	$.ajax({
		   type: "GET",
		   url: "/eshop/account/viewProductAutoRechargeThresholdEmpty.htm",
		   data: ({ "product.id": product, "company.id": company}),
		   success: function(data){
				document.getElementById("refill-level").innerHTML=data
		   		},
		   error: function(data,options,error){
				document.getElementById("refill-level").innerHTML='Error Code '+data.status	
				}
		 });
	
	$.ajax({
		   type: "GET",
		   url: "/eshop/account/viewProductAutoRechargeDenominationEmpty.htm",
		   data: ({ "product.id": product, "company.id": company}),
		   success: function(data){
				document.getElementById("refill-amount").innerHTML=data
		   		},
		   error: function(data,options,error){
				document.getElementById("refill-amount").innerHTML='Error Code '+data.status	
				}
		 });
}

function changeViewProductDetailsUrl(){
	var theHref ;
	var product = document.getElementById("product.id").options[document.getElementById("product.id").selectedIndex].value;
	theHref = theHref + "&" + $(this).attr("name") + "=" + $(this).attr("value");
	if (product > 0) {
		theHref = "/eshop/productlist/viewProductDetailsEmpty.htm?product.id=" + product;
	} else {
		theHref = "#";
	}
	$('.viewProductDetailsPopup').each(
			function() {
				$(this).attr("href", theHref);
			}
		);
}


function successArea(){
	setTimeout(function(){
		$("div.successArea").fadeOut("slow", function () {
		$("div.successArea").remove();
		});
	}, 5000);
}

/** 
 * Function called to check the ie warning cookie. 
 */
function checkIEWarningCookie(){
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
    {
            x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
            y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
            x=x.replace(/^\s+|\s+$/g,"");
            if (x=="ieWarningCookie")
            {
            	return true;
            }
    }
    
    return false;
}

/** 
 * Function called to create cookie. 
 */
function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

/** 
 * Function that generate the ie older version warning div.
 * ---- need international message ...  
 */
function populateIeOlderVersionWarningDiv(){
	if (($.browser.msie && $.browser.version.substr(0,1) < 8 ) && (checkIEWarningCookie() == false)) {
		document.getElementById("ieOlderVersionWarningDinamicDiv").innerHTML='<div id="warningIe" class="warningIe"><div><img src="/images/enjoyprepaid/global/ie_warning.gif">You are using an old browser. To enjoy a safer and better browsing experience on our website, we recommend you to upgrade <a class="colorRed" target="_blank" href="http://windows.microsoft.com/en-US/internet-explorer/products/ie/home">to a newer version.</a></div><div id="clickme" class="closeWarning">Close <img src="/images/enjoyprepaid/global/closeWarning.gif"></div></div>';
	} else {
		document.getElementById("ieOlderVersionWarningDinamicDiv").innerHTML='';
	}
}

/** 
 * Function that generate the login/my account buttons from the top.
 *   
 */
function populateRightTopDinamicDivId(){
	var randomKey = Math.floor(Math.random() * 1000000);
	$.ajax({
		type: "GET",
		url: "/eshop/top/getTopRightCornerEmpty.htm",
		data:({ "key": randomKey}),
		success: function(data){
			$('#rightTopDinamicDivId').html(data);
			$('#rightTopDinamicDivId').show();
		},
		error: function(data,options,error){
			$('#rightTopDinamicDivId').show();
		}
	});
}

/** 
 * Function that generates the phone contact info.
 */
function populatePhoneContactInfo(){
	var urlPhoneContactInfo = "/eshop/misc/getPhoneContactInfoEmpty.htm";
	$.ajax({
		type: "GET",
		url: urlPhoneContactInfo,
		data:({}),
		success: function(data){
			$('#phoneContactInfoDivId').append(data);
		},
		error: function(data,options,error){
			$('#phoneContactInfoDivId').innerHTML='';
		}
	});
}
