// JavaScript Document
// Extended Tooltip Javascript
// copyright 9th August 2002, 3rd July 2005, 24th August 2008
// by Stephen Chapman, Felgall Pty Ltd

var DH = 0;var an = 0;var al = 0;var ai = 0;
if (document.getElementById) {ai = 1; DH = 1;}
else 
	{if (document.all) {al = 1; DH = 1;} 
		else { browserVersion = parseInt(navigator.appVersion); 
		if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4)) {an = 1; DH = 1;}}} 
		
function fd(oi, wS) {if (ai) return wS ? document.getElementById(oi).style:document.getElementById(oi); if (al) return wS ? document.all[oi].style: document.all[oi]; if (an) return document.layers[oi];}
function pw() {return window.innerWidth != null? window.innerWidth: document.body.clientWidth != null? document.body.clientWidth:null;}
function mouseX(evt) {if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return null;}
function mouseY(evt) {if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return null;}

function popUp(evt,oi,tblId,curLst)
 {	
	if (DH) 
	{
		var wp = pw(); ds = fd(oi,1); dm = fd(oi,0); st = ds.visibility; 
		if (dm.offsetWidth) 
			ew = dm.offsetWidth; 
		else if (dm.clip.width) 
			ew = dm.clip.width; 
			
		if (st == "visible" || st == "show")
		{
			 ds.visibility = "hidden"; 
			 ToggleSelects(true,tblId);
		} 
		else 
		{
			ToggleSelects(false,tblId,curLst);
			tv = mouseY(evt) + 20; lv = mouseX(evt) - (ew/4); 
			if (lv < 2) lv = 2; 
			else if (lv + ew > wp) lv -= ew/2; 
			
			if (!an) 
			{
				lv += 'px';tv += 'px';
			} 
			ds.left = lv; ds.top = tv; ds.visibility = "visible";
		}
	}
}

 function ToggleSelects(flagVisible,tblId,curLst)
    {
		//----- it will work only for Ie and for tableid which we have passed				
		var win_ie_ver = parseFloat(navigator.appVersion.split("MSIE")[1]);
		var tmpFlag = false;		
		if (isNaN(win_ie_ver) == false && win_ie_ver < 7 && tblId)
		{
			var lists = document.getElementById(tblId).getElementsByTagName("SELECT");      
			for ( var i = 0; i <lists.length; i++ )
			{         
				var list = lists[i];
				if(flagVisible)
				{
					list.style.visibility = (flagVisible) ? "visible" : "hidden";
				}
				else
				{		
					//--------- Hide only those select list which are below on current list			
					if(tmpFlag == true)
						list.style.visibility = (flagVisible) ? "visible" : "hidden";	
					else if(list.id == curLst)
					{
						tmpFlag = true;
					}					
				}	
			}
		}			
    }

