// JavaScript Document

function email(a,b){
	document.location = "mailto:" + a + "\x40" + b;
}

function email2(addr){
	//addr is the address with a @ sign replaced by an image
	//find the first <
	str=addr.innerHTML
	//alert (str)
	bit=str.split(/[<>]/)
	a=bit[0]
	b=bit[2]
	document.location = "mailto:" + a + "\x40" + b;
}

function buy($amazon, $isbn){ //buy from Amazon using 10 or 13 digit ISBNs
	if ($amazon=="US"){
		$tag='leonchaitowco-20';
		$url='http://www.amazon.com';
	}
	else{
		$tag='wwwleonchaito-21';
		$url='http://www.amazon.co.uk';
	}
	
	$re=/-/g;  //the regular expression to remove hyphens
	$isbnraw=$isbn.replace($re,"");
			
	$amazonlink="";
	if ($isbnraw.length==10){//traditional
		$amazonlink= $url  + "/exec/obidos/ASIN/" + $isbnraw + "/" + $tag ;
		}
	else  {//13 digit means look it up in amazon
		$amazonlink=$url + "/gp/search?keywords=" + $isbnraw + "&index=books&linkCode=qs&tag=" + $tag ;
					
	}
	window.location.href=$amazonlink;
	//am=window.open($amazonlink, 'amazon');
	//am.focus()
}
function buy2($country,$isbn){
 //buy from Amazon using 10 or 13 digit ISBNs
	if ($country=="US"){
		$tag='leonchaitowco-20';
		$url='http://www.amazon.com';
	}
	else{ 
		$tag='wwwleonchaito-21';
		$url='http://www.amazon.co.uk';
	}
	//debugger
	$re=/[- ]/g;  //the regular expression to remove hyphens and spaces
	$isbnraw=$isbn.replace($re,"");
			
	$amazonlink="";
	if ($isbnraw.length==10){//traditional
		$amazonlink= $url  + "/exec/obidos/ASIN/" + $isbnraw + "/" + $tag ;
		}
	else if($isbnraw.length==13) {//13 digit means work out the 10 digit equivalent
		$isbncore=$isbnraw.substr(3,9);
		mult=10;
		totsum=0
		//alert($isbncore);
		for(c=0; c<9; c++){
			totsum += mult * $isbncore.substr(c,1)
			mult--
		}
		rem=totsum%11
		if(rem==0) addon="0";	else addon=String(11-rem)
		
		if(addon=="10"){addon="X"}
		$isbn10=$isbncore + addon
		$amazonlink=$url  + "/exec/obidos/ASIN/" + $isbn10 + "/" + $tag ;
		//alert($amazonlink)
					
	}
	else {
		alert ("Sorry " + $isbn + " is not a valid ISBN")
	}
	window.location.href=$amazonlink;
	
}
