
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "" : "") +
      ((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));
}

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}


var incomes = new Array();
incomes[0] = "0";
incomes[1] = "1000";
incomes[2] = "5000";
incomes[3] = "10000";
incomes[4] = "20000";
incomes[5] = "100000";
incomes[6] = "1000000";

var hours = new Array();
hours[0] = "0";
hours[1] = "4";
hours[2] = "6";
hours[3] = "8";
hours[4] = "10";
hours[5] = "12";
hours[6] = "16";
hours[7] = "20";
hours[8] = "24";

//constants
var imgPath = "Images/alterego_jpg/";
var img = new Array();
img[0] = imgPath+"afrc_man90X60.jpg";
img[1] = imgPath+"afrcguyvelvet90X75.jpg";
img[2] = imgPath+"asian_guitar90X60.jpg";
img[3] = imgPath+"asian_shop90X69.jpg";
img[4] = imgPath+"bitchy90X66ff.jpg";
img[5] = imgPath+"blu_straightjack90X73.jpg";
img[6] = imgPath+"dom_wom90X59.jpg";
img[7] = imgPath+"dudebeer90X66.jpg";
img[8] = imgPath+"flower_black_girl90X60.jpg";
img[9] = imgPath+"flowerpower90X66.jpg";
img[10] = imgPath+"late90X66.jpg";
img[11] = imgPath+"pencilmouth90X70.jpg";
img[12] = imgPath+"purple_senior90X60.jpg";
img[13] = imgPath+"rockguy90X71.jpg";
img[14] = imgPath+"sleepyhead90X78.jpg";
img[15] = imgPath+"tatoo_guy_bw90X60.jpg";
thumbs = new Array();
for(i=0; i<16; i++) {
	thumbs[i] = new Image();
	thumbs.src = img[i];
}

var imgPath2 = "../Images/alterego_jpg/";
var bigimg = new Array();
bigimg[0] = imgPath2+"afrc_man180x272.jpg";
bigimg[1] = imgPath2+"afrcguyvelvet180X215.jpg";
bigimg[2] = imgPath2+"asian_guitar180X271.jpg";
bigimg[3] = imgPath2+"asian_shop180X235.jpg";
bigimg[4] = imgPath2+"bitchy180X246ff.jpg";
bigimg[5] = imgPath2+"blu_straightjack180X222psd.jpg";
bigimg[6] = imgPath2+"dom_wom180X277.jpg";
bigimg[7] = imgPath2+"dudebeer180X244.jpg";
bigimg[8] = imgPath2+"flower_black_girl180X271.jpg";
bigimg[9] = imgPath2+"flowerpower180X245.jpg";
bigimg[10] = imgPath2+"late180X246.jpg";
bigimg[11] = imgPath2+"pencilmouth180X231.jpg";
bigimg[12] = imgPath2+"purple_senior180X268.jpg";
bigimg[13] = imgPath2+"rockguy180X229.jpg";
bigimg[14] = imgPath2+"sleepyhead180X209.jpg";
bigimg[15] = imgPath2+"tatoo_guy_bw180X264.jpg";

var imgPath = "Images/";
arrows = new Array(4);
arrows[0] = imgPath+"left_arrow1.jpg";
arrows[1] = imgPath+"left_arrow2.jpg";
arrows[2] = imgPath+"right_arrow1.jpg";
arrows[3] = imgPath+"right_arrow2.jpg";

arrpic = new Array(4);
for(i=0; i<4; i++) {
	arrpic[i] = new Image();
	arrpic.src = arrows[i];
}

var gender = new Array("his","his","her","her","her","her","her",
				"his","her","his","his","her","her","his","her","his");

var id = 0;

function setID(setid) {
	//set image id for alter ego
	id = setid;
}

function setChar(form) {

	//read income from form
	var income;
	for(i=0; i<form.income.length; i++) {
		if(form.income[i].checked) {
			income = incomes[i];
			break;
		}
	}
	//read hours from form
	var hour;
	for(i=0; i<form.hour.length; i++) {
		if(form.hour[i].checked) {
			hour = hours[i];
			break;
		}
	}
	//read nickname from form
	var nickname = form.nickname.value;
	//read days from form
	var days = form.daysperweek.options[form.daysperweek.selectedIndex].value;
	//calculate opportunity costs
	var oc = ( (12*income) / (hour*days*52)); 

	setCookie("id", ""+id ,null);
	setCookie("nickname", ""+nickname, null);
	setCookie("income", ""+income, null);
	setCookie("hours", ""+hour, null);
	setCookie("days", ""+days, null);
	setCookie("oc", ""+oc, null);
	
	//go to shopping page...
	document.location.href = 'cgi-bin/dcshop.cgi?database=timeproducts';
}


