// trimming help string
var trimChars = "\s\t\n\r\0\x0B";

// main structure object
var jsonZones = null;
var language = 'ru';

// default values
var defaultTariffPlan = 3;
var defaultPhoneType  = 2;
var defaultCountryFrom = 172;
var defaultCountryTo = 172;

// selected parameters
//var selectedTariffPlan = defaultTariffPlan;
var selectedPhoneTypeFrom = defaultPhoneType;
var selectedPhoneTypeTo = defaultPhoneType;				
var selectedCountryFrom = defaultCountryFrom;
var selectedCountryTo = defaultCountryTo;				
var selectedRegionFrom = null;
var selectedRegionTo = null;
//var selectedZoneArray = null;
// roaming calc
//var selectedTariffPlanRoaming = defaultTariffPlan;
var selectedCountryFromRoaming = defaultCountryFrom;
var selectedCountryToRoaming = defaultCountryTo;				
var selectedCountryToRoamingIn = defaultCountryTo;
var selectedRegionFromRoaming = null;
var selectedRegionToRoaming = null;
var selectedRegionToRoamingIn = null;
var selectedPhoneTypeFromRoaming = defaultPhoneType;
var selectedPhoneTypeToRoaming = defaultPhoneType;	
var selectedPhoneTypeToRoamingIn = defaultPhoneType;


// trimming functions		
function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function getJsonZones() {
    var xmlHttpReq = false;
    var self = this;
    
    
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    //self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    
    self.xmlHttpReq.open('POST', '/calc.php?lang='+language, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
	    if (self.xmlHttpReq.readyState == 4) {
            data = self.xmlHttpReq.responseText;
            data = trim(data, trimChars);
			jsonZones = eval('('+ data +')');
			
			setUpCountries('country_from');
			selectedCountryFrom = setSelected('country_from', selectedCountryFrom);
			setUpCountries('country_to');
			selectedCountryTo = setSelected('country_to', selectedCountryTo);
			setUpCountries('country_from_roaming');
			selectedCountryFromRoaming = setSelected('country_from_roaming', selectedCountryFromRoaming);
			setUpCountries('country_to_roaming');
			selectedCountryToRoaming = setSelected('country_to_roaming', selectedCountryToRoaming);
			setUpCountries('country_to_roaming_in');
			selectedCountryToRoamingIn = setSelected('country_to_roaming_in', selectedCountryToRoamingIn);

			
			//showing up interface
			//setUpRegionsFrom();
			//setUpRegionsTo();
						
			//document.getElementById("zone_panel").style.display = "block";
			//document.getElementById("zone_panel_roaming").style.display = "block";
			//document.getElementById("zone_panel_roaming_in").style.display = "block";
							
			setUpPhoneTypes();
			setUpPhoneTypesRoaming();
			showRatePlanDetails(selectedTariffPlan);
				
			// rate plan select enabled	
			document.getElementById("rate_plan").disabled = false;
			document.getElementById("rate_plan_roaming").disabled = false;
			
			
			// show first calculations
			//setInnerHTML('cost_label', Math.round(calculateSum('common')*1000)/1000+'<br>'+calculateSum('common'));
			setInnerHTML('cost_label', calculateSum('common'));				
			setInnerHTML('cost_label_roaming', calculateSum('roaming') + rubPerMin);
			setInnerHTML('cost_label_roaming_in', calculateSum('roaming_in') + rubPerMin);
			
			Custom.init();
			document.getElementById("indicator").style.display = "none";
			document.getElementById("calculator").style.display = "block";
			
        }
    }
    self.xmlHttpReq.send('param=paramdata');
}

function getZoneByCountryId(tariffPlan, idCountry, phoneType) {
    var selectedZones = jsonZones.phoneTypes[tariffPlan].zones[phoneType].zoneArray;
    for (i = 0; i < selectedZones.length; i++){
    	if (selectedZones[i].country.countryId == idCountry) {
    		return selectedZones[i];
    	}
    }
    return null;
}

function getRegionById(zoneObject, regionId) {
    if (zoneObject != null && zoneObject.regions != null && zoneObject.regions.length > 0){
	    for (i = 0; i < zoneObject.regions.length; i++){
	    	if (zoneObject.regions[i].regionId == regionId) {
	    		return zoneObject.regions[i];
	    	}
	    }
    } else {
    	return null;
    }
}				

