function err(a,b,c) {
	var errimg=new Image;
	errimg.src='http://jserrors.bookcrossing.com/'+escape(a)+','+escape(b)+','+escape(c)+'/';
	return true;
}
//window.onerror=err;

function noenter(){
	return !(window.event && window.event.keyCode == 13); 
}

function Set_Cookie(name,value,expires,domain,path,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

expires = new Date();
expires.setTime(expires.getTime() + (365 * 24 * 60 * 60 * 1000));

function checkBCIDform(myForm){
	if(myForm.bookprefix.value==""){
		alert("You must enter both parts of the BCID.");
		myForm.sysSubmitButton.value="Go!";	
    myForm.bookprefix.focus();
    return false;
	}
  if(myForm.bookid.value==""){
		alert("You must enter both parts of the BCID.");
		myForm.sysSubmitButton.value="Go!";	
    myForm.bookid.focus();
    return false;
	}
  return true
}

function toggleSubMenu(strMenuID){
	mySubMenuDivID='m'+strMenuID;
	myMenuArrowImgID='mi'+strMenuID;
	myObj=GetObjectByID(mySubMenuDivID);
	myImgObj=GetObjectByID(myMenuArrowImgID);
	if(myObj.className=='submenuexpanded'){
		myObj.className='submenu';
		myImgObj.src="/images/menu-arrow-closed-11.gif";
		Set_Cookie(mySubMenuDivID,'0',expires,".bookcrossing.com","/");
		Set_Cookie(mySubMenuDivID,'0',expires,null,"/");
	}else{
		myObj.className='submenuexpanded';
		myImgObj.src="/images/menu-arrow-open-11.gif";
		Set_Cookie(mySubMenuDivID,'1',expires,".bookcrossing.com","/");
		Set_Cookie(mySubMenuDivID,'1',expires,null,"/");
	}
}

function toggleProfile(){
	myObj=GetObjectByID('profilebio');
	myImgObj=GetObjectByID('profileicon');
	if(myObj.className=='profilebio'){
		myObj.className='profilebiohidden';
		myImgObj.src="/images/menu-arrow-closed-11.gif";
		Set_Cookie('profileHide','1',expires,null,"/");
		Set_Cookie('profileHide','1',expires,".bookcrossing.com","/");
	}else{
		myObj.className='profilebio';
		myImgObj.src="/images/menu-arrow-open-11.gif";
		Set_Cookie('profileHide','0',expires,null,"/");
		Set_Cookie('profileHide','0',expires,".bookcrossing.com","/");
	}
}

function checkEmail(myField)
{
    var newstr = "";
    var at = false;
    var dot = false;
    if (myField.value.indexOf("@") != -1) {
      at = true;
    } else if (myField.value.indexOf(".") != -1) {
      dot = true;
    }
    for (var i = 0; i < myField.value.length; i++) {
        ch = myField.value.substring(i, i + 1);
        if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
                || (ch == "@") || (ch == ".") || (ch == "_")
                || (ch == "-") || (ch >= "0" && ch <= "9")) {
                newstr += ch;
                if (ch == "@") {
                    at=true;
                }
                if (ch == ".") {
                    dot=true;
                }
        }
    }
    if (((at == true) && (dot == true))||myField.value=='') {
				myField.value=newstr;
    }else{
			alert('The email address you entered does not appear valid. Please re-enter.');
			myField.focus();
			myField.select();
			return false;
    }
}

var DOM = (document.getElementById)? true : false;
var OP = (window.opera)? true : false;
var OP5 = (OP && DOM)? true : false;
var NS = (window.outerWidth && !OP)? true : false;
var NS6 = (NS && DOM)? true : false;
var NS4 = (NS && !DOM)? true : false;
var IE = (document.all && !OP)? true : false;
var IE5 = (IE && DOM)? true : false;
var IE4 = (IE && !DOM)? true : false;


function GetObjectByID(strID){
     var Obj;
     if (NS4) Obj = document.layers[strID];
     else if (IE4) Obj = document.all[strID];
     else if (DOM) Obj = document.getElementById(strID);
     return Obj;
}

function w(x){
	document.write(x)
}


// handle blank images from Amazon
var replacementImage = new Image(1,1);
replacementImage.src = '/images/clearpixel.gif';
function imageLoadError(myImage){
  myImage.src=replacementImage.src
	return true;
}
var myHash=null
function checkAllImages(){
	//if(myHash!=null) self.location.hash=myHash;
	if(IE){
		for(i=0;i<document.images.length;i++){
			if(document.images[i].src.indexOf("amazon.com")>=0 &&
			   parseInt(document.images[i].height)<=30)
				document.images[i].src=replacementImage.src
		} 
	}
}
function checkImage(theImage){
  if(parseInt(theImage.width)<10 || theImage.width=="" || !theImage.width) theImage.src=replacementImage.src;
}

function myOnloadFunction(){
	checkAllImages();
}

window.onload=myOnloadFunction;
// end handling blank images from Amazon


