/**
 * @author updoor
 */
var timerID = 0;
var smethod = '';

semethod = function (input, othr)
{
	if(input.value == ''){
		smethod = othr;
	}
	else{
		smethod = input.id;
	}
}

sesearch = function ()
{
	search(smethod,'');
}

setimer = function (input)
{
	var timstr;

	timstr = "search('" + input + "','')"; 
	if(timerID != 0){
		clearTimeout(timerID);
	}
	timerID = setTimeout(timstr, 400);
}

search = function (input, evt)
{
	timerID = 0;

	var tcd = get_tcd();
	var min_length = 2;
	var uri = 'res_list.php';
	//var uri = 'isbn_search.php';
	//var params = Form.serialize('form01');
	var params = 'tcd=' + tcd;
	var id = input;
	var enable_search = false;
	switch (id) {
	case 'ctitle_s':
		if ($F('ctitle_s') != '') enable_search = true;
		params += '&ctitle_s=' + encodeURI($F('ctitle_s'));
		break;
	case 'ctitle_c':
		if ($F('ctitle_c') != '') enable_search = true;
		params += '&ctitle_c=' + encodeURI($F('ctitle_c'));
		break;
	default:
		if ($F('ctitle') != '') enable_search = true;
		params += '&ctitle=' + encodeURI($F('ctitle'));
	}
	//$('content_list').innerHTML = params;
	//return false;
	if (enable_search == true) {
			var options = {
			method: 'post',
			parameters: params,
			evalScripts: true,
			onComplete: done_search
		}
		var SearchAjax = new Ajax.Updater('content_list', uri, options);
	}
	
}
done_search = function ()
{
	parent.map0.location.href = 'syousai1.php?done_search=true';
	blinkString('msg_blink',1000);
}

set2Frame = function(jpURL1, jpURL2)
{
	parent.map0.location.href = jpURL1;
	parent.map1.location.href = jpURL2;
}

get_tcd = function ()
{
	var tcd = $F('tcd');
	return tcd;
}

searchByIsbn = function(){
	var isbn = $('isbn_number').value;
	timerID = 0;
	var tcd = get_tcd();
	var uri = 'isbn_search.php';
	var orign_isbn = isbn;
	do{
		isbn= isbn.replace(/-|[ ]/,'');
	}while( isbn.match(/-|[ ]/) != null );
	
	if(!isRightIsbn(isbn)){
		alert('ISBNコードが不正です。');
		return ;
	}
	if( isbn.length == 13){
		isbn = calcIsbn10From13(isbn);
	}
	var params = 'tcd=' + tcd+'&isbn='+isbn+'&orign_isbn='+orign_isbn;	
	var options = {
		method: 'post',
		parameters: params,
		evalScripts: true,
		onComplete: done_search
	}
	var SearchAjax = new Ajax.Updater('content_list', uri, options);
	
}

isRightIsbn = function(isbn){
	if( null == isbn.match(/[^0-9xX]/) ){
		if( 10 == isbn.length ){
			isbn = isbn.substr(0,9);
			if( null == isbn.match(/[^0-9]/) ){
				return true;
			}
		}else if( 13 == isbn.length ){
			isbn = isbn.substr(3,9);
			if( null == isbn.match(/[^0-9]/) ){
				return true;
			}
		}
	}
	return false;
}

calcIsbn10From13 = function(isbn){
	if( isbn.length != 13){ return null; }
	isbn = isbn.substr(3,9);
	l1 = isbn.charAt(0);
	l2 = isbn.charAt(1);
	l3 = isbn.charAt(2);
	l4 = isbn.charAt(3);
	l5 = isbn.charAt(4);
	l6 = isbn.charAt(5);
	l7 = isbn.charAt(6);
	l8 = isbn.charAt(7);
	l9 = isbn.charAt(8);
	
	rs1 = 10*l1 + 9*l2 + 8*l3 + 7*l4 + 6*l5 + 5*l6 + 4*l7 +3*l8 + 2*l9;
	rs2 = 11 - (rs1%11);
	c_d = 0;
	switch(rs2){
	case 11:
		c_d = 0;
		break;
	case 10:
		c_d = 'x';
		break;
	default :
		c_d = rs2;
	}
	return isbn+c_d;
}

var Uss = {};
Uss._call_count = 0;
Uss.timer_id = null;
Uss.before_filters = [];

var blinkString = function(id,time){
	var doc = document.getElementById(id);
	if( doc == null ){
		return ;
	}
	var blinkF = function(id,c){
		var doc = document.getElementById(id);
		return function(){	
			if( doc.style.visibility == 'hidden' ){
				doc.style.visibility = 'visible';
			}else{
				doc.style.visibility = 'hidden';
			}
			//alert(c);
		};
	}
	if( Uss.timer_id != null ){
		clearInterval(Uss.timer_id);
	} 
	Uss.timer_id = setInterval(blinkF(id,Uss._call_count++),time);
}

Uss.beforeFilter = function(func){
	this.before_filters[this.before_filters.length] = func;
}

Uss.doBeforeFilter = function(){
	for(index=0;index<this.before_filters.length;index++){
		this.before_filters[index]();
	}
}

//Uss.beforeFilter(function(){ /* alert('hoge'); */  });

window.onload = function()
{
	Form.focusFirstElement('form01');
	Uss.doBeforeFilter();
}


