/********************************************************************
* File:		globals.inc.js											*
* Author:	Jeremy Eastburn / Glenn Hughes							*
* Date:		07.29.04												*
* Description: Contains all JavaScript functions to be included on	*
*	each page.														*
********************************************************************/

/********************************************************************
* Javascript window.open attributes (separated by commas)			*
*																	*
* width=300															*
* height=200														*
* resizable=yes or no												*
* scrollbars=yes or no												*
* toolbar=yes or no													*
* location=yes or no (show address bar)								*
* directories=yes or no												*
* status=yes or no													*
* menubar=yes or no													*
* copyhistory=yes or no												*
********************************************************************/

/* Jump to State Page with selected ID */
function JumpToState(list) {
	var stateID = list.options[list.selectedIndex].value
	if (stateID != "None") {
		location.href = "/opxgr/state.php?stid="+stateID+""
	}
}

/* Jump to Product Page with selected ID (in popup window) */
function JumpToProduct(list) {
	var prodID = list.options[list.selectedIndex].value
	if (prodID != "None") {
		location.href = "/opxgr/product.php?prid="+prodID+""
	}
}

/* Jump to Published Report page according to date in value field  */
function JumpToPubReport(list) {
	var datevar = list.options[list.selectedIndex].value
	if (datevar != "None") {
		location.href = "/opxgr/admin/rptsPublished.php?filter="+datevar+""
	}
}

/* Verify both states have been chosen on compare1.php to proceed */
function CheckStates(list1,list2) {
	if ((list1.options[list1.selectedIndex].value == "0") || (list2.options[list2.selectedIndex].value == "0"))
	{
		alert("Please choose a state from each menu!");
		return false;
	}
	else {
		return true;
	}
}

/* Verify both markets have been chosen on compare1.php to proceed to compare2.php, but not the same market */
function CheckMarkets(list1,list2) {
	if ((list1.options[list1.selectedIndex].value == "0") || (list2.options[list2.selectedIndex].value == "0"))
	{
		alert("Please choose a market to compare from each menu!");
		return false;
	}
	else if (list1.options[list1.selectedIndex].value == list2.options[list2.selectedIndex].value)
	{
		alert ("You cannot compare the same market. Please choose two different markets to compare.");
		return false;
	}
	else {
		return true;
	}
}

/* Verify password, other fields is ok on reporter/signup.php */
function CheckForm(signupform) {
	var invalid = " "; // Invalid character is a space
	var minLength = 6; // Minimum length
	var pw1 = signupform.pwd1.value;
	var pw2 = signupform.pwd2.value;
	var rfname = signupform.rfname.value;
	var rlname = signupform.rlname.value;
	var remail = signupform.remail.value;
	var mname = signupform.mname.value;
	var mcity = signupform.mcity.value;
	var mstid = signupform.stid.options[signupform.stid.selectedIndex].value;

	// Check for a value in both fields.
	if (pw1 == '' || pw2 == '') {
		alert('Please enter your password twice.');
		return false;
	}
	// Check for minimum length
	if (pw1.length < minLength) {
		alert('Your password must be at least ' + minLength + ' characters long. Try again.');
		return false;
	}
	// Check for spaces
	if (pw1.indexOf(invalid) > -1) {
		alert("Sorry, spaces are not allowed.");
		return false;
	}
	else {
		if (pw1 != pw2) {
			alert ("You did not enter the same new password twice. Please re-enter your password.");
			return false;
		}
		else {
			// Good to go, proceed to check other fields
			if ((rfname == "") || (rlname == "") || (remail == "") || (mname == "") || (mcity == "") || (mstid == 0))
			{
				alert("You must fill in all required fields.");
				return false;
			}
			else
				return true;
		}
	}
}
/*Dreamweaver JavaScript functions*/

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  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;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/*Glenn JavaScript functions*/

function NewWindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function winClose()
{    
  if(viewWindow != null) viewWindow.close() 
}

function popEmail(url){
	window.open(url,"pop","width=500,height=300,toolbars=0,scrollbars=1")
}

/* Jump to Admin Reporter Page with selected ID */
function JumpToReporter(list) {
	var reporterID = list.options[list.selectedIndex].value
	if (reporterID != "None") {
		location.href = "/opxgr/admin/reporter.php?rid="+reporterID+""
	}
}

/* Jump to Admin Market Page with selected ID */
function JumpToMarket(list) {
	var marketID = list.options[list.selectedIndex].value
	if (marketID != "None") {
		location.href = "/opxgr/admin/market.php?mid="+marketID+""
	}
}

/* SwitchMarkets for /reporter/market.php */
function SwitchMarkets(list) {
	var marketID = list.options[list.selectedIndex].value
	if (marketID != "None") {
		location.href = "/opxgr/reporter/market.php?mid="+marketID+""
	}
}

/* SwitchMarkets for /reporter/marketInfo.php (popup) */
function SwitchMarketsPopup(list) {
	var marketID = list.options[list.selectedIndex].value
	if (marketID != "None") {
		location.href = "/opxgr/reporter/marketInfo.php?mid="+marketID+""
	}
}

/* Validates form in /reporter/reportAdd.php (no empty fields) */
function checkReportFields(thisForm) {

	/* LOOP THROUGH & VALIDATE STANDARD PRODUCTS */
	for (var i = 1; i < thisForm.totalStdProdCnt.value; i++) {
		pr_high = eval("thisForm.var_" + i + "_pr_high.value");
		pr_low = eval("thisForm.var_" + i + "_pr_low.value");
		qid = eval("thisForm.var_" + i + "_qid.value");

		/* If ALL required fields are empty, just skip it */
		if ((pr_high=='') && (pr_low=='') && (qid==0))
		{
			// All fields blank, that's ok, so skip
		}
		/* Else, if not all required fields are filled, error */
		else {
			if ((pr_high=='') && (pr_low==''))
			{
				alert("At least one price field must be filled in!");
				focus_obj = eval("thisForm.var_" + i + "_pr_high");
				focus_obj.focus();
				return false;
			}
			else if (qid==0)
			{
				alert("[Quantity] field required!");
				focus_obj = eval("thisForm.var_" + i + "_qid");
				focus_obj.focus();
				return false;
			}
		}
	}

	/* LOOP THROUGH & VALIDATE NON-STANDARD PRODUCTS */
	for (var i = 1; i < thisForm.totalNonStdProdCnt.value; i++) {
		nonstdprod = eval("thisForm.spclvar_" + i + "_nonstdprod.value");
		pr_high = eval("thisForm.spclvar_" + i + "_pr_high.value");
		pr_low = eval("thisForm.spclvar_" + i + "_pr_low.value");
		qid = eval("thisForm.spclvar_" + i + "_qid.value");
		/* If ALL required fields are empty, just skip it */
		if ((nonstdprod=='') && ((pr_high=='') || (pr_low=='')) && (qid==0))
		{
			// All fields blank, that's ok, so skip
		}
		/* Else, if not all required fields are filled, error */
		else {
			if (nonstdprod=='')
			{
				alert("[Product Name] field required!");
				focus_obj = eval("thisForm.spclvar_" + i + "_nonstdprod");
				focus_obj.focus();
				return false;
			}
			else if ((pr_high=='') && (pr_low==''))
			{
				alert("At least one price field must be filled in!");
				focus_obj = eval("thisForm.spclvar_" + i + "_pr_high");
				focus_obj.focus();
				return false;
			}
			else if (qid==0)
			{
				alert("[Quantity] field required!");
				focus_obj = eval("thisForm.spclvar_" + i + "_qid");
				focus_obj.focus();
				return false;
			}
		}
	}

	return true;
}