/*
  $Id: general.js 7953 2009-11-24 12:50:49Z dpi $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Supercharged to OpenBizBox
  http://www.openbizbox.com

  Copyright (c) 2003-2007, Golden Planet ApS
  http://www.goldenplanet.com

  Released under the GNU General Public License v.2
  http://www.gnu.org/copyleft/gpl.html
*/

function SetFocus(TargetFormName) {
  var target = 0;
  if (TargetFormName != "") {
    for (i=0; i<document.forms.length; i++) {
      if (document.forms[i].name == TargetFormName) {
        target = i;
        break;
      }
    }
  }

  var TargetForm = document.forms[target];

  for (i=0; i<TargetForm.length; i++) {
    if ( (TargetForm.elements[i].type != "image") && (TargetForm.elements[i].type != "hidden") && (TargetForm.elements[i].type != "reset") && (TargetForm.elements[i].type != "submit") ) {
      TargetForm.elements[i].focus();

      if ( (TargetForm.elements[i].type == "text") || (TargetForm.elements[i].type == "password") ) {
        TargetForm.elements[i].select();
      }

      break;
    }
  }
}

function RemoveFormatString(TargetElement, FormatString) {
  if (TargetElement.value == FormatString) {
    TargetElement.value = "";
  }

  TargetElement.select();
}

function CheckDateRange(from, to) {
  if (Date.parse(from.value) <= Date.parse(to.value)) {
    return true;
  } else {
    return false;
  }
}

function IsValidDate(DateToCheck, FormatString) {
  var strDateToCheck;
  var strDateToCheckArray;
  var strFormatArray;
  var strFormatString;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var intDateSeparatorIdx = -1;
  var intFormatSeparatorIdx = -1;
  var strSeparatorArray = new Array("-"," ","/",".");
  var strMonthArray = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
  var intDaysArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  strDateToCheck = DateToCheck.toLowerCase();
  strFormatString = FormatString.toLowerCase();

  if (strDateToCheck.length != strFormatString.length) {
    return false;
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strFormatString.indexOf(strSeparatorArray[i]) != -1) {
      intFormatSeparatorIdx = i;
      break;
    }
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strDateToCheck.indexOf(strSeparatorArray[i]) != -1) {
      intDateSeparatorIdx = i;
      break;
    }
  }

  if (intDateSeparatorIdx != intFormatSeparatorIdx) {
    return false;
  }

  if (intDateSeparatorIdx != -1) {
    strFormatArray = strFormatString.split(strSeparatorArray[intFormatSeparatorIdx]);
    if (strFormatArray.length != 3) {
      return false;
    }

    strDateToCheckArray = strDateToCheck.split(strSeparatorArray[intDateSeparatorIdx]);
    if (strDateToCheckArray.length != 3) {
      return false;
    }

    for (i=0; i<strFormatArray.length; i++) {
      if (strFormatArray[i] == 'mm' || strFormatArray[i] == 'mmm') {
        strMonth = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'dd') {
        strDay = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'yyyy') {
        strYear = strDateToCheckArray[i];
      }
    }
  } else {
    if (FormatString.length > 7) {
      if (strFormatString.indexOf('mmm') == -1) {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mm'), 2);
      } else {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mmm'), 3);
      }

      strDay = strDateToCheck.substring(strFormatString.indexOf('dd'), 2);
      strYear = strDateToCheck.substring(strFormatString.indexOf('yyyy'), 2);
    } else {
      return false;
    }
  }

  if (strYear.length != 4) {
    return false;
  }

  intday = parseInt(strDay, 10);
  if (isNaN(intday)) {
    return false;
  }
  if (intday < 1) {
    return false;
  }

  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) {
    for (i=0; i<strMonthArray.length; i++) {
      if (strMonth == strMonthArray[i]) {
        intMonth = i+1;
        break;
      }
    }
    if (isNaN(intMonth)) {
      return false;
    }
  }
  if (intMonth > 12 || intMonth < 1) {
    return false;
  }

  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    return false;
  }
  if (IsLeapYear(intYear) == true) {
    intDaysArray[1] = 29;
  }

  if (intday > intDaysArray[intMonth - 1]) {
    return false;
  }

  return true;
}

function IsLeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0) {
      return true;
    }
  } else {
    if ((intYear % 4) == 0) {
      return true;
    }
  }

  return false;
}

function popupConditions(url) {
  window.open(url,'popupConditions','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=300,screenX=150,screenY=150,top=150,left=150')
  return false;
}


 function html_entity_decode(s) {
     var span = document.createElement('span');
     span.innerHTML = s;
     return span.firstChild.nodeValue;
 }

function number_format(number, decimals, dec_point, thousand_sep)
{
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1)
  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null)
  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)
  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  thousand_sep = (thousand_sep != dec_point || fractional.length == 0) ? 
                  thousand_sep : null;
  if (thousand_sep != null && thousand_sep != "") {
		for (i = integer.length - 3; i > 0; i -= 3)
			integer = integer.substring (0 , i) + thousand_sep + integer.substring (i);
  }

  return sign + integer + fractional + exponent;
}

 // Tricky function for creating DOM element correct in IE, FF, Opera, etc.
 function ce(tag,name){
    if (name && window.ActiveXObject){
        element = document.createElement('<'+tag+' name="'+name+'">');
    }else{
        element = document.createElement(tag);
        element.setAttribute('name',name);
    }
    return element;
 };


/**
* Simulation of Function call() method that is missing in IE 5.0.
*/
if (!Function.prototype.call) {
	Function.prototype.call = function() {
		var objThis = arguments[0];
		objThis._this_func = this;
		var arrArgs = [];
		for (var iArg = 1; iArg < arguments.length; iArg++) {
			arrArgs[arrArgs.length] = 'arguments[' + iArg + ']';
		}
		var ret = eval('objThis._this_func(' + arrArgs.join(',') + ')');
		objThis._this_func = null;
		return ret;
	};
}

function formatCurrency(number, cents, decimal, thousand, before, after) {
	var m = (cents = Math.abs(cents) + 1 ? cents : 2, decimal = decimal || ",", thousand = thousand || ".", /(\d+)(?:(\.\d+)|)/.exec(number + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;
	var s = Math.round(m[2]*Math.pow(10, cents)) || 0;
	return (before ? before : '') + (x ? m[1].substr(0, x) + thousand : "") + m[1].substr(x).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (cents ? '<span>' + decimal + (s<10 ? '0' : '') + s + '</span>' : "") /* + (after ? after : '') */;
};

function popupWindow(url, width, height) {
	width = width || 250;
	height = height || 150;
	var win = window.open(url,'','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width='+width+',height='+height+',screenX=50,screenY=50,top=50,left=50');
	if (win) win.focus();
	return false;
}

function clone(myObj) {
	if(typeof(myObj) != 'object') return myObj;
	if(myObj == null) return myObj;
	var myNewObj = new Object();
	for(var i in myObj)	myNewObj[i] = clone(myObj[i]);
	return myNewObj;
}

function filterEnterKeyPress(e) {
	if (!e) var e = window.event;
	if (e.keyCode == 13) {
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		return false;
	}
}

function stockSubscribe(product_id, suggested_email, language) {
	var email;
	if (!suggested_email) {
		suggested_email = window.suggested_email ? window.suggested_email : "";
	}
	if (email = prompt(SUBSCRIBE_QUESTION, suggested_email)) {
		window.suggested_email = email;
		$("#subscribe_"+product_id+" img.loading").css("visibility", "visible");
		$.ajax({
			type: "POST",
			url: "stock_subscribe.php",
			data: "email="+email+"&product_id="+product_id+"&language="+language,
			success: function(msg){
				if (msg == STOCK_SUBSCRIBE_CORRECT_RESULT) {
					$("#subscribe_"+product_id+" img.loading").attr("src", "images/checked.gif");
				} else {
					$("#subscribe_"+product_id+" img.loading").attr("src", "images/error.gif");
				}
			}
		});
	}
	return false;
}
