function FindElement(elementName)
{
	if (document.getElementById) {
		var e1 = document.getElementById(elementName);
		if (e1 != null) return e1;
	}
	
	var frm = document.forms[0];
	var el = frm.elementName;
	if (el != null) return el;
	
	return null;
}

function HandleEnterKey(e, buttonID) 
{    
	var e  = (e) ? e : event;
	
	if ( (e.which && e.which == 13) || (e.keyCode == 13) )
	{
		var btn = FindElement(buttonID);
		if (btn != null)
		{
			e.cancelBubble = true;
			e.returnValue = false;
			btn.click();
			return false;
		}
	}
	return true;
} 

function LimitText(fieldObj,maxChars)
{
	var result = true;
	if (fieldObj.value.length >= maxChars) {
		result = false;
		fieldObj.value = fieldObj.value.substring(0, maxChars);
	}  

	if (window.event) {
		window.event.returnValue = result;
	}
	return result;
}

function SetFocus(controlId)
{
	var c = FindElement(controlId);
	if (c) {
		c.focus();
		if (c.type == "text") //select all text
			c.select();
	}
}

function tab(from,to)
{
	var c1 = FindElement(from);
	var c2 = FindElement(to);
	if (c1 && c2)
	{
		if (c1.value.length==c1.getAttribute("maxlength")) 
			c2.select();
	}
}
        
function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	
	if ((charCode >= 46 && charCode < 58) || (charCode>95 && charCode <106) || charCode==9 || charCode==8 ) //tab ok
		return true;

	return false;
}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

function getAvail(term,item,classID,schoolCode)
{
	window.open("avail.aspx?term="+term+"&item="+item+"&classId="+classID+"&schoolCode="+schoolCode,"avail","left=500,top=300,width=275,height=180,menubar=no,toolbar=no,titlebar=no,dependent=yes"); 
	return false;
}

function popupBookStore()
{
	window.open('http://store330.collegestoreonline.com',null,'width=700,height=600,scrollbars=yes,status=no,toolbar=no,resizable=yes');
}

function popupReceipt(c)
{
	window.open('../Receipt/Receipt.aspx?orderNum='+c,null,'width=650,height=600,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes');
}
		
function emailFriend(cid,c)
{
	window.open('../Email/EmailFriend.aspx?cid='+cid + '&c=' +c,null,'left=200,top=200,width=500,height=450,directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no');
}

function courseRequest(c)
{
	window.open('../Email/CourseRequest.aspx?'+c,null,'left=200,top=200,width=400,height=250,directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no');
}

function coursePrint(c)
{
	window.open('../Course/print.aspx?'+c,null,'left=100,top=100,width=600,height=600,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes');
}

function getMore(catID)
{
	window.open('more.aspx?Cat='+catID,null,'left=400,top=210,width=320,height=380,menubar=no,toolbar=no,titlebar=no,dependent=yes,scrollbars=yes,resizable=yes'); 
}

function feedBack(url)
{
    if(!url)    url='../feedBack/feedBack.aspx';
	window.open(url,null,'left=200,top=200,width=550,height=400,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes');
}

function mod10( cardNumber ) 
{ // LUHN Formula for validation of credit card numbers.
  var ar = new Array( cardNumber.length );
  var i = 0,sum = 0;

  for( i = 0; i < cardNumber.length; ++i )
  {
    ar[i] = parseInt(cardNumber.charAt(i));
  }
  for( i = ar.length -2; i >= 0; i-=2 ) 
  {                           // you have to start from the right, and work back.
    ar[i] *= 2;               // every second digit starting with the right most (check digit)

    if( ar[i] > 9 ) ar[i]-=9;   // will be doubled, and summed with the skipped digits.
  }          // if the double digit is > 9, ADD those individual digits together 

  for( i = 0; i < ar.length; ++i ) 
  {
    sum += ar[i]; // if the sum is divisible by 10 mod10 succeeds
  }
  return (((sum%10)==0)?true:false);                   
}

function validateCard(cardNumber,cardType) 
{
  var length = cardNumber.length;  

  switch( cardType ) 
  {
    case 'Amex':
      if( length != 15 ) 
      {
        alert("Please enter a valid American Express Card number.");
        return false;
      }

      var prefix = parseInt( cardNumber.substring(0,2));
      if( prefix != 34 && prefix != 37 )
      {
        alert("Please enter a valid American Express Card number.");
        return false;
      }
		break;
    case 'Discover':
      if( length != 16 ) 
      {
        alert("Please enter a valid Discover Card number.");
        return false;
      }
      var prefix = parseInt( cardNumber.substring(0,4));
      if( prefix != 6011 ) 
      {
        alert("Please enter a valid Discover Card number.");
        return false;
      }
      break;
    case 'Master':
      if( length != 16 ) 
      {
        alert("Please enter a valid MasterCard number.");
        return false;
      }
      var prefix = parseInt( cardNumber.substring(0,2));
      if( prefix < 51 || prefix > 55) 
      {
        alert("Please enter a valid MasterCard Card number.");
        return false;
      }
      break;
      case 'Visa':
        if( length != 16 && length != 13 ) 
        {
          alert("Please enter a valid Visa Card number.");
          return false;
        }
        var prefix = parseInt( cardNumber.substring(0,1));
        if( prefix != 4 ) 
        {
          alert("Please enter a valid Visa Card number.");
          return false;
        }
      break;
  }
  if( !mod10( cardNumber ) ) 
  { // run the check digit algorithm
    alert("Sorry! this is not a valid credit card number.");
    return false;
  }
  return true; // at this point card has not been proven to be invalid

}

