﻿// JScript File
function limparCampo(id_campo, texto){
var campo = document.getElementById(id_campo);
if (campo.value == texto)
    {
        campo.value = '';
    }
}

function repoeCampo(id_campo, texto){
var campo = document.getElementById(id_campo);
if (campo.value == '')
    {
        campo.value = texto;
    }
}



var Address = "";
var City = "";
var CountryIndex = 0;
var Country = "";


function InitSaveVariables(form) {
Address = form.Address_delivery.value;
City = form.City_delivery.value;
CountryIndex = form.Country_Deliv.selectedIndex;
Country=form.ShipState[CountryIndex].value;
}

function ShipToBillPerson(form) {
if (form.copy.checked) {
InitSaveVariables(form);
form.Address_delivery.value = form.Address.value;
form.City_delivery.value = form.City.value;
form.Country_delivery.selectedIndex= form.Country.selectedIndex;

}
else {

form.Address_delivery.value = Address;
form.City_delivery.value = City;
form.Country_delivery.selectedIndex= CountryIndex;

   }
}

function setFocus(control, color)
{
    control.style.border =  'solid 1px #' + color;
}
function copyFormFields(checkbox, fields)
{
    for (pairIndex in fields.split(';'))
    {
        var origin = document.getElementById(fields.split(";")[pairIndex].split(',')[0])
        var destination = document.getElementById(fields.split(";")[pairIndex].split(',')[1])
        
        if (checkbox.checked)
        {
            if ((origin.tagName == 'INPUT') || (origin.tagName == 'TEXTAREA'))
            {
                destination.value = origin.value;
            }
            else if (origin.tagName == 'SELECT')
            {
                destination.selectedIndex = origin.selectedIndex;
            }
        }
        else
        {
            if ((origin.tagName == 'INPUT') || (origin.tagName == 'TEXTAREA'))
            {
                destination.value = '';
            }
            else if (origin.tagName == 'SELECT')
            {
                destination.selectedIndex = 0;
            }
        }
        
    }

    //var control = document.getElementById(fields);
    //alert(control.tagName);
}

//function to display or hide a given element
function showHideItems(divname, sender){

var myItem = document.getElementById(divname + "-" + sender.options[sender.selectedIndex].value);

var elem = document.getElementsByTagName('div');
    for(var i = 0; i < elem.length; i++)
    {
       if (elem[i].id.substring(0,divname.length)==divname)
        {
        elem[i].style.display = 'none'
        }
    }
    
    myItem.style.display="block";
	
}

//function to display or hide a given element
function showHideItemsByVal(divname, value){

var myItem = document.getElementById(divname + "-" + value);
    myItem.style.display="block";
}

//LIMITA O INPUT SÓ A NÚMEROS

var numbersonly = "()- 0123456789";
var numb = "0123456789";
var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";

function res(t,v){
	var w = "";
	for (i=0; i < t.value.length; i++) {
		x = t.value.charAt(i);
		if (v.indexOf(x,0) != -1)
			w += x; 
	}
	t.value = w;
}
function SetUniqueRadioButton(nameregex, current)
{
   re = new RegExp(nameregex);
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
      elm = document.forms[0].elements[i]
      if (elm.type == 'radio')
      {
         if (re.test(elm.name))
         {
            elm.checked = false;
         }
      }
   }
   current.checked = true;
}

function menu_goto( control, url )
{
    // see http://www.thesitewizard.com/archive/navigation.shtml
    // for an explanation of this script and how to use it on your
    // own site

    var baseurl = url;
    newurl = control.options[ control.selectedIndex ].value ;
    if (newurl.length != 0) {
      location.href = baseurl + newurl ;
    }
}

function calc_grapes (sender,grape1,grape2,result)
{

var myGrapeVal1 = document.getElementById(grape1);
var myGrapeVal2 = document.getElementById(grape2);
var myGrapeResult = document.getElementById(result);
if (sender.value == '')
    {
        sender.value = '0';
    }

if (parseFloat(sender.value) > 100)
    {
        sender.value = '100';
    }
    
myGrapeResult.value= 100 - (parseFloat(sender.value) + parseFloat(myGrapeVal1.value) + parseFloat(myGrapeVal2.value));
if (parseFloat(myGrapeResult.value) < 0)
    myGrapeResult.value = '0';
}

function setMyfocus(source) 
{ 
var nomeCampo = document.getElementById(source);
nomeCampo.focus(); 
} 

function windowpopup(url)
  {
   window.open("'" & url & "'")
  }

