
function add_compare(chkbox, add_url){
 var new_elem ='<li class="compare_element" >\
 <div class="compare-content">\
 <a href="#{url}" title="#{name}">\
 <img src="#{thumb}" alt="#{name}" width="75" height="75" /><br />'+ 
 '<a class="comp-120" href="#{url}" title="#{name}">#{name}</a><br />\
 #{price}\
 </div>\
 <div class="compare-remove">\
 <a id="#{sku}" href="#" onclick="return del_compare(this, \'#{del_url}\');"><img src="#{button}" alt="remove" /></a>\
 </div>\
 </li>';
 
 if($(chkbox))chkbox.disable();
 
 // the template (our formatting expression)
 var myTemplate = new Template(new_elem);
 //do request 
 new Ajax.Request(add_url,
 {
 onSuccess: function(transport){
 var response = transport.responseText;
 //alert("Success! \n\n" + response);
 if(response === ''){ 
 alert('The item is already on the compare list');
 if(chkbox)chkbox.checked = true;
 return;
 }
 var data = response.evalJSON((true));
 
 //add element
 var elemHTML = myTemplate.evaluate(data);
 $('compare-last').insert( { before: elemHTML });
 
 //alert(elemHTML); 
 
 //update no of elements
 var n_items = $('n_items').innerHTML*1+1;
 $('n_items').update(n_items);
 //show div if needed
 if(n_items === 1) Effect.BlindDown('compare-summit');
 },
 onFailure: function(){ window.location.reload(); }
 }); 
 
 return false;
}

function del_compare(elem, del_url){
 //remove elements
 //alert('algo');
 var comp_elem = $(elem).ancestors()[1]; 
 Effect.Fade(comp_elem); 
 //update no of elements
 var n_items = $('n_items').innerHTML-1;
 $('n_items').update(n_items);
 //do request
 new Ajax.Request(del_url,
 {
 onSuccess: function(transport){
 var response = transport.responseText;
 //alert("Success! \n\n" + response);
 if (response !== 'OK') window.location.reload();
 comp_elem.remove();
 //enable checkbox
 if($('chk_'+elem.id)){
 $('chk_'+elem.id).enable();
 $('chk_'+elem.id).checked = false;
 }
 if($('lchk_'+elem.id)){
 $('lchk_'+elem.id).enable();
 $('lchk_'+elem.id).checked = false;
 } 
 //hide div if needed
 if(n_items < 1) Effect.BlindUp('compare-summit');
 },
 onFailure: function(){ window.location.reload(); }
 }); 
 
 return false; 
}

function clear_compare(clear_url){
 if (!confirm('Are you sure you would like to remove all products from your comparison?')) return false;
 
 //hide div 
 Effect.BlindUp('compare-summit'); 
 
 var checkboxes = $$('.compare-checkbox');
 checkboxes.each(function(chk){
 chk.enable();
 chk.checked = false;
 }); 
 //do request
 new Ajax.Request(clear_url,
 {
 onSuccess: function(transport){
 var response = transport.responseText;
 //alert("Success! \n\n" + response);
 if (response !== 'OK') window.location.reload();
 var comp_elems = $$('.compare_element');
 //remove elements
 comp_elems.each(function(elem){
 elem.remove();
 }); 
 //update no of elements
 $('n_items').update('0');
 },
 onFailure: function(){ window.location.reload(); }
 }); 
 
 return false;
}
function fetch_object(idname)
{
 if (document.getElementById)
 {
 return document.getElementById(idname);
 }
 else if (document.all)
 {
 return document.all[idname];
 }
 else if (document.layers)
 {
 return document.layers[idname];
 }
 else
 {
 return null;
 }
}

function colexp(objid,action)
{
 obj = fetch_object(objid);
 cel = fetch_object(objid);
 img = fetch_object(objid);
 
 
 switch (action)
 {
 case 'expand':
 obj.style.display = '';
 break;
 
 case 'colapse':
 obj.style.display = 'none';
 break;
 
 case '':
 if (obj.style.display == 'none')
 {
 
 obj.style.display = '';
 }
 else
 {
 obj.style.display = 'none';
 } 
 break;
 
 }
 return false;

}

