// -------------------------------------- //
// Mine Top Page Input Data Check
// -------------------------------------- //

function nullCheck(value) {
	if (value == "") {
		window.alert("Please input anyword.");
		return false;
	}
	return true;
}

function id_check(value) {
	var pos = value.indexOf("*");

	if (nullCheck(value) == false) {
		return false;
	}

	if (pos == -1) {
	        if( value.match(/^[1-9][0-9a-zA-Z]{3}$/)) {
			return true;
		} else {
	                window.alert("PDB ID is [1-9][0-9a-zA-Z]{3}. \nYou can include '*' in PDB ID for searching.");
	                return false;
		}
	} else if (pos > 4) {
               window.alert("PDB ID is 4 character ([1-9][0-9a-zA-Z]{3}).");
               return false;
	} else {
		if (value.length > 5) {
	               window.alert("PDB ID is 4 character ([1-9][0-9a-zA-Z]{3}).");
	               return false;
		} else {
		        if( value.match(/^[1-9'*'][0-9a-zA-Z'*']{0,3}$/)) {
				return true;
			} else {
		                window.alert("PDB ID is [1-9][0-9a-zA-Z]{3}. \nYou can include '*' in PDB ID for searching.");
				return false;
			}
		}
	}
	return true;
}

function keyword_check(value) {

	if (nullCheck(value) == false) {
		return false;
	}

	var new_value = value;
	value = value.replace(/[\n\r\s]/g,"");
	if (value == "") {
               window.alert("Please input anyword.");
               return false;
	}
	new_value = new_value.replace(/[\n]/g, " ");

	var element = document.getElementById("mine_query");
	
	if (element) {
		element.value = new_value;
	}
        return true;
}

function mine_search_check(value, check_value) {

	if (check_value == true) {
		return id_check(value);
	} else {
		return keyword_check(value);
	}
}

function check_sql_data(value, size) {

	if (check_size_data(size) == false) {
		return false;
	}

	value = value.replace(/[\n\r]/g," ");
	document.sql_form.query.value = value;

	return nullCheck(value);
}

function check_query(value) {

	value = value.replace(/^\s+|\s+$/g, "");

	if (nullCheck(value) == false) {
		return false;
	}

	var index = document.subform.type.selectedIndex;
	if (index == 1) {
	        if(!value.match(/^[a-zA-Z]*$/)) {
	                window.alert("sequence data is [a-zA-Z]*.");
			return false;	
		}
		if (value.length < 5) {
	                window.alert("sequence length is longer than 5.");
			return false;	
		}
		return true;
	}

        if (value.indexOf("'")!= -1 || value.indexOf("&") != -1 || value.indexOf("\\") != -1) {
		window.alert("PDB ID is 4 characters.([1-9][0-9a-zA-Z]{3}).\nKeywords are characters only.([0-9a-zA-Z])");
		return false;
	} 

	if (value.indexOf("(") != -1 && value.indexOf(")") == -1) {
		window.alert("invalid character found ... \"(\"");
		return false;
	}

	if (value.indexOf("(") == -1 && value.indexOf(")") != -1) {
		window.alert("invalid character found ... \")\"");
		return false;
	}
	if (value.indexOf("(") != -1 && value.indexOf(")") != -1) {
		if (value.indexOf("polypeptide(D)") == -1 && value.indexOf("polypeptide(L)") == -1 &&
		    value.indexOf("polysaccharide(D)") == -1 && value.indexOf("polysaccharide(L)") == -1) {
			window.alert("invalid character found ... \")\", \"(\"");
			return false;
		} else if (value.indexOf(" ") != -1) {
			window.alert("invalid character found ... \")\", \"(\"");
			return false;
		}
	}
	return true;
}

function check_size_data(size) {
	if (isNaN(size) || size.indexOf("-") != -1 || size == 0 || !size.match(/^[1-9][0-9]*$/) ) {
		window.alert( "[Display size per page]\nPlease input a positive integer." );
		return false;
	}
	return true;
}

