function isblank(s)
{
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

function getFullYear(d)
{
	var y=d.getYear();
		if ((navigator.appName.indexOf("Microsoft")!=-1) || (navigator.appName.indexOf("Netscape")!=-1) && y<1000) y+=1900;
	return y;
}

function YearsBetweenDateAndNow(yr, mo, dy)
{
  var d, r, t1, t2, t3;
  
  var MinMilli = 1000 * 60
  var HrMilli = MinMilli * 60
  var DyMilli = HrMilli * 24
  var YrMilli = DyMilli * 365
  
  t1 = Date.UTC(yr,mo,dy);
  d = new Date();
  t2 = d.getTime();
  
  if (t2 >= t1) 
    t3 = t2 - t1;
  else
    t3 = t1 - t2;
    
  r = Math.round(t3 / YrMilli);
  return(r);
}

function verify(f,p,s,t,u,g,o,a1)
{

    var errcnt=0;	
    var errcnt2=0;	
    var msg;
    var empty_fields = "";
    var errors = "";
	var atpos=0;
	var dotpos=0;
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
        //errors  += "\n          t." + e.type + ":n." + e.name + ":v." + e.value + ":c." + e.checked + ":r." + e.required + ":r." + e.description;
        if (((e.type == "checkbox"))  && e.required) {
			if (!(e.checked==true)) {
				errcnt ++;
				errors += "- " + e.description + "\n";
			}
        }
        else if ((e.type == "select-one") && e.required) {
            if ((e.options[e.selectedIndex].value == null) 
				|| (e.options[e.selectedIndex].value == "") 
				|| (e.options[e.selectedIndex].value == "ignore") 
				|| isblank(e.options[e.selectedIndex].value)) {

		if (e.description == "Month To" || e.description == "Month From" || e.description == "Year To" || e.description == "Year From")
			{errcnt2 ++;}
		else
			{errcnt ++;}


                empty_fields += "\n          " + e.description;
                continue;
            }

        }
        else if (((e.type == "text") || (e.type == "textarea") || (e.type=="password")) && e.required) {
            if ((e.value == null) || (e.value == "") || (e.value == "ignore") || (e.value == "ignore") || isblank(e.value)) {
		errcnt ++;
                empty_fields += "\n          " + e.description ;
                continue;
            }



            if (e.maxlength) {
				if (e.value.length>e.maxlength) {
					errors += "- " + e.description + " must contain less than " + e.maxlength + " characters.\n";
				}
			}

            if (e.minlength) {
				if (e.value.length<e.minlength) {
					errors += "- " + e.description + " must contain at least " + e.minlength + " characters.\n";
				}
		            }
			if (e.email) {
				apos=e.value.indexOf("@"); 
				dotpos=e.value.lastIndexOf(".");
				lastpos=e.value.length-1;
				if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
					errors += "- " + e.description + " is not a valid email address.\n";
				}
			}
			if (e.creditcard) {
				if (!isCreditCard(e.value)) {
					errors += "- " + e.value + " is not a valid credit card number.\n";
				}
			}
            if (e.numeric || (e.min != null) || (e.max != null)) { 
                var v = parseFloat(e.value);
                if (isNaN(v) || 
                    ((e.min != null) && (v < e.min)) || 
                    ((e.max != null) && (v > e.max))) {
                    errors += "- The field " + e.description + " must be a number";
                    if (e.min != null) 
                        errors += " that is greater than " + e.min;
                    if (e.max != null && e.min != null) 
                        errors += " and less than " + e.max;
                    else if (e.max != null)
                        errors += " that is less than " + e.max;
                    errors += ".\n";
                }
            }
            //if (e.date) {
			//	var today=new Date(e.value)
			//	var v=Date.parse(e.value);
				//if (v!=Date) {
			//		errors +="- The fields " + typeof(today) +":" + e.name + " must be a valid date";
				//}
			//}
        }
    }
	if (p) {
		if (f.fldLogonPwd.value!=f.fldLogonPwd2.value) {
			errors +="- Your Password and Repeat Password do not match.\n"
			}
		}
	if (s) {
		if ((f.fldRegionCodeUK[f.fldRegionCodeUK.selectedIndex].value != 'ignore') && (f.fldRegionCodeUS[f.fldRegionCodeUS.selectedIndex].value != 'ignore')) {	
				errors +="- Select state or region.\n"
			}
		}
    if (t) {
		if (f.xvNewLogonPwd1.value!=f.xvNewLogonPwd2.value) {
			errors +="- Your Password and Repeat Password do not match.\n"
			}
		}
    
	if (u) {
			if (YearsBetweenDateAndNow(f.xvbirthyear.value, f.xvbirthmonth.selectedIndex+1,f.xvbirthday.value) < 18) {
				errors +="- Users must be 18 or over.\n"
			}
		}
    if (g) {
		if (!((f.fldgender[0].checked) || (f.fldgender[1].checked))) {
			errors +="- Your gender must be either Male or Female.\n"
			}
	} 
    if (o) {
		if (!((f.fldover18[0].checked) || (f.fldover18[1].checked))) {
			errors +="- Please tell us if you're over 18.\n"
			}
	} 
	//errors +="- Your gender must be either Male or Female." + f.fldgender[1].checked

