//***********************
//*						*
//*	Search engine part	*
//*						*
//***********************

var schObj = new Object();

schObj.finderPass = $("finderPass").value;
schObj.cookieName = "";

schObj.serchMax=4;
schObj.cateMax= 1;

schObj.dsMain = new Spry.Data.XMLDataSet(schObj.finderPass + "products_db.xml" , "/productslist", {useCache: true});
schObj.dsSym = new Spry.Data.XMLDataSet(schObj.finderPass + "products_symbol.xml" , "/products_symbol/dlist", {useCache: true});
schObj.dsCat = new Spry.Data.XMLDataSet(schObj.finderPass + "main_category.xml" , "/main_category/dlist", {useCache: true});
schObj.dsCat_sub = new Spry.Data.XMLDataSet(schObj.finderPass + "sub_category.xml", "/sub_category/dlist", {useCache: true});

schObj.obMain = new Object();
schObj.obSym = new Object();
schObj.obCat = new Object();
schObj.obCat_sub = new Object();

schObj.dsMain_Ok = false;
schObj.dsSym_Ok = false;
schObj.dsCat_Ok = false;
schObj.dsCat_sub_Ok = false;

schObj.all_Ok = false;

schObj.msgArrowMax=3;
schObj.msgEnd = false;
schObj.dsIntId="";
schObj.serchArrowId="";
schObj.serchArrowOrg="";

schObj.serchMsgCnt=0;


schObj.serchNum =0;
schObj.subNum =0;

schObj.setCnt =0;
schObj.searchValue = new Array("",0,0,0,0);
schObj.searchValue_num = new Array("",0,0,0,0);
schObj.searchValue_2_num =0;
schObj.searchValue_4 = new Array();
schObj.searchValue_4_row = new Array();

schObj.lotConFlg = new Array("",false,false,false,true);		// Two or more conditions flg

schObj.scSym = new Array("","mainID","subID","psymbol");
schObj.xPath ="";		// SchObj.xPath initial value

schObj.mainIdArray = new Array();
schObj.subIdArray = new Array();

schObj.cookieString ="";
schObj.getString1="";
schObj.getString2="";
schObj.getString3= new Array();

schObj.captionTitle ="";

schObj.thisObj = this;
schObj.msgObj = new msgControler();


//xml Callback processing
window.onload = function() {
	schObj.captionTitle = $("searchDatacaption").innerHTML;
}

schObj.obMain.onPostLoad = function(dataSet, data) {
	schObj.dsMain_Ok = true;
}
schObj.dsMain.addObserver(schObj.obMain);

schObj.obSym.onPostLoad = function(dataSet, data) {
	schObj.dsSym_Ok = true;
}
schObj.dsSym.addObserver(schObj.obSym);

schObj.obCat.onPostLoad = function(dataSet, data) {
	schObj.dsCat_Ok = true;
}
schObj.dsCat.addObserver(schObj.obCat);

schObj.obCat_sub.onPostLoad = function(dataSet, data) {
	schObj.dsCat_sub_Ok = true;
}
schObj.dsCat_sub.addObserver(schObj.obCat_sub);


