function goPage(obj)
{
	document.form1.operation.value=obj;
	document.form1.submit();
}

function goPageParent(obj)
{
	opener.form1.operation.value=obj;
	opener.form1.submit();
	window.close();

}

function Trim(STRING){
STRING = LTrim(STRING);
return RTrim(STRING);
}

function RTrim(STRING){
while(STRING.charAt((STRING.length -1))==" "){
STRING = STRING.substring(0,STRING.length-1);
}
return STRING;
}


function LTrim(STRING){
while(STRING.charAt(0)==" "){
STRING = STRING.replace(STRING.charAt(0),"");
}
return STRING;
}

function findCountryIndex(destinationArea){
		var countryIndex=1;
		
		for (i=0; i<document.form1.countryRates.options.length; i++) {
	      	if(document.form1.countryRates.options[i].text.toLowerCase() == destinationArea.toLowerCase()){
	     		countryIndex=i;
	     		break;
	     	}	      
     	}	
		
		document.form1.countryRates.selectedIndex =countryIndex;	
}


function getPlanRatePerMin4Country(destinationArea){
	
			qstr = 'ajaxoperation=getPlanRatePerMin&destinationArea='+destinationArea;  // NOTE: no '?' before querystring
	   		   		
		    var strURL = "../service/AjaxServlet?"+qstr;
		    // Mozilla/Safari
		    if (window.XMLHttpRequest) {
		        self.xmlHttpReq = new XMLHttpRequest();
		    }
		    // IE
		    else if (window.ActiveXObject) {
		        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		    }
		    self.xmlHttpReq.open('POST', strURL, true);
		    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		    self.xmlHttpReq.onreadystatechange = function() {
		        if (self.xmlHttpReq.readyState == 4) {
		            updatePlanPerPinPage(self.xmlHttpReq.responseText);
		        }
		    }
		    self.xmlHttpReq.send(null);
}

function getPlanRatePerMin() {

    var xmlHttpReq = false;
    var self = this;
    var index = document.form1.countryRates.selectedIndex;
    
   	
   	if(index > 0){
   		var destinationArea = document.form1.countryRates.options[index].value;
   		getPlanRatePerMin4Country(destinationArea);
   	}
}

function onefloatingdigit(rate){

	var  ret = (Math.floor(parseFloat(rate)*10))/10;
	
	if(parseFloat(ret) - Math.floor(parseFloat(ret)) == 0){
		ret = ret +'.0';
	} 
	
	return ret;
}

