/*
  Javascript routines for Neutrina  module Eshop
  Copyright (C) 2010, Christoph Marti

  This Javascript routines are free software. You can redistribute it and/or modify it 
  under the terms of the GNU General Public License - version 2 or later, 
  as published by the Free Software Foundation: http://www.gnu.org/licenses/gpl.html.

  The Javascript routines are distributed in the hope that it will be useful, 
  but WITHOUT ANY WARRANTY; without even the implied warranty of 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  GNU General Public License for more details.
*/

// Function to test is value integer
function is_int(value){
  if((parseFloat(value) == parseInt(value)) && !isNaN(parseInt(value))){
      return true;
 } else {
      return false;
 }
}

// **********************************************************************************
//   Function to delete an item in the cart
// **********************************************************************************

function mod_eshop_delete_item_f(id) {
	if (id != '') {
		document.getElementById('id_' + id).value=0;
		document.getElementById('update').click();
	}
}



// **********************************************************************************
//   Function to toggle between state text field and drop down menu
// **********************************************************************************

function mod_eshop_toggle_state_f(shopCountry, type, clean) {
	if (shopCountry != '') {
		var country = document.getElementsByName(type + '_country')[0].value;
		if (country == shopCountry) {
			document.getElementById(type + '_state_text').style.display = 'none';
			document.getElementById(type + '_state_select').style.display = 'table-row';
			document.getElementsByName(type + '_state')[1].value = document.getElementsByName(type + '_state')[0].value;
		} else {
			document.getElementById(type + '_state_select').style.display = 'none';
			document.getElementById(type + '_state_text').style.display = 'table-row';
			if (clean == 1) {
				document.getElementsByName(type + '_state')[1].value = '';
				document.getElementsByName(type + '_state')[1].focus();
			}
		}
	}
}



// **********************************************************************************
//   Functions to take over the state select value to the state text field
// **********************************************************************************

function mod_eshop_synchro_cust_state_f() {
	document.getElementsByName('cust_state')[1].value = document.getElementsByName('cust_state')[0].value;
}

function mod_eshop_synchro_ship_state_f() {
	document.getElementsByName('ship_state')[1].value = document.getElementsByName('ship_state')[0].value;
}



// **********************************************************************************
//   Function to check if customer has agreed to the terms & conditions of the shop
// **********************************************************************************

function checkTaC(txt_agree) {
	if (document.getElementById("agree").checked != true) {
		alert(txt_agree);
		document.getElementById("agree").focus();
		return false;
	} else {
		return true;
	}
}

