// ------------------------ GENERAL ROUTINES ------------------------

//  CHECKNUMBER function
//  Checks the validity of a number.
//  Value:		the number itself.
//  Min:		minimum valid value, 0 if no limit
//  Max:		maximum valid value, 0 if no limit
//  Msgcode:	message to display if the number is invalid

function checknumber(value,min,max,msgcode) {

  if ((value<min && min>0) || (value>max && max>0) || value == '' || isNaN(value) || String(Number(value)) == 'undefined') {
     alert(msgcode);
     return false;
  } else return true;
}

//  -------------------------------------------------------------------------

//  CHECKSTRING function
//  Checks the validity of a string.
//  Value: 		the string itself.
//  Minlength: 		the minimum length of the string.
//  Maxlength:		the maximum length of the string.
//  Emptystring:	the code of the message to display if the string is empty.
//  Tooshort:		the code of the message to display if the string is too short.
//  Toolong:		the code of the message to display if the string is too long.

function checkstring(value, minlength, maxlength, emptystring, tooshort, toolong) {

   if (value =='') {
    alert(emptystring);
    return false;
   }

   if (value.length < minlength) {
    alert(tooshort);
    return false;
   }

   if (value.length > maxlength && maxlength > 0) {
    alert(toolong);
    return false;
   }

   return true;
}

//  -------------------------------------------------------------------------

//  GETDATEMAX function
//  Returns the number of days in a given month.

function getdatemax(mnth) {

switch (mnth) {
	case 1:		return(31);
	case 2:		return(29);
	case 3:		return(31);
	case 4:		return(30);
	case 5:		return(31);
	case 6:		return(30);
	case 7:		return(31);
	case 8:		return(31);
	case 9:		return(30);
	case 10:	return(31);
	case 11:	return(30);
	case 12:	return(31);
}

return false;

}

//  CHECKRADIO function
//  Checks if any values have been selected of the radio group, and returns the value. If none was selected, it 
//  returns the specified error message.

function checkradio(radiogroup, errormsg) {

for (i=0, n=radiogroup.length; i<n; i++) {
   if (radiogroup[i].checked) {
      var checkvalue = radiogroup[i].value;
      break;
   }
}

if (checkvalue == undefined) { 
	alert(errormsg);
	return false;
}

return checkvalue;

}


//  -------------------------------------------------------------------------

//  GETPARAM function
//  Returns the value of a HTTP parameter

function getparam(param) {

s = '';
t = 0;

while (document.location.search.substring(t, t+param.length) != param && t<=document.location.search.length) {
    t++;
}

while (document.location.search.substring(t, t+1) != '=' && t<=document.location.search.length) {
    t++;
}

t++;

while (document.location.search.substring(t, t+1) != '&' && t<=document.location.search.length) {
    s = s + document.location.search.substring(t, t+1);
    t++;
}

return s;

}

//  -------------------------------------------------------------------------
//  UNACCENT
//  Removes Hungarian accented characters from a string and replaces them with others

function unaccent(source) {
	output = '';
	for (a=0; a<source.length; a++) {
		added = false;
		if (source.substring(a, a+1) == 'ő') { output += 'õ'; added = true; }
		if (source.substring(a, a+1) == 'ű') { output += 'û'; added = true; }
		if (source.substring(a, a+1) == 'Ő') { output += 'Õ'; added = true; }
		if (source.substring(a, a+1) == 'Ű') { output += 'Û'; added = true; }
		if (added == false) { output += source.substring(a, a+1); }
	}
	return(output);
}

//  -------------------------------------------------------------------------
//  GETCOOKIE
//  Gets a cookie of a given name

function getcookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//  -------------------------------------------------------------------------
//  SETCOOKIE
//  Creates a cookie

function setcookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

//  -------------------------------------------------------------------------
//  DELETECOOKIE
//  Deletes a cookie

function deletecookie(name)
{
	setcookie(name,"",-1);
}
//  -------------------------------------------------------------------------
//  GETHTTPOBJECT
//  Creates a HTTPobject

function getHTTPObject() {
  if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
  else if (window.XMLHttpRequest) return new XMLHttpRequest();
  else {
    alert('Micsoda ócska böngészod van neked? Tölts le egy olyat, ami támogat AJAX-ot, mégiscsak a XXI. században élünk!');
    window.location='http://www.firefox.com';
    return null;
  }
} 

//  -------------------------------------------------------------------------
//  POST
//  Sends the given values

function post(postvalue, target, oncomplete) {

    http_request = getHTTPObject();
    http_request.onreadystatechange = function() { oncomplete(); }
    http_request.open('POST', target, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
    http_request.setRequestHeader("Content-length", postvalue.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(postvalue);

}

//  -------------------------------------------------------------------------
//  RETURNDOCUMENT
//  Retrieves the filename of the current document

function returnDocument() {
        var file_name = document.location.href;
        var end = (file_name.indexOf("?") == -1) ? file_name.length : file_name.indexOf("?");
        return file_name.substring(file_name.lastIndexOf("/")+1, end);
}

//  -------------------------------------------------------------------------
//  IN_ARRAY
//  Tells if a value is present in an array

function in_array(needle, haystack) {

        for (key in haystack) {
            if (haystack[key] == needle) { return key; }
        }
     return false;
}

//  -------------------------------------------------------------------------
//  FORMATCURRENCY
//  Formats a number as a currency

function formatcurrency(amount) { 
  Num = "" + eval(amount);
  dec = Num.indexOf(".");
  var end = ((dec > -1) ? "" + Num.substring(dec,Num.length) : ".00");
  Num = "" + parseInt(Num);
  var temp1 = "";
  var temp2 = "";
  if (end.length == 2) end += "0";
  if (end.length == 1) end += "00";
  if (end == "") end += ".00";
  var count = 0;
  for (var k = Num.length-1; k >= 0; k--) {
  var oneChar = Num.charAt(k);
  if (count == 3) {
  temp1 += " ";
  temp1 += oneChar;
  count = 1;
  continue;
  }
  else {
  temp1 += oneChar;
  count ++;
  }
  }
  for (var k = temp1.length-1; k >= 0; k--) {
  var oneChar = temp1.charAt(k);
  temp2 += oneChar;
  }
  return temp2;
}


function teszt() {
  alert('it worx');
}


