/*********************************************
 **                                         **
 **        Javascript code copyright        **
 **       (c)2005 by Arvana Robinson        **
 **          www.arvanadesign.com           **
 **           ALL RIGHTS RESERVED           **
 **                                         **
 **      Some code used & adapted from      **
 **        Paul's Javascript Cookie         **
 **       Libraries (paulspages.co.uk)      **
 **                   &                     **
 **      Dynamic Drive Amazon Drop Box      **
 **         (www.dynamicdrive.com)          **
 **                                         **
 *********************************************/

hover = new Image();
unhover = new Image();
hover.src = "graphics/bookmark-creating-website.gif" ;
unhover.src = "graphics/bookmark-creating-a-website.gif" ;


//  ******  HOME PAGE FUNCTIONS  ******

function pageInit() {    // functions to run on page load

	// check if user has already signed up to newsletter
	var pause = 500 ;  // time before box drops (ms)
	userCookie = new cookieObject("homePageData", 365, "/", "asked", "subscribed", "q1", "q2", "q3", "q4") ;
//  if (!(userCookie.found) || ((userCookie.found) && (userCookie.get("asked") == "undefined"))) {
//		newsDrop = setTimeout("newsBoxInit()",pause) ;  // drop in newsletter signup box
//	}

	//set question values from cookie if present
	var form = document.designQuestions ;
	var q, question ;
	if (userCookie.found) {
		for (i=1 ; i<=4 ; i++) {
			question = "q" + i ;
			q = userCookie.get(question) * 1 ;
			if (q) {
				if (i==1) form.designTime[q - 1].checked = true ;
				if (i==2) form.designBudget[q - 1].checked = true ;
				if (i==3) form.selfUpdate[q - 1].checked = true ;
				if (i==4) form.techDesign[q - 1].checked = true ;
			}
		}
	}
	var setAnswers = designPath() ;  // set answers if all questions checked

}




function designPath() {		// determine visitor's best approach to web design

	// check which radio buttons are selected.
	// Note - variables are set non-zero (i+1) so that they can be tested as defined.
	// The 'window' object is used to prevent potential errors in checking undefined variables.
	var form = document.designQuestions ;
  for (i=0; i <= 4; i++) {
		if (i<=3 && form.designTime[i].checked) window.time = i+1 ;
		if (form.designBudget[i].checked) window.budget = i+1 ;
		if (i<=1 && form.selfUpdate[i].checked) window.update = i+1 ;
		if (i<=1 && form.techDesign[i].checked) window.tech = i+1 ;
	}

	// test if all radio-button groups have been answered; otherwise, do nothing
	if ( (window.time) && (window.budget) && (window.update) && (window.tech) ) {

		// set matrix of answers based on combinations of responses
		var lookup = [[[5,5,5,5],[6,67,6,67],[16,17,126,127]  ,[127,127,1247,1247],[247,247,24,24]],
		              [[5,5,5,5],[6,67,6,67],[126,127,1234,124]   ,[148,148,1348,1348],[348,348,348,348]],
		              [[8,8,8,8],[8,8,8,8]  ,[138,138,1238,1238],[1348,1348,1348,1348],[348,348,348,348]],
		              [[8,8,8,8],[8,8,8,8]  ,[38,38,38,38]      ,[348,348,348,348]  ,[348,348,348,348]]];

		var dim = (2 * (window.tech - 1)) + (window.update - 1) ;
		var designOptions = lookup[window.time - 1][window.budget - 1][dim] ;
		designOptions = String(designOptions) ;

		for (i=1; i <= 8; i++) {    // turn off all suggestions
			answer = "answer" + i ;
			document.getElementById(answer).style.display = "none" ;
		}
		for (i = 0; i < designOptions.length; i++) {     // turn on suggestions from lookup matrix
			answer = "answer" + designOptions.charAt(i) ;
			document.getElementById(answer).style.display = "block" ;
		}

		// set cookie to remember answers for next time
		var userCookie = new cookieObject("homePageData", 365, "/", "asked", "subscribed", "q1", "q2", "q3", "q4") ;
		userCookie.put("q1", window.time) ;
		userCookie.put("q2", window.budget) ;
		userCookie.put("q3", window.update) ;
		userCookie.put("q4", window.tech) ;
		userCookie.write() ;
	}

	return true ;
}