function calculateSum(calculatorType){
	var sum;
	var sumFrom;
	var sumTo;					

	var tariffPlan;		
	var countryFrom;
	var countryTo;
	var phoneTypeFrom;
	var phoneTypeTo;
	var regionFrom;
	var regionTo;

	if (calculatorType == 'common'){
		tariffPlan    = selectedTariffPlan;		
		countryFrom   = selectedCountryFrom;
		countryTo     = selectedCountryTo;
		phoneTypeFrom = selectedPhoneTypeFrom;
		phoneTypeTo   = selectedPhoneTypeTo;
		regionFrom    = selectedRegionFrom;
		regionTo      = selectedRegionTo;
	} else if (calculatorType == 'roaming'){
		tariffPlan    = selectedTariffPlanRoaming;
		countryFrom   = selectedCountryFromRoaming;
		countryTo     = selectedCountryToRoaming;
		phoneTypeFrom = selectedPhoneTypeFromRoaming;
		phoneTypeTo   = selectedPhoneTypeToRoaming;
		regionFrom    = selectedRegionFromRoaming;
		regionTo      = selectedRegionToRoaming;
	} else if (calculatorType == 'roaming_in'){
		tariffPlan    = selectedTariffPlanRoaming;
		countryTo     = selectedCountryToRoamingIn;
		phoneTypeTo   = selectedPhoneTypeToRoamingIn;
		regionTo      = selectedRegionToRoamingIn;			
	}

	if (calculatorType != 'roaming_in'){
		var zoneFrom = getZoneByCountryId(tariffPlan, countryFrom, phoneTypeFrom);
		var regionFrom = getRegionById(zoneFrom, regionFrom);
		if (regionFrom != null){
			sumFrom = regionFrom.sum;
		} else {
			sumFrom = zoneFrom.country.sum;
		}
	}	
	
	var zoneTo = getZoneByCountryId(tariffPlan, countryTo, phoneTypeTo);
	var regionTo = getRegionById(zoneTo, regionTo);				
	
	if (regionTo != null){
		sumTo = regionTo.sum;
	} else {
		sumTo = zoneTo.country.sum;
	}
	
	//alert ("sumFrom: " + sumFrom + " sumTo: " + sumTo);
	if (calculatorType == 'roaming_in'){
		return sumTo + 3;
	} else {
		return Math.round((sumFrom + sumTo)*100)/100;
	}	
}

// Setting up environment after form change
function setSelectedParameters() {
	var rate_plan = document.getElementById('rate_plan');
    selectedTariffPlan = rate_plan.options[rate_plan.selectedIndex].value;
    
	var country_from = document.getElementById('country_from');
    selectedCountryFrom = country_from.options[country_from.selectedIndex].value;
    
	var country_to = document.getElementById('country_to');
    selectedCountryTo = country_to.options[country_to.selectedIndex].value;
 
	try {
		var region_from = document.getElementById('region_from');
	    selectedRegionFrom = region_from.options[region_from.selectedIndex].value;
    } catch(e) { }

	try {
	    var region_to = document.getElementById('region_to');
	    selectedRegionTo = region_to.options[region_to.selectedIndex].value;
    } catch(e) { }
}

function setSelectedParametersRoaming() {
	var rate_plan = document.getElementById('rate_plan_roaming');
    selectedTariffPlanRoaming = rate_plan.options[rate_plan.selectedIndex].value;
    
	var country_from = document.getElementById('country_from_roaming');
    selectedCountryFromRoaming = country_from.options[country_from.selectedIndex].value;
    
	var country_to = document.getElementById('country_to_roaming');
    selectedCountryToRoaming = country_to.options[country_to.selectedIndex].value;

	var country_to_in = document.getElementById('country_to_roaming_in');
    selectedCountryToRoamingIn = country_to_in.options[country_to_in.selectedIndex].value;
 
	try {
		var region_from = document.getElementById('region_from_roaming');
	    selectedRegionFromRoaming = region_from.options[region_from.selectedIndex].value;
    } catch(e) { }

	try {
	    var region_to = document.getElementById('region_to_roaming');
	    selectedRegionToRoaming = region_to.options[region_to.selectedIndex].value;
    } catch(e) { }
    
	try {
	    var region_to_in = document.getElementById('region_to_roaming_in');
	    selectedRegionToRoamingIn = region_to_in.options[region_to_in.selectedIndex].value;
    } catch(e) { }	    
}	

// select elements enterface works
function setInnerHTML(objectId, htmlText) {
	element = document.getElementById(objectId);
	element.innerHTML = htmlText; 
}	