function updatePlanPerPinPage(str)
{
	var cinfo = str.split(';');
	var country = cinfo[0];
	var ratePerMinStandart = '' ;
	var ratePerMinDomestic  = '' ;
	var ratePerMinInternational = ''   ;
	var bef1 = '$'  ;
	var aft1 = '&cent;' ;
	var bef2 = '$'  ;
	var aft2 = '&cent;' ;
	var bef3 = '$'  ;
	var aft3 = '&cent;' ;
	
	add = 0;
	
	if(document.form1.radiobutton && document.form1.radiobutton[1].checked){
		add = 1;
	}

	if(cinfo[1] != undefined){
		/*if(cinfo[1]>=1){
			ratePerMinStandart =parseFloat(cinfo[1])+parseFloat(add/10);
			aft1 = '';
		}else{*/
			ratePerMinStandart =(parseFloat(cinfo[1])*1000)/10+add;
			bef1 = '';
		//}
		if((ratePerMinStandart - parseInt(ratePerMinStandart)) > 0){
			ratePerMinStandart = onefloatingdigit(ratePerMinStandart);
		}
		
		/*

			ratePerMinStandart =(parseFloat(cinfo[1])*1000)/10+add;
			bef1 = '';
		//}
		if((ratePerMinStandart - parseInt(ratePerMinStandart)) > 0){
			ratePerMinStandart = onefloatingdigit(ratePerMinStandart);
		}

	}else{
		aft1 = '';bef1 = '';ratePerMinStandart='-';
	}
		*/

	}else{
		aft1 = '';bef1 = '';ratePerMinStandart='-';
	}
	if(cinfo[2] != undefined){
		/*if(cinfo[2]>=1){
			ratePerMinDomestic =parseFloat(cinfo[2])+add/10;
			aft2 = '';
		}else{*/
			ratePerMinDomestic =(parseFloat(cinfo[2])*1000)/10+add;
			bef2 = '';
		//}
		if((ratePerMinDomestic - parseInt(ratePerMinDomestic)) > 0){
			ratePerMinDomestic = onefloatingdigit(ratePerMinDomestic);
		}
	}else{
		aft2 = '';bef2 = '';ratePerMinDomestic='-';
	}
	
	if(cinfo[3] != undefined){
		/*if(cinfo[3]>=1){
			ratePerMinInternational =parseFloat(cinfo[3])+add/10;
			aft3 = '';
		}else{*/
			ratePerMinInternational =(parseFloat(cinfo[3])*1000)/10+add;
			bef3 = '';
		//}
		if((ratePerMinInternational - parseInt(ratePerMinInternational)) > 0){
			ratePerMinInternational = onefloatingdigit(ratePerMinInternational);
		}
	}else{
		aft3 = '';bef3 = '';ratePerMinInternational='-';
	}

	if(document.form1.country != null)
		document.form1.country.value = cinfo[0];
		
	if(document.form1.ratePerMinStandart != null){
		document.form1.ratePerMinStandart.value      = ratePerMinStandart.toFixed(1);
		document.form1.ratePerMinDomestic.value      = ratePerMinDomestic.toFixed(1);
		document.form1.ratePerMinInternational.value = ratePerMinInternational.toFixed(1);
	}
	
		var   rPerMinStandart     = ratePerMinStandart;
		var   rPerMinDomestic     = ratePerMinDomestic;
		var   rPerMinInternational = ratePerMinInternational;
	
	if(document.getElementById("ratePerMin1")){
		if(ratePerMinDomestic >0){
			document.getElementById("ratePerMin1").innerHTML = "<span style=\"font-weight:bold;\"><span class=\"rate\">"+bef2+rPerMinDomestic +aft2+" </span>/min.</span>"
		}else{
			document.getElementById("ratePerMin1").innerHTML = "<span style=\"font-weight:bold;\"><span class=\"rate\">N/A</span></span>"
		}
	}
	if(document.getElementById("ratePerMin2")){
		if(ratePerMinStandart >0){
			document.getElementById("ratePerMin2").innerHTML = 	"<span style=\"font-weight:bold;\"><span class=\"rate\">"+bef1+rPerMinStandart+aft1+" </span>/min.</span>"
		}else{
			document.getElementById("ratePerMin2").innerHTML = "<span style=\"font-weight:bold;\"><span class=\"rate\">N/A</span></span>"
		}
	}
	
	/*if(ratePerMinInternational >0){
		document.getElementById("ratePerMin3").innerHTML = "<span style=\"font-weight:bold;\"><span class=\"rate\">"+bef3+rPerMinInternational+aft3+" </span>/min.</span>"
	}else{
		document.getElementById("ratePerMin3").innerHTML = "<span style=\"font-weight:bold;\"><span class=\"rate\">N/A</span></span>"
	}*/
	
	if(document.getElementById("dest1")){
		document.getElementById("dest1").innerHTML = "<span><strong>"+country+"<strong><span>";
	}
	if(document.getElementById("dest2")){
		document.getElementById("dest2").innerHTML = "<span><strong>"+country+"<strong><span>";
	}
	/*document.getElementById("dest1").innerText = country;
	document.getElementById("dest2").innerText = country;*/
	var suggestedPlan = document.getElementById("suggestedPlan");
	if (suggestedPlan != null)
	{
		if (ratePerMinStandart <= ratePerMinDomestic)
			suggestedPlan.innerHTML = "The <span class=\"welcometitle\">Standard</span> plan will save you more money when you call <span class=\"welcometitle\">" + country + "</span>!";
		else {
			var thresholdMinutes = (195 / (ratePerMinStandart - ratePerMinDomestic));
			suggestedPlan.innerHTML = "If you speak more than <span class=\"welcometitle\">" + Math.floor(thresholdMinutes) + "</span> minutes per month when calling <span class=\"welcometitle\">" + country + "</span>, then the <span class=\"welcometitle\">Premium</span> plan will save you more money!";
		}
	}

}

//-----------

