function appServerUrl(data)
{
  var myURL = "http://www2.impacctraining.net/" + data;
  return myURL;
}

function getTop()
{
    if (frames.length != 0)
        return frames[0];
    return top;
}

function offline()
{
alert('The IMPACC courseware system is offline for maintenance from 5:15 to 6:00. We apologize for any inconvenience this may cause!');
}

function isInStr(input, searchFor){
    var _input = input.toLowerCase();
    var _searchFor = searchFor.toLowerCase();
    var result = (_input.indexOf(_searchFor) != -1);

    return result;
}

function launchBS(){
  launchImpaccCourse('impacc_bs');
}

function launchNA(){
  launchImpaccCourse('impacc_na');
}

function launchOE(){
  launchImpaccCourse('impacc_oe');
}

function launchLMS(){
  var url = appServerUrl('pub/lms/index.html');
  popup(url,'lms',622,462,'no')
}

function demoBS(){
  launchImpaccDemo('impacc_bs', isLocal());
}

function demoNA(){
  launchImpaccDemo('impacc_na', isLocal());
}

function demoOE(){
  launchImpaccDemo('impacc_oe', isLocal());
}

function showCourseRequirements(){
  var url = './../pub/impacc_bs/course/supconfig.html';
  popup(url,'courseReq',500,450,'yes')
}

function isLocal(){
  var url = window.location.href;
  if (isInStr(url, 'file:'))
    return true;
  return false;
}

function launchImpaccDemo(courseCode, shouldAutoPlay){
  var url;
  if (shouldAutoPlay){
    url = './../pub/' + courseCode + '/course/preparelaunch_autoplay.html';
  }
  else{
    course_url = '/pub/' + courseCode + '/course/preparelaunch_demo.html';
    url = '/pub/php/demo_form.php?course=' + courseCode + '&IMPACC_REDIRECT_SUCCESS=' + escape(course_url);
//    url = '/pub/' + courseCode + '/course/preparelaunch_demo.html';
  }
  popupCourse(url, courseCode, '795', '570', 'yes')
}

function launchImpaccDemoOriginal(courseCode, shouldAutoPlay){
  var url;
  if (shouldAutoPlay){
    url = './../pub/' + courseCode + '/course/preparelaunch_autoplay.html';
  }
  else{
    url = './../pub/' + courseCode + '/course/preparelaunch_demo.html';
  }
  popupCourse(url, courseCode, '795', '570', 'yes')
}



function launchImpaccCourse(courseCode){
  //var url = './../pub/' + courseCode + '/course/preparelaunch.html';
  //var url = 'http://www2.impacctraining.net/pub/' + courseCode + '/course/preparelaunch.html';
  var url = 'pub/' + courseCode + '/course/preparelaunch.html';
  popupCourse(url, courseCode, '795', '570', 'yes')
}

function getWidth(aDefault){
  var result = aDefault;

  if (screen.availWidth){
    result = (screen.availWidth * 0.95);
    if (result < aDefault)
      result = aDefault;
  }

  return result;
}

function getHeight(aDefault){
  var result = aDefault;

  if (screen.availHeight){
    result = (screen.availHeight * 0.95);
    if (result < aDefault)
      result = aDefault;
  }

  return result;
}

function getLeft(aWidth){
  var result = 0;

  if (screen.availWidth){
      result = (screen.availWidth - aWidth) / 2;
      if (result < 0)
        result = 0;
  }

  return result;
}

function getTop(aHeight){
  var result = 0;

  if (screen.availHeight){
      result = (screen.availHeight - aHeight) / 4;
      if (result < 0)
        result = 0;
  }

  return result;
}

function popup(myURL,myName,myWidth,myHeight,hasScrollBars){
  var aLeft = getLeft(myWidth);
  var aTop = getTop(myHeight);

  return popupWindow(myURL,myName,aLeft,aTop,myWidth,myHeight,hasScrollBars,"no");
}

function popupCentered(myURL,myName,myWidth,myHeight,hasScrollBars,isSizeable){
  var aLeft = getLeft(myWidth);
  var aTop = getTop(myHeight);

  var ornamentation = ",menubar=yes,toolbar=yes,status=yes,location=yes";
  return popupWindow(myURL,myName,aLeft,aTop,myWidth,myHeight,hasScrollBars,isSizeable,ornamentation);
}