//__________________________________________________________________________________________________________________
//__________________________________________________________________________________________________________________
//need to populate the search text from a quick search .. called here because function will return true as the quick text field will be populated

if ((!a1) && (f.name == 'QuickSearch')) {SearchTextQuick(); }

if ((!a1) && (f.name == 'QuickSearchLimited')) {SearchTextQuick1(); }
//__________________________________________________________________________________________________________________
//__________________________________________________________________________________________________________________


    if (!empty_fields && !errors) return true;

    msg  = "______________________________________________________\n\n"
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"



if (!a1)
{  
    if (empty_fields) {
        msg += "- The following required field(s) are empty:\n" 
                + empty_fields + "\n";
        if (errors) msg += "\n";
    }
   msg += errors;
   alert(msg);
   return false;
}
else 
{
if ((errcnt==7) && (errcnt2>=1))
	{
	alert("The form was not submitted. Please enter a search value, subject area or date range.");
	return false;
	}
else
	{
	SearchText();
	return true;
	}
}	
}	

function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);
}



function CalcPrice(area,price,disc)
{
	if (area == "USA")
	{
	  if (document.bookorderform.Area2.checked == true)
		{
		document.bookorderform.Area[0].checked = true;
		price = document.bookorderform.VPriceUSA2.value;
		}
	  else
		{
		price = document.bookorderform.VPriceUSA1.value;
		}
	}
	else
	{
		document.bookorderform.Area2.checked = false
	}

    var PriceOut = (price/100) * (100-disc);
    PriceOut = (PriceOut * 100);
    PriceOut = Math.round(PriceOut);
    PriceOut = (PriceOut / 100);
    document.bookorderform.TDispPriceAM.value = PriceOut;
    return true;
}


function popUp(url) 
{
	sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
	self.name = "mainWin"; 
}