/******  NEWSLETTER WINDOW FUNCTIONS  ******

var yPos = (window.screen.height / 2) - 400 ;
if (yPos < 10) yPos = 10 ;

var dropSpeed = 8 ;  // step size of drop (px)
var stepTime = 15 ;  // drop step interval (ms)

var ie=document.all ;
var dom=document.getElementById ;

function newsBoxInit(){
	if (!dom&&!ie)
		return ;
	boxWrap = (dom) ? document.getElementById("newsBoxWrap") : document.all.newsBoxWrap ;
	box = (dom) ? document.getElementById("newsBox") : document.all.newsBox ;
	scroll_top = (ie) ? trueBody().scrollTop : window.pageYOffset ;
	box.height = box.offsetHeight ;
	boxWrap.style.height = parseInt(box.height) + "px" ;
	box.style.top = box.height * (-1) + "px" ;
	boxWrap.style.top = yPos + "px" ;
	boxWrap.style.display = "block" ;
	dropstart = setInterval("dropBox()",stepTime) ;
}

function dropBox() {
	scroll_top = (ie) ? trueBody().scrollTop : window.pageYOffset ;
  if (parseInt(box.style.top) < 0) {
		boxWrap.style.top = scroll_top + yPos + "px" ;
		box.style.top = parseInt(box.style.top) + dropSpeed + "px" ;
	} else {
		clearInterval(dropstart) ;
		box.style.top = 0 ;
	}
}

function closeBox(){
	if (window.dropstart) clearInterval(dropstart) ;
	boxWrap.style.display = "none" ;
	var userCookie = new cookieObject("homePageData", 365, "/", "asked", "subscribed", "q1", "q2", "q3", "q4") ;
	
}

function trueBody(){
	return (document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body ;
}



//		timer = setTimeout("movedown()",pause);



function get_cookie(cookieName) {
	var search = cookieName + "=" ;
	var returnvalue = "" ;
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search) ;
		if (offset != -1) {
			offset += search.length ;
			end = document.cookie.indexOf(";", offset) ;
			if (end == -1)
				end = document.cookie.length ;
			returnvalue=unescape(document.cookie.substring(offset, end)) ;
		}
	}
	return returnvalue ;
}


*/



//  ******  COOKIE HANDLING FUNCTIONS  ******


function cookieObject(cookieName, cookieExpires, accessPath) {
  var i, j ;
  this.name = cookieName ;
  this.fieldSeparator = "#" ;
  this.found = false ;
  this.expires = cookieExpires ;
  this.accessPath = accessPath ;
  this.rawValue = "" ;
  this.fields = new Array() ;
  this.fieldnames = new Array() ;
  if (arguments.length > 3) { // field name(s) specified
    j = 0 ;
    for (i = 3; i < arguments.length; i++) {
      this.fieldnames[j] = arguments[i] ;
      j++ ;
    }
    this.fields.length = this.fieldnames.length ;
  }
  this.read = ucRead ;

  this.write = ucWrite ;

  this.remove = ucDelete ;
  this.get = ucFieldGet ;
  this.put = ucFieldPut ;
  this.namepos = ucNamePos ;
  this.read() ;
}


function ucFieldGet(fieldname) {
var i = this.namepos(fieldname) ;
if (i >=0) {
  return this.fields[i] ;
} else {
  return "BadFieldName!" ;
}
}

function ucFieldPut (fieldname, fieldval) {
var i = this.namepos(fieldname) ;
if (i >=0) {
  this.fields[i] = fieldval ;
  return true ;
} else {
  return false ;
}
}

function ucNamePos(fieldname) {
var i ;
for (i = 0; i < this.fieldnames.length; i++) {
  if (fieldname == this.fieldnames[i]) {
    return i ;
  }
}
return -1 ;
}


function ucWrite() {
  var cookietext = this.name + "=" ;

// concatenate array elements into cookie string

// Special case - single-field cookie, so write without # terminator
	if (this.fields.length == 1) {
		cookietext += escape(this.fields[0]) ;
	} else {   // multi-field cookie
		for (i=0; i < this.fields.length; i++) {
			cookietext += escape(this.fields[i]) + this.fieldSeparator ; }
	}

// Set expiry parameter, if specified
    if (this.expires != null) {
      if (typeof(this.expires) == "number") { // Expiry period in days specified
        var today=new Date() ;
        var expiredate = new Date() ;
        expiredate.setTime(today.getTime() + 1000*60*60*24*this.expires) ;
        cookietext += "; expires=" + expiredate.toGMTString() ;
      } else { // assume it's a date object
        cookietext +=  "; expires=" + this.expires.toGMTString() ;
      } // end of typeof(this.expires) if
    } // end of this.expires != null if

	// add path, if specified
   if (this.accessPath != null) {
   cookietext += "; PATH="+this.accessPath ; }

	// write cookie
	// alert("writing "+cookietext)
   document.cookie = cookietext ;
   return null ;
}


function ucRead() {
  var search = this.name + "=" ;
  var CookieString = document.cookie ;
  this.rawValue = null ;
  this.found = false ;
  if (CookieString.length > 0) {
    offset = CookieString.indexOf(search) ;
    if (offset != -1) {
      offset += search.length ;
      end = CookieString.indexOf(";", offset) ;
      if (end == -1) {  // cookie is last item in the string, so no terminator
       end = CookieString.length ; }
      this.rawValue = CookieString.substring(offset, end) ;
      this.found = true ;
      }
    }

if (this.rawValue != null) { // unpack into fields

  var sl = this.rawValue.length ;
  var startidx = 0 ;
  var endidx = 0 ;
  var i = 0 ;

// Special case - single-field cookies written by other functions,
// so without a '#' terminator

if (this.rawValue.substr(sl-1, 1) != this.fieldSeparator) {
  this.fields[0] = unescape(this.rawValue) ;
  } else { // separate fields

  do
  {
   endidx = this.rawValue.indexOf(this.fieldSeparator, startidx) ;
   if (endidx !=-1) {
     this.fields[i] = unescape(this.rawValue.substring(startidx, endidx)) ;
     i++ ;
     startidx = endidx + 1 ; }
  }
  while (endidx !=-1 & endidx != (this.rawValue.length -1));
}
} // end of unpack into fields if block
  return this.found ;
} // end of function


function ucDelete() {
  this.expires = -10 ;
  this.write() ;
  return this.read() ;
}