function popupCourse(myURL,myName,myWidth,myHeight,oldHasScrollbars){
//  var aWidth = getWidth(myWidth);
//  var aHeight = getHeight(myHeight);
  var aWidth = myWidth;
  var aHeight = myHeight;
  var aLeft = getLeft(aWidth);
  var aTop = getTop(aHeight);

  myURL = appServerUrl(myURL);
  return popupWindow(myURL,myName,aLeft,aTop,aWidth,aHeight,"no","yes");
}

function popupWindow(myURL,myName,myLeft,myTop,myWidth,myHeight,hasScrollBars,mySizeable, ornamentation){
  if (IsMissing(ornamentation)){
    ornamentation = ",menubar=no,toolbar=no,status=no,location=no";
  }
  var windowSpecs = "left=" + myLeft + ",top=" + myTop + ",width=" + myWidth + ",height=" + myHeight + ",scrollbars=" + hasScrollBars + ornamentation + ",resizable=" + mySizeable;
  var myWin = window.open(myURL,myName,windowSpecs);
  myWin.focus();
  return myWin;
}

function toDecimals(value, decimalPlaceCount){
  var powerOfTen = Math.pow(10,decimalPlaceCount);
  value = Math.round(value*powerOfTen) / powerOfTen;

	var result = value+"";
	var pos = result.indexOf('.');

	var count;
	if (pos == -1){
		result = result + '.';
		count = 2;
	}
	else{
		currentNumberOfDecimals = result.length - pos - 1;
		count = decimalPlaceCount - currentNumberOfDecimals;
	}
	for (var i=0; i < count; i++){
		result += '0';
	}

//	trace("result:" + result + ", value:" + value + ", count:" + count + ", pos:" + pos);

	return result;
}

function cartForm(){
	return document.forms[0];
}

function cgiForm(){
	return document.forms[1];
}

function getDiscountAsCode(){
  var result = getPromoDiscount(getPromoCode());
  if (result == 0)
    return '';
  return result + '';
}

// Clear all form fields except for the one we want for the target # of seats.
function setCgiNumberOfSeats(code){
  var numberOfSeats = getTotalNumberOfSeats();
  var numberOfSeatsForCode = getNumberOfSeats(code);
  var codeNumbers = [1,50,100,500];
  var discounts = ['','10','15','20','25','30','35','40','45','50'];
  var codeNumber = getCodeNumber(numberOfSeats);
  var discount = getDiscountAsCode();
  var currentCodeNumber;
  var i;
  var j;

  for (i=0; i < codeNumbers.length; i++){
    currentCodeNumber = codeNumbers[i];

    for (j=0; j < discounts.length; j++){
      currentDiscount = discounts[j];

      if ((currentCodeNumber == codeNumber) && (currentDiscount == discount)){
//        window.alert('got it' + codeNumber + ', ' + discount + ',' + currentDiscount);
        setCgiSeat(code, codeNumber, numberOfSeatsForCode, currentDiscount);
      }
      else{
        setCgiSeat(code, currentCodeNumber, 0, currentDiscount);
      }
    }
  }
}

function getPromoPercentage(code){
  var discount = getPromoDiscount(code);
  var result = (100 - discount) / 100;
  return result;
}

function setCgiSeat(code, codeNumber, numberOfSeats, discountCode){
  var form = cgiForm();
  var codeMap;
  if (code.toUpperCase() == 'BS'){
    codeMap = 'IMP_701S-';
  }
  else if (code.toUpperCase() == 'NA'){
    codeMap = 'IMP_710S-';
  }
  else{
    codeMap = 'IMP_720S-';
  }

  var courseCode = codeMap + codeNumber;
  if (! IsMissing(discountCode))
    courseCode = courseCode + '-' + discountCode;

  var numberOfSeatsName = courseCode + "-Qty";
  var deleteName = courseCode + "-Del";
  var fldNumberOfSeats = form[numberOfSeatsName];
  var fldDelete = form[deleteName];

  if (numberOfSeats <= 0){
    fldDelete.value = 'ON';
  }
  else{
    fldDelete.value = '';
    fldNumberOfSeats.value = numberOfSeats;
  }
}

function IsMissing(AParam){
	var lParam = AParam + "";

	if  ((lParam == "") || (lParam == " ") || (lParam == "NaN") || (lParam == "undefined") || (lParam == "null") || (AParam == null))
		return true;
	return false;
}

function IsDefined(AValue){
	return (! IsMissing(AValue));
}

