// JavaScript Document
// function to check the valid of keywords
function check(word){
  ValidFlag = true;
  for (atLoop=0; atLoop<word.value.length; atLoop++){
	if (word.value.charCodeAt(atLoop) <= 47){ ValidFlag = false; }
    if (word.value.charCodeAt(atLoop) >= 58 && word.value.charCodeAt(atLoop) <= 64){ ValidFlag = false; }
    if (word.value.charCodeAt(atLoop) >= 91 && word.value.charCodeAt(atLoop) <= 96){ ValidFlag = false; }
    if (word.value.charCodeAt(atLoop) >= 123){ ValidFlag = false; }
	if (word.value.charCodeAt(atLoop) == 32 || word.value.charCodeAt(atLoop) == 39 || word.value.charCodeAt(atLoop) == 95 || (word.value.charCodeAt(atLoop) >= 44 && word.value.charCodeAt(atLoop) <= 46)){ ValidFlag = true; }
	if (word.value.charCodeAt(atLoop) == 39){ ValidFlag = false; }
	//alert(word.value.charCodeAt(atLoop).toString())
  }
  return ValidFlag;
}

//Store the furhter search keywords to the field
function More(url){
  if (document.SearchMore.morekey.value != "") {
    if (document.SearchMore.cond[0].checked == true) {
	  document.SearchMore.keywords.value = document.SearchMore.keywords.value + "," + document.SearchMore.morekey.value;
	} else {
	  if (document.SearchMore.notkey.value == "") {
	    document.SearchMore.notkey.value = document.SearchMore.morekey.value;
	  } else {
  	    document.SearchMore.notkey.value = document.SearchMore.notkey.value + "," + document.SearchMore.morekey.value;
	  }
	}
	document.SearchMore.action = url;
	document.SearchMore.submit();
  } else {
    alert ("Please enter keyword!");
  }
}


function KeywordSearch(inputxt){// check the user input of keywords
  if (inputxt.value != ""){
	if (!check(inputxt)){
	  alert("Please enter valid keyword\n (only numbers, alphabets,''', '-', ',' and '.' is allowed)");
	  inputxt.focus();
	  return false;
	}
  } else {
    alert("Please enter keyword first!");
	inputxt.focus();
	return false;
  }
  return true;
}

function KeywordSearchAll(form){
  if (KeywordSearch(form.keywords)) return true;
  return false;
}

function isValidKeyword(form){
var inputxt=form.keywords;
  if (inputxt.value != ""){
	if (!check(inputxt)){
	  alert("Please enter valid keyword\n (only numbers, alphabets,''', '-', ',' and '.' is allowed)");
	  inputxt.focus();
	  return false;
	}
  }
  return true;
}

// functions check the valid of CDID (only alphebetic, number and '-' is allowed
function checkCDID(word){
  ValidFlag = true;
  for (atLoop=0; atLoop<word.value.length; atLoop++){
	if (word.value.charCodeAt(atLoop) <= 47 && word.value.charCodeAt(atLoop) != 45){ ValidFlag = false; }
    if (word.value.charCodeAt(atLoop) >= 58 && word.value.charCodeAt(atLoop) <= 64){ ValidFlag = false; }
    if (word.value.charCodeAt(atLoop) >= 91 && word.value.charCodeAt(atLoop) <= 96){ ValidFlag = false; }
    if (word.value.charCodeAt(atLoop) >= 123){ ValidFlag = false; }
  }
  return ValidFlag;
}
function CDIDSearch(form){// check the user input of CDID
  if (form.cdid.value != ""){
	if (!checkCDID(form.cdid)){
	  alert("Please enter a valid Product ID \n(only numbers, alphabets and '-' is allowed)!");
	  form.cdid.focus();
	  return false;
	}
  } else {
    alert("Please enter a Product ID first!");
	form.cdid.focus();
	return false;
  }
  return true;
}

//functions for changing pages
function Page(form, page){
  form.page.value = page;
  form.submit();
}

//functions for the filter options
function submitsearch(form){
	if (form.illust[0].checked && form.illust[1].checked){
	  form.illust[0].checked = false;
	  form.illust[1].checked = false;
	 }
	if (form.color[0].checked && form.color[1].checked){
	  form.color[0].checked = false;
	  form.color[1].checked = false;
	}
	if (form.orient[0].checked && form.orient[1].checked){
	  form.orient[0].checked = false;
	  form.orient[1].checked = false;
	}
}

//functions for the filter options
function nextsearch(form, keywords){
	form.keywords.value = keywords;
	form.notkey.value = "";
	form.submit();
}

//functions for checking input not excess totalpages in search result
function checkpage(form, TotalPages){
  if (form.page.value < 0 || form.page.value > TotalPages || isNaN(form.page.value)){
    alert ("Please enter a value between 1 to "+TotalPages);
    return false;
  }
  return true;
}

//searchBrand
function chkSname(sname){
	if(sname == ""){
		window.alert("Please enter a brand name."); return false;
	}
	else return true;
}