function getChar(id) {

	var value = Math.round( getCookie("oc")*100 )/100;
	if(isNaN(value)) { value = '0.00' }
	var nickname = getCookie("nickname");
	if((nickname=="")||(nickname==null)) { nickname = "Your alter ego"; }
	
	var content = '<span class="subcharacterfont"><center>'
					+'<table border="0" width="190" cellpadding="0" cellspacing="5"><tr><td>'
					+'<IMG SRC="'+bigimg[ id ]+'" WIDTH="180" BORDER=0 ALT="" align="bottom" hspace=0>'
					+'</td></tr><tr><td align="center">'
					+nickname+' earns $'+getCookie("income")+' a month and '
					+'works '+getCookie("days")+' days a week for about '+getCookie("hours")+' '
					+'hours each day.'
					+'</td></tr><tr><td align="center">'
					+'Thus, 1 hour of '+gender[ id ]+' life has a value of <font color="#9999FF"><b>'
					+'$'+value+'</b></font>.<BR>'
					+'In economics this amount is referred to as '+gender[ id ]+' '
					+'opportunity cost.</td></tr>'
					+'</table></span>';
	
	if(document.layers) {
		charDiv2=document.character.document.subcharacter;
		if(charDiv2) {
			charDiv2.document.open();
			charDiv2.document.write( content );
			charDiv2.document.close();
		}
	} else {
		return content;
	}
}

/*function sentence() {

	var content = ''+names[ id ]+' earns $'+incomes[ id ]+' a month and '
				+'works about '+hours[ id ]+' hours a day.';

	var divString = "sentence";
	var subdivString = "subsentence";
	charDiv=document.getElementById?document.getElementById(subdivString):document.all?document.all[subdivString]:document.layers?document.layers[divString].document.layers[subdivString]:null;
		
	if(charDiv) {

		if(document.all) {
			charDiv.innerHTML = content;
		}
		else if(document.layers) {
			charDiv.document.open();
			charDiv.document.write( content );
			charDiv.document.close();
		}
	} else {
		alert('character div not defined'+id);
	}
}
*/

charDiv = null;
scrollDiv = null;
leftscrollDiv = null;
rightscrollDiv= null;
leftscrollDivDoc = null;
rightscrollDivDoc = null;

function getCharacters() {
		
	var content = '<!--span class="subcharacterfont"-->'
				+'<table border="0" cellspacing="0" cellpadding="0" height="90"><tr>'
					+'<FORM METHOD="POST" ACTION="" name="alterego">';

	for(i=0; i<img.length; i++) {
		
		content += '<td valign="middle">'
				+'<INPUT TYPE="radio" NAME="avatar" value="'+i+'" onClick="setID('+i+')"'
				+''+((i==0)? ' checked>' : '>')
				+'</td><td valign="middle">'
				+'<IMG SRC="'+img[i]+'" WIDTH="66" HEIGHT="90" BORDER=0 ALT="" '
				+'align="right" hspace="0" onClick="javascript:setID('+i+');document.alterego.avatar['+i+'].checked=true;"></td>'
				+'<td><IMG SRC="Images/shim.gif" WIDTH="20" HEIGHT="1" BORDER=0 ALT=""></td>';
	}
	
	content += '</FORM></tr></table><!--/span-->';
	//content += '</tr></table></span>';

	return content;
}

function left() {

	if(document.all) {
		if (charDiv.offsetLeft < 0) {
			charDiv.style.pixelLeft += 12;
		}
	}
	else if(document.getElementById) {
		if (parseInt(charDiv.style.left) < 0) {
			charDiv.style.left = parseInt(charDiv.style.left)+12;
		}
	}
	else if(document.layers) {
		if (charDiv.left < 0) {
			charDiv.moveBy(12,0);
		}
	}
	timer = window.setTimeout("left()",20);
}