function UseDefault(AValue, ADefaultValue){
	if (IsMissing(AValue))
		return ADefaultValue
	else
		return AValue;
}

function NowInSeconds(){
	var lNow;
	lNow = new Date();
	return Math.floor(lNow.getTime() / 1000);
}

function costForSeats(numberOfSeats, promoCode){
  var seatCost = costPerSeat(numberOfSeats);
  var percentage = getPromoPercentage(promoCode);
  var result = numberOfSeats * seatCost * percentage;

  return result;
}

function costPerSeat(numberOfSeats){
  if (numberOfSeats <= 49)
    return 30;
  if (numberOfSeats <= 99)
    return 27;
  if (numberOfSeats <= 499)
    return 24;
  return 21;
}

function getCodeNumber(numberOfSeats){
  if (numberOfSeats <= 49)
    return 1;
  if (numberOfSeats <= 99)
    return 50;
  if (numberOfSeats <= 499)
    return 100;
  return 500;
}

function clearCart(){
	if (window.confirm("Are you sure you want to empty your cart ?")){
		cartForm().reset();
	}
}

function checkOutCart(){
  refreshPromoCode();

  var costMsg = totalCart();
  var cost = totalCartCost();
  var msg = "";

  if (cost == 0){
    msg = "Your shopping cart is empty.\n\nPlease add some items to the cart and try again.";
    window.alert(msg);
    return;
  }

  msg = "Proceed to checkout and purchase\n\n" + costMsg + "\n\nof Online Training ?";
	if (window.confirm(msg)){

    setCgiNumberOfSeats('bs');
    setCgiNumberOfSeats('na');
    setCgiNumberOfSeats('oe');

    cgiForm().submit();

  }
}

function getPromoCode(){
  var txtField = eval('cartForm().promo');
  var result = txtField.value;
  result = UseDefault(result, '');
  result = result.toUpperCase();
//  txtField.value = result+"";
  return result;
}

function refreshPromoCode(){
  var txtField = eval('cartForm().promo');
  var code = getPromoCode();
  var result = getPromoDiscount(code);
  var noDiscount = (result == 0);

  if (! IsMissing(code) && noDiscount)
    window.alert(code + ' is an invalid promotional code.');

  if (noDiscount)
    code = "";
  txtField.value = code;
}


function getNumberOfSeats(courseId){
  var txtField = eval('cartForm().quantity' + courseId);
  var result = parseInt(txtField.value);
  result = UseDefault(result, 0);
  txtField.value = result+"";
  return result;
}

function getTotalNumberOfSeats(){
  var bsSeats = getNumberOfSeats('bs');
  var naSeats = getNumberOfSeats('na');
  var oeSeats = getNumberOfSeats('oe');
  var result = bsSeats + naSeats + oeSeats;

  return result;
}

function totalCartCost(){
  var numberOfSeats = getTotalNumberOfSeats();
  var promoCode = getPromoCode();
	var result = costForSeats(numberOfSeats, promoCode);
  return result;
}

function totalCart(){
  var result = totalCartCost();

  result = "$ " + toDecimals(result, 2) + " US";
  cartForm().total.value = result;
  return result;
}

function userTotalCart(){
  refreshPromoCode();

  var total = totalCart();

  window.alert('Current total is:\n\n' + total);
}

function getCookie(name, defaultValue) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;

    if ((!start) && (name != document.cookie.substring(0,name.length)))
			return defaultValue;

    if (start == -1)
      return defaultValue;

    var end = document.cookie.indexOf(";",len);
    if (end == -1)
			end = document.cookie.length;

    var result;
    result = unescape(document.cookie.substring(len,end));
    result = result + '';
    if ((result == 'NULL') || (result == '') || (result == null))
      return defaultValue;

    return result;
}

function getAffiliateCode(){
  var result;
  result = getCookie('impacc-aid', 'IMPACC');
  result = result.toUpperCase();
  return result;
}

function displayAffiliateCode(){
  var code;
  code = getAffiliateCode();
  document.write(code);
}

function displayAffiliateInfo(){
  var code;
  code = getAffiliateCode();
  document.write('<FONT COLOR="#ffffff" SIZE="-1" FACE="Arial, Helvetica, sans-serif">');
  document.write('Please reference the following source code when placing your call : <b>' + code + '</b>');
  document.write('</FONT>');
}

function displayAffiliateFormField(){
  document.write('<INPUT TYPE="HIDDEN" NAME="impacc-aid" value="' + getAffiliateCode() + '">');
}