schObj.selVisible = function() {

	var dsp_Ok = true; 
	if(schObj.dsMain_Ok == false) dsp_Ok = false;
	if(schObj.dsSym_Ok == false) dsp_Ok = false;
	if(schObj.dsCat_Ok == false) dsp_Ok = false;
	if(schObj.dsCat_sub_Ok == false) dsp_Ok = false;

	if(dsp_Ok==true) {
	
		delAllObserver();			//Area Observer deletion
		
		schObj.searchValue[1] =1;
		schObj.searchValue[2] =0;
		schObj.searchValue[3] =0;

		initRunner();

		schObj.all_Ok = true;
		
		getIdArray();


		//The arrangement passing of xml is converted into the cookie name. 
		var cookieName1 = schObj.finderPass.split("/");
		var i=0;
		while(i<cookieName1.length) {
			if(cookieName1[i] !="") schObj.cookieName +=cookieName1[i];
			i++;
		}
	
		schObj.getCookie();		//The search condition is called from cookie. 

		if(schObj.cookieString != "") {
			document.product_form["productName_11"].selectedIndex = schObj.searchValue_num[2];

			schObj.subNum =0;
			schObj.dsCat_sub.filter(myFilterFunc);			// Filter the rows in the data set.

			if(schObj.subNum !=0) {
				$("productName2").style.display = "block";
				document.product_form["productName2_11"].selectedIndex = schObj.searchValue_num[3];
			} else {
				$("productName2").style.display = "none";
			}
				
			if(schObj.searchValue_4_row.length !=0) {
				var i=0;
				while(i<schObj.searchValue_4_row.length) {
					if(schObj.searchValue_4_row[i]) {
						if(schObj.searchValue_4_row[i]==1) {
							document.product_form["product_symbol" +i].checked = true;
							schObj.searchValue_4[i] = schObj.dsSym.getData()[i].symID;
						} else {
							document.product_form["product_symbol" +i].checked = false;
							schObj.searchValue_4[i] =0;
						}
					}
					i++;
				}

			} else {
				document.product_form.product_symbol0.checked = true;
				schObj.searchValue_4[0] = "*";
				schObj.searchValue_4_row[0] =1;
			}
			
			schObj.retrievalResult();
			schObj.dataDspRun();

		} else {
			document.product_form.product_symbol0.checked = true;
			schObj.searchValue_4[0] = "*";
			schObj.searchValue_4_row[0] =1;
		}

		clearInterval(schObj.dsIntId);
	
	}

}

//MainID. subID extraction
function getIdArray() {
	var num =1;
	var mId ="";
	var sId ="";

	while(schObj.dsCat.getData()[num]) {
		mId = schObj.dsCat.getData()[num].mainID;
		schObj.mainIdArray[mId] = schObj.dsCat.getData()[num].dname;
		num++;
	}
	
	num =1;
	while(schObj.dsCat_sub.getData()[num]) {
		mId = schObj.dsCat_sub.getData()[num].mainID;
		sId = schObj.dsCat_sub.getData()[num].subID;
		schObj.subIdArray[mId+sId] = schObj.dsCat_sub.getData()[num].dname;
		num++;
	}
}


//Area Observer deletion
function delAllObserver() {
	if(schObj.dsMain_Ok) schObj.dsMain.removeObserver(schObj.obMain);
	if(schObj.dsSym_Ok) schObj.dsSym.removeObserver(schObj.obSym);
	if(schObj.dsCat_Ok) schObj.dsCat.removeObserver(schObj.obCat);
	if(schObj.dsCat_sub_Ok) schObj.dsCat_sub.removeObserver(schObj.obCat_sub);
}

	
//Initial display processing
function initRunner(){

	schObj.serchArrowOrg = $("searchArrowArea").childNodes.item(0).src;

	schObj.msgObj.msgClear();	//Message clearness

	if($("selectArea")) $("selectArea").style.display = "block";
	
	if($("selectInputArea")) $("selectInputArea").style.display = "block";

	if($("searchArrowArea")) $("searchArrowArea").style.display = "block";

	if($("productName2")) $("productName2").style.display ="none";

	document.product_form["productName_11"].selectedIndex = 0;

}


//Actual processing execution
schObj.msgObj.msgDisp(0);	// "Be reading" message display. 
schObj.dsIntId = setInterval("schObj.selVisible()",100); // Check that has prepared data set at 0.1 cycles of second

//Retrieval of data and display
schObj.dataDspInit = function() {
	schObj.msgEnd = false;
	schObj.serchMsgCnt=0;
	schObj.serchArrowDsper();
	schObj.serchArrowId = setInterval("schObj.serchArrowDsper()", 250);

	if(schObj.searchValue != 0) {
		$("searchDataBlock").style.display = "none";
		schObj.msgObj.msgDisp(10);

		schObj.dsIntId = setInterval("product_form.submit()",1000);

	} else {
		schObj.msgObj.msgDisp(200);
	}
}