function price_simulation(txtfield_SubTotal, txtfield_Total,  _Price, _vatrate, _commission, _shipping, _iec)
  {
    var SubTotal = document.getElementById(txtfield_SubTotal);
    var Total = document.getElementById(txtfield_Total);
    var UnitPrice = document.getElementById(_Price);
    var vatrate =  document.getElementById(_vatrate);
    var commission = _commission
    var shipping = _shipping
    var iec_elem = document.getElementById(_iec);
      
    var price = UnitPrice.value.replace(',','.')
    var iec = iec_elem.value.replace(',','.')
    commission = commission.replace(',','.')
    vatrate = vatrate.value.replace(',','.')
    shipping = shipping.replace(',','.')
    
    if (iec == '')
    { 
        iec = '0';
    }
    
    
    var value1 = parseFloat(price) * parseFloat(commission);
    var value2 = parseFloat(value1) + parseFloat(price);
    var value3 = (value2+parseFloat(iec)) * (1+parseFloat(vatrate / 100));
    var value4 = value3
    var value5 = value4 + parseFloat(shipping);
    
    SubTotal.value = value4.toFixed(2).replace('.',',') ;
    Total.value = value5.toFixed(2).replace('.',',');
    UnitPrice.value=price.replace('.',',');
    
    
    if (SubTotal.value == 'NaN')
    {
        SubTotal.value = '';
    }
      if (Total.value == 'NaN')
    {
        Total.value = '';
    }
}

function price_simulation_percentage(txtfield_SubTotal, txtfield_Total, txtfield_SubTotalProm, txtfield_TotalProm, _Price, _PriceProm, _vatrate, _commission, _shipping, _iec)
  {
    var SubTotal = document.getElementById(txtfield_SubTotal);
    var Total = document.getElementById(txtfield_Total);
    var SubTotalProm = document.getElementById(txtfield_SubTotalProm);
    var TotalProm = document.getElementById(txtfield_TotalProm);
    var UnitPrice = document.getElementById(_Price);
    var UnitPriceProm = document.getElementById(_PriceProm);
 
    var price = UnitPrice.value.replace(',','.')
    var priceProm = UnitPriceProm.value.replace(',','.');
    
    var vatrate =  document.getElementById(_vatrate);
    var commission = _commission
    var shipping = _shipping
    
    var iec_elem = document.getElementById(_iec);
    var iec = iec_elem.value.replace(',','.')
    
    if (iec == '')
    { 
        iec = '0';
    }
       
    commission = commission.replace(',','.')
    vatrate = vatrate.value.replace(',','.')
    shipping = shipping.replace(',','.')   
    
    var value1 = parseFloat(price) * parseFloat(commission);
    var value2 = parseFloat(value1) + parseFloat(price);
    var value3 = (value2+parseFloat(iec)) * (1+parseFloat(vatrate / 100));
    var value4 = value3;
    var value5 = value4 + parseFloat(shipping);
    SubTotal.value = value4.toFixed(2).replace('.',',');
    Total.value = value5.toFixed(2).replace('.',',');
    UnitPrice.value=price.replace('.',',');
    
      if (SubTotal.value == 'NaN')
    {
        SubTotal.value = '';
    }
      if (Total.value == 'NaN')
    {
        Total.value = '';
    }
    
    var valueProm1 = parseFloat(priceProm) * parseFloat(commission);
    var valueProm2 = parseFloat(valueProm1) + parseFloat(priceProm);
    var valueProm3 = (valueProm2+parseFloat(iec)) * (1+parseFloat(vatrate / 100));
    var valueProm4 = valueProm3;
    var valueProm5 = valueProm4 + parseFloat(shipping);
    SubTotalProm.value = valueProm4.toFixed(2).replace('.',',') ;
    TotalProm.value = valueProm5.toFixed(2).replace('.',',');
    UnitPriceProm.value=priceProm.replace('.',',');
     
      if (SubTotalProm.value == 'NaN')
    {
        SubTotalProm.value = '';
    }
      if (TotalProm.value == 'NaN')
    {
        TotalProm.value = '';
    }
}

function validate_interval (_dropdown1, _dropdown2) {
    var dropdown1 =  document.getElementById(_dropdown1);
    var dropdown2 =  document.getElementById(_dropdown2);
    
    var myindex1  = dropdown1.selectedIndex;
    var SelValue1 = dropdown1.options[myindex1].value;
    var myindex2  = dropdown2.selectedIndex;
    var SelValue2 = dropdown2.options[myindex2].value;

    if (myindex2 < myindex1) {
        dropdown1.selectedIndex = myindex2;
    }
  
}