// Functions for inter-okno
function get_price_dim(count, item_id) {
    jQuery.blockUI({ css: {border: 'none',padding: '15px',backgroundColor: '#000','-webkit-border-radius': '5px','-moz-border-radius': '5px',opacity: '.8',color: '#fff'}, message:'<h3 style="font-size: 14px;">Prosím čakajte...</h3>', timeout: 2000 });
    var width = jQuery('#win_width').val(), height = jQuery('#win_height').val(), count_val = jQuery('#'+count).val(), max_width_set = false, max_height_set = false;
    
    jQuery(".mod_eshop_bt_add_f").attr("disabled","disabled");
    
    if (width.length > 2 && height.length > 2) {
    //parameters
    var profile = jQuery('input[name=win_profile]:checked').val(), farba = jQuery('input[name=farba]:checked').val(), sklo = jQuery('input[name=win_sklo]:checked').val(), sklo_variant = jQuery('input[name=win_sklo_variant]:checked').val(), vonk_parapet = jQuery('#vonk_parapet').val(), vnut_parapet = jQuery('#vnut_parapet').val(), zaluzie = jQuery('#zaluzie').val(), rolety = jQuery('#rolety').val(), sietka = jQuery('#sietka').val(), vonk_parapet_farba = jQuery('#vonk_parapet_farba').val(), vnut_parapet_farba = jQuery('#vnut_parapet_farba').val(), sietka_farba = jQuery('#sietka_farba').val(), rolety_farba = jQuery('#rolety_farba').val(), zaluzie_farby = jQuery('#zaluzie_farby').val();
    
    jQuery.ajax({
     type: "POST",
     url: WB_URL+"/modules/eshop/scripts/get_price.php",
     dataType: 'xml',
     data: { item_id: item_id,
      width: width,
      height: height,
      count: count_val,
      profile: profile,
      farba: farba,
      sklo: sklo,
      sklo_variant: sklo_variant,
      vonk_parapet: vonk_parapet,
      vnut_parapet: vnut_parapet,
      zaluzie: zaluzie,
      rolety: rolety,
      sietka: sietka,
      vonk_parapet_farba: vonk_parapet_farba,
      vnut_parapet_farba: vnut_parapet_farba,
      sietka_farba: sietka_farba,
      rolety_farba: rolety_farba,
      zaluzie_farby: zaluzie_farby},
     success: function(xml){
        var price = 0, height_set = 0, max_width, min_width, max_height, min_height,price_num = 0, max_height_set=false, max_width_set=false, min_height_set=false, min_width_set=false, delenie0 = false, delenie1 = false, delenie2 = false;
        
                
        jQuery(xml).find("response").each(function() {
          price = jQuery("price", this).text();
          price_num = jQuery("pricenum", this).text();
          height_set = jQuery("height", this).text();
          
          max_width = jQuery("max_width", this).text();
          min_width = jQuery("min_width", this).text();
          
          max_height = jQuery("max_height", this).text();
          min_height = jQuery("min_height", this).text();
        });
        
        if (jQuery("#delenie0").length > 0) {
          if (jQuery("#delenie0").val() == '') {
            delenie0 = false;
            jQuery('#delenie_error0').empty();
            jQuery('#delenie_error0').html('Napíšte hodnotu.');
            jQuery('#delenie_error0').show();
          } else {
            delenie0 = true;
            jQuery('#delenie_error0').hide();
          }
        } else {
          delenie0 = true;
        }
        
        if (jQuery("#delenie1").length > 0) {
          if (jQuery("#delenie1").val() == '') {
            delenie1 = false;
            jQuery('#delenie_error1').empty();
            jQuery('#delenie_error1').html('Napíšte hodnotu.');
            jQuery('#delenie_error1').show();
          } else {
            delenie1 = true;
            jQuery('#delenie_error1').hide();
          }
        } else {
          delenie1 = true;
        }
        
        if (jQuery("#delenie2").length > 0) {
          if (jQuery("#delenie2").val() == '') {
            delenie2 = false;
            jQuery('#delenie_error2').empty();
            jQuery('#delenie_error2').html('Napíšte hodnotu.');
            jQuery('#delenie_error2').show();
          } else {
            delenie2 = true;
            jQuery('#delenie_error2').hide();
          }
        } else {
          delenie2 = true;
        }
        
        if (is_int(width)) {
          if (width == '') {
            //jQuery('#win_width').val(min_width);
            //jQuery(".mod_eshop_bt_add_f").attr("disabled","disabled");
          } else {
            if (Number(width) > Number(max_width)) {
              jQuery('#error_width').empty();
              jQuery('#error_width').html('Vyberte rozmer od '+min_width+' mm do '+max_width+' mm.');
              jQuery('#error_width').show();
              max_width_set = false;
              //jQuery(".mod_eshop_bt_add_f").attr("disabled","disabled");
            } else {
              jQuery('#error_width').hide();
              //jQuery(".mod_eshop_bt_add_f").removeAttr("disabled");
              max_width_set = true;
            }
            
            if (Number(width) < Number(min_width)) {
              jQuery('#error_width').empty();
              jQuery('#error_width').html('Vyberte rozmer od '+min_width+' mm do '+max_width+' mm.');
              jQuery('#error_width').show();
              //jQuery(".mod_eshop_bt_add_f").attr("disabled","disabled");
              min_width_set = false;
            } else {
              /*if (max_width_set == false) {
                jQuery('#error_width').hide();
                jQuery(".mod_eshop_bt_add_f").removeAttr("disabled");
              }*/
              min_width_set = true;
            }
          }
        } else {
          jQuery(".mod_eshop_bt_add_f").attr("disabled","disabled");
          jQuery('#error_width').empty();
          jQuery('#error_width').html('Prosím zadajte číslá.');
          jQuery('#error_width').show();
        }

        if (is_int(height)) {
          if (height == '') {
            //jQuery('#win_height').val(height_set);
            //jQuery(".mod_eshop_bt_add_f").attr("disabled","disabled");
          } else {
            if (Number(height) > Number(max_height)) {
              jQuery('#error_height').empty();
              jQuery('#error_height').html('Vyberte rozmer od '+min_height+' mm do '+max_height+' mm.');
              jQuery('#error_height').show();
              //jQuery(".mod_eshop_bt_add_f").attr("disabled","disabled");
              max_height_set = false;
            } else {
              jQuery('#error_height').hide();
              //jQuery(".mod_eshop_bt_add_f").removeAttr("disabled");
              max_height_set = true;
            }
          
            if (Number(height) < Number(min_height)) {
              jQuery('#error_height').empty();
              jQuery('#error_height').html('Vyberte rozmer od '+min_height+' mm do '+max_height+' mm.');
              jQuery('#error_height').show();
              //jQuery(".mod_eshop_bt_add_f").attr("disabled","disabled");
              min_height_set = false;
            } else {
              /*if (max_height_set == false) {
                jQuery('#error_height').hide();
                jQuery(".mod_eshop_bt_add_f").removeAttr("disabled");
              }*/
              min_height_set = true;
            }
          }
        } else {
          jQuery(".mod_eshop_bt_add_f").attr("disabled","disabled");
          jQuery('#error_height').empty();
          jQuery('#error_height').html('Prosím zadajte číslá.');
          jQuery('#error_height').show();
        }
        
      //alert(delenie0+'/n'+delenie1+'/n'+delenie2)
        
        if (is_int(height) && is_int(width)) {
          jQuery('#current_price').val(price_num);
          
          jQuery('#price').empty();
          jQuery('#price').html(price);
        } else {
          jQuery('#current_price').val(0);
          
          jQuery('#price').empty();
          jQuery('#price').html('0,00');
        }
        
        if (max_width_set == true &&  min_width_set == true && max_height_set == true && min_height_set == true && delenie0 == true && delenie1 == true && delenie2 == true) {
        jQuery(".mod_eshop_bt_add_f").removeAttr("disabled");
       } else {
        jQuery(".mod_eshop_bt_add_f").attr("disabled","disabled");
       } 
      }
    });
    }
  jQuery.unblockUI();
  }

