var ppState = "closed";
function hidePricePromise(){
    var el= document.getElementById('price_promise');
    el.style.height="90px";

}
function revealPricePromise() {
  if (ppState=="closed") {
    func='openPP()';
  } else {
    func='closePP()';
  }
    setTimeout(func,15);
    return false;
}

/**
* open the more info box
*/
function openPP() {
  var el=document.getElementById("price_promise");
  var cHeight=parseInt(el.style.height.replace("px",""));
  var nHeight=125;

  if(cHeight<(460-10)) nHeight=cHeight+10;
  else nHeight=460;

  if(cHeight<460) {
    el.style.height=nHeight+"px";
    setTimeout('openPP()',15);
  } else {
    ppState = "open";
  }
}

/**
* close the more info box
*/
function closePP() {

  var el=document.getElementById("price_promise");

  var cHeight=parseInt(el.style.height.replace("px",""));
  var nHeight=90;
  if(cHeight>90) nHeight=cHeight-10;
  else nHeight=18;

  if(cHeight>90) {
    el.style.height=nHeight+"px";
    setTimeout('closePP()',15);
  } else {
    ppState = "closed";
  }
}

function hideHPricePromise(){
    var el= document.getElementById('price_promise');
    el.style.height="125px";
}
function revealHPricePromise() {
  if (ppState=="closed") {
    func='openHPP()';
  } else {
    func='closeHPP()';
  }
    setTimeout(func,15);
    return false;
}

/**
* open the more info box
*/
function openHPP() {
  var el=document.getElementById("price_promise");
  var cHeight=parseInt(el.style.height.replace("px",""));
  var nHeight=125;

  if(cHeight<(560-10)) nHeight=cHeight+10;
  else nHeight=560;

  if(cHeight<560) {
    el.style.height=nHeight+"px";
    setTimeout('openHPP()',15);
  } else {
    ppState = "open";
  }
}

/**
* close the more info box
*/
function closeHPP() {

  var el=document.getElementById("price_promise");

  var cHeight=parseInt(el.style.height.replace("px",""));
  var nHeight=125;

  if(cHeight>125) nHeight=cHeight-10;
  else nHeight=18;

  if(cHeight>125) {
    el.style.height=nHeight+"px";
    setTimeout('closeHPP()',15);
  } else {
    ppState = "closed";
  }
}

/*
* ajax call to retrieve and possible execute code on response from server
*
*/
function doTask(params) {


  if (document.implementation && window.document.implementation.createDocument)
  {
    xmlDoc = document.implementation.createDocument("", "", null);
    xmlDoc.onload = doResponse;
  }
  else if (window.ActiveXObject)
  {
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.onreadystatechange = function () {
      if (xmlDoc.readyState == 4) doResponse()
    };
  }
  else
  {
    return false;
  }
  d = new Date; // Generic - use to create unique url requests
  var rand = d.getTime();
  var url=document.location.protocol+'//'+document.location.host+'/alt/doTask.xml?rand='+rand+'&'+params;

  try{
    xmlDoc.load(url); // this is what we like to do

  } catch(e) { // if failed try using httprequest as a fallback - (e.g. Safari)
    try {
      req = new XMLHttpRequest();
    } catch(e) {
      req = false;
    }
    if(req) {
      req.onreadystatechange = processHttpRequest;
      req.open("GET", url, true);
      req.send("");

    }

  }
  return false;
}

function fadeIn(){
  var id ='content-popup';
  var opacStart = '0';
  var opacEnd = '100';
  var millisec = '1500';
  var speed = Math.round(millisec / 100);
  var timer = 0;

  for(i = opacStart; i <= opacEnd; i++) {
    setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
    timer++;
  }
  return true;
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

function giftwrapDynamic(textArea,maxLength){
    var displayDiv=document.getElementById('textAreaCount');
    var charCount=textArea.value.length;
    var charRemain=maxLength-charCount;

    if (textArea.value.length > maxLength) {
    textArea.value = textArea.value.substring(0, maxLength);
    } else {
       displayDiv.innerHTML=charRemain+' characters remaining';
    }

}


/*
* some browsers require this extra step to catch the http reponse
*
*/
function processHttpRequest() {
  if (req.readyState == 4) {
        if (req.status == 200) {
      xmlDoc=req.responseXML;
      doResponse();
        }
    }
}

/*
* process the response from server to the doTask function,
*
*/
function doResponse() {
  var tasks = xmlDoc.getElementsByTagName('task');
  var cmd=null;

  for(var i=0;i<tasks.length;i++) {
    cmd=tasks[i].getElementsByTagName('cmd')[0];

    if (cmd.childNodes[0]) {
      switch(cmd.childNodes[0].nodeValue) {
        case 'replace': // replace the innerHTML of an element
          var id=tasks[i].getElementsByTagName('id')[0].childNodes[0].nodeValue;
          var content=tasks[i].getElementsByTagName('content')[0].childNodes[0].nodeValue;
          if (id.length>0 && document.getElementById(id)) document.getElementById(id).innerHTML=content;
          break;
        case 'execute': // run some js
          var script=tasks[i].getElementsByTagName('script')[0].childNodes[0].nodeValue;
          eval(script);
          break;

      }
    }
  }
}


/*
* use JS to hide controls which are only required when JS is not enabled
* - do this by adding hideJS as a class to the css
*/
function hideElements() {
  if (document.styleSheets) {
    if (document.styleSheets[0].addRule) {    // IE
      document.styleSheets[0].addRule('.hideJS','display: none',0);
    } else {    // !IE
      document.styleSheets[0].insertRule('.hideJS {display: none}', 0);
    }
  }
}

/*
* Add the onload function to any other onload function
*/
function addLoadEvent(func) {
  var oldonload = window.onload;

  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(hideElements);

// show the form wait box
function showWait(id) {
  if (document.getElementById(id)) {
    var el=document.getElementById(id);

    //el.style.position='absolute';
    //el.style.left='50%';
    //el.style.top='50%';
    //el.style.display='block';
    //el.style.marginLeft="-" + parseInt(el.offsetWidth  / 2)+'px';
    //el.style.marginTop ="-" + parseInt(el.offsetHeight / 2)+'px';
    // IE has a quirk which means we need to reload the animated gif to make it animate
    if (document.getElementById(id+'wImg')) setTimeout("document.getElementById('"+id+"wImg').src=document.getElementById('"+id+"wImg').src",20);
  }
}

// show the RM wait box
function showRMWait(id) {
  if (document.getElementById(id)) {
    var el=document.getElementById(id);
    el.style.display='block';
    if (document.getElementById(id+'wImg')) setTimeout("document.getElementById('"+id+"wImg').src=document.getElementById('"+id+"wImg').src",20);
  }
}
/**
 * Pop Up Images
 */
function showPopup(url) {
  window.open(url,null,'status=yes,titlebar=yes,scrollbars=yes,resizable=yes,toolbar=no,menubar=no,height=580,width=760');
  return false;
}

function setFadeHeight() {
  document.getElementById('fade').style.height=document.getElementById('page').offsetHeight+"px";
}
// close popup with esc key
window.onkeyup = function (event) {
  if (event.keyCode == 27) {
    document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none';
  }
}

function handlePayPalOnclick(e) {
  togglePaymentFields(false);
}

function togglePaymentFields(showFields) {
  document.getElementById('cardDetails').style.display = (showFields?'block':'none');
  document.getElementById('checkoutF_skip').value=(showFields?'0':'1');
}