/*************************************************************************/
/****** Scripts used for validating serial numbers for purchased books ***/
/******         and giving access to downloadable book content         ***/
/*************************************************************************/


//=====================================================
//          		globals
//=====================================================

// Associative array for book codes and corresponding examples pages
// Append to this array to add more books
var bookCodes = new Array( );
bookCodes["6126"] = "/intelpress/pciexpress/6126.htm";
bookCodes["6127"] = "/intelpress/upnp/6127.htm";
bookCodes["6138"] = "/intelpress/video21stcentury/6138.htm";
bookCodes["6160"] = "/intelpress/vtune/6160.htm";
bookCodes["6161"] = "/intelpress/ixp4/6161.htm";
bookCodes["6162"] = "/intelpress/sm64/6162.htm";
bookCodes["6163"] = "/intelpress/swcb2/6163.htm";
bookCodes["6164"] = "/intelpress/haud/6164.htm";
bookCodes["6166"] = "/intelpress/mcp/6166.htm";
bookCodes["6169"] = "/intelpress/ipp2/6169.htm";
bookCodes["6170"] = "/intelpress/bvm/6170.htm";
bookCodes["6171"] = "/intelpress/efi/6171.htm";
bookCodes["6172"] = "/intelpress/inov/6172.htm";
bookCodes["6173"] = "/intelpress/soa/6173.htm";
bookCodes["6174"] = "/intelpress/grid/6174.htm";
bookCodes["6175"] = "/intelpress/iamt/6175.htm";
bookCodes["6176"] = "/intelpress/qpi/6176.htm";
bookCodes["6177"] = "/intelpress/qpied/6177.htm";
bookCodes["6178"] = "/intelpress/secc2/6178.htm";
bookCodes["6179"] = "/intelpress/rpcs/6179.htm";
bookCodes["6180"] = "/intelpress/eshl/6180.htm";
bookCodes["6181"] = "/intelpress/tv30/6181.htm";
bookCodes["6182"] = "/intelpress/ms2a/6182.htm";
bookCodes["6184"] = "/intelpress/efi2/6184.htm";
bookCodes["6185"] = "/intelpress/plc/6185.htm";
bookCodes["6186"] = "/intelpress/xpaut/6186.htm";
bookCodes["6187"] = "/intelpress/virtn/6187.htm";
// misc vars
var cookieName = "intelpress_books";
var loginSuccessfull = false;


//=====================================================
//          	cookie functions 
//=====================================================

function CheckBookAccess(CurrentBookCode){
//Check for the existance of this book's code in the intelpress books cookie.
//If it exists, bypass the access form and redirect to the examples URL desgnated for this book.
	var bookCodeList = getCookie(cookieName);
	if(bookCodeList == null){
		bookCodeList = "";
	}	
	if(bookCodeList.indexOf(CurrentBookCode) > 0){
		//8-3-2011 - during download server outage, temporarily disable redirect to download page. Digital content is being delivered manually via email.
		//window.location.replace(bookCodes[CurrentBookCode]);
	}
	
}

//---------------------------------------
//8-3-2011
//during download server outage, temporarily show a thank you message and information after successfull sn registration
window.onload = function ()
{
	if(window.location.search.substring(1).indexOf('thankyou') > -1){
		document.getElementById('accessForm').parentNode.parentNode.innerHTML = '<p class="sectiontext">We have received your request and your digital content will be sent to you using the email address you provided.</p><p>&nbsp;</p>';
		
	}
}
//---------------------------------------

function UpdateBookAccessCookie(CurrentBookCode){
//Append the current book code to codes that may already exist in the cookie
	var bookCodeList = getCookie(cookieName);
	var expiration_date = new Date("December 31, 2023");
	setCookie(cookieName, bookCodeList + CurrentBookCode + ",", expiration_date);
}

function setCookie(name, value, expires, path, domain, secure) {
  path = "/"; // forces cookie to work from your root on down
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
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;
  return unescape(dc.substring(begin + prefix.length, end));
}

//=====================================================
//			 form field validation
//=====================================================

var currentField, nextField; 
function advance(c,n) { 
    currentField = c, nextField = n;
    setTimeout('effect()',500);
}

function effect() {
    if (currentField.value.length == 4)
        document.accessForm[nextField].focus();
}

function validate(form) {
	if (form.firstname.value == "") {
		alert("Please enter your first name.")
		form.firstname.focus();
		return false;
	}
	
	if (form.lastname.value == "") {
		alert("Please enter your last name.")
		form.lastname.focus();
		return false;
	}
	
	//if (form.company.value == "") {
	//	alert("Please enter your company.")
	//	form.company.focus();
	//	return false;
	//}
	
	if (form.email.value == "") {
		alert("Please enter your email.")
		form.email.focus();
		return false;
	}
	
	if (form.sn1.value == "") {
		alert("Please enter a serial number.")
		form.sn1.focus();
		return false;
	}
	
	return true;
}
function doerror()
{
	if (request("sn1") == "") {
		location = "/intelpress/";
	} else {
		alert("Sorry. " + bsn + " is an invalid serial number."); 
		history.back(); 
	}
	
}
function x(s)
{
  // strip off leading 0 
  var j = Number(s);
  // convert to hex
  j=j.toString(16)
  // if 0 return empty string
  return j == "0" ?  "" :  j;
}


//=====================================================
//			 misc functions
//=====================================================

function request(what) 
{
   if (location.search.length > 1) 
   {
         // if there are query strings...
		var re = new RegExp();
		re.compile("[?|&]" + what + "=([^&]*)","i");
      	if (re.test(location.search)) return RegExp.$1;
   } 
   return "";
}

function binarySearch(array, find)
{
	//Validate:
	//alert("array:"+array.length)
//	alert(find);
	if(!array || typeof(array)!="object" || typeof(find)=="undefined" || !array.length){return null};
	//Initialize:
	array = array.sort();
	var low=0;
	var high=array.length-1;
	var highOnTop=(array[0].substring(0) >array[array.length-1].substring(0))?1:0;
	find = find.substring(0);
	//RUN:
	while(low<=high)
	{
		var aTry=parseInt((low+high)/2);
		var checkThis =	array[aTry].substring(0);
			if(!highOnTop)
			{
				if(checkThis<find){low=aTry+1;continue;};
				if(checkThis>find){high=aTry-1;continue;};
			}
			else
			{
				if(checkThis>find){low=aTry+1;continue;};
				if(checkThis<find){high=aTry-1;continue;};
			};
		return aTry;/*success: returns index*/
	}
	return null; /*no match: returns null*/
}