function doFormValidate(){
 var strError="";
 
 
 
 if ($('budget'))
 {
 if((isNaN(parseFloat($('budget').value)))||(isNaN($('budget').value))||(parseFloat($('budget').value)<=0)){
 strError = "Budget value should be greater than zero. <br/>";
 }
 }
 if($('show-meters').style.display=='none'){
 //the user is submitting meters values
 
 $('measure-type').value = 'meters';
 if ($('dimensions').checked){
 if (($('dwm').value)=="")
 $('dwm').value = 0;
 if (($('dwcm').value)=="")
 $('dwcm').value = 0;
 if (($('dlm').value)=="")
 $('dlm').value = 0;
 if (($('dlcm').value)=="")
 $('dlcm').value = 0;
 
 
 
 if((isNaN(parseFloat($('dwm').value))) || (isNaN(parseFloat($('dwcm').value))) ||(isNaN($('dwm').value))||(isNaN($('dwcm').value)) || parseFloat($('dwm').value)<0 || parseFloat($('dwcm').value) < 0 || parseFloat($('dwm').value)+parseFloat($('dwcm').value)<=0){
 strError += "Width value should be a number greater than zero. <br/>";
 }
 if((isNaN(parseFloat($('dlm').value))) || (isNaN(parseFloat($('dlcm').value))) ||(isNaN($('dlm').value))||(isNaN($('dlcm').value)) || parseFloat($('dlm').value)<0 || parseFloat($('dlcm').value) < 0 || parseFloat($('dlm').value)+parseFloat($('dlcm').value)<=0){
 strError += "Length value should be a number greater than zero. <br/>";
 }
 }else{
 if (($('awm').value)=="")
 $('awm').value = 0;
 if (($('awcm').value)=="")
 $('awcm').value = 0;
 
 if((isNaN(parseFloat($('awm').value))) || (isNaN(parseFloat($('awcm').value))) ||(isNaN($('awm').value)) ||(isNaN($('awcm').value))|| parseFloat($('awm').value)<0 ||parseFloat($('awcm').value < 0) || parseFloat($('awm').value)+parseFloat($('awcm').value)<=0){
 strError += "Area value should be a number greater than zero. <br/>";
 }
 }
 }else{
 //the user is submitting inches value
 $('measure-type').value = 'inches';
 if ($('dimensions').checked){
 if (($('iwm').value)=="")
 $('iwm').value = 0;
 if (($('ilm').value)=="")
 $('ilm').value = 0;
 
 if((isNaN(parseFloat($('iwm').value)))||(isNaN($('iwm').value)) || ((parseFloat($('iwm').value)) <= 0)){
 strError += "Width value should be a number greater than zero. <br/>";
 }
 if((isNaN(parseFloat($('ilm').value)))||(isNaN($('ilm').value)) || ((parseFloat($('ilm').value)) <= 0)){
 strError += "Length value should be a number greater than zero. <br/>";
 }
 }else{
 if (($('iawm').value)=="")
 $('iawm').value = 0;
 
 if((isNaN(parseFloat($('iawm').value))) ||(isNaN($('iawm').value))|| ((parseFloat($('iawm').value)) <= 0)){
 strError += "Area value should be a number greater than zero. <br/>";
 }
 }
 
 }
 return strError;
}

function doFormValidateSubmit(){
 
 var strError = ""; 
 strError = doFormValidate();
 if (strError.length>0){
 //alert(strError);
 $('error-messages').innerHTML = strError;
 colexp('form-errors','expand');
 }else{
 colexp('form-errors','colapse');
 $('form-validate').submit();
 }
}

function doLoginFormValidateSubmit(){
 var strError = ""; 
 strError = doFormValidate();
 if (strError.length>0){
 //alert(strError);
 $('error-messages').innerHTML = strError;
 colexp('form-errors','expand');
 colexp('calculator-form','colapse');
 }else{
 colexp('form-errors','colapse');
 $('form-validate').submit();
 }
}

function showCalculatorForm(){
 colexp('form-errors','colapse');
 colexp('calculator-form','expand');
}

function calculateMts(){
 //inches to meters

 if ($('dimensions').checked){
 var elem = Array('iwm','ilm');
 for (var i = 0 ; i < elem.length; i ++ ){
 checkNull(elem[i]);
 }
 
 var w = (parseFloat($('iwm').value))*0.0254;
 var wmts = Math.round(w);
 var wcm = (w - wmts)*100;
 
 var l = (parseFloat($('ilm').value))*0.0254;
 var lmts = Math.round(l);
 var lcm = (l - lmts)*100;
 
 
 if (wmts > 0){
 $('dwm').value= wmts.toFixed(2);
 }else{
 $('dwm').value='';
 }
 
 if (wcm > 0){
 $('dwcm').value= wcm.toFixed(2);
 }else{
 $('dwcm').value= '';
 }
 
 if (lmts > 0){
 $('dlm').value=lmts.toFixed(2);
 }else{
 $('dlm').value= '';
 }
 
 if (lcm > 0){
 $('dlcm').value=lcm.toFixed(2);
 }else{
 $('dlcm').value= '';
 }
 
 
 }else{
 var elem = Array('iawm');
 for (var i = 0 ; i < elem.length; i ++ ){
 checkNull(elem[i]);
 }
 var a = (parseFloat($('iawm').value))*0.0254;

 var amts = Math.round(a);
 var acm = (a - amts)*100;
 
 if (amts > 0){
 $('awm').value= amts.toFixed(2);
 }else{
 $('awm').value='';
 }
 if (acm > 0){
 $('awcm').value= acm.toFixed(2);
 }else{
 $('awcm').value='';
 }
 
 }
}

