/**
*TODO This header requires comments.
*
*
*@version 1.0 (17/05/2007)
*@since EC 1.0
*/
/**
 * Toggles the visibility of almost anything.
 */
function toggle_visibility(id) {
   var e = document.getElementById(id);
   if(e.style.display == 'block') {
      e.style.display = 'none';
   }
   else {
      e.style.display = 'block';
   }
}

/**
 * Moves cursor focus to next element if user is at end of current element
 */
function checkFocus (element, nextElement) {
  if (eval('document.sales.' + element + '.value.length >= ' + 'document.sales.' + element + '.maxLength') ) {
    eval('document.sales.' + nextElement + '.focus()');
  }
}

/*
pop up window function
*/
function NewWindow(url, name, width, height) {
  var Win = window.open(url,"" + name + "",'width=0' + width + ',height=0' + height + ',resizable=yes,scrollbars=yes,menubar=no,status=no' );
  Win.focus();
}

/*
refresh parent window and close popup
*/
function closeRefresh() {
    window.opener.location.refresh();
    self.close();
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

// Zero padding
function pad0(string, newlength) {
  var pad = "";
  var len = newlength-String(string).length;
  var i;
  for (i = 0; i<len; i++) {
    pad += "0";
  }
  return pad+string;
}

function checkForCookies() {

	createCookie('testCookie', 'testCookie', 1);
	var cook = readCookie('testCookie');
	
	if (cook.length > 0) {
		//alert('Cookies are on');
	} else {
		//alert('Cookies are off');
	}
	
	eraseCookie('testCookie');
	
}

function createCookie(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=/";
}

function readCookie(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;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}
