// JavaScript Document

// following cookie functions are from http://tech.irt.org/articles/js064/index.htm
// it said on the web, "Code examples on irt.org can be freely copied and used"
// as it can be found on http://www.irt.org/utility/smprint.htm#copyright
function Get_Cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
    return name;
}

// Look up price by journal code which is extracted from the article URL
function isJournal(articleString) {
	var startLoc;
	var eo_pattern;
	if (articleString == undefined) { return ""; }
	
	
  startLoc = articleString.indexOf("/journals/sw/swa/"); // exclude Space Weather non-tech from article sales
  if (startLoc != -1) {
    idLoc = articleString.indexOf("?id=");
    if (idLoc != -1)
      return "";
  }

	// assume rga or brr for sale; TBD: update line 33 with additional types
	// test cases: /journals/eo/v083/i051/EO083i051p00601-02/EO083i051p00601-02_rga.pdf
	eo_pattern = /^\/journals\/eo\/v\d{3}\/i\d{3}\/(\w{8,20})\/\1_(rga|brr)\.pdf$/;
	startLoc = articleString.search(eo_pattern); // include EOS by matching types
	if (startLoc != -1) {
  	return true;
	}

  startLoc = articleString.indexOf("/journals/eo/"); // exclude EOS from article sales
  if (startLoc != -1) {
    return "";
  }

//  startLoc = articleString.indexOf("/journals/gl/v"); // exclude GRL backissue
//  if (startLoc != -1) {
//    return "";
//  }

  startLoc = articleString.indexOf("/contents/journals/ViewPapersInPress.do"); // exclude paper in press
  if (startLoc != -1) {
    return "";
  }

  startLoc = articleString.indexOf("/journals/pip"); // exclude paper in press
  if (startLoc != -1) {
    return "";
  }

  // Display purchase option if it is a 'journal' file

  startLoc = articleString.indexOf("journals");

  // The purchase option will not appear unless a pdf is selected
  if (startLoc != -1) {
    return articleString;
  }

  // Display purchase option if it is a 'wps' file

  startLoc = articleString.indexOf("wps");

  // The purchase option will not appear unless a pdf is selected
  if (startLoc != -1) {
    return articleString;
  }

  // Display purchase option if it is a 'WPS' file

  startLoc = articleString.indexOf("WPS");

  // The purchase option will not appear unless a pdf is selected
  if (startLoc != -1) {
    return articleString;
  }

  // Display purchase option for books; by Shuli on 06/02/2011
	// book URL: '/books/ce/v055/CE055p0029/CE055p0029.pdf' or '/books/ce/v055/CE055p0029/'
	books_pattern = /^\/books\/\w{2}\/v\d{3}\/(\w{6,20})\/(\1\.pdf)?$/;
	startLoc = articleString.search(books_pattern); // include books by matching types
	if (startLoc != -1) {
  	return true;
	}

  return "";
}


// Look up price by journal code which is extracted from the article URL
function priceLookup(articleString) {

  // get journal code from articleString
  var startLoc = articleString.indexOf("/", 2); // the 2nd '/'
  if (startLoc == -1) {
    return("");  // No such cookie
  }

  startLoc = startLoc + 1; // the position of journal code
  var endLoc = articleString.indexOf("/", startLoc);
  if (endLoc == -1) { // Last one has no "/"
    endLoc = articleString.length;
  }

  var jc = articleString.substring(startLoc, endLoc);

  // define price ; copied from price_ppv.properties
  var arrayPrice = new Object();
  arrayPrice["gb"]=25.00
  arrayPrice["gc"]=25.00
  arrayPrice["gl"]=25.00
  arrayPrice["ja"]=25.00
  arrayPrice["jb"]=25.00
  arrayPrice["jc"]=25.00
  arrayPrice["jd"]=25.00
  arrayPrice["je"]=25.00
  arrayPrice["jf"]=25.00
  arrayPrice["pa"]=25.00
  arrayPrice["rg"]=25.00
  arrayPrice["rs"]=25.00
  arrayPrice["sw"]=25.00
  arrayPrice["tc"]=25.00
  arrayPrice["wr"]=25.00
  arrayPrice["eo"]=25.00


  if (jc in arrayPrice)
    return(arrayPrice[jc]);
  else
    return 25.00;
}
 function putFocus(formInst, elementInst) {
   document.forms[formInst].elements[elementInst].focus();
 }