function UpdSearchSelection(SelType, objOpt, intItem)
{


/*
*		"ALL"
*		objOpt.options[5].value = 'B--_Binding_Description';
*		objOpt.options[5].text = 'Book Type';
*
*		"Book"	
*		objOpt.options[5].value = 'Binding_Description';
*		objOpt.options[5].text = 'Book Type';
*/


	switch(SelType)
	{

	case 'All':

		objOpt.options[0].value = 'BJA_Title';
		objOpt.options[0].text = 'Title';
		objOpt.options[1].value = 'BJA_Authors';
		objOpt.options[1].text = 'Author/Editor';
		objOpt.options[2].value = 'BJ-_Description_1';
		objOpt.options[2].text = 'Description';
		objOpt.options[3].value = 'B--_ISBN';
		objOpt.options[3].text = 'ISBN';
		objOpt.options[4].value = 'B--_Series_Description';
		objOpt.options[4].text = 'Series';

		objOpt.options[5].value = '-JA_ISSN';
		objOpt.options[5].text = 'ISSN';
		objOpt.options[6].value = '-J-_Editorial_Board';
		objOpt.options[6].text = 'Editorial Board';

		objOpt.options[7].value = '-JA_Keyword';
		objOpt.options[7].text = 'Keyword';
		objOpt.options[8].value = '--A_Article_Title';
		objOpt.options[8].text = 'Article Title';
		objOpt.options[9].value = '--A_Article_Author';
		objOpt.options[9].text = 'Article Author';
		objOpt.options[10].value = '--A_Article_Abstract';
		objOpt.options[10].text = 'Article Abstract';
		objOpt.options[11].value = '--A_Volume';
		objOpt.options[11].text = 'Volume';
		objOpt.options[12].value = '--A_Issue';
		objOpt.options[12].text = 'Issue';
		objOpt.options[13].value = '--A_Article';
		objOpt.options[13].text = 'Article';

		objOpt.options[14].value = '-J-_Society_Name';
		objOpt.options[14].text = 'Society Name';
		objOpt.options[15].value = '-J-_Society_Blurb';
		objOpt.options[15].text = 'Society Description';

		objOpt.options[16].value = '';
		objOpt.options[16].text = '';
		objOpt.options[17].value = '';
		objOpt.options[17].text = '';
		objOpt.options[18].value = '';
		objOpt.options[18].text = '';
		objOpt.options[19].value = '';
		objOpt.options[19].text = '';
		objOpt.options[20].value = '';
		objOpt.options[20].text = '';
		objOpt.options[21].value = '';
		objOpt.options[21].text = '';
		objOpt.options[22].value = '';
		objOpt.options[22].text = '';
		objOpt.options[23].value = '';
		objOpt.options[23].text = '';
		objOpt.options[24].value = '';
		objOpt.options[24].text = '=================';
		break;

	case 'Books':
		objOpt.options[0].value = 'Title';
		objOpt.options[0].text = 'Title';
		objOpt.options[1].value = 'Authors';
		objOpt.options[1].text = 'Author';
		objOpt.options[2].value = 'Description_1';
		objOpt.options[2].text = 'Description';
		objOpt.options[3].value = 'ISBN';
		objOpt.options[3].text = 'ISBN';
		objOpt.options[4].value = 'Series_Description';
		objOpt.options[4].text = 'Series';
		objOpt.options[5].value = 'Title';
		objOpt.options[5].text = 'Title';
		objOpt.options[6].value = '';
		objOpt.options[6].text = '';
		objOpt.options[7].value = '';
		objOpt.options[7].text = '';
		objOpt.options[8].value = '';
		objOpt.options[8].text = '';
		objOpt.options[9].value = '';
		objOpt.options[9].text = '';
		objOpt.options[10].value = '';
		objOpt.options[10].text = '';
		objOpt.options[11].value = '';
		objOpt.options[11].text = '';
		objOpt.options[12].value = '';
		objOpt.options[12].text = '';
		objOpt.options[13].value = '';
		objOpt.options[13].text = '';
		objOpt.options[14].value = '';
		objOpt.options[14].text = '';
		objOpt.options[15].value = '';
		objOpt.options[15].text = '';
		objOpt.options[16].value = '';
		objOpt.options[16].text = '';
		objOpt.options[17].value = '';
		objOpt.options[17].text = '';
		objOpt.options[18].value = '';
		objOpt.options[18].text = '';
		objOpt.options[19].value = '';
		objOpt.options[19].text = '';
		objOpt.options[20].value = '';
		objOpt.options[20].text = '';
		objOpt.options[21].value = '';
		objOpt.options[21].text = '';
		objOpt.options[22].value = '';
		objOpt.options[22].text = '';
		objOpt.options[23].value = '';
		objOpt.options[23].text = '';
		objOpt.options[24].value = '';
		objOpt.options[24].text = '=================';
		break;
	case 'Journals':
		objOpt.options[0].value = 'Title';
		objOpt.options[0].text = 'Title';
		objOpt.options[1].value = 'Editors';
		objOpt.options[1].text = 'Editor';
		objOpt.options[2].value = 'Description_1';
		objOpt.options[2].text = 'Description';
		objOpt.options[3].value = 'ISSN';
		objOpt.options[3].text = 'ISSN';
		objOpt.options[4].value = 'Editorial_Board';
		objOpt.options[4].text = 'Editorial Board';
		objOpt.options[5].value = 'Title';
		objOpt.options[5].text = 'Title';
		objOpt.options[6].value = '';
		objOpt.options[6].text = '';
		objOpt.options[7].value = '';
		objOpt.options[7].text = '';
		objOpt.options[8].value = '';
		objOpt.options[8].text = '';
		objOpt.options[9].value = '';
		objOpt.options[9].text = '';
		objOpt.options[10].value = '';
		objOpt.options[10].text = '';
		objOpt.options[11].value = '';
		objOpt.options[11].text = '';
		objOpt.options[12].value = '';
		objOpt.options[12].text = '';
		objOpt.options[13].value = '';
		objOpt.options[13].text = '';
		objOpt.options[14].value = '';
		objOpt.options[14].text = '';
		objOpt.options[15].value = '';
		objOpt.options[15].text = '';
		objOpt.options[16].value = '';
		objOpt.options[16].text = '';
		objOpt.options[17].value = '';
		objOpt.options[17].text = '';
		objOpt.options[18].value = '';
		objOpt.options[18].text = '';
		objOpt.options[19].value = '';
		objOpt.options[19].text = '';
		objOpt.options[20].value = '';
		objOpt.options[20].text = '';
		objOpt.options[21].value = '';
		objOpt.options[21].text = '';
		objOpt.options[22].value = '';
		objOpt.options[22].text = '';
		objOpt.options[23].value = '';
		objOpt.options[23].text = '';
		objOpt.options[24].value = '';
		objOpt.options[24].text = '=================';
		break;
	case 'Articles':
		objOpt.options[0].value = 'Article_Title';
		objOpt.options[0].text = 'Article Title';
		objOpt.options[1].value = 'Article_Author';
		objOpt.options[1].text = 'Article Author';
		objOpt.options[2].value = 'Article_Abstract';
		objOpt.options[2].text = 'Article Abstract';
		objOpt.options[3].value = 'Journal_Title';
		objOpt.options[3].text = 'Journal Title';
		objOpt.options[4].value = 'ISSN';
		objOpt.options[4].text = 'ISSN';
		objOpt.options[5].value = 'Article';
		objOpt.options[5].text = 'Article';
		objOpt.options[6].value = '';
		objOpt.options[6].text = '';
		objOpt.options[7].value = '';
		objOpt.options[7].text = '';
		objOpt.options[8].value = '';
		objOpt.options[8].text = '';
		objOpt.options[9].value = '';
		objOpt.options[9].text = '';
		objOpt.options[10].value = '';
		objOpt.options[10].text = '';
		objOpt.options[11].value = '';
		objOpt.options[11].text = '';
		objOpt.options[12].value = '';
		objOpt.options[12].text = '';
		objOpt.options[13].value = '';
		objOpt.options[13].text = '';
		objOpt.options[14].value = '';
		objOpt.options[14].text = '';
		objOpt.options[15].value = '';
		objOpt.options[15].text = '';
		objOpt.options[16].value = '';
		objOpt.options[16].text = '';
		objOpt.options[17].value = '';
		objOpt.options[17].text = '';
		objOpt.options[18].value = '';
		objOpt.options[18].text = '';
		objOpt.options[19].value = '';
		objOpt.options[19].text = '';
		objOpt.options[20].value = '';
		objOpt.options[20].text = '';
		objOpt.options[21].value = '';
		objOpt.options[21].text = '';
		objOpt.options[22].value = '';
		objOpt.options[22].text = '';
		objOpt.options[23].value = '';
		objOpt.options[23].text = '';
		objOpt.options[24].value = '';
		objOpt.options[24].text = '=================';
		break;
	case 'Societies':
		objOpt.options[0].value = 'Society_Name';
		objOpt.options[0].text = 'Society Name';
		objOpt.options[1].value = 'Society_Blurb';
		objOpt.options[1].text = 'Society Description';
		objOpt.options[2].value = '';
		objOpt.options[2].text = '';
		objOpt.options[3].value = '';
		objOpt.options[3].text = '';
		objOpt.options[4].value = '';
		objOpt.options[4].text = '';
		objOpt.options[5].value = '';
		objOpt.options[5].text = '';
		objOpt.options[6].value = '';
		objOpt.options[6].text = '';
		objOpt.options[7].value = '';
		objOpt.options[7].text = '';
		objOpt.options[8].value = '';
		objOpt.options[8].text = '';
		objOpt.options[9].value = '';
		objOpt.options[9].text = '';
		objOpt.options[10].value = '';
		objOpt.options[10].text = '';
		objOpt.options[11].value = '';
		objOpt.options[11].text = '';
		objOpt.options[12].value = '';
		objOpt.options[12].text = '';
		objOpt.options[13].value = '';
		objOpt.options[13].text = '';
		objOpt.options[14].value = '';
		objOpt.options[14].text = '';
		objOpt.options[15].value = '';
		objOpt.options[15].text = '';
		objOpt.options[16].value = '';
		objOpt.options[16].text = '';
		objOpt.options[17].value = '';
		objOpt.options[17].text = '';
		objOpt.options[18].value = '';
		objOpt.options[18].text = '';
		objOpt.options[19].value = '';
		objOpt.options[19].text = '';
		objOpt.options[20].value = '';
		objOpt.options[20].text = '';
		objOpt.options[21].value = '';
		objOpt.options[21].text = '';
		objOpt.options[22].value = '';
		objOpt.options[22].text = '';
		objOpt.options[23].value = '';
		objOpt.options[23].text = '';
		objOpt.options[24].value = '';
		objOpt.options[24].text = '=================';
		break;
	case 'dtSelect':
		objOpt.options[0].value = 'Books Published';
		objOpt.options[0].text = 'Books Published';
		objOpt.options[1].value = 'Journals Published';
		objOpt.options[1].text = 'Journals Published';
		break;
	case 'dtSelectBook':
		objOpt.options[0].value = 'Books Published';
		objOpt.options[0].text = 'Books Published';
		objOpt.options[1].value = '';
		objOpt.options[1].text = '';
		break;
	case 'dtSelectArticle':
		objOpt.options[0].value = 'Journals Published';
		objOpt.options[0].text = 'Journals Published';
		objOpt.options[1].value = '';
		objOpt.options[1].text = '';
		break;
	}
objOpt.options[intItem].selected = true;
}