/** *********************************************************************************/
//Card number Checkings
/** *********************************************************************************/
	var ccErrorNo = 0;
	var ccErrors = new Array ()

	ccErrors [0] = "Unknown card type";
	ccErrors [1] = "No card number provided";
	ccErrors [2] = "Credit card number is in invalid format";
	ccErrors [3] = "Credit card number is invalid";
	ccErrors [4] = "Credit card number has an inappropriate number of digits";

	function checkCard(cardnumber, cardname) 
	{
		if(checkCreditCard (cardnumber, cardname) == true)
			return true;
		else
		{
			alert(ccErrors[ccErrorNo]);
			return false;
		}	
	}
	
	function checkCreditCard (cardnumber, cardname) 
	{
				
		// Array to hold the permitted card characteristics
		var cards = new Array();

		// Define the cards we support. You may add addtional card types.
			
		//  Name:      As in the selection box of the form - must be same as user's
		//  Length:    List of possible valid lengths of the card number for the card
		//  prefixes:  List of possible prefixes for the card
		//  checkdigit Boolean to say whether there is a check digit
			
		cards [0] = {name: "Visa", 
					length: "13,16", 
					prefixes: "4",
					checkdigit: true};
		cards [1] = {name: "MasterCard", 
					length: "16", 
					prefixes: "51,52,53,54,55",
					checkdigit: true};
		cards [2] = {name: "DinersClub", 
					length: "14,", 
					prefixes: "300,301,302,303,304,305,36,38",
					checkdigit: true};
		cards [3] = {name: "CarteBlanche", 
					length: "14", 
					prefixes: "300,301,302,303,304,305,36,38",
					checkdigit: true};
		cards [4] = {name: "AmEx", 
					length: "15", 
					prefixes: "34,37",
					checkdigit: true};
		cards [5] = {name: "Discover", 
					length: "16", 
					prefixes: "6011",
					checkdigit: true};
		cards [6] = {name: "JCB", 
					length: "15,16", 
					prefixes: "3,1800,2131",
					checkdigit: true};
		cards [7] = {name: "Enroute", 
					length: "15", 
					prefixes: "2014,2149",
					checkdigit: true};
				        
		// Establish card type
		var cardType = -1;
		for (var i=0; i<cards.length; i++) 
		{
			// See if it is this card (ignoring the case of the string)
			if (cardname.toLowerCase () == cards[i].name.toLowerCase()) 
			{
			cardType = i;
			break;
			}
		}
			
		// If card type not found, report an error
		if (cardType == -1) 
		{
			ccErrorNo = 0;
			return false; 
		}
			
		// Ensure that the user has provided a credit card number
		if (cardnumber.length == 0)  
		{
			ccErrorNo = 1;
			return false; 
		}
			
		// Check that the number is numeric, although we do permit a space to occur  
		// every four digits. 
		var cardNo = cardnumber
		var cardexp = /^([0-9]{4})\s?([0-9]{4})\s?([0-9]{4})\s?([0-9]{1,4})$/;
		if (!cardexp.exec(cardNo))  
		{
			ccErrorNo = 2;
			return false; 
		}
		// Now remove any spaces from the credit card number
		cardexp.exec(cardNo);
		cardNo = RegExp.$1 + RegExp.$2 + RegExp.$3 + RegExp.$4;
				
		// Now check the modulus 10 check digit - if required
		if (cards[cardType].checkdigit) 
		{
			var checksum = 0;                                  // running checksum total
			var mychar = "";                                   // next char to process
			var j = 1;                                         // takes value of 1 or 2
			
			// Process each digit one by one starting at the right
			var calc;
			for (i = cardNo.length - 1; i >= 0; i--) 
			{
			
			// Extract the next digit and multiply by 1 or 2 on alternative digits.
			calc = Number(cardNo.charAt(i)) * j;
			
			// If the result is in two digits add 1 to the checksum total
			if (calc > 9) 
			{
				checksum = checksum + 1;
				calc = calc - 10;
			}
			
			// Add the units element to the checksum total
			checksum = checksum + calc;
			
			// Switch the value of j
			if (j ==1) {j = 2} else {j = 1};
			} 
			
			// All done - if checksum is divisible by 10, it is a valid modulus 10.
			// If not, report an error.
			if (checksum % 10 != 0)  
			{
			ccErrorNo = 3;
			return false; 
			}
		}  

		// The following are the card-specific checks we undertake.
		var LengthValid = false;
		var PrefixValid = false; 
		var undefined; 

		// We use these for holding the valid lengths and prefixes of a card type
		var prefix = new Array ();
		var lengths = new Array ();
			
		// Load an array with the valid prefixes for this card
		prefix = cards[cardType].prefixes.split(",");
				
		// Now see if any of them match what we have in the card number
		for (i=0; i<prefix.length; i++) {
			var exp = new RegExp ("^" + prefix[i]);
			if (exp.test (cardNo)) PrefixValid = true;
		}
				
		// If it isn't a valid prefix there's no point at looking at the length
		if (!PrefixValid) {
			ccErrorNo = 3;
			return false; 
		}
			
		// See if the length is valid for this card
		lengths = cards[cardType].length.split(",");
		for (j=0; j<lengths.length; j++) 
		{
			if (cardNo.length == lengths[j]) LengthValid = true;
		}
			
		// See if all is OK by seeing if the length was valid. We only check the 
		// length if all else was hunky dory.
		if (!LengthValid) 
		{
			ccErrorNo = 4;
			return false; 
		};   
			
		// The credit card is in the required format.
		return true;
	}
	
/** *********************************************************************************/	
