﻿/*
  BROWSER IDENTIFICATION
*/
  telj.forms.isIE6 = function(){
    if( (jQuery.browser.msie) && (jQuery.browser.version == "6.0") ) return true;
    else return false;
  };

/*
  FORM ELEMENTS VERIFICATION : RESET ERRORS
*/   
  
  telj.forms.resetErrors = function(){
    $(".error_label").fadeOut('normal');
    $(":input").removeClass("error_input");
  };

/*
  FORM ELEMENTS VERIFICATION : SHOW ERROR
*/ 
  telj.forms.showError = function(elem, name){
    $(elem).focus();
    $(elem).addClass('error_input');

    if(name)
      $('#' + name + '_err').fadeIn('normal');
    else
      $('#'+$(elem).attr('id')+'_err').fadeIn('normal');    
  }; 

/*
  FORM ELEMENTS VERIFICATION
*/
  
  telj.forms.checkElem = function(elem){
    var res = true;
    var curr_elem = $(elem);
    var curr_elem_name = false;
    var reg_email = new RegExp("[a-zA-Z0-9_\-]+@[a-zA-Z0-9_\-]+\.+[a-zA-Z0-9_\-]+", "g");
    var reg_cp1 = new RegExp("[a-zA-Z][0-9][a-zA-Z]", "g");
    var reg_cp2 = new RegExp("[0-9][a-zA-Z][0-9]", "g");
    var reg_numeric = new RegExp("^[0-9\.]+$", "g");
    var curr_value = (curr_elem.val()) ? curr_elem.val() : '';
    
    if(curr_elem.hasClass('multi_field')) 
    {
      curr_elem_name = curr_elem.attr("name").substring(0, curr_elem.attr("name").length - 2);
     // alert(curr_elem_name);
    }
    
    if(curr_elem.is('input[type=text]'))
    {
      if(curr_elem.hasClass('email_field') && curr_value != '') 
      {
        res = ( curr_value.match(reg_email) );
      }
      else if(curr_elem.hasClass('email_validation_field') && curr_value != '') 
      {
        res = ( curr_value == curr_elem.parent().eq(0).find('.email_field').val() );
      }
      else if(curr_elem.hasClass('cp_field1') && curr_value != '') 
      {
        res = ( curr_value.match(reg_cp1) );
      }
      else if(curr_elem.hasClass('cp_field2') && curr_value != '') 
      {
        res = ( curr_value.match(reg_cp2) );
      }
      else if(curr_elem.hasClass('numeric_field') && curr_value != '') 
      {
        //res = ( parseInt(curr_value)+""!="NaN" && (parseInt(curr_value).toString().length == curr_value.length));
        res = ( curr_value.match(reg_numeric) );
      }
      else if(curr_elem.hasClass('float_field') && curr_value != '') 
      {
        //res = ( parseFloat(curr_value)+""!="NaN" && (parseFloat(curr_value).toString().length == curr_value.length));
        res = ( curr_value.match(reg_numeric) );
      }
      else if (curr_elem.hasClass('mdy_field'))
      {
        res = ( curr_value.length > 0 );
      }
    } 
    else if(curr_elem.is('input[type=radio]'))
    {
      var curr_radio_checked = $(":radio[name='"+curr_elem.attr("name")+"']:checked").length;
      var curr_radio_val = '';
      curr_elem_name = curr_elem.attr("name");

      if(curr_radio_checked) curr_radio_val = $(":radio[name='"+curr_elem.attr("name")+"']:checked").val();
      res = ( curr_radio_val != '' );
    }
    else if(curr_elem.is('input[type=checkbox]'))
    {
      res = true;
    }
    else if(curr_elem.is('select'))
    {
      res = ( ( curr_value.length > 1 ) || (parseInt(curr_value) > 0) );
    }
    else if(curr_elem.is('textarea'))
    {
      res = ( curr_value.length > 0 );
    }

    if(!res) telj.forms.showError(curr_elem, curr_elem_name);

  return res;
  };
  
/*
  TOOLTIPABLE
*/
  
  telj.forms.initTooltipable = function(elem){
    $(elem).tooltip({ 
      track: false, 
      delay: 0, 
      showURL: false,
      opacity: 1, 
      left: 0,
      top: 0,
      bodyHandler: function() { 
        return $(this).attr("rel"); 
      }
    });   
  };

/*
  SONDAGE API VOTE
*/

telj.forms.api_sondageVote = function(params)
{
  var callback = function(response){
    if (response.type != telj.SUCCESS)
		{
		  alert(response.msg);
		  return;
		}
    
    var results_html = '<ul>';
    var total_votes = parseInt(response.data.totalvotes);
        
    $.each(response.data._choices, function(i,choice)
    {
      var curr_percent = parseFloat(choice.votes / total_votes)*100; 
      results_html += '<li>' + choice.choice_text + ' [' + parseInt(curr_percent) + '%]</li>';
    });
    
    results_html += '</ul>';
    $("#form_sondage").hide();
    $("#form_sondage_results").append(results_html).fadeIn();
    
    return; 
  };
  
  telj.api.sondages.vote(callback, params);
  
  return false;
};

