
var requester = null;
var rebate_array = new Array();
var extra_array = new Array();
var expectedHash = "";

var cookie_domain = ".oreillyschool.com";

function poppy(whereat,wth,hth){

      newdes=null;
      newdes = eval( "window.open('" + whereat + "','newdes','directories=no,toolbar=yes,menubar=yes,location=no,resize=no,status=yes,resizable=yes,scrollbars=yes,width=" + wth +",height="+ hth +"')" );
     // newdes.moveTo(150,70);
      newdes.focus();
}


//COOKIE FUNCTION SECTION

/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/
function setCookie(name, value, expires, path, domain, secure) {
  if (!(domain)) {
	domain = cookie_domain;
  }
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
//alert(curCookie);
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  var thereturn_value = unescape(dc.substring(begin + prefix.length, end));
  return thereturn_value;
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/
function deleteCookie(name, path, domain)
{
    var date = new Date();
    date.setTime(date.getTime()+(-1*24*60*60*1000));
    if (!(domain))
    {
    	domain = cookie_domain;
    }
    if (getCookie(name))
    {
    	document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires="+date.toGMTString();
    }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function get_now() {

                // create an instance of the Date object
                var now = new Date();
                // fix the bug in Navigator 2.0, Macintosh
                fixDate(now);

                /*
                cookie expires in one month
                365 days in a year
                24 hours in a day
                60 minutes in an hour
                60 seconds in a minute
                1000 milliseconds in a second
                */
                now.setTime(now.getTime() + 30 * 24 * 60 * 60 * 1000);

   return now;
}

//END COOKIE SECTION

function openwindow(loc, wid, hei) {
        w = screen.availWidth;  h = screen.availHeight;
        var TopPosition = (w-wid)/2, LeftPosition = (h-hei)/2;
        stuff =
'scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,width='+ wid + ',height=' +  hei + ",left=" + LeftPosition + ",top=" + TopPosition;
        wn="useractive" + (Math.round(Math.random(1) * 1000));
        window.open(loc,wn,stuff);
}


function URLencode(sStr) {
    return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27');
}

function getCheckedValue(radioObj) {
        if(!radioObj)
                return "";
        var radioLength = radioObj.length;
        if(radioLength == undefined)
                if(radioObj.checked)
                        return radioObj.value;
                else
                        return "";
        for(var i = 0; i < radioLength; i++) {
                if(radioObj[i].checked) {
                        return radioObj[i].value;
                }
        }
        return "";
}

//BEGIN MANAGER SECTION


//END MANAGER SECTION

//BEGIN ENROLLMENT SECTION


//END ENROLLMENT SECTION

function fix_decimals(float_var, decimals) {
   var float_nodec = Math.round(float_var * 100);
   var float_str = float_nodec.toString();
   while (float_str.length < 3) {
	float_str = "0" + float_str;
   }
   var dec_index = float_str.length - 2;
   var new_str = float_str.substring(0,dec_index) + "." + float_str.substr(dec_index, 2);
   return new_str;
}

//BEGIN ROSTER COLUMN SECTION

function retrieve_cookies(myform) {
   if (!(myform)) {
	myform = "document.course_form";
   }
   var theform = eval(myform);

   //check all course cookies
   var num_courses = 0;
   var course_tally = new Array();
   if (null==theform || null==theform.elements)
   {
	   return;
   }
   for (i = 0; i < theform.elements.length; i++) {
      if (theform.elements[i].type == 'checkbox' &&
          theform.elements[i].name.indexOf('course') != -1) {
         var course_var = theform.elements[i].name;
         var underscore = course_var.indexOf('_');
         var course_name = course_var.substring(0, underscore);
         var which = course_var.substring(underscore + 1, course_var.length);
         var courseid = parseInt(course_name.substring(6,course_name.length));
         if (getCookie(course_name)) {  //TMG 01/17/07 changed from course_var
            num_courses++;
            if (!(course_tally[courseid])) {
                theform.elements[i].checked = true;
                course_tally[courseid] = 1;
                checkbox_toggle(courseid.toString(),which);
            }
         }
         else {
            theform.elements[i].checked = false;
            checkbox_toggle(courseid.toString(),which);
         }
      }
      else if (theform.elements[i].name == 'end_of_courses') {
         break;
      }
   }

   //toggle payment according to what's checked
   if (theelement = theform.payment_type) {
      toggle_payment("");
   }


   //finally, if errcode cookie is set, show error panel and get task_field cookie
   if (the_errcode = getCookie("errcode")) {
      if (theform.login_email) {
         show_login();
         show_roster_error("errcode"+the_errcode);
      }
      else {
         show_error_panel(the_errcode);
      }
      deleteCookie("errcode", "/");
      if (thetask = getCookie("task_field")) {
         if (thetask == "undefined") {
            thetask = "checkout";
         }
	 if (thetaskfield = theform.task_field) {
            thetaskfield.value = thetask;
	 }
      }
  }
  else if (the_errcode = getCookie("checkout_errcode")) {
     show_error_div(the_errcode);
     deleteCookie("checkout_errcode", "/");
  }
}


function show_error_panel(errcode) {
   var error_element;
   var prefs_element;
   var login_element;
   var price_element;
   var error_panel;

  if (error_element = document.getElementById("error_td") && parseInt(errcode) >= 0) {
	if (error_element.style) {
           error_element.style.display = "block";
	}
        if (prefs_element = document.getElementById("prefs_td")) {
           prefs_element.style.display = "none";
        }
        if (login_element = document.getElementById("login_td")) {
           login_element.style.display = "none";
        }
        if (price_element = document.getElementById("price_td")) {
           price_element.style.display = "none";
        }
  }
  //now show the proper error content
  for (i = 0; i < 6; i++) {
     var error_str = "error" + i.toString();
     if (error_panel = document.getElementById(error_str + "_panel")) {
	//var error_intro = document.getElementById(error_str + "_intro");
	if (i == parseInt(errcode)) {
	   error_panel.style.display = "";  //CHANGED 02/08/07 FOR table
	   //error_intro.style.display = "block";
	}
	else {
	   error_panel.style.display = "none";
           //error_intro.style.display = "none";
	}
     }
  }
}


function valid_email(email) {
   if (!email.match(/^[\w\.\-_]+@([\w\-]+\.)+[a-zA-Z]+$/)) {
      isvalid = false;
   }
   else {
      isvalid = true;
   }
   return isvalid;
}

function valid_login(login) {
   if (!login.match(/^[\w_]+/)) {
      isvalid = false;
   }
   else {
      isvalid = true;
   }
   return isvalid;
}

function hide_roster_error(field) {
   var errorfield = field + "_error";
   var error_div;

   if (error_div = document.getElementById(errorfield)) {
      error_div.style.display = "none";
   }
}

function show_roster_error(field) {
   var errorfield = field + "_error";
   var error_div;

   if (error_div = document.getElementById(errorfield)) {
      error_div.style.display = "";  //CHANGED 01/22/07 for tr table
   }
}


function clear_email_field(myfield, myform) {
   if (!(myform)) {
      myform = 'document.course_form';
   }
   var thefield;
   var theform = eval(myform);
   if (myfield) {
	thefield = eval(myform+'.'+myfield);	
   }
   else if (theform.theemail) {
     thefield = theform.theemail;
   }
   hide_roster_error('email_field');
   //hide_roster_error('errcode6');
   hide_roster_error('login_email');
   hide_roster_error('promo_code_field');
   if (thefield && (thefield.value == "Your email" || thefield.value == "Enter Login or Email" || thefield.value == "Your name" || thefield.value == "Verify your email")) {
      thefield.value = '';
   }
   show_error_panel(-1);
   //  the following code will check if cookies are enabled
   var cookieEnabled=(navigator.cookieEnabled)? true : false
   //if not IE4+ nor NS6+
   if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
       setCookie("testcookie", "testcookie", get_now(), "/");
       cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
   }

  if (!(cookieEnabled)) { //if cookies are not enabled on client's browser

     show_roster_error('cookies_error');
     show_error_panel("cookies_error");
  }

}

function show_please_wait() {
   var learn_more;
   var roster;
   var mystatus;
   var please_wait;

   if (learn_more = document.getElementById("learn_more_submit")) {
      learn_more.style.display = "none";
   }
   if (roster = document.getElementById("roster_submit")) {
      roster.style.display = "none";
   }
   if (mystatus = document.getElementById("status_submit")) {
      mystatus.style.display = "none";
   }
   if (please_wait = document.getElementById("please_wait")) {
      please_wait.style.display = "inline";
   }
}

function hide_please_wait(task) {
   var roster_div;
   var myroster;
   var mysubmit_div;
   var learn_more_submitme;
   var mystatus;
   var please_wait_div;

   found = 0;
   if (roster_div = document.getElementById("roster")) {
      if (!(roster_div.style.display == "none")) {
         if (myroster = document.getElementById("roster_submit")) {
                myroster.style.display = "inline";
		found = 1;
         }
      }
   }
   else if (mysubmit_div = document.getElementById("submit_div")) {
                mysubmit_div.style.display = "inline";
                found = 1;
   }
   if (found == 0) {
      //if (learn_more_div = document.getElementById("learn_more")) {
      //if (!(learn_more_div.style.display == "none")) {
	 if (learn_more_submitme = document.getElementById("learn_more_submit")) {
      		learn_more_submitme.style.display = "inline";
		found = 1;
   	 }
      //}
      //}
   }
   if (found == 0) {
      if (mystatus = document.getElementById("status_submit")) {
         mystatus.style.display = "block";
      }
   }
   if (please_wait_div = document.getElementById("please_wait")) {
      please_wait_div.style.display = "none";
   }
   if (please_wait_div = document.getElementById("shipping_please_wait")) {
      please_wait_div.style.display = "none";
   }
}

function goto_url(url) {
//alert('hello');
   //document.course_form.goto_url.value = url;
   //window.location = 'redirect.php?url='+URLencode(url);
   window.location.hash = 'goto_url';
   window.location = url;
}


//END ROSTER COLUMN SECTION

//BEGIN CHECKOUT SECTION

function show_error_div(errcode) {
   var error_element;

  if (document) {
  if (error_element = document.getElementById("error_div")) {
     error_element.style.display = "none";
  }
  else if (error_element = document.getElementById("shipping_error_div")) {
     error_element.style.display = "none";
  }

  //show the proper error content
  for (i = 0; i < 10; i++) {
     var error_str = "error" + i.toString();
     if (error_element = document.getElementById(error_str + "_div")) {
        if (i == parseInt(errcode)) {
           error_element.style.display = "block";
        }
        else {
           error_element.style.display = "none";
        }
     }
  }
  }
}

function lab_info() {
   var lab_window = window.open('http://www.oreillyschool.com/faqs.php#lab');
   lab_window.focus();
}

function po_info() {
   var lab_window = window.open('http://www.oreillyschool.com/faqs.php#pay');
   lab_window.focus();
}


//END CHECKOUT SECTION

//BEGIN STUDENT SECTION




function toggle_faq(thehash) {
   var thefaq;
   var thex;

   if (thefaq = document.getElementById(thehash)) {
      if (thefaq.style.display == "none") {
	thefaq.style.display = "block";
      	if (thex = document.getElementById(thehash+"_x")) {
	  thex.style.display = "inline";
	}
      }
      else {
        thefaq.style.display = "none";
	if (thex = document.getElementById(thehash+"_x")) {
           thex.style.display = "none";
	}
      }
   }
}


//END STUDENT SECTION

