function showHideDiv(id,c_id) {
  var div = document.getElementById(id);
  var displaymode = div.style.display;
  if(displaymode=='none') {
    if(c_id!='') document.getElementById(c_id).innerHTML='<a onclick="showHideDiv(\''+id+'\',\''+c_id+'\');"><img src="/img/icons/minus.gif" alt=""/></a>';  
    div.style.display='block';
  } else {
    if(c_id!='') document.getElementById(c_id).innerHTML='<a onclick="showHideDiv(\''+id+'\',\''+c_id+'\');"><img src="/img/icons/plus.gif" alt=""/></a>';
    div.style.display='none';
  }
}

function showDiv(id) {
  document.getElementById(id).style.display='block';
}

function hideDiv(id) {
  document.getElementById(id).style.display='none';
}

function returnMsg(msg) {
  alert(msg);
}

function enterInput(id,val) {
	if (document.getElementById(id).value==val) {
		document.getElementById(id).value='';
	}
}

function leaveInput(id,val) {
	if (document.getElementById(id).value=='') {
		document.getElementById(id).value=val;
	}
}

function registerForm(id,id2) {
  document.getElementById('user-status').value=id;
  document.getElementById('form').style.display='none';
  document.getElementById(id2).style.background="#FFFFFF";
  document.getElementById(id).style.background="#EAEAEA";
  new Effect.Highlight(id, { startcolor:'#FFFFFF',endcolor:'#EAEAEA' });
  document.getElementById(id).style.border="1px dotted #B3D5AB";
  document.getElementById(id2).style.border="0";
  if(id=='designer') {
    document.getElementById('form-designer').style.display='block';
    document.getElementById('form-shopname').style.display='block';
    document.getElementById('form-ytunnus').style.display='block';
  } else {
    document.getElementById('form-designer').style.display='none';
    document.getElementById('form-shopname').style.display='none';
    document.getElementById('form-ytunnus').style.display='none';
    document.getElementById('about-me-EN').value='';
    document.getElementById('about-me-FI').value='';
  }
  new Effect.Appear('form');
}

function updateGallery(src,i,num) {
  for(j=1;j<=num;j++) {
    document.getElementById('thumb'+j).style.border='0';
  }
  document.getElementById('thumb'+i).style.border='3px double #EAEAEA';
  document.getElementById('gallery').innerHTML='<a href="'+src+'.jpg" rel="lightbox"><img src="'+src+'_300.jpg" alt=""/></a>';
}

function appearFadeDiv(id) {
  var div = document.getElementById(id);
  var displaymode = div.style.display;
  if(displaymode=='none') {
    Effect.Appear(id);
  } else {
    div.style.display='none';
  }
}

var IE=document.all ? true : false;
info=null;

function updateInfo(e) {
  x=0; y=0;
  if (IE) {
   x=event.clientX + document.documentElement.scrollLeft;
   y=event.clientY + document.documentElement.scrollTop;
   if(document.documentElement.clientHeight - event.clientY<80) y=y-80;
   if(document.documentElement.clientHeight - event.clientY>540) y=y+20;
   if(document.documentElement.clientWidth<1050) {
      move_x=800;
   } else {
      move_x=960;
   }
   if(document.documentElement.clientWidth - event.clientX<(document.documentElement.clientWidth-move_x)) x=x-245;
  } else {
	 x=(document.all) ? window.event.x + document.body.scrollLeft : e.pageX;
	 y=(document.all) ? window.event.y + document.body.scrollTop : e.pageY;
	 if(document.body.clientHeight - e.clientY<80) y=y-80;
   if(document.body.clientHeight - e.clientY>540) y=y+20;
   if(document.body.clientWidth<1050) {
      move_x=800;
   } else {
      move_x=960;
   }
   if(document.body.clientWidth - e.clientX<(document.body.clientWidth-move_x)) x=x-245;
	}
	if(info!=null) {
		info.style.left=(x+20)+"px";
		info.style.top=(y-20)+"px";
	}
}

function showInfo(id) {
	info=document.getElementById(id);
  if(IE) updateInfo();
	info.style.display="block";
}

function hideInfo() {
	info.style.display="none";
}

function selectSwitchColor(id,form) {
  c=document.forms[form].elements['data[Product][color]['+id+']'].value;
  if(form=='search-form') {
    field='color';
  } else if(form=='adv-search-form') {
    field='adv-color';
  }
  if(c==0) {
    document.getElementById(field+id).style.border='1px solid #B1B1B1';
    document.forms[form].elements['data[Product][color]['+id+']'].value=1;
  } else {
    document.getElementById(field+id).style.border='1px solid #FFF';
    document.forms[form].elements['data[Product][color]['+id+']'].value=0;
  }
}

function updateAddress(field,value) {
  document.getElementById(field).innerHTML=value;
}