function SearchText() 
{
	SearchText = ""
	if (document.searchform.textfield_1.value != "")
		{
		SearchText = SearchText + document.searchform.Options_1.options[document.searchform.Options_1.selectedIndex].text + " = ";
		SearchText = SearchText + document.searchform.textfield_1.value + " ";
		SearchText = SearchText + document.searchform.bool_1.options[document.searchform.bool_1.selectedIndex].text + " ";
		}

	if (document.searchform.textfield_2.value != "")
		{
		SearchText = SearchText + document.searchform.Options_2.options[document.searchform.Options_2.selectedIndex].text + " = ";
		SearchText = SearchText + document.searchform.textfield_2.value + " ";
		SearchText = SearchText + document.searchform.bool_2.options[document.searchform.bool_2.selectedIndex].text + " ";
		}

	if (document.searchform.textfield_3.value != "")
		{
		SearchText = SearchText + document.searchform.Options_3.options[document.searchform.Options_3.selectedIndex].text + " = ";
		SearchText = SearchText + document.searchform.textfield_3.value + " ";
		SearchText = SearchText + document.searchform.bool_3.options[document.searchform.bool_3.selectedIndex].text + " ";
		}

	if (document.searchform.textfield_4.value != "")
		{
		SearchText = SearchText + document.searchform.Options_4.options[document.searchform.Options_4.selectedIndex].text + " = ";
		SearchText = SearchText + document.searchform.textfield_4.value + " ";
		SearchText = SearchText + document.searchform.bool_4.options[document.searchform.bool_4.selectedIndex].text + " ";
		}

	if (document.searchform.textfield_5.value != "")
		{
		SearchText = SearchText + document.searchform.Options_5.options[document.searchform.Options_5.selectedIndex].text + " = ";
		SearchText = SearchText + document.searchform.textfield_5.value + " ";
		SearchText = SearchText + document.searchform.bool_5.options[document.searchform.bool_5.selectedIndex].text + " ";
		}

	if (document.searchform.textfield_6.value != "")
		{
		SearchText = SearchText + document.searchform.Options_6.options[document.searchform.Options_6.selectedIndex].text + " = ";
		SearchText = SearchText + document.searchform.textfield_6.value + " ";
		SearchText = SearchText + document.searchform.bool_6.options[document.searchform.bool_6.selectedIndex].text + " ";
		}



	if (document.searchform.Subject_List.options[document.searchform.Subject_List.selectedIndex].text != "Please select")
		{
		SearchText = SearchText + "Subject = "
		SearchText = SearchText + document.searchform.Subject_List.options[document.searchform.Subject_List.selectedIndex].text + " ";
		SearchText = SearchText + " AND "
		}

	if ((document.searchform.Month_From.options[document.searchform.Month_From.selectedIndex].text != "- - -") && (document.searchform.Year_From.options[document.searchform.Year_From.selectedIndex].text != "- -") && (document.searchform.Month_To.options[document.searchform.Month_To.selectedIndex].text != "- - -") && (document.searchform.Year_To.options[document.searchform.Year_To.selectedIndex].text != "- -"))
		{
		SearchText = SearchText + "Published Date From "
		SearchText = SearchText + document.searchform.Month_From.options[document.searchform.Month_From.selectedIndex].text + " ";
		SearchText = SearchText + document.searchform.Year_From.options[document.searchform.Year_From.selectedIndex].text + " ";
		SearchText = SearchText + " To "
		SearchText = SearchText + document.searchform.Month_To.options[document.searchform.Month_To.selectedIndex].text + " ";
		SearchText = SearchText + document.searchform.Year_To.options[document.searchform.Year_To.selectedIndex].text + " ";
		SearchText = SearchText + " AND "
		}


	document.searchform.DispT.value = SearchText;
	return SearchText;
}

function SearchTextQuick() 
{
	SearchText = "";
	if (document.QuickSearch.QuickText.value != "")
		{
		//Only set the text if the form that called the function was a search one
		SearchText = SearchText + document.QuickSearch.QuickText.value + " PAD ";
//		SearchText = SearchText + document.searchform.textfield_1.value + " ";
//		SearchText = SearchText + document.searchform.bool_1.options[document.searchform.bool_1.selectedIndex].text + " ";
		document.QuickSearch.DispT.value = SearchText;
		}
	return SearchText;
}

function SearchTextQuick1() 
{
	SearchText = "";
	if (document.QuickSearchLimited.QuickText.value != "")
		{
		//Only set the text if the form that called the function was a search one
		SearchText = SearchText + document.QuickSearchLimited.QuickText.value + " PAD ";
//		SearchText = SearchText + document.searchform.textfield_1.value + " ";
//		SearchText = SearchText + document.searchform.bool_1.options[document.searchform.bool_1.selectedIndex].text + " ";
		document.QuickSearchLimited.DispT.value = SearchText;
		}
	return SearchText;
}

