function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
// -------- Popup windows ------------

function MM_openBrWindow(theURL,winName,features) { //v2.1 by LV
	if(MM_openBrWindow.arguments[3]) {
		caller=MM_openBrWindow.arguments[3];
		if(caller.href) { caller.href="#"; } //clear simple HTML link provided for compatibility
	}
	close_MM_popupWindow()
	document.MM_popupWindow = window.open(theURL,winName,features);
}

function close_MM_popupWindow() {
		if(document.MM_popupWindow != null) {
			if((document.all && document.MM_popupWindow) || (document.MM_popupWindow.document)){
				document.MM_popupWindow.close()
				document.MM_popupWindow = null
		}  }

}


function showStock(f, min) {
  if(Ajax == undefined) return false; //don't show error if prototype is not loaded (not included in all pages)
  var offset;
  var n ;
  
  //homogenize numeric value
  f.value = f.value.trim().stripChar(' '); // transform '  12 000 ' -> '12000'
  f.value = f.value.replace(',', '.');
  //reset value to '0' if not numeric
  if(isNaN(Number(f.value)) || String(Number(f.value)) != f.value) { // ' ' doesn't give NaN as result
    f.value = 0;
  }

  //retrieve id from field name
  var re = new RegExp('(darab|szorzo)_([0-9]+)');
  var match = re.exec(f.name);
  var n = match ? match[2] : null;
  var quantity = 0;
  //check which type of field has called us
  if(f.name.indexOf('darab') == 0) {
    // check mininmal order quantity
    checkMin(f, min);
  }
  else if(f.name.indexOf('szorzo') == 0) {
      if(f.value.indexOf('.') >= 0) {
        f.value = f.value.replace(new RegExp('\\..*$'), '');
        alert('Végtelenített szalagfűrész darabszáma csak egész szám lehet!\n\nA tizedes jegyeket levágtuk.');
      }
      if(f.value == 0) f.value = ''; // a value of 0 could be misleading so empty it
  }
  else {
    alert('Incorrect field name in function ShowStock!');
    return false;
  }

  if(f.form.elements['szorzo_'+n] && f.form.elements['szorzo_'+n].value) quantity = f.form.elements['szorzo_'+n].value * f.form.elements['darab_'+n].value;
  else quantity = f.form.elements['darab_'+n].value;
  //update stock feedback icon
  var updater = new Ajax.Updater('keszlet_'+n, 'keszlet_info.php?p=' + f.form.elements['azon_'+n].value + '&d=' + quantity);

}

function checkMin(f, min) {

  if(min == 99.99 && f.value != 0) {
  	alert('Ezen termék minimális rendelési mennyisége és ára csak a renedelést követően, egyedi ajánlat alapján állapítható meg!');
  	return false;
  }
  var fraction = f.value / min;
  fraction += 0.0000001;
  fraction = Math.round(fraction*100)/100;
  if(fraction == 0 && f.value != 0) fraction = 1;
  if((fraction)%1 != 0) {
    f.value = Math.ceil(f.value/min)*min;
    f.value = Math.round(f.value*100)/100;
    alert('A rendelési mennyiség csak a mennyiségi egység többszöröse lehet! \n\nA mennyiséget felkerekítettük.');
  } else {
    f.value = Math.round(fraction * min * 100) / 100;
  }

}


//Adding trim function to String

String.prototype.trim = function (c) {
    var str = this.rtrim(c).ltrim(c);
    return str;
}

String.prototype.ltrim = function (c) {
    var chars = c || "\\s";
    var re = new RegExp("^[" + chars + "]+", "g");
    return this.replace(re, "");
}

String.prototype.rtrim = function (c) {
    var chars = c || "\\s";
    var re = new RegExp("[" + chars + "]+$", "g");
    return this.replace(re, "");
}

String.prototype.stripChar = function (chars) {
    var re = new RegExp("[" + chars + "]", "g");
    return this.replace(re, "");
}


String.prototype.isEmail = function () {
    //quasi rfc2822 compliant without TLD validation
    //see http://www.regular-expressions.info/email.html
    var regexp = /^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;

    if(this.length < 6) 
      return false; // "a@b.cd".length = 6

    var ret = this.search( regexp ) ;

    if ( ret < 0 )
      return false ;

    return true;
}