function getPlanRatePerMin4CountrybyRateId(destinationArea,rateId){
	
			qstr = 'ajaxoperation=getPlanRatePerMinbyRateID&destinationArea='+destinationArea+'&rateid='+rateId;  // NOTE: no '?' before querystring
	   		   		
		    var strURL = "../service/AjaxServlet?"+qstr;
		    // Mozilla/Safari
		    if (window.XMLHttpRequest) {
		        self.xmlHttpReq = new XMLHttpRequest();
		    }
		    // IE
		    else if (window.ActiveXObject) {
		        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		    }
		    self.xmlHttpReq.open('POST', strURL, true);
		    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		    self.xmlHttpReq.onreadystatechange = function() {
		        if (self.xmlHttpReq.readyState == 4) {
		            updatePlanPerPinPagebyRateId(self.xmlHttpReq.responseText);
		        }
		    }
		    self.xmlHttpReq.send(null);
}

function getPlanRatePerMinbyRateId(rateId) {

    var xmlHttpReq = false;
    var self = this;
    var index = document.form1.countryRates.selectedIndex;
    
   	
   	if(index > 0){
   		var destinationArea = document.form1.countryRates.options[index].value;
   		getPlanRatePerMin4CountrybyRateId(destinationArea,rateId);
   	}
}
function updatePlanPerPinPagebyRateId(str)
{
	var cinfo = str.split(';');
	var country = cinfo[0];
	var ratePerMin = '' ;
	var bef1 = '$'  ;
	var aft1 = '&cent;' ;
	add = 0;
	
	if(document.form1.radiobutton && document.form1.radiobutton[1].checked){
		add = 1;
	}
	
	if(cinfo[1] != undefined){

		/*	ratePerMin =(parseFloat(cinfo[1])*1000)/10+add;
			bef1 = '';

		if((ratePerMin - parseInt(ratePerMin)) > 0){
			ratePerMin = onefloatingdigit(ratePerMin);
		}*/
		var pureRate = cinfo[1];
		if(pureRate>=1){
			rate = parseFloat(pureRate) + (add/100);
			aft1 = '';
		}else{
			rate = (Math.round(pureRate*10000))/100 + add;
			bef1 = '';
		}
		ratePerMin = rate;

	}else{
		aft1 = '';bef1 = '';ratePerMin='-';
	}
	if(document.form1.country != null)
		document.form1.country.value = cinfo[0];
	
	var   rPerMin     = ratePerMin;
	
	if(document.getElementById("ratePerMin1")){
		if(rPerMin >0){
			document.getElementById("ratePerMin1").innerHTML = "<span style=\"font-weight:bold;\"><span class=\"rate\">"+bef1+rPerMin +aft1+" </span>/min.</span>"
		}else{
			document.getElementById("ratePerMin1").innerHTML = "<span style=\"font-weight:bold;\"><span class=\"rate\">N/A</span></span>"
		}
	}
}

//--------

function updateCountryPriceLocal(){
	getPlanRatePerMin();
}

function updateCountryPriceTollFree(){
	getPlanRatePerMin();
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function clearWarning(fieldname){
	if(document.getElementById(fieldname) != null)
		document.getElementById(fieldname).innerHTML = '';
}


function dontRefresh(event) 
{
	var key;
	if (window.event)
	   key = window.event.keyCode;
	else if (event)
	   key = event.which;

	if (key==116) {
		if (window.event){
			window.event.keyCode=0; 
			window.event.returnValue=false;
		}	
		return false
	}	
	return true;
}

function goPageCheckPermission(targetpage,message){

		var xmlHttpReq = false;
	    var self = this;
	    var selectedaccountid = document.form1.selectedaccountid.value;

    	qstr = 'ajaxoperation=checkPermission&selectedaccountid='+escape(selectedaccountid);  // NOTE: no '?' before querystring
    
	    var strURL = "../service/AjaxServlet?"+qstr;
	    // Mozilla/Safari
	    if (window.XMLHttpRequest) {
	        self.xmlHttpReq = new XMLHttpRequest();
	    }
	    // IE
	    else if (window.ActiveXObject) {
	        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    self.xmlHttpReq.open("POST", strURL, true);
	    self.xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    self.xmlHttpReq.onreadystatechange = function() {
	        if (self.xmlHttpReq.readyState == 4) {
	            updateCheckPermission(self.xmlHttpReq.responseText,targetpage,message);
	        }
	    }
	    self.xmlHttpReq.send(null);
}

function updateCheckPermission(str,targetpage,message){
	if(str == '1'){
		goPage(targetpage);
	}else{
		alert(message);
	}
}

