// display form errors in javascript alert box.
function showErrors(errors) {
	alert(errors);
}

// disable a submit button
function disableButton(form, btn)
{
	document.form.btn.disabled = true;
}

// open popup windows
function MM_openBrWindow(theURL,winName,features) {
	if (winName == "categorySelect") {
		var catval = document.form1.fCategories.value;
		theURL = theURL+"&cv="+catval;
	} else if (winName == "countrySelect") {
		var catval = document.form1.fCountries.value;
		theURL = theURL+"&cv="+catval;
	} else if (winName == "fromSelect") {
		var catval = document.form1.fPurchase_from.value;
		theURL = theURL+"&cv="+catval;
	} else {
		theURL = theURL;
	}
	window.open(theURL,winName,features);
}

// generic window pop up.
var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{
	if(popUpWin)
	{
		if(!popUpWin.closed) popUpWin.close();
	}
	popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

//reloads the window if Nav4 resized
function MM_reloadPage(init) {  
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);


// find object
function MM_findObj(n, d) { 
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


// set text of layer
function MM_setTextOfLayer(objName,x,newText) { 
  if ((obj=MM_findObj(objName))!=null) with (obj)
    if (document.layers) {document.write(unescape(newText)); document.close();}
    else innerHTML = unescape(newText);
}

// redirect to another page
function goToUrl(page) {
	document.location = page;
}

// show / hide layers
function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

/*---------------------------------------------------------------------
	These functions are used by the 'set_items_new' and 'set_items_feature' pages.
	 - id is the item id
	 - type refers to 'mark as new' or 'mark as feature'
---------------------------------------------------------------------*/
// simple date validation
function validateDate(mydate) {
	var ch1 = mydate.substr(4,1); var ch2 = mydate.substr(7,1);
	if (ch1 == "-" && ch2 == "-") {
		// convert the passed date to milliseconds.
		var myyear = mydate.substr(0,4) * 1; var mymonth = (mydate.substr(5,2) * 1) - 1; var mydate = mydate.substr(8,2) * 1;
		var testDate = new Date(myyear,mymonth,mydate); testDate = testDate.getTime();
		// get the current date in milliseconds.
		var curTime = new Date(); curTime = curTime.getTime();
		// one year from now.
		var oneYear = curTime + (1000*60*60*24*365);
		if (curTime < testDate && testDate < oneYear) { return true; }else{ return false; }
	} else { return false; }
}


//
function removeFromList(id, type) {
	var xList = "";
	if (type == "mark_new") { xList = "Mark as New"; } else { xList = "Mark as Feature"; }
	var value = prompt("To remove this Item from the "+xList+" list, enter \"yes\" then click OK.", "");
	if (value) { value = value.toLowerCase();
		if (value == "yes") {
			/* then remove this item from the list */
			if (type == "mark_new") { window.location="set_items_new.php?remove_record="+id+"";
			} else { window.location="set_items_featured.php?remove_record="+id+""; }
		}
	}
}


//
function changeItemDate(id, type) {
	var value = prompt("To change the expiry date, enter the new date as \"YYYY-MM-DD\" then click OK.", "");
	if (value) { 

		if (validateDate(value)) {
			/* then remove this item from the list */
			if (type == "mark_new") { window.location="set_items_new.php?update_record="+id+"&ndate="+value+"";
			} else { window.location="set_items_featured.php?update_record="+id+"&ndate="+value+""; }
		} else {
			alert("The date entered '"+value+"' is not valid.");
		}
	}
}


/*---------------------------------------------------------------------
	functions called from the result page.
---------------------------------------------------------------------*/
// type refers to "purchase, borrow, free" and id is the product id.
function goToCart(type, id) {
	document.location="cart.php?pt="+type+"&pid="+id+"";
}


/*-----------------------------------------------------------------------
	fumction called from the advanced search page
-----------------------------------------------------------------------*/
function validate(form)
{
	var count = 0;
	var temp = form.fSearch_text.value;
	var err = "";

	/* replace blanks */
    while (temp.substring(0,1) == ' ') temp = temp.substring(1);
    while (temp.substring(temp.length-1,temp.length) == ' ') temp = temp.substring(0,temp.length-1);
	var mytext = temp;

	if (mytext.length > 0) 
	{ 
		if (mytext.length < 3) {
			err += "Search text must contain at least 3 characters.\r\n";
		} else {
			if(form.fItem_title.checked || form.fItem_desc.checked || form.fItem_keys.checked)
			{
				// ok
			} else {
				err += "When using text in search, select at least 1 of the 3 text categories.\r\n";
			}
		}
	} else {
		count++;
	}

	if (form.fSearch_category.value == "") { count++; }
	if (form.fSearch_country.value == "") { count++; }
	if (form.fSearch_cc.value == "") { count++; }
	if (form.fSearch_resource.value == "") { count++; }
	if (form.fSearch_language.value == "") { count++; }
	if (form.fSearch_pbf.value == "") { count++; }

	if (count == 7 || err > "")
	{
		if (err.length < 1)
		{
			err += "You must enter search text or select at least 1 option.\r\n";
		}
		alert(err);
	} else {
		//form.submit();
		document.frmSearch_adv.action="result.php";
		document.frmSearch_adv.submit();
	}
}


function updateSearch()
{
	document.frmSearch_adv.action="asearch.php";
	document.frmSearch_adv.submit();
}
