
/*
Language: JavaScript
Library:  Common mouse over and popup routines
Version:  1.0
*/

// array to hold the swap images.
var swappedOut = new Array(), nSwappedOut = 0, pics = new Array();

function mout()
{
  /***** supports multiple images *****/
  // no need to pass any arguments

  var i;
  
  for(i = 0; i < nSwappedOut; i++)
  {
    // swap all tracked images
    swappedOut[i].src = swappedOut[i].oldSrc;
  }
}

function mover()
{
  /***** supports multiple images *****/
  // pass the arguments in as a list of place holder name
  // ie: mover('holder1', 'holder2', 'holder3');
  // it will find the extension and path of the original image
  // and use the same one for the new image
  // it will also assume that the mouseover image start with an 
  // underscore '_' character

  var i, j = 0, imgSwapped, args = mover.arguments, path = "", file = "", slash = 0;
  nSwappedOut = args.length;

  for(i = 0; i < nSwappedOut; i++)
  {
    imgSwapped = document.images[args[i]];  // gets the image
      
    if(imgSwapped != null)
    {
      // get the filename of the image
      slash = imgSwapped.src.lastIndexOf("/") + 1;
      file = imgSwapped.src.substring(slash);
      path = imgSwapped.src.substring(0, slash);

      // if the image is already m/o, then don't do it again
      if ( file.indexOf("_") != 0 )
      {
        // track all swapped images
        swappedOut[j++] = imgSwapped;
        imgSwapped.oldSrc = imgSwapped.src;
        imgSwapped.src = path + "_" + file;
      }
      else
      {
        nSwappedOut = 0;
      }
    }
  }
}

function preload()
{
  if( document.images )
  {
    if( ! document.pics ) document.pics = new Array();

    var i;
    var j = 0;
    var args = preload.arguments;

    for(i = 0; i < args.length; i++)
    {
      if (args[i].indexOf("#") != 0)
      {
        document.pics[j] = new Image;
        document.pics[j++].src = args[i];
      }
    }
  }
}


var popupwin = null;

function popup(psize, url)
{
	var findx = psize.indexOf("x", 0);
	
	if(findx != -1)
	{
		//Found the character x, assume that the psize string contains an XxY notation
		var x = psize.substr(0, findx);
		var y = psize.substr(findx+1, psize.length-findx-1);

		dopopup(x, y, url, "_blank");
	}
	else
	{
		switch(psize)
		{
			case "TINY":
				dopopup(200, 120, url, "_blank");	
				break;
			case "SMALL":
				dopopup(320, 240, url, "_blank");
				break;
			case "MED":
				dopopup(640, 480, url, "_blank");
				break;
			case "BIG":
				dopopup(750, 500, url, "_blank");
				break;
			default:
				dopopup(580, 400, url, "_blank");
				break;
		}
	}
}

function dopopup(wx, wy, url, name)
{
	var nwidth = (window.screen.width / 2) - ((wx / 2) + 10);
	var nheight = (window.screen.height / 2) - ((wy / 2) + 50);

	popupwin = window.open(url, name, "location=no,directories=no,status=yes,menubar=no,toolbar=no,scrollbars=1,resizable=yes,height=" + wy + ",width=" + wx + ",left=" + nwidth + ",top=" + nheight + ",screenX=" + nwidth + ",screenY=" + nheight)
	popupwin.focus();
  return popupwin;
}

function inspect( obj )
{
  if ( ! obj )
  {
    ret = prompt ("Enter object", "document");
    obj = eval(ret);
  }

  var temp = "";
  for (x in obj)
  {
    temp += x + ": " + obj[x] + "\n";
    if ( temp.length > 700 )
    {
      alert(temp);temp='';
    }
  }
  alert (temp);
}

function isblank(s)
{
  var i, c;
  
  for(i = 0; i < s.length; i++)
  {
    c = s.charAt(i);
    if(c != ' ' && c != '\n' && c != '\t')
      return false;
  }
  return true;
}
  
function regex_validate( f, error_string, previous_error )
{
   var i, e, msg, bypass, rdmsg, rdoname, posmsg, emsg, phmsg;
   
   if ( previous_error == null )
   { 
     previous_error = "";
   }

   msg = previous_error;
   rdmsg = '';
   bypass = true;
   rdoname = '';
   
   for(i = 0; i < f.length; i++) 
   {   
      e = f.elements[i];
      if(e.regex || e.required)
      {                 
         if(e.type == 'radio')
         {
            if(rdoname != e.name)
            {
                bypass = true;
                msg += rdmsg;
                rdoname = e.name;
            }

            if(!e.checked && bypass)
            {
                rdmsg = e.display + '\n';
            }
            else 
            {
                rdmsg = '';
                bypass = false;
            }
        }
        else if(e.type == 'select-one')
        {
            var elemval = e.options[e.selectedIndex].value;

            if(e.regex)
            {
                if(elemval.search(e.regex) == -1)
                {
                    msg += e.display + '\n';
                }
            }
            else
            {
                if(elemval == null || elemval == '' || isblank(elemval))
                {
                    msg += e.display + '\n';
                }
            }
        }
        else 
        {
            if(e.regex)
            {
                if(e.value.search(e.regex) == -1)
                {
                    msg += e.display + '\n';
                }
            }
            else
            {
                if(e.value == null || e.value == '' || isblank(e.value))
                {
                    msg += e.display + '\n';
                }
            }
        }  
  
      }//end e.regex
   }
   
   msg += rdmsg; 
   if( msg != '' )
   {
      if ( error_string != '' )
        alert( error_string + msg);
      else
        alert( msg);

      return false;
   }
   else
   {
      return true;
   }
}


function getRadioButtonValue( radio )
{
  var i;

  for ( i = 0; i < radio.length; i++ )
  {
    if ( radio[i].checked )
    {
      return radio[i].value;
    }
  }

  return null;

}


/*  ================================================================
    FUNCTION:  isCreditCard(st)
 
    INPUT:     st - a string representing a credit card number

    RETURNS:  true, if the credit card number passes the Luhn Mod-10
		    test.
	      false, otherwise
    ================================================================ */

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--;
  }
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
//    the next multiple of 10.

//  document.writeln("<BR>Sum      = ",sum,"<BR>");
//  alert("Sum      = " + sum);

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);

} // END FUNCTION isCreditCard()



/*  ================================================================
    FUNCTION:  isVisa()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid VISA number.
		    
	      false, otherwise

    Sample number: 4111 1111 1111 1111 (16 digits)
    ================================================================ */

function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) && (cc.substring(0,1) == 4))
    return isCreditCard(cc);

  return false;
}  // END FUNCTION isVisa()


/*  ================================================================
    FUNCTION:  isMasterCard()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid MasterCard
		    number.
		    
	      false, otherwise

    Sample number: 5500 0000 0000 0004 (16 digits)
    ================================================================ */

function isMasterCard(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);

  if ((cc.length == 16) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5)))
    return isCreditCard(cc);

  return false;

} // END FUNCTION isMasterCard()


/*  ================================================================
    FUNCTION:  isAmericanExpress()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid American
		    Express number.
		    
	      false, otherwise

    Sample number: 340000000000009 (15 digits)
    ================================================================ */

function isAmericanExpress(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  
  if ((cc.length == 15) && (firstdig == 3) && ((seconddig == 4) || (seconddig == 7)))
    return isCreditCard(cc);

  return false;

} // END FUNCTION isAmericanExpress()