//Diagnosis and treatment department name set for data filter processing/display
function myFilterFunc(dataSet, row, rowNumber)
{
	if(schObj.searchValue[2] !=0) {

		if(row["mainID"] == "*") {
			return row;		// Return the row to keep it in the data set.
		} else 	if(row["mainID"] == schObj.searchValue[2]) {		//Data filter processing
			schObj.subNum++;
			return row;		// Return the row to keep it in the data set.
			
		} else {
			return null;	// Return null to remove the row from the data set.
		}
	
	} else {
		
		return row;		// Return the row to keep it in the data set.
	}
		
}



//Processing that is called in select/check/radio event
schObj.selectResult = function(thisObj,rowNum,num,stat) {
	
	var getValueData = thisObj.value;
	serchStatClear(num);		//Selection and nether search condition clearness

	switch(stat) {

		case "sel":
			if(num==2) {

				if(getValueData != "*") {
					schObj.searchValue[num] = getValueData;
					schObj.searchValue_num[num] = document.product_form["productName_11"].selectedIndex;
					schObj.subNum =0;
					schObj.dsCat_sub.filter(null);
					schObj.dsCat_sub.filter(myFilterFunc);			// Filter the rows in the data set.

					if(schObj.subNum != 0) {
						if(schObj.subNum !=0) {
							$("productName2").style.display = "block";
							document.product_form["productName2_11"].selectedIndex = 0;
						} else {
							searchClear_3();	//Search condition clearness
						}

					} else {
						searchClear_3();	//Search condition clearness
					}
				
				} else {
					//schObj.searchValue[num] = getValueData; 
					searchClear_3();	//Search condition clearness
				}

			} else if(num==3) {
				schObj.searchValue_num[3] = document.product_form["productName2_11"].selectedIndex;

				if(getValueData != "*") {
					schObj.searchValue[num] = getValueData;
				} else {
					schObj.searchValue[num] = 0;
				}

			}

			break;
		
		case "chek":

			var rows = schObj.dsSym.getData();
	
			if(getValueData !="*") {

				if(schObj.searchValue_4_row[rowNum] == 1) {
					schObj.searchValue_4[rowNum]= 0;
					schObj.searchValue_4_row[rowNum] = 0;
				} else {
					schObj.searchValue_4[rowNum] = getValueData;
					schObj.searchValue_4_row[rowNum] = 1;
				}

				var chekCnt=0;
				var rowCnt=1;
				while(rowCnt<rows.length) {
					if(schObj.searchValue_4[rowCnt]) {
						if(schObj.searchValue_4_row[rowCnt] == 1) chekCnt++;
					}
					rowCnt++;
				}

				if(chekCnt >0) {
					if(document.product_form.product_symbol0) {
						document.product_form.product_symbol0.checked = false;
						schObj.searchValue_4_row[0] = 0;
						schObj.searchValue_4[0] = 0;

					}
				} else {
					if(document.product_form.product_symbol0) document.product_form.product_symbol0.checked = true;
					schObj.searchValue_4 = new Array();
					schObj.searchValue_4_row = new Array();
					schObj.searchValue_4[0] = "*";
					schObj.searchValue_4_row[0] = 1;
					var rowCnt=1;
					while(rowCnt<rows.length) {
						schObj.searchValue_4[rowCnt] =0;
						schObj.searchValue_4_row[rowCnt] =0;
						rowCnt++;
					}

					//schObj.searchValue[4] = 0;
				}

			} else {
				schObj.searchValue_4_row = new Array();
				schObj.searchValue_4[0] = "*";
				schObj.searchValue_4_row[0] = 1;

				var rowCnt=1;
				while(rowCnt<rows.length) {
					if(document.product_form["product_symbol" + rowCnt]) document.product_form["product_symbol" + rowCnt].checked = false;
					schObj.searchValue_4[rowCnt] =0;
					schObj.searchValue_4_row[rowCnt] =0;
					var tempId = "product_symbol" + rowCnt;
					rowCnt++;
				}
				if(document.product_form.product_symbol0) {
					document.product_form.product_symbol0.checked = true;
				}
			}
			
			break;

		default:
			break;
	}



	//Selection and nether search condition clearness
	function serchStatClear(num) {
		var i=num;
		while(i<=schObj.serchMax) {
			if(schObj.lotConFlg[i]==false) schObj.searchValue[i] =0;
			i++;
		}
	}

	//Search condition clearness
	function searchClear_2() {
		schObj.searchValue[2] = 0;
		schObj.searchValue[3] = 0;
		if($("productName")) $("productName").style.display ="none";

	}
	
	function searchClear_3() {
		schObj.searchValue[3] = 0;
		if($("productName2")) $("productName2").style.display ="none";

	}

	//Initial display execution when data set has been prepared
	function selCategory(catNum) {
		if($("productName")) $("productName").style.display = "block";
		if($("productName_" + catNum)) {
			schObj.searchValue[2] = "-";
			$("productName_" + catNum).style.display = "block";
			document.product_form["productName_" + catNum + catNum].selectedIndex = 0;
		}
		searchClear_3();
	}

	function selCategory_sub(catNum) {
		if($("productName2")) $("productName2").style.display = "block";
		if($("productName2_" + 	catNum)) {
			schObj.searchValue[3] = "-";
			$("productName2_" + catNum).style.display ="block";
			document.product_form["productName2_" + catNum + catNum].selectedIndex = 0;
		}
	}

}