function checkTextarea(id) {
  var comment=document.getElementById(id).value;
  if(comment.length<1) {
    return false;
  }
}

function switchToDot(id) {
  
}

function updatePrice(profit) {
  basic=document.getElementById('price_basic').value;
  if(basic!='') {
    calculatePrice('price',profit);
  }
  basic_prom=document.getElementById('price_prom_basic').value;
  if(basic_prom!='') {
    calculatePrice('price_prom',profit);
  }
}

function promotionPrice() {
  if(document.getElementById('prom').checked==true) {
    showDiv('price-prom-div');
    showDiv('price-prom-vat');
    showDiv('price-prom-margin');
    showDiv('price-prom-no-tax');
    showDiv('price-prom');
  } else {
    hideDiv('price-prom-div');
    hideDiv('price-prom-vat');
    hideDiv('price-prom-margin');
    hideDiv('price-prom-no-tax');
    hideDiv('price-prom'); 
    document.getElementById('price_prom_basic').value='';
    document.getElementById('price_prom_no_tax').innerHTML='';
    document.getElementById('price_prom_tax').innerHTML='';
    document.getElementById('price_prom_margin').innerHTML='';
    document.getElementById('price_prom').innerHTML='';
    document.getElementById('price_prom_no_tax_value').value='0';
    document.getElementById('price_prom_tax_value').value='0';
    document.getElementById('price_prom_margin_value').value='0';
    document.getElementById('price_prom_margin_tax_value').value='0';
    document.getElementById('price_prom_value').value='0';
  }
}

function calculatePrice(field,profit) {
  if(document.getElementById('vat9').checked) {
    vat=0.09;
  } else if(document.getElementById('vat23').checked) {
    vat=0.23;
  } else {
    vat=0;
  }
  vat=parseFloat(1+parseFloat(vat));
  basic=document.getElementById(field+'_basic').value;
  if(basic!='') {
    basic=parseFloat(basic);
    var price_no_tax=(basic/vat).toFixed(2);
    tax=(basic-price_no_tax).toFixed(2);
    margin_no_tax=parseFloat((price_no_tax*profit/100).toFixed(2));
    var margin=parseFloat((margin_no_tax+(margin_no_tax*0.23)).toFixed(2));
    margin_tax=(margin-margin_no_tax).toFixed(2);
    var price=basic+margin;
    price=price.toFixed(2);
    if(field=='price_prom') document.getElementById('prom').checked=true;
  } else {
    tax=''; price_no_tax=''; margin=''; margin_no_tax=''; margin_tax=''; price='';
    if(field=='price_prom') document.getElementById('prom').checked=false;
  }
  document.getElementById(field+'_tax_value').value=tax;
  document.getElementById(field+'_tax').innerHTML=tax;
  document.getElementById(field+'_no_tax_value').value=price_no_tax;
  document.getElementById(field+'_no_tax').innerHTML=price_no_tax;
  document.getElementById(field+'_margin_value').value=margin;
  document.getElementById(field+'_margin').innerHTML=margin;
  document.getElementById(field+'_margin_tax_value').value=margin_tax;
  document.getElementById(field).innerHTML=price;
  document.getElementById(field+'_value').value=price;
  
  dis=document.getElementById('discount-checkbox').checked;
  if(dis==true) {
    countDiscount(profit);
  }
}

function countDiscount(profit) {
  discount_val=document.getElementById('discount_value_value').value;
  price_basic=document.getElementById('price_basic').value;
  if(discount_val!='' && price_basic!='') {
    discount_val=parseFloat(discount_val);
    price_basic=parseFloat(price_basic);
    discount_price_basic=parseFloat((price_basic-(price_basic*(discount_val/100))).toFixed(2));
    document.getElementById('price_after_discount_basic_value').value=discount_price_basic;
    document.getElementById('price_after_discount_basic').innerHTML=discount_price_basic;
    margin=parseFloat((discount_price_basic*(profit/100)).toFixed(2));
    document.getElementById('price_after_discount_margin_value').value=margin;
    document.getElementById('price_after_discount_margin').innerHTML=margin;
    total=parseFloat((discount_price_basic+margin).toFixed(2));
    document.getElementById('price_after_discount_value').value=total;
    document.getElementById('price_after_discount').innerHTML=total;
  }
}

function discount(ch,profit) {
  if(ch==true) {
    document.getElementById('discount').style.display='';
    countDiscount(profit);
  } else {
    document.getElementById('discount').style.display='none';
  } 
}

function updateRates(num) {
  for(i=1;i<=num;i++) {
    document.getElementById('rate-'+i).src='/img/icons/rate.png';
  }
  if(num<5) {
    for(i=num+1;i<=5;i++) {
      document.getElementById('rate-'+i).src='/img/icons/rate-grey.png';
    }
  }
}