/*
  URL ENCODE
*/
  telj.forms.URLEncode = function(clearString) {
    var output = '';
    var x = 0;
    clearString = clearString.toString();
    var regex = /(^[a-zA-Z0-9_.]*)/;
    while (x < clearString.length) {
      var match = regex.exec(clearString.substr(x));
      if (match != null && match.length > 1 && match[1] != '') {
        output += match[1];
        x += match[1].length;
      } else {
        if (clearString[x] == ' ')
          output += '+';
        else {
          var charCode = clearString.charCodeAt(x);
          var hexVal = charCode.toString(16);
          output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
        }
        x++;
      }
    }
    return output;
  };
  
/*
  CUSTOM FORM ELEMENTS
*/  
  telj.forms.custom_cbx_elems = {};
  telj.forms.custom_cbx_elems.checkboxHeight = "25";
  telj.forms.custom_cbx_elems.radioHeight = "22";
  telj.forms.custom_cbx_elems.selectWidth = "190";

  telj.forms.custom_cbx = {
  	init: function() {
  		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
  		for(a = 0; a < inputs.length; a++) {
  			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
  				span[a] = document.createElement("span");
  				span[a].className = inputs[a].type;

  				if(inputs[a].checked == true) {
  					if(inputs[a].type == "checkbox") {
  						position = "0 -" + (telj.forms.custom_cbx_elems.checkboxHeight*2) + "px";
  						span[a].style.backgroundPosition = position;
  					} else {
  						position = "0 -" + (telj.forms.custom_cbx_elems.radioHeight*2) + "px";
  						span[a].style.backgroundPosition = position;
  					}
  				}
  				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
  				inputs[a].onchange = telj.forms.custom_cbx.clear;
  				span[a].onmousedown = telj.forms.custom_cbx.pushed;
  				span[a].onmouseup = telj.forms.custom_cbx.check;
  				document.onmouseup = telj.forms.custom_cbx.clear;
  			}
  		};
  		inputs = document.getElementsByTagName("select");
  		for(a = 0; a < inputs.length; a++) {
  			if(inputs[a].className == "styled") {
  				option = inputs[a].getElementsByTagName("option");
  				active = option[0].childNodes[0].nodeValue;
  				textnode = document.createTextNode(active);
  				for(b = 0; b < option.length; b++) {
  					if(option[b].selected == true) {
  						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
  					}
  				};
  				span[a] = document.createElement("span");
  				span[a].className = "select";
  				span[a].id = "select" + inputs[a].name;
  				span[a].appendChild(textnode);
  				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
  				inputs[a].onchange = telj.forms.custom_cbx.choose;
  			}
  		};
  	},
  	pushed: function() {
  		element = this.nextSibling;
  		if(element.checked == true && element.type == "checkbox") {
  			this.style.backgroundPosition = "0 -" + telj.forms.custom_cbx_elems.checkboxHeight*3 + "px";
  		} else if(element.checked == true && element.type == "radio") {
  			this.style.backgroundPosition = "0 -" + telj.forms.custom_cbx_elems.radioHeight*3 + "px";
  		} else if(element.checked != true && element.type == "checkbox") {
  			this.style.backgroundPosition = "0 -" + telj.forms.custom_cbx_elems.checkboxHeight + "px";
  		} else {
  			this.style.backgroundPosition = "0 -" + telj.forms.custom_cbx_elems.radioHeight + "px";
  		}
  	},
  	check: function() {
  		element = this.nextSibling;
  		if(element.checked == true && element.type == "checkbox") {
  			this.style.backgroundPosition = "0 0";
  			element.checked = false;
  		} else {
  			if(element.type == "checkbox") {
  				this.style.backgroundPosition = "0 -" + telj.forms.custom_cbx_elems.checkboxHeight*2 + "px";
  			} else {
  				this.style.backgroundPosition = "0 -" + telj.forms.custom_cbx_elems.radioHeight*2 + "px";
  				group = this.nextSibling.name;
  				inputs = document.getElementsByTagName("input");
  				for(a = 0; a < inputs.length; a++) {
  					if(inputs[a].name == group && inputs[a] != this.nextSibling) {
  						inputs[a].previousSibling.style.backgroundPosition = "0 0";
  					}
  				};
  			}
  			element.checked = true;
  		}
      $('#'+element.id).trigger('change');
  	},
  	clear: function() {
  		inputs = document.getElementsByTagName("input");
  		for(var b = 0; b < inputs.length; b++) {
  			if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
  				inputs[b].previousSibling.style.backgroundPosition = "0 -" + telj.forms.custom_cbx_elems.checkboxHeight*2 + "px";
  			} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
  				inputs[b].previousSibling.style.backgroundPosition = "0 0";
  			} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
  				inputs[b].previousSibling.style.backgroundPosition = "0 -" + telj.forms.custom_cbx_elems.radioHeight*2 + "px";
  			} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
  				inputs[b].previousSibling.style.backgroundPosition = "0 0";
  			}
  		};
  	},
  	choose: function() {
  		option = this.getElementsByTagName("option");
  		for(d = 0; d < option.length; d++) {
  			if(option[d].selected == true) {
  				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
  			}
  		};
  	}
  };
 