function check_quick_data(value, size) {
	if (check_size_data(size) == false) {
		return false;
	}

	return check_query(value);
}


// -------------------------------------- //
// Advanced Search Page Input Data Check
// -------------------------------------- //
function check_advanced_data(pdbid, rel_after_year, rel_after_month, rel_after_day, rel_before_year, rel_before_month, rel_before_day, dep_after_year, dep_after_month, dep_after_day, dep_before_year, dep_before_month, dep_before_day, journal_year, mol_num, chain_num, chain_length_first, chain_length_last, resol1, resol2, size) {

	if (rel_after_year.match(/[^0-9]/)) return ReturnFalse("Release Date: after year");
	if (rel_before_year.match(/[^0-9]/)) return ReturnFalse("Release Date: before year");

	if (dep_after_year.match(/[^0-9]/)) return ReturnFalse("Deposition Date: after year");
	if (dep_before_year.match(/[^0-9]/)) return ReturnFalse("Deposition Date: before year");

	if (journal_year.match(/[^0-9]/)) return ReturnFalse("Journal: year");
	if (mol_num.match(/[^0-9]/)) return ReturnFalse("Number of Chains: min");
	if (chain_num.match(/[^0-9]/)) return ReturnFalse("Number of Chains: max");
	if (chain_length_first.match(/[^0-9]/)) return ReturnFalse("Chain length: min");
	if (chain_length_last.match(/[^0-9]/)) return ReturnFalse("Chain length: max");
	if (resol1.match(/[^0-9.]/)) return ReturnFalse("Resolusion: ");
	if (resol2.match(/[^0-9.]/)) return ReturnFalse("Resolusion: ");

	if ( pdbid.length != 0 ) {
		if (id_check(pdbid) == false) {
	                return false;
		}
	}

	if ( (rel_after_year.length != 0) && (rel_before_year.length != 0) ){
		if ( ( rel_after_year.substring(0) + rel_after_month.substring(0) + rel_after_day.substring(0) ) > 
		    ( rel_before_year.substring(0) + rel_before_month.substring(0) + rel_before_day.substring(0) ) ){
			window.alert ( "Their relation between after date and before date is unusual." );
			return false;
		}
	}

	if ( (dep_after_year.length != 0) && (dep_before_year.length != 0) ){
		if ( ( dep_after_year.substring(0) + dep_after_month.substring(0) + dep_after_day.substring(0) ) > 
		    ( dep_before_year.substring(0) + dep_before_month.substring(0) + dep_before_day.substring(0) ) ){
			window.alert ( "Their relation between after date and before date is unusual." );
			return false;
		}
	}

	if ( (mol_num.length != 0) && (chain_num.length != 0)){
		if (parseInt(mol_num) > parseInt(chain_num)) {
			window.alert("Number of Chains min < max !!");
			return false;
		}
	}

	if ( ( resol1.length != 0 ) && ( resol2.length != 0 ) ) {
		if ( parseInt(resol1) > parseInt(resol2) ) {
			window.alert("Resolution : Two values are wrong.");
			return false;
		}
	}
	return check_size_data(size);
}

function ReturnFalse(value) {
	window.alert("[" + value + "] value is wrong!");
	return false;
}

function onAdChangeSort() {
	var element = document.getElementById("advanced_search_form");
	if (element) {
		var index = element.sort.selectedIndex;
		element.sort2.options[index].selected = true;
	}
}

function onAdChangeSort2() {
	var element = document.getElementById("advanced_search_form");
	if (element) {
		var index = element.sort2.selectedIndex;
		element.sort.options[index].selected = true;
	}
}


function onQueryReset() {
	document.subform.query.value='';
	document.subform.type.options[0].selected = true;
	if (document.subform.new_query != null) {
		document.subform.new_query.value='';
	}
	
	if (document.subform2 != null) {
		document.subform2.query.value='';
		document.subform2.type.options[0].selected = true;
		if (document.subform2.new_query != null) {
			document.subform2.new_query.value='';
		}
	}
}