//XPath generation for retrieval
schObj.retrievalResult = function() {
	schObj.xPath = "productslist/products[";			// SchObj.xPath initial value
	var num =2;
	schObj.setCnt =0;

	while(num<=4) {
		
		if(schObj.searchValue[num] =="-") {
			schObj.setCnt = null;
			break;
		}
		
		var searchSymbol = schObj.scSym[num-1];
		if((searchSymbol == "mainID") && (schObj.searchValue[1] !=0)) searchSymbol = "mainID";
		if((searchSymbol == "subID") && (schObj.searchValue[1] !=0)) searchSymbol = "subID";

		var xpsymStat = searchSymbol.indexOf("psymbol");
		var psymStat = xpsymStat.toString();
		
		if(num==4) {			//チェックボックス

			var i=1;
			var setCnt2 =0;
			if(schObj.searchValue_4) {
				while(i < schObj.searchValue_4.length) {
					var searchValue1 = schObj.searchValue_4[i];
					
					if(searchValue1) {
						if((schObj.setCnt ==0) && (setCnt2 ==0)) {
							schObj.xPath += '(';
						} else if((schObj.setCnt !=0) && (setCnt2 ==0)) {
							schObj.xPath += ' and (';
						} else if(setCnt2!=0) 	{
							if(psymStat !=-1){
								schObj.xPath += ' or ';
							} else {
								schObj.xPath += ' and ';
							}
						}
					
						var x =0;
						var searchValue2 = searchValue1.split(",");
						
						while(x<searchValue2.length) {
							if(searchValue2[x] != "") {
								if(x !=0) schObj.xPath += ' or ';
								schObj.xPath += " (" + searchSymbol+ "='" +  searchValue2[x] + "')";
							}
							x++;
						}
						setCnt2++;
					}
					i++;
				}
			} else {
				xpathSET_3();	//The parameter of the function is added to the value of schObj.xPath.
			}
			
			if(setCnt2 !=0) schObj.xPath += ')';
			schObj.setCnt++;

		} else {
			xpathSET_3();	//The parameter of the function is added to the value of schObj.xPath.
		}

		num++;
	}
	
	schObj.xPath += "]";


	//The parameter of the function is added to the value of schObj.xPath. 
	function xpathSET_3() {
		if(schObj.searchValue[1] !=0) {
			if(((num==2)||(num==3))&&(schObj.searchValue[num] ==0)) xpathSET_3_A();
			if(schObj.searchValue[num] !=0) xpathSET_3_B();
		}
	}

	function xpathSET_3_A() {
		if(num==3) schObj.xPath += ' and ';
		schObj.xPath += '(' + searchSymbol + "!= 'undefined')" ;	  //The parameter of the function is added to the value of schObj.xPath.
		schObj.setCnt++;

	}

	function xpathSET_3_B() {
		if(num==3) {
			schObj.xPath += ' and (' + searchSymbol + "='" + schObj.searchValue[num] +"')" ;	  //The parameter of the function is added to the value of schObj.xPath.
		} else {
			if(schObj.setCnt>0) {
				schObj.xPath += ' and (';
			} else {
				schObj.xPath += '(';
			}
			schObj.xPath += searchSymbol + "='" + schObj.searchValue[num] +"')" ;	  //The parameter of the function is added to the value of schObj.xPath.
			schObj.setCnt++;
		}
	}
							
}