// forum javascript
function checkPost(myForm){
	if(myForm.subject.value==''){
		alert("Please provide a subject for your post.");
		myForm.submitpost.value="Post";
		myForm.submitpost2.value="Post";
		myForm.subject.focus();
		return false;
	}
	if(myForm.message.value.length>7000){
		alert("The message length is limited to 7000 characters.\n\nYours is currently "+myForm.message.value.length+" characters long. Please shorten accordingly.");
		myForm.submitpost.value="Post";
		myForm.submitpost2.value="Post";
		myForm.message.focus();
		return false;
	}
	return true;
}

		
// used in forum replies
function replyWrap(strText,intLineLength,strPrefix){
  strTextOriginal=strText;
  if(strTextOriginal==null || strTextOriginal=='')return '';
	// allow for longer lines if pre-quoted text present
	if(strTextOriginal.indexOf(strPrefix)>=0) intLineLength+=strPrefix.length
	if(strTextOriginal.indexOf(strPrefix+strPrefix)>=0) intLineLength+=strPrefix.length
	if(strTextOriginal.indexOf(strPrefix+strPrefix+strPrefix)>=0) intLineLength+=strPrefix.length
	if(strTextOriginal.indexOf(strPrefix+strPrefix+strPrefix+strPrefix)>=0) intLineLength+=strPrefix.length
  strTextNew='';
  intStartPoint=0;
  intWrapPoint=0;
  while(intWrapPoint<strTextOriginal.length){
		// move forware to find pre-existing line breaks
		while(strTextOriginal.charAt(intWrapPoint)!='\n' &&
          intWrapPoint<=intStartPoint+intLineLength){
      intWrapPoint++
		}
		intWrapPoint++
		// now move backwards to find breakable character
    while(strTextOriginal.charAt(intWrapPoint) != ' ' && 
           strTextOriginal.charAt(intWrapPoint) != '' &&
           strTextOriginal.charAt(intWrapPoint) != '-' &&
					 strTextOriginal.charAt(intWrapPoint) != '\n' &&
           intWrapPoint > intStartPoint){
      intWrapPoint--;
    }
    intWrapPoint++;
		//force break on long lines w/o natural break points
		if (intWrapPoint-1==intStartPoint) intWrapPoint+=intLineLength
    if(strTextNew!='' && strTextNew.charAt(strTextNew.length-1) != '\n')strTextNew+='\n';
    strTextNew+=strPrefix+strTextOriginal.substring(intStartPoint,intWrapPoint);
    intStartPoint+=(intWrapPoint-intStartPoint);
  }
  strTextNew+='\n';
  return strTextNew;
}

// these used for book statuses
// arrays loaded in dynamic file
									
function statusMouseOver(iBCID,iOverWhich){
	var obj;
	var iStatusVal;
	iStatusVal = eval('bStatus'+iBCID);
	myStatusName=aStatusChoices[iOverWhich-1][2];
	if(iStatusVal != iOverWhich) {
		obj = document.getElementById("status_" +iOverWhich +"_" +iBCID);
		obj.className = aStatusChoices[iOverWhich-1][1];
		self.status='Click to set this book\'s status to "'+myStatusName+'"... ';
	}else{
		self.status='This book\'s status is currently "'+myStatusName+'"';
	}
	return true;
}

function statusMouseOut(iBCID,iOverWhich){
	var iStatusVal;
	var obj;
	obj = document.getElementById("status_" +iOverWhich +"_" +iBCID);
	iStatusVal = eval('bStatus'+iBCID);
	for(i=0;i<aStatusChoices.length;i++){
			if(iOverWhich == iStatusVal) {
				obj.className = aStatusChoices[iOverWhich-1][1];
				break;
			}
			obj.className = "unselectedBookStatus";
	}
	self.status='';
	return true;
}

function setStatus(sBCID,iNewStatus, sScreenName){
	var objSpan;
	var objImg;
	var i;
	var sNewSource;
	var str;
	//reloadCount = 0;		
	temp=eval('bStatus'+sBCID+'=iNewStatus');
	var objImg=new Image;
	var myRandomNumber=Math.random() //prevents caching 
	sNewSource = "/actSetBookStatus.htm?BCID="+sBCID+"&newstatus="+iNewStatus+"&username="+sScreenName+"&refresh="+myRandomNumber; 		
	objImg.src= sNewSource;
	for (i=1;i<=5;i++){
		if(i!=iNewStatus){
			str = "status_"+i+"_"+sBCID;
			objSpan = document.getElementById(str);
			objSpan.className = "unselectedBookStatus";
		}
	}
}


function s(drawingType, myBCID, displayBookStatus, myScreenName){
	var i,j;
	for(i=0; i<(aStatusChoices.length);i++){
		w("<span title=\"" +aStatusChoices[i][2] +"\" ");
		if(displayBookStatus == aStatusChoices[i][0]) {
			w(" class='" +aStatusChoices[i][1] +"static'");
		}else{
			w(" class=\"unselectedBookStatus\" ");
			switch(drawingType){
			case 0:
				break;
			case 1:
				w(" onMouseOver=\"statusMouseOver(" +myBCID +"," +aStatusChoices[i][0] +");\"");
				w(" onMouseOut=\"statusMouseOut(" +myBCID +"," +aStatusChoices[i][0] +");\"");
				w(" onClick=\"setStatus(" +myBCID +"," +aStatusChoices[i][0] +", '" +myScreenName +"');\""); 
				break;
			}
		}
		w(" id=\"status_" +aStatusChoices[i][0] +"_" +myBCID +"\"");
		w(">" +aStatusChoices[i][1]);
		w("</span>" );
		if(i!=(aStatusChoices.length -1)){
			w(" | ");
		}
	}
}