function disable_sietka() {
		var profile = jQuery('input[name=win_profile]:checked').val();
		if (profile == 6) {
			jQuery('#sietka').attr('disabled', 'disabled');
		} else {
			jQuery('#sietka').removeAttr('disabled');
		}
	}
  
function show_login(){
  jQuery("#shop_login").slideDown("slow");
  jQuery("#shop_register").slideUp("slow");
}

function show_register(){
  jQuery("#shop_register").slideDown("slow");
  jQuery("#shop_login").slideUp("slow");
}
  
jQuery(document).ready(function(){ 
	jQuery.noConflict();
  
  jQuery("#detail").validate({
    rules: {
      "delenie[]": "required"
    }
  });
  jQuery.extend(jQuery.validator.messages, {
		required: " Povinná položka.",
		minlength: jQuery.validator.format("Minimálne {0} znakov."),
		rangelength: jQuery.validator.format("Minimálne {0} a Maximálne {0} znakov."),
		email: "Napíšte platný e-mail (meno@domena.sk)",
		equalTo: "Heslá nie sú rovnaké.",
	});
  //jQuery("#shop_register").slideUp("slow");
  jQuery('input[name=farba]').click(function (){
    if (jQuery('input[name=farba]:checked').val() == 2 || jQuery('input[name=farba]:checked').val() == 3 ) {
      jQuery('#okno_farby').show();
    } else {
      jQuery('#okno_farby').hide();
    }
  });
  
  jQuery('#zaluzie').click(function (){
    if (jQuery('#zaluzie').val() != '' ) {
      jQuery('#zaluzie_farby').show();
    } else {
      jQuery('#zaluzie_farby').hide();
    }
  });
  
  
  //Calculate price from parameters
  var count_current = jQuery('#count_name').val(), type_id = jQuery('#type_id').val();
  
  jQuery('input[name=win_profile]').click(function(){
    get_price_dim(count_current, type_id);
  });
  
  jQuery('input[name=farba]').click(function(){
    get_price_dim(count_current, type_id);
  });
  
  jQuery('input[name=win_sklo]').click(function(){
    get_price_dim(count_current, type_id);
  });
  
  jQuery('input[name=win_sklo_variant]').click(function(){
    get_price_dim(count_current, type_id);
  });
  
  jQuery('#vonk_parapet').change(function(){
    get_price_dim(count_current, type_id);
  });
  
  jQuery('#vnut_parapet').change(function(){
    get_price_dim(count_current, type_id);
  });
  
  jQuery('#zaluzie').change(function(){
    get_price_dim(count_current, type_id);
  });
  
  jQuery('#sietka').change(function(){
    get_price_dim(count_current, type_id);
  });
  
  jQuery('#rolety').change(function(){
    get_price_dim(count_current, type_id);
  });
  
  jQuery('#vonk_parapet_farba').change(function(){
    get_price_dim(count_current, type_id);
  });
  
  jQuery('#vnut_parapet_farba').change(function(){
    get_price_dim(count_current, type_id);
  });
  
  jQuery('#zaluzie_farby').change(function(){
    get_price_dim(count_current, type_id);
  });
  
  jQuery('#sietka_farba').change(function(){
    get_price_dim(count_current, type_id);
  });
  
  jQuery('#rolety_farba').change(function(){
    get_price_dim(count_current, type_id);
  });
  
  jQuery('#prace').click(function(){
    if (jQuery('#vymur').val() == '') {
      if (jQuery('#prace').is(':checked')) {
        jQuery('#vymur_table').show();
      } else {
        jQuery('#vymur_table').hide();
      }
    }
  });
  
  jQuery('.user_type').click(function(){
    if (jQuery(this).val() == 2) {
      jQuery('#ut_firm').show();
    } else {
      jQuery('#ut_firm').hide();
    }
  });
  
  // Flipping image
  // Globals
  var mirrored;
  var original;

  // CLick handler
  jQuery(".flip").click(function(){
                
  // Image src
  var source = jQuery('.horizontal').find("img").attr('src');

  var flip = 1;
 
  // Are we reverting back to the original?
  if(source.indexOf(WB_URL+"/modules/eshop/image_flip.php") != -1){
  jQuery('.horizontal').find("img").attr('src',original);
  jQuery('#otocenie').val(0);
  } else {
    
  // Mirroring - show feedback
  //jQuery('.horizontal').parent().prepend('<p id="mirror_feedback">Crunching...</p>');

  // Store original so we can revert
  original = jQuery('.horizontal').find("img").attr('src');

  // Switch the image src to the server-side processor
  jQuery('.horizontal').find("img").attr('src',WB_URL+'/modules/eshop/image_flip.php?img='+source+"&flip="+flip);

  // Triggered when new image has loaded
  jQuery('.horizontal').find("img").load(function () {
  //jQuery("#mirror_feedback").remove();
  });
  jQuery('#otocenie').val(1);
  }
  // Stop anchor links being followed
  return false;
  });
});

