// common.js for Romak

// navigation

// preloading images
var imgPath = 'images/nav/';
imgNews = new Image();
imgNews.src = 'images/nav/news-off.gif';
imgNewsOn = new Image();
imgNewsOn.src = 'images/nav/news-on.gif';
imgProducts = new Image();
imgProducts.src = 'images/nav/products-off.gif';
imgProductsOn = new Image();
imgProductsOn.src = 'images/nav/products-on.gif';
imgAbout = new Image();
imgAbout.src = 'images/nav/about-off.gif';
imgAboutOn = new Image();
imgAboutOn.src = 'images/nav/about-on.gif';
imgHelp = new Image();
imgHelp.src = 'images/nav/help-off.gif';
imgHelpOn = new Image();
imgHelpOn.src = 'images/nav/help-on.gif';
imgContact = new Image();
imgContact.src = 'images/nav/contact-off.gif';
imgContactOn = new Image();
imgContactOn.src = 'images/nav/contact-on.gif';

function swapImg( imgName, imgFilename )
{
	if ( document.images )
	{
		document[imgName].src = imgPath + imgFilename;
	}
}

// popups
function popupWin( url, winame, wintype )
{
  if ( wintype == null ) { extras = '' }
  else if ( wintype == 'basic' ) { extras = 'scrollbars,resizable,width=350,height=450,left=20,top=20' }
  else if ( wintype == 'full' ) { extras = 'menubar,toolbar,location,status,scrollbars,resizable,width=780,height=450,left=20,top=20' }

  romakPop = window.open(url, winame, extras)
  romakPop.focus();
}

// form validation
function checkQty( frmObj )
{
  if (frmObj.qty.value.length > 0)
  {
    if (isNaN(frmObj.qty.value))
    {
      frmObj.qty.value = "1";
      alert("Please enter a valid numerical value.");
      return false;
    }
	else
	{
		valid_qty = parseInt(frmObj.qty.value);
		frmObj.qty.value = valid_qty;
		// all okay
		return true;
	}
  }
  else
  {
    alert("Please enter a number in the Quantity field.");
    return false;
  }
}

function checkLogin( frmObj )
{
	if ( frmObj.userid.value.length <= 1 )
	{
		alert("Please enter a valid User ID.");
		frmObj.userid.focus();
		return false;
	}
	else if ( frmObj.passwd.value.length <= 3 )
	{
		alert("Please enter a valid Password.");
		frmObj.passwd.focus();
		return false;
	}
	else
	{
		return true;
	}
	
}

function confirmDel ( url )
{
	if ( confirm("Are you sure you want to DELETE this user?\nNOTE: This action is not reversible.") )
	{
		window.location = '/' + url;
		return true;
	}
}

/* changeProdGroup */
function changeProdGroup ( objSelect)
{
    catID = objSelect.options[ objSelect.selectedIndex].value;
    catText = objSelect.options[ objSelect.selectedIndex].text;
    if( catID.length > 0) 
    {
        // get rid of current sub categories
        subCatSelect = objSelect.form.prod_subgroup;
        for( i=subCatSelect.options.length-1; i>=0; i--)
        {
            subCatSelect.options[i] = null;
        }
        
        // display sub categories
        subCatSelect[0] = new Option( 'Choose sub-category of ' + catText, '', false, false);
        var newSubcats = 1;
        for( j=0; j<subcats.length; j++) 
        {
            if( catID == subcats[j][0]) 
            {
                subCatSelect[ newSubcats++] = new Option( subcats[j][1]+'. '+subcats[j][2], subcats[j][1], false, false);
            }
        }
        if ( subCatSelect.disabled)
        {
            subCatSelect.disabled = false;
        }
        var loadedSubcats = newSubcats - 1;
        window.status = loadedSubcats + ' sub categories loaded.';
        //history.go(0);
    }
}
function changeProdSubGroup ( objSelect, pg, targetFile )
{
    n = objSelect.selectedIndex;
    url = targetFile+ '?prod_group=' +pg+ '&prod_subgroup=' +objSelect.options[n].value;
    if ( objSelect.options[n].value.length >= 2 ) 
    { 
      objSelect.form.submit(); 
    }
}

function addLoadEvent(func) 
{
    var oldonload = window.onload;
    if ( typeof window.onload != 'function' ) 
    {
        window.onload = func;
    } 
    else 
    {
        window.onload = function() 
        {
            oldonload();
            func();
        }
    }
}

function keepAlivePing()
{
    var pimg = new Image();
    pimg.src = './keepalive.php?m='+encodeURIComponent( Math.random() );
    setTimeout( "keepAlivePing();", (5*60*1000) );
}

addLoadEvent( keepAlivePing );