var ButtonClicked = ''; //used to store which button was clicked to submit the form
var SubmittedFlag=false; //used to store if the form has already been submitted once

history.forward(1); //effectively, this will disable the back button

// ----------- Above is used for generic processing on all pages -------------

function ResetForm() {
  SubmittedFlag=false;
  HidePleaseWait();
}

function HidePleaseWait() {
  if (document.all.divWaitMsg != undefined) {
    document.all.divWaitMsg.style.visibility='hidden';
  }
}

function JVPopupParams(pWidth, pHeight) {
  return 'width=' + pWidth + ',height=' + pHeight + ',addressbar=no,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,titlebar=no,center=yes';
}

function LaunchRecipeComments(pRecipeID) {
  window.open('/Popup/RecipeComments.asp?RecipeID=' + pRecipeID, '_RecipeComments', JVPopupParams('750', '550'), true);
  ResetForm();
}

function LaunchDemo(pPage) {
  window.open('/Popup/Demo.asp?Page='+pPage, '_Demo', JVPopupParams('700', '500'), true);
  ResetForm();
}

function CabinetItemDetails(pItem) {
  window.open('/Popup/CabinetItemDetails.asp?Item='+pItem, '_cid', JVPopupParams('600', '400'), true);
  ResetForm();
}

function ISLItemDetails(pItem) {
  window.open('/Popup/ISLItemDetails.asp?Item='+pItem, '_iid', JVPopupParams('600', '400'), true);
  ResetForm();
}

function ShowRecipes(pIDs, pResize) {
  window.open('/Popup/ShowRecipes.asp?rIDs=' + pIDs +'&rResize=' + pResize, '_ShowRecipe', JVPopupParams('750', '550'), true);
  ResetForm();
}

function CoolThings() {
  window.open('/Popup/CoolThings.asp', '_CoolThings', JVPopupParams('500', '350'), true);
  ResetForm();
}

function PreviewRecipeGroup(pGroupID) {
  window.open('/Popup/RecipesInGroup.asp?GroupID='+pGroupID, '_Demo', JVPopupParams('550', '350'), true);
  ResetForm();
}

function LaunchVote() {
  window.open('/Popup/Vote.asp', '_Vote', JVPopupParams('500', '230'), true);
  ResetForm();
}

function FindCaterer() {
  window.open('/Popup/FindCaterer.asp', '_FindCaterer', JVPopupParams('600', '450'), true);
  ResetForm();
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// -------- This is just a separator for the General System Functions --------
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------

function GatherSelected(pField) {
  var selIDs = '';
  
  if (pField=='[object]') {
    if (pField.length==undefined) {
      if (pField.checked) selIDs = pField.value;
    } else {
      for (var i=0;i<pField.length;i++) {
        if (pField[i].checked) selIDs = selIDs + ', ' + pField[i].value;
      }
      if (selIDs.length > 0) selIDs = selIDs.substring(2, selIDs.length);
    }
  }
  return selIDs;
}

function isBlankCBO(pValue) {
  if (pValue=='' || pValue=='-') {
    return true;
  } else {
    return false;
  }
}

function ReloadPage() {
  self.window.navigate(self.location);
}

function VisibleScreenWidth() { 
  var ActualWidth = window.innerwidth? window.innerwidth : document.body.offsetWidth; 
  return ActualWidth - 21;  //Accounts for scroll bar
} 

function ScreenInfo() {
  if (window.innerWidth) { 
    //if browser supports window.innerWidth
    myWidth = window.innerWidth - 31; // account for scrollbar
    myHeight = window.innerHeight;
    myXOffset = window.pageXOffset;
    myYOffset = window.pageYOffset;
  } else if (document.all) {
    //else if browser supports document.all (IE 4+)
    myWidth = document.body.clientWidth; 
    myHeight = document.body.clientHeight;
    myXOffset = document.body.scrollLeft;
    myYOffset = document.body.scrollTop;
  }

  return [myWidth, myHeight, myXOffset, myYOffset];
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ---------- This is just a separator for the Formatting Functions ----------
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------

function nameOfMonth(pMonth) {
  var tmpMonthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");  
  return tmpMonthNames[ pMonth ];
}

// format an amount as money
function formatMoney( num, showpennies ) {
  var isNegative = false;
  num = num.toString().replace(/\\$|\\,/g,'');
  if( isNaN( num ) ) {
    num = "0";
  }
  if ( num < 0 ) {
    num = Math.abs( num );
    isNegative = true;
  }
  cents = Math.floor( ( num * 100 + 0.5 ) % 100 );
  num = Math.floor( ( num * 100 + 0.5 ) / 100 );
  if (showpennies!=2) {
    num = parseInt(num);
    if (cents>=50) num = num + 1;
  }
  num = num.toString();

  if ( cents < 10 ) {
    cents = "0" + cents;
  }
  for ( 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 ) );
  }

  var result = '$' + num;
  if (showpennies==2) result += '.' + cents;
  
  if ( isNegative ) {
    result = "-" + result;
  }
  return result;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ---------- This is just a separator for the Other Functions ---------------
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------

function validEMail(pEMail) {
  var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
  var regex = new RegExp(emailReg);
  return regex.test(pEMail);
}

function validDate(pStringDate){
  var validformat=/^\d{2}\/\d{2}\/\d{4}$/ 
  if (!validformat.test(pStringDate)) {
    return false;
  } else { 
    var DateArray=pStringDate.split("/");
    var monthfield=DateArray[0];
    var dayfield=DateArray[1];
    var yearfield=DateArray[2];
    var dayobj = new Date(yearfield, monthfield-1, dayfield);
    if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield)) {
      return false;
    } else {
      return true;
    }
  }
}