/**
 * handles the ajax response codes
 */
function handle_ajax_errors(s_status_code)
{
 
  switch (parseInt(s_status_code)) 
  {

    case 401:
    {
    
      document.location.href = $("#DOMAIN_NAME").val()+ "login/";
      break;
    }//cs
    
    
    case 404:
    {
    
      alert("not found");
      break;
    }//cs
     
         
    default:
    {
    
      alert("error status:"+ s_status_code);  
      break;
     }//df
  }//sw

  return(false);
}//fun

function detect_browser()
{
  var s_resp = "FF";
  
  if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
  { 
  	//test for MSIE x.x;
    s_resp = "IE";
  }//if
  return(s_resp);
}//fun

function listen_action_key(s_id_element, n_key, s_action_name)
{ 
 $("#"+ s_id_element).keydown(function(e)
 {
   var n_captured_key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
   if (n_captured_key == n_key)
   {
    
     eval(s_action_name)();
   }//if
 });  
}//fun

function avoid_back_flickr()
{
	try {
  document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}//fun

function listener_highlight_inputs(s_identificator) 
{
  $(s_identificator).focus(function()
	{

		$(this).css({'background-color' : '#FFFEC0', 'font-weight' : 'normal', 'border': '2px solid #F5E0A2'});
	});
	$(s_identificator).blur(function()
	{

		$(this).css({'background-color' : 'white', 'font-weight' : 'normal', 'border': '1px solid #5D86BA'});
	});
	
}//fun

//http://www.quirksmode.org/js/events_properties.html
function get_event(e)
{
  if (!e) var e = window.event;
  return(e);
}//fun

function get_target_element(e)
{
 
  var targ;
  var e = get_event(e);
  if (e.target) targ = e.target;
  else if (e.srcElement) targ = e.srcElement;
  if (targ.nodeType == 3) // defeat Safari bug
    targ = targ.parentNode;
  return(targ);  
}//fun

function check_mailformat(s_email)
{
  var b_resp = true;
  var mail_pattern = new RegExp("^[a-zA-Z0-9._-]+([+][a-zA-Z0-9._-]+){0,1}[@][a-zA-Z0-9._-]+[.][a-zA-Z]{2,6}$");
 
	if(!mail_pattern.test(s_email))
	{
	  b_resp = false;
	}//if

  return(b_resp);	  
}//fun

function ajax_petition(a_data, s_url, s_responsef, b_sync)
{
  b_async = true;
  if (b_sync == true)
  {
    b_async = false;
  }//if
  $.ajax({url: s_url, dataType: "text", cache: false, type: "POST", async:b_async,
    data: a_data, error: function(xmlhttp){handle_ajax_errors(xmlhttp.status); 
    },success: eval(s_responsef)});
}//fun

//---- PROGRAM ----
$(document).ready(function()
{
	
	
	avoid_back_flickr();
	listener_highlight_inputs(".input4form");
	
});


function urldecode(str)
{

	var histogram = {}, ret = str.toString(), unicodeStr='', hexEscStr='';
	 
	var replacer = function(search, replace, str) {
	    var tmp_arr = [];
	    tmp_arr = str.split(search);
	    return tmp_arr.join(replace);
	};
     
    // The histogram is identical to the one in urlencode.
    histogram["'"]   = '%27'; histogram['(']   = '%28'; histogram[')']   = '%29';
    histogram['*']   = '%2A'; histogram['~']   = '%7E'; histogram['!']   = '%21';
    histogram['%20'] = '+';  histogram['\u00DC'] = '%DC'; histogram['\u00FC'] = '%FC';
    histogram['\u00C4'] = '%D4'; histogram['\u00E4'] = '%E4'; histogram['\u00D6'] = '%D6';
    histogram['\u00F6'] = '%F6'; histogram['\u00DF'] = '%DF'; histogram['\u20AC'] = '%80';
    histogram['\u0081'] = '%81'; histogram['\u201A'] = '%82'; histogram['\u0192'] = '%83';
    histogram['\u201E'] = '%84'; histogram['\u2026'] = '%85'; histogram['\u2020'] = '%86';
    histogram['\u2021'] = '%87'; histogram['\u02C6'] = '%88'; histogram['\u2030'] = '%89';
    histogram['\u0160'] = '%8A'; histogram['\u2039'] = '%8B'; histogram['\u0152'] = '%8C';
    histogram['\u008D'] = '%8D'; histogram['\u017D'] = '%8E'; histogram['\u008F'] = '%8F';
    histogram['\u0090'] = '%90'; histogram['\u2018'] = '%91'; histogram['\u2019'] = '%92';
    histogram['\u201C'] = '%93'; histogram['\u201D'] = '%94'; histogram['\u2022'] = '%95';
    histogram['\u2013'] = '%96'; histogram['\u2014'] = '%97'; histogram['\u02DC'] = '%98';
    histogram['\u2122'] = '%99'; histogram['\u0161'] = '%9A'; histogram['\u203A'] = '%9B';
    histogram['\u0153'] = '%9C'; histogram['\u009D'] = '%9D'; histogram['\u017E'] = '%9E';
    histogram['\u0178'] = '%9F'; 
    for (unicodeStr in histogram) {
        hexEscStr = histogram[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }
     
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);
 
    return ret;
}

function get_millisec_mark()
{
  var d = new Date();
  return((d.getMinutes() * 1000000) + (d.getSeconds() * 1000) + d.getMilliseconds());
}//fun

function array_search( needle, haystack, argStrict )
{
    var strict = !!argStrict;
    var key = '';
    for(key in haystack){
        if( (strict && haystack[key] === needle) || (!strict && haystack[key] == needle) ){
            return key;
        }
    }
    return null;
}//fun

function array_keys (input, search_value, argStrict) {
    var tmp_arr = {}, strict = !!argStrict, include = true, cnt = 0;
    var key = '';
    for (key in input) {
        include = true;
        if (search_value != undefined) {
            if (strict && input[key] !== search_value){
                include = false;
            } else if (input[key] != search_value){
                include = false;
            }
        }
        
        if (include) {
            tmp_arr[cnt] = key;
            cnt++;
        }
    }
    return tmp_arr;
}

function addslashes (str)
{
  return(str+'').replace(/([\\"'])/g, "\\$1").replace(/\u0000/g, "\\0");
}//fun
/*
function base64_decode(s){return(atob(s)); //return(Base64.decode(s));
}//fun
function base64_encode(s){return(btoa(s));//return(Base64.encode(s));
}//fun
*/
function base64_decode(s){return(Base64.decode(s)); //return(Base64.decode(s));
}//fun
function base64_encode(s){return(Base64.encode(s));//return(Base64.encode(s));
}//fun

function str_pad(input, pad_length, pad_string, pad_type)
{
var half = '', pad_to_go;

    var str_pad_repeater = function (s, len) {
        var collect = '', i;

        while (collect.length < len) {collect += s;}
        collect = collect.substr(0,len);

        return collect;
    };

    input += '';
    pad_string = pad_string !== undefined ? pad_string : ' ';
    
    if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') { pad_type = 'STR_PAD_RIGHT'; }
    if ((pad_to_go = pad_length - input.length) > 0) {
        if (pad_type == 'STR_PAD_LEFT') { input = str_pad_repeater(pad_string, pad_to_go) + input; }
        else if (pad_type == 'STR_PAD_RIGHT') { input = input + str_pad_repeater(pad_string, pad_to_go); }
        else if (pad_type == 'STR_PAD_BOTH') {
            half = str_pad_repeater(pad_string, Math.ceil(pad_to_go/2));
            input = half + input + half;
            input = input.substr(0, pad_length);
        }
    }
    return input;
}//fn    

function change_page(s_url)
{
  document.location.href = s_url;
}//fun

function adjust_size()
{
  var n_doch = $(document).height();
  n_headerh = $("#header_cnt").height();
  n_footerh = $("#footer").height();
  
  if (n_footerh != null)
  {
    n_tot = n_doch - (n_headerh + n_footerh); 
  	$("#content_body").css("height", n_tot+ "px");
  }//if
}//fun

function code2female(s_code){return("1"+ s_code.substr(1, s_code.length));}//fun
function code2male(s_code){return("0"+ s_code.substr(1, s_code.length));}//fun

//---- PROGRAM ----
$(document).ready(function()
{
	avoid_back_flickr();
	adjust_size();
});

$(window).resize(function() {
  adjust_size();
});