function setSelected(objectId, valueToSelect)
{
	var objectSelect = document.getElementById(objectId);
	for (i = 0; i < objectSelect.options.length; i++){
		if (objectSelect.options[i].value == valueToSelect){
			try {
				objectSelect.options.selectedIndex=i;
				objectSelect.options[i].selected = true;
           			} catch(e) {
               	if(e.error!=undefined) {
		    		alert(e.error);
               	}
			}						
		}
	}
	return objectSelect.options[objectSelect.selectedIndex].value;
}	

function setSelectedPhoneTypeFrom(value)
{
	selectedPhoneTypeFrom = value;
}	

function setSelectedPhoneTypeTo(value)
{
	selectedPhoneTypeTo = value;
}

function setSelectedPhoneTypeFromRoaming(value)
{
	selectedPhoneTypeFromRoaming = value;
}

function setSelectedPhoneTypeToRoaming(value)
{
	selectedPhoneTypeToRoaming = value;
}

function setSelectedPhoneTypeToRoamingIn(value)
{
	selectedPhoneTypeToRoamingIn = value;
}	

function setUpCountries(selectId)
{
	var oZoneFrom = document.getElementById(selectId);
	oZoneFrom.length = 0;
     
    var selectedZones = jsonZones.phoneTypes[selectedTariffPlan].zones[selectedPhoneTypeFrom].zoneArray;
    for (i = 0; i < selectedZones.length; i++){
        if (document.createElement) {
            var newZoneOption = document.createElement("OPTION");
            newZoneOption.text = selectedZones[i].country.name;
            newZoneOption.value = selectedZones[i].country.countryId;
            
            // &#209;&#130;&#209;&#131;&#209;&#130; &#208;&#188;&#209;&#139; &#208;&#184;&#209;&#129;&#208;&#191;&#208;&#190;&#208;»&#209;&#140;&#208;·&#209;&#131;&#208;µ&#208;&#188; &#208;&#180;&#208;»&#209;&#143; &#208;&#180;&#208;&#190;&#208;±&#208;°&#208;&#178;&#208;»&#208;µ&#208;&#189;&#208;&#184;&#209;&#143; &#209;&#141;&#208;»&#208;µ&#208;&#188;&#208;µ&#208;&#189;&#209;&#130;&#208;° &#208;»&#208;&#184;&#208;±&#208;&#190; &#208;&#188;&#208;µ&#209;&#130;&#208;&#190;&#208;&#180; IE, &#208;»&#208;&#184;&#208;±&#208;&#190; DOM, &#208;&#186;&#208;&#190;&#209;&#130;&#208;&#190;&#209;&#128;&#209;&#139;&#208;µ, alas, &#208;&#189;&#208;µ &#209;&#129;&#208;&#190;&#208;&#178;&#208;&#191;&#208;°&#208;&#180;&#208;°&#209;&#142;&#209;&#130; &#208;&#191;&#208;&#190; &#208;&#191;&#208;°&#209;&#128;&#208;°&#208;&#188;&#208;µ&#209;&#130;&#209;&#128;&#208;°&#208;&#188;&#226;&#128;¦
            (oZoneFrom.options.add) ? oZoneFrom.options.add(newZoneOption) : oZoneFrom.add(newZoneOption, null);
        } else {
            // &#208;&#180;&#208;»&#209;&#143; NN3.x-4.x
            oZoneFrom.options[i] = new Option(selectedZones[i].country.name, selectedZones[i].country.countryId, false, false);
        }
    }
}
	    