function calculateInches(){
 
 //meters to inchs
 if ($('dimensions').checked){
 var elem = Array('dwm','dwcm','dlm','dlcm');
 for (var i = 0 ; i < elem.length; i ++ ){
 checkNull(elem[i]);
 }
 var w =(parseFloat($('dwm').value) + parseFloat($('dwcm').value)/100)* 39.37;
 var l = (parseFloat($('dlm').value) + parseFloat($('dlcm').value)/100)* 39.37;
 if (w != 0){
 $('iwm').value= w.toFixed(2);
 }else{
 $('iwm').value='';
 
 }
 if (l != 0){
 $('ilm').value=l.toFixed(2);
 }else{
 $('ilm').value='';
 }
 }else{
 var elem = Array('awm','awcm');
 for (var i = 0 ; i < elem.length; i ++ ){
 checkNull(elem[i]);
 }
 var a =(parseFloat($('awm').value) + parseFloat($('awcm').value)/100)* 39.37;
 
 if (a != 0){
 $('iawm').value= a.toFixed(2);
 }else{
 $('iawm').value='';
 }
 
 }
}

function showInches(){ 
 if ($('dimensions').checked){
 colexp('inches-fields','expand');
 }else{
 colexp('inches-area-fields','expand');
 }
 colexp('show-meters','expand');
 colexp('show-inches','colapse');
 colexp('dimensions-fields','colapse');
 colexp('area-fields','colapse');
 calculateInches();
}

function showMeters(){
 if ($('dimensions').checked){
 colexp('dimensions-fields','expand');
 }else{
 colexp('area-fields','expand');
 }
 colexp('show-meters','colapse');
 colexp('show-inches','expand');
 colexp('inches-fields','colapse');
 colexp('inches-area-fields','colapse');
 calculateMts();
}

function showDimensions(){
 if ($('show-inches').style.display=='none'){
 colexp('inches-fields','expand');
 colexp('dimensions-fields','colpase');
 }else{
 colexp('inches-fields','colapse');
 colexp('dimensions-fields','expand');
 }
 colexp('area-fields','colapse');
 colexp('inches-area-fields','colapse'); 
}
function showArea(){
 if ($('show-inches').style.display=='none'){
 colexp('area-fields','colpase');
 colexp('inches-area-fields','expand');
 }else{
 colexp('area-fields','expand');
 colexp('inches-area-fields','colapse');
 }
 colexp('dimensions-fields','colapse');
 colexp('inches-fields','colapse');
}
function checkNull(Id){
 if ( isNaN(parseInt($(Id).value))){
 $(Id).value=0;
 }
}
function clean(myclass){
 $$('.'+myclass).each(function(s) { 
 s.value="";
 });
 $('check-fitting').checked = false;
 $('check-underlay').checked = false;
}

function clearCalculatorSessionAjax(url){
 
 var ajaxRequest = new Ajax.Request(
 url,
 {
 method: 'post',
 asynchronous: true,
 onSuccess: function(transport) {
 res=transport.responseText.evalJSON(true);
// if(res=="ok"){
// alert('borre la sesion');
// }
 },
 onFailure: function(){window.location.reload(); }
 });
}
function doFormQuickFindValidateSubmit(){
 var room =$('room_type').value;
 var colour =$('sleepright_colour').value;
 var from =$('from').value;
 var to =$('to').value;
 var cat = $('category').value; 

 if (room!="" || colour !="" || from !="" || to !="" || cat !=""){ 
 $('form-validate-quickfind').submit();
 }
}
function comboStates(){
 var room =$('room_type');
 var colour =$('sleepright_colour');
 var from =$('from');
 var to =$('to');
 if ($('category').value==''){
 room.disabled=true;
 colour.disabled=true;
 from.disabled=true;
 to.disabled=true;
 }else{
 room.disabled=false;
 colour.disabled=false;
 from.disabled=false;
 to.disabled=false;
 }
}