function openWin( url, w, h, name ) {
	var windowWidth  = parseInt(w,10) + 40;
	var windowHeight = parseInt(h,10) + 90;
	var wo;
	wo = window.open( "", name, "menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=yes,left=0,top=0,width=" + windowWidth + ",height=" + windowHeight);
	wo.resizeTo(windowWidth, windowHeight);
	wo.document.open();
	wo.document.write("<html>");
	wo.document.write("<img name=\'image\' src=\"" + url + "\"" + " width=\"" + w  + "\" height=\"" + h +"\">");
	wo.document.write("</html>");
	wo.document.close();
	wo.focus();
}


function openSchemaWin( url ) {
	var wo;
	wo = window.open( url, "schema_win", "menubar=yes,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,width=600,height=600");
	wo.focus();
}


function doSubmit( dir ) {
        pos = Number( document.subform.position.value );
        sz  = Number( document.subform.resultSize.value );
        if ( (pos * dir ) < 1 ) {
            pos = pos -  sz;
        } else {
            pos = pos + sz;
        }
        document.subform.position.value = pos;
        document.subform.submit();
}


function openViewer(xslname) {
	var wo;
	wo = window.open( "/mine/Detail?PDBID=" + document.forms[0].PDBID.value + "&PAGEID=" + xslname, "mine_Viewer_" + xslname, "menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,left=0,top=0,width=680,height=830");
	wo.focus();
}

function openViewer2(url) {
	var wo;
	wo = window.open( url, "mine_Viewer2", "menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,left=0,top=0,width=680,height=680");
	wo.focus();
}


function onJumpPage(pageno, page_size) {
	var start = (pageno-1)*page_size+1;
	document.subform.position.value = start;
	document.subform.submit();
}


function onSelectSort(sort_item) {
	if (document.subform.sort != null) {
		var len = document.subform.sort.options.length;
		var i;
		for (i = 0; i < len; i++) { 
			if (document.subform.sort.options[i].value == sort_item) { 
				document.subform.sort.options[i].selected=true; 
				if (document.subform2 != null) {
					document.subform2.sort.options[i].selected=true; 
				}
			} 
		}
	}
}


function onChangeSort() {
	var index = document.subform.sort.selectedIndex;
	document.subform2.sort.options[index].selected = true;
}

function onChangeSort2() {
	var index = document.subform2.sort.selectedIndex;
	document.subform.sort.options[index].selected = true;
}

function onAdPrevSubmit() {
	document.subform.position.value = 1;
	return true;
}

function onAdPrevSubmit2() {
        document.subform2.position.value = 1;
	return true;
}

function onSelectType(type) {
	if (document.subform != null) {
		if (document.subform.type != null) {
			var len = document.subform.type.options.length;
			var i;
			for (i = 0; i < len; i++) { 
				if (document.subform.type.options[i].value == type) { 
					document.subform.type.options[i].selected=true; 
					if (document.subform2 != null) {
						document.subform2.type.options[i].selected=true; 
					}
				} 
			}
		}
	}
}


function onChangeType() {
	var index = document.subform.type.selectedIndex;
	document.subform2.type.options[index].selected = true;
}

function onChangeType2() {
	var index = document.subform2.type.selectedIndex;
	document.subform.type.options[index].selected = true;
}


function onShowExample() {
	var index = document.subform.type.selectedIndex;
	if (index == 0) {
		document.getElementById('example0').style.display = 'inline';
		document.getElementById('example1').style.display = 'none';
		document.getElementById('example2').style.display = 'none';
	} else if (index == 1) {
		document.getElementById('example0').style.display = 'none';
		document.getElementById('example1').style.display = 'inline';
		document.getElementById('example2').style.display = 'none';
	} else if (index == 2) {
		document.getElementById('example0').style.display = 'none';
		document.getElementById('example1').style.display = 'none';
		document.getElementById('example2').style.display = 'inline';
	}
}