//Retrieval of data and display
schObj.dataDspRun = function(stat) {

	clearInterval(schObj.dsIntId);

	schObj.setCookie();	// The search condition is set in cookie.

	schObj.serchNum =0;

	schObj.msgObj.msgDisp(10);	//"Be retrieving it" message display.
		
	schObj.retrievalResult();

	if((schObj.setCnt !=0)&&(schObj.setCnt !=null)) {

		if((schObj.searchValue[1]==0)&&(schObj.searchValue[4]==0)) {
			schObj.xPath="productslist/products";
			schObj.msgObj.msgClear();	// Message deletion
			searchDataDsp();
		} else {
			schObj.msgObj.msgClear();	// Message deletion
			searchDataDsp();
		}

	} else {
		schObj.msgEnd = true;

		if(schObj.setCnt ==0) {
			schObj.msgObj.msgDisp(100);	//"There is no object data" message display.
		} else 	if(schObj.setCnt ==null) {
			schObj.msgObj.msgDisp(200);	//"Please specify the search condition" message display.
		}
	}

	//Data display/non-display switch
	function searchDataDsp() {
		
		schObj.dsMain.setXPath(schObj.xPath);	//SchObj.xPath is set to the data set again. 
		
		if(schObj.dsMain.getData().length ==0) {
			schObj.msgObj.msgDisp(100);		//"There is no object data" message display. 
		} else {
			schObj.msgObj.msgClear();		//Message deletion

			parent.location.href='#prList';
			parent.location.href='#prList';

			$("searchDataBlock").style.display = "block";

		}

		schObj.msgEnd = true;

	}

}


// serch Arrow Dsp
schObj.serchArrowDsper = function() {

	var imgNameOrg = schObj.httpCutter(schObj.serchArrowOrg);
	var imgName = imgNameOrg.split(".");
	var animeName ="";

	schObj.serchMsgCnt++;
	if(schObj.serchMsgCnt>schObj.msgArrowMax) schObj.serchMsgCnt=1;
	animeName = imgName[0] + "_" + schObj.serchMsgCnt + "." + imgName[1];
	
	$("searchArrowArea").childNodes.item(0).src = animeName;

	if((schObj.msgEnd == true)&&(schObj.serchMsgCnt >= schObj.msgArrowMax)) {
		clearInterval(schObj.serchArrowId);		//serch Arrow interval clear
		$("searchArrowArea").childNodes.item(0).src = schObj.serchArrowOrg;
	}

}

//Category name extraction for display
schObj.setDspCategory = function(rowNum){

	schObj.serchNum++;
	$("searchDatacaption").innerHTML = schObj.captionTitle + " (" + schObj.serchNum +")";

	var retCat="";
	var id01="";
	var id02="";
				
	if(schObj.all_Ok && (schObj.searchValue[1] != "undefined")) {
		var rowsData = schObj.dsMain.getData()[rowNum];

		if(rowsData.mainID) this.mainID = rowsData.mainID;
		if(rowsData.subID) this.subID = rowsData.subID;

		var dscat =1;
		var cat01="";
		var cat02="";

		id01 = rowsData.mainID;
		id02 = rowsData.subID;

		if(id01) cat01 = schObj.mainIdArray[id01];
		if(id02 && (id02 !="-")) cat02 = schObj.subIdArray[id01+id02];
		if(cat01 !="") {
			retCat = cat01;
			if(cat02 !="") retCat += (" / " + cat02);
		} 

		if($("outputcategory" + rowNum)) $("outputcategory" + rowNum).innerHTML = retCat;
	}
	
	
	//Category name extraction for data filter processing/display
	function catFilterFunc_1(dataSet, row, rowNumber)
	{
		if(row["mainID"] == id01) {
			cat01 = row["dname"];		// Return the row to keep it in the data set.
		} 
	}

	function catFilterFunc_2(dataSet, row, rowNumber)
	{
		if((row["mainID"] == id01)&&(row["subID"] == id02)&&(row["subID"] != "_")) {
			cat02 = row["dname"];		// Return the row to keep it in the data set.
		} 
	}

	
}