/*
function setUpRegionsFrom(){
	var selectedZone = getZoneByCountryId(selectedCountryFrom, selectedPhoneTypeFrom);
	var regions_from_panel = document.getElementById("regions_from_panel");
	
	if (selectedZone != null && selectedZone.regions != null && selectedZone.regions.length > 0) {
		regions_from_panel.style.display = "block";
		var oRegionFrom = document.getElementById("region_from");
		oRegionFrom.length = 0;
	     
	    for (i = 0; i < selectedZone.regions.length; i++){
	        if (document.createElement) {
	            var newZoneOption = document.createElement("OPTION");
	            newZoneOption.text = selectedZone.regions[i].name;
	            newZoneOption.value = selectedZone.regions[i].regionId;
	            
	            // &#209;&#130;&#209;&#131;&#209;&#130; &#208;&#188;&#209;&#139; &#208;&#184;&#209;&#129;&#208;&#191;&#208;&#190;&#208;»&#209;&#140;&#208;·&#209;&#131;&#208;µ&#208;&#188; &#208;&#180;&#208;»&#209;&#143; &#208;&#180;&#208;&#190;&#208;±&#208;°&#208;&#178;&#208;»&#208;µ&#208;&#189;&#208;&#184;&#209;&#143; &#209;&#141;&#208;»&#208;µ&#208;&#188;&#208;µ&#208;&#189;&#209;&#130;&#208;° &#208;»&#208;&#184;&#208;±&#208;&#190; &#208;&#188;&#208;µ&#209;&#130;&#208;&#190;&#208;&#180; IE, &#208;»&#208;&#184;&#208;±&#208;&#190; DOM, &#208;&#186;&#208;&#190;&#209;&#130;&#208;&#190;&#209;&#128;&#209;&#139;&#208;µ, alas, &#208;&#189;&#208;µ &#209;&#129;&#208;&#190;&#208;&#178;&#208;&#191;&#208;°&#208;&#180;&#208;°&#209;&#142;&#209;&#130; &#208;&#191;&#208;&#190; &#208;&#191;&#208;°&#209;&#128;&#208;°&#208;&#188;&#208;µ&#209;&#130;&#209;&#128;&#208;°&#208;&#188;&#226;&#128;¦
	            (oRegionFrom.options.add) ? oRegionFrom.options.add(newZoneOption) : oRegionFrom.add(newZoneOption, null);
	        } else {
	            // &#208;&#180;&#208;»&#209;&#143; NN3.x-4.x
	            oRegionFrom.options[i] = new Option(selectedZone.regions[i].name, selectedZone.regions[i].regionId, false, false);
	        }
	    }
		selectedRegionFrom = setSelected('region_from', selectedRegionFrom);					    
	} else {
		regions_from_panel.style.display = "none";
	}
}

function setUpRegionsTo(){
	var selectedZone = getZoneByCountryId(selectedCountryTo, selectedPhoneTypeTo);
	var regions_to_panel = document.getElementById("regions_to_panel");
	
	if (selectedZone != null && selectedZone.regions != null && selectedZone.regions.length > 0) {
					regions_to_panel.style.display = "block";
					
		var oRegionTo = document.getElementById("region_to");
		oRegionTo.length = 0;
	     
	    for (i = 0; i < selectedZone.regions.length; i++){
	        if (document.createElement) {
	            var newZoneOption = document.createElement("OPTION");
	            newZoneOption.text = selectedZone.regions[i].name;
	            newZoneOption.value = selectedZone.regions[i].regionId;
	            
	            // &#209;&#130;&#209;&#131;&#209;&#130; &#208;&#188;&#209;&#139; &#208;&#184;&#209;&#129;&#208;&#191;&#208;&#190;&#208;»&#209;&#140;&#208;·&#209;&#131;&#208;µ&#208;&#188; &#208;&#180;&#208;»&#209;&#143; &#208;&#180;&#208;&#190;&#208;±&#208;°&#208;&#178;&#208;»&#208;µ&#208;&#189;&#208;&#184;&#209;&#143; &#209;&#141;&#208;»&#208;µ&#208;&#188;&#208;µ&#208;&#189;&#209;&#130;&#208;° &#208;»&#208;&#184;&#208;±&#208;&#190; &#208;&#188;&#208;µ&#209;&#130;&#208;&#190;&#208;&#180; IE, &#208;»&#208;&#184;&#208;±&#208;&#190; DOM, &#208;&#186;&#208;&#190;&#209;&#130;&#208;&#190;&#209;&#128;&#209;&#139;&#208;µ, alas, &#208;&#189;&#208;µ &#209;&#129;&#208;&#190;&#208;&#178;&#208;&#191;&#208;°&#208;&#180;&#208;°&#209;&#142;&#209;&#130; &#208;&#191;&#208;&#190; &#208;&#191;&#208;°&#209;&#128;&#208;°&#208;&#188;&#208;µ&#209;&#130;&#209;&#128;&#208;°&#208;&#188;&#226;&#128;¦
	            (oRegionTo.options.add) ? oRegionTo.options.add(newZoneOption) : oRegionTo.add(newZoneOption, null);
	        } else {
	            // &#208;&#180;&#208;»&#209;&#143; NN3.x-4.x
	            oRegionTo.options[i] = new Option(selectedZone.regions[i].name, selectedZone.regions[i].regionId, false, false);
	        }
	    }
	    
					selectedRegionTo = setSelected('region_to', selectedRegionTo);
				} else {
					regions_to_panel.style.display = "none";
				}
}
*/ 				