function right() {

	if(document.all) {
		
		if (charDiv.offsetLeft > -hiddenWidth) {
			charDiv.style.pixelLeft -=12;
		}
	}
	else if(document.getElementById) {
		//document.infos.nickname.value=parseInt(document.getElementById("subcharacters").style.left)+">"+(-hiddenWidth);
		if (parseInt(charDiv.style.left) > -hiddenWidth+100) {
			
			charDiv.style.left = parseInt(charDiv.style.left)-12;
		}
		
	}
	else if(document.layers) {
		if(charDiv.left > -hiddenWidth) {
			charDiv.moveBy(-12, 0)
		}
	}
	timer = window.setTimeout("right()",20);
}

var timer;

function scrollLefty() {
	
	document.leftarrow.src = arrows[1];
	left();
}

function scrollRight() {
	
	document.rightarrow.src = arrows[3];
	right();
}

function scrollStop() {

	document.leftarrow.src = arrows[0];
	document.rightarrow.src = arrows[2];
	window.clearTimeout(timer);
	window.status = "";
}


var scr_width;
var con_width;
var hiddenWidth;

function scrollInit() {

	scrollDiv=document.getElementById?document.getElementById("characters"):document.all?document.all["characters"]:document.layers?document.layers["characters"]:null;
	charDiv=document.getElementById?document.getElementById("subcharacters"):
		document.all?document.all["subcharacters"]:
		document.layers?document.layers["characters"].document.layers["subcharacters"]:null;
	divString = "leftscroll";
	subdivString = "subleftscroll";
	leftscrollDiv=document.getElementById?document.getElementById(subdivString):document.all?document.all[subdivString]:document.layers?document.leftscroll.document.subleftscroll:null;
	divString = "rightscroll";
	subdivString = "subrightscroll";
	rightscrollDiv=document.getElementById?document.getElementById(subdivString):document.all?document.all[subdivString]:document.layers?document.rightscroll.document.subrightscroll:null;

	if(document.all) {
		
		scr_width = scrollDiv.offsetWidth;
		version = navigator.appVersion.toLowerCase();
		if(version.indexOf("mac")!=-1) charDiv.offsetWidth //Mac hack, forces scrollWidth to fill due to IE 5 mac bug
		con_width = parseInt(charDiv.scrollWidth);
	}
	else if(document.getElementById) {
		
		scr_width = scrollDiv.offsetWidth;
        con_width = parseInt(charDiv.offsetWidth);
		//Mozilla Bug  if 0 then NaN
		if(isNaN(parseInt(charDiv.style.left))) { charDiv.style.left = 0 };
	}
	else if(document.layers) {
			
		leftscrollDiv.style = leftscrollDiv;
		rightscrollDiv.style = rightscrollDiv;

		scr_width = scrollDiv.clip.width;
		con_width = charDiv.document.width;
	}

	if(con_width > scr_width) {

		leftscrollDiv.style.visibility = "visible";
		rightscrollDiv.style.visibility = "visible";
	}
	else { 
		leftscrollDiv.style.visibility = "hidden";
		rightscrollDiv.style.visibility = "hidden";
	}
	
	//alert('scr_width: '+scr_width+'; con_width: '+con_width);

	//var con_height = (document.layers)? cont.clip.height : cont.scrollHeight;
	//var con_width = (document.layers)? cont.clip.width : cont.scrollWidth;

	// find offsetHeight
	//windowHeight = scr_height;
	//contentHeight = (document.layer)? scrollContent.doc.height : scrollContent.event.scrollHeight
	hiddenWidth = con_width - scr_width;
}

function verify(form) {

	if(form.elements["Shipping EMail"]) {

		var entry = form.elements["Shipping EMail"].value;
		if( (entry==null) || (entry=="") || isblank(entry) ) {
			alert('Please fill in your email address. Thanks!');
			return false;
		}
	}
	else {
		for(i=0; i<form.length; i++) {

			var e = form.elements[i];
			if(e.type=="text") {

				if( (e.value==null) || (e.value=="") ) {
					alert('Please fill in all the amount fields. Thanks!');
					return false;
				} 
				else {
					var v = parseFloat(e.value);
					if( isNaN(v) || (v<=0) ) {
						alert('Please enter positive numbers. Thanks!');
						return false;
					}
				}
			}
		}
	}

	return true;
}