//Message control class
function msgControler() {
	msgTbl = {0: valueGetter(0), 10:valueGetter(10), 100:valueGetter(100), 200:valueGetter(200)}

	msgControler.prototype.msgClear = function() {
		$("searchMsgArea").style.display ="none";
	}

	msgControler.prototype.msgDisp = function(num) {
		var msgWk ="";
		if(num == 0) {
			if(msgTbl[num]) msgWk = '<p class="lead">' + msgTbl[num] + '</p>';
		} else {
			if(msgTbl[num]) msgWk = '<p class="lead warn">' + msgTbl[num] + '</p>';
		}
		
		if(msgWk !="") {
			$("searchMsgArea").style.display ="block";
			$("searchMsgArea").innerHTML = msgWk;
			$("searchDataBlock").style.display ="none";
		} 
	
	}


	function valueGetter(valueNum) {
		var retText ="";
		if($("msgTbl_" + valueNum)) retText = $("msgTbl_" + valueNum).value;
		return retText;	
	}

}

/*-----------------------
// http://xxxxx.xxx/ cut
-----------------------*/
schObj.httpCutter = function(srcText) {

	var hrefObj01=srcText;

	var httpChek = hrefObj01.indexOf("http://");

	if(httpChek !=-1) {

		var charChg01 = hrefObj01.split("http://");

		var charChg02 = charChg01[1].indexOf("/");

		var charChg03 = charChg01[1].slice(charChg02);

		hrefObj01 = charChg03;

	}

	return hrefObj01;

}


/*------------
cookie module
--------------*/
//cookie write
schObj.setCookie = function(){
	var sc4wk ="";
	var sc4wk_row ="";

	var i=0;
	while(schObj.dsSym.getData()[i]) {
		if(i !=0) {
			sc4wk+= "/";
			sc4wk_row += "/";
		}
		if(schObj.searchValue_4[i]) {
			sc4wk+= schObj.searchValue_4[i].toString();
		} else {
			sc4wk+= "0";
		}
		if(schObj.searchValue_4_row[i]) {
			sc4wk_row += schObj.searchValue_4_row[i].toString();
		} else {
			sc4wk_row += "0";
		}
		i++;
	}

	document.cookie = schObj.cookieName + "=" + schObj.searchValue[2] + ":" + schObj.searchValue_num[2] + ":" + schObj.searchValue_2_num + "::" + schObj.searchValue[3] + ":" + schObj.searchValue_num[3] + "::" + sc4wk + ":" + sc4wk_row + ";";
}

//cookie read
schObj.getCookie = function(){
	schObj.cookieString ="";

	var cookieCheck1 = document.cookie.split(";");

	var i=0;
	while(i<cookieCheck1.length) {
		if(cookieCheck1[i].indexOf(schObj.cookieName) !=-1) {
			schObj.cookieString = cookieCheck1[i];	
			break;
		}
		i++;
	}

	if(schObj.cookieString != "") {
		var getTemp = schObj.cookieString.split(schObj.cookieName +"=");
		
		var getTemp2 = getTemp[1].split("::");
		
		if(getTemp2[0]) {
			var getTemp20 = getTemp2[0].split(":");
			schObj.searchValue[2] = getTemp20[0];
			schObj.searchValue_num[2] = getTemp20[1];
			
		}
		if(getTemp2[1]) {
			var getTemp21 = getTemp2[1].split(":");
			schObj.searchValue[3] = getTemp21[0];
			schObj.searchValue_num[3] = getTemp21[1];
			schObj.searchValue_2_num = getTemp21[2];
		}
		if(getTemp2[2]) {
			var getTemp22 = getTemp2[2].split(":");
			schObj.searchValue_4 = getTemp22[0].split("/");
			schObj.searchValue_4_row = getTemp22[1].split("/");
		}

	}

}


/*-----------------------
Anti-cash measure module
-----------------------*/
schObj.getRandam = function(){
var retRndNum = Math.random()* 65535;

	if(retRndNum == 0){

		retRndNum =1;

	}else{

		retRndNum =Math.ceil(retRndNum);

	}

	return retRndNum;

}