function setUpPhoneTypes(){
	//document.getElementById("phone_type_panel").style.display = "block";
}	

function setUpPhoneTypesRoaming(){
	//document.getElementById("phone_type_panel_roaming").style.display = "block";
	//document.getElementById("phone_type_panel_roaming_in").style.display = "block";		
}

function setUpRatePlanSwitch(){
	document.getElementById("switch_rate_link").href = "../eztalk/rate_plan.action?rate_plan=" + selectedTariffPlan;
	if (isUserLoged == 0 || currentUserTariffPlan == selectedTariffPlan || currentUserTariffPlan == tariffPlanCorporativeBlocked) {
		document.getElementById("switch_rate_proposition").style.display = "none";
	} else {
		document.getElementById("switch_rate_proposition").style.display = "block";
	}
}	

function setUpRatePlanSwitchRoaming(){
	document.getElementById("switch_rate_link_roaming").href = "../eztalk/rate_plan.action?rate_plan=" + selectedTariffPlanRoaming;
}

function showRatePlanDetails(ratePlanId) {
	/*if (ratePlanId == 1){
		document.getElementById("details_other").style.display = "none";
		document.getElementById("details_base").style.display = "block";
		//document.getElementById("details_base_regions").style.display = "block";						
	} else if (ratePlanId == 2 || ratePlanId == 3){
		document.getElementById("details_base").style.display = "none";
		document.getElementById("details_other").style.display = "block";	
		//document.getElementById("details_base_regions").style.display = "none";
	} else if (ratePlanId == 4){
		document.getElementById("details_base").style.display = "none";
		document.getElementById("details_other").style.display = "block";	
	} else if (ratePlanId == 5){
		document.getElementById("details_base").style.display = "none";
		document.getElementById("details_other").style.display = "block";	
	} else if (ratePlanId == 6){
		document.getElementById("details_base").style.display = "none";
		document.getElementById("details_other").style.display = "block";	
	}*/
}

function onChange(element) {
	setSelectedParameters();

	if (element.id == 'rate_plan'){
		setUpCountries('country_from');
		selectedCountryFrom = setSelected('country_from', selectedCountryFrom);
		setUpCountries('country_to');
		selectedCountryTo = setSelected('country_to', selectedCountryTo);
		
		setUpPhoneTypes();
		setUpRatePlanSwitch();
		showRatePlanDetails(selectedTariffPlan);			
	}
	if (element.id == 'rate_plan' || element.id == 'country_from' || element.id == 'country_to'){
		//setUpRegionsFrom();
		//setUpPhoneTypes();			
	}
		
	//setInnerHTML('cost_label', Math.round(calculateSum('common')*1000)/1000+'<br>'+calculateSum('common'));
	setInnerHTML('cost_label', calculateSum('common'));
}

function onChangeRoaming(element) {
	setSelectedParametersRoaming();

	if (element.id == 'rate_plan_roaming'){
		setUpCountries('country_from_roaming');
		selectedCountryFromRoaming = setSelected('country_from_roaming', selectedCountryFromRoaming);
		setUpCountries('country_to_roaming');
		selectedCountryToRoaming = setSelected('country_to_roaming', selectedCountryToRoaming);
	
		setUpPhoneTypesRoaming();
		setUpRatePlanSwitchRoaming();			
	}
	if (element.id == 'rate_plan_roaming' || element.id == 'country_from_roaming' || element.id == 'country_to_roaming' || element.id == 'country_to_roaming_in'){
		//setUpRegionsFrom();
		//setUpPhoneTypes();			
	}
		
	setInnerHTML('cost_label_roaming', calculateSum('roaming') + rubPerMin);
	setInnerHTML('cost_label_roaming_in', calculateSum('roaming_in') + rubPerMin);
}	

// starting script
function startRatePlanCalculators(lang) {
	if(lang) language = lang;
	document.getElementById("rate_plan").disabled = true;
	document.getElementById("rate_plan_roaming").disabled = true;	
	setSelected('rate_plan', selectedTariffPlan);
	setSelected('rate_plan_roaming', selectedTariffPlan);
	getJsonZones();
	setUpRatePlanSwitch();
	setUpRatePlanSwitchRoaming();		

	setInnerHTML('cost_label', loadingText);
	setInnerHTML('cost_label_roaming', loadingText);
	setInnerHTML('cost_label_roaming_in', loadingText);
}