// JavaScript Document
function rand()
{
	var myDate = new Date();
	return myDate.getYear()+""+myDate.getMonth()+""+myDate.getDate()+""+myDate.getHours()+""+myDate.getMinutes()+""+myDate.getSeconds()+""+myDate.getTime();
}

function twoDigits(text)
{
	text = ""+text+"";
	if(text.length == 1)
	{
		text = "0" + text;
	}
	return text;
}

function fbs_click() {
  var u=location.href;
  var t=document.title;
  window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&amp;t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}

function el_name(element)
{
	return document.getElementsByName(element);
}

function number_format (number, decimals, dec_point, thousands_sep) {
    var n = number, prec = decimals;
 
    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };
 
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
 
    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
 
    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;
 
    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;
 
        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }
 
    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}

function isset () {
    var a=arguments, l=a.length, i=0;
    
    if (l===0) {
        throw new Error('Empty isset'); 
    }
    
    while (i!==l) {
        if (typeof(a[i])=='undefined' || a[i]===null) { 
            return false; 
        } else { 
            i++; 
        }
    }
    return true;
}

function strlen (string) {
    var str = string+'';
    var i = 0, chr = '', lgth = 0;

    var getWholeChar = function (str, i) {
        var code = str.charCodeAt(i);
        var next = '', prev = '';
        if (0xD800 <= code && code <= 0xDBFF) { // High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters)
            if (str.length <= (i+1))  {
                throw 'High surrogate without following low surrogate';
            }
            next = str.charCodeAt(i+1);
            if (0xDC00 > next || next > 0xDFFF) {
                throw 'High surrogate without following low surrogate';
            }
            return str.charAt(i)+str.charAt(i+1);
        } else if (0xDC00 <= code && code <= 0xDFFF) { // Low surrogate
            if (i === 0) {
                throw 'Low surrogate without preceding high surrogate';
            }
            prev = str.charCodeAt(i-1);
            if (0xD800 > prev || prev > 0xDBFF) { //(could change last hex to 0xDB7F to treat high private surrogates as single characters)
                throw 'Low surrogate without preceding high surrogate';
            }
            return false; // We can pass over low surrogates now as the second component in a pair which we have already processed
        }
        return str.charAt(i);
    };

    for (i=0, lgth=0; i < str.length; i++) {
        if ((chr = getWholeChar(str, i)) === false) {
            continue;
        } // Adapt this line at the top of any loop, passing in the whole string and the current iteration and returning a variable to represent the individual character; purpose is to treat the first part of a surrogate pair as the whole character and then ignore the second part
        lgth++;
    }
    return lgth;
}

function strpos (haystack, needle, offset) {
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}

function str_repeat ( input, multiplier ) {
    // Returns the input string repeat mult times  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/str_repeat
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // *     example 1: str_repeat('-=', 10);
    // *     returns 1: '-=-=-=-=-=-=-=-=-=-='
    
    
    return new Array(multiplier+1).join(input); 
}

function substr (f_string, f_start, f_length) {
    // Returns part of a string  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/substr
    // +     original by: Martijn Wieringa
    // +     bugfixed by: T.Wild
    // +      tweaked by: Onno Marsman
    // *       example 1: substr('abcdef', 0, -1);
    // *       returns 1: 'abcde'
    // *       example 2: substr(2, 0, -6);
    // *       returns 2: ''
    f_string += '';

    if (f_start < 0) {
        f_start += f_string.length;
    }

    if (f_length == undefined) {
        f_length = f_string.length;
    } else if (f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }

    if (f_length < f_start) {
        f_length = f_start;
    }

    return f_string.substring(f_start, f_length);
}

function LimitText(name,counter,tmax)
{
  var tNow=name.value.length;
  if(tNow>tmax)
  {
    name.value=name.value.substring(0,tmax);
  }else
  {
    counter.value=tmax-tNow;
  }
}


function set_round(number, precision)
{
  
  if(strpos(number, '.') && (strlen(substr(number, strpos(number, '.')+1)) > precision))
  {
	number = substr(number, 0, strpos(number, '.') + 1 + precision + 1);
	alert(substr(number,-1));
	if(substr(number, -1) >=5)
	{
	  if (precision > 1) {
          number = substr(number, 0, -1) + ('0.' + str_repeat(0, precision-1) + '1');
      } else if (precision == 1) {
          number = substr(number, 0, -1) + 0.1;
      } else {
          number = substr(number, 0, -1) + 1;
      }
	}else{
	  number = substr(number, 0, -1);
	}
  }
  return number;
}

function _ext(name,Ext)
{
	var extValid=Ext.split(",");
	var len=extValid.length;
	var ada=false;
	var splitFile=name.split(".");
	var getExt=splitFile.length-1;
	var ext=splitFile[getExt].toLowerCase();
	for(var pointer=0; pointer<len; pointer++)
	{
		if(ext==extValid[pointer])
		{
			ada=true;
		}
	}
	if(ada==true)
	{
		return 1;
	}else
	{
		return 0;
	}
}

/*
function set_round(number, precision) {
    if (strpos(number, '.') && (strlen(substr(number, strpos(number, '.')+1)) > precision)) {
      number = substr(number, 0, strpos(number, '.') + 1 + precision + 1);

      if (substr(number, -1) >= 5) {
        if (precision > 1) {
          number = substr(number, 0, -1) + ('0.' . str_repeat(0, precision-1) . '1');
        } else if (precision == 1) {
          number = substr(number, 0, -1) + 0.1;
        } else {
          number = substr(number, 0, -1) + 1;
        }
      } else {
        number = substr(number, 0, -1);
      }
    }

    return number;
  }
*/
