//  GLOBAL DECLARATIONS

	var NewWindow;
	self.name="Main";

//==============




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 expired( month, year ) {
        	var now = new Date();					// this function is designed to be Y2K compliant.
        	var expiresIn = new Date(year,month,0,0,0);		// create an expired on date object with valid thru expiration date
        	expiresIn.setMonth(expiresIn.getMonth()+1);		// adjust the month, to first day, hour, minute & second of expired month
        	if( now.getTime() < expiresIn.getTime() ) return false;
        	return true;						// then we get the miliseconds, and do a long integer comparison
    }



function validateCard(cardNumber,cardType,cardMonth,cardYear) {
        	if( cardNumber.length == 0 ) {						//most of these checks are self explanitory
        		alert("Please enter a valid card number.");
        		return false;				
        	}
        	for( var i = 0; i < cardNumber.length; ++i ) {		// make sure the number is all digits.. (by design)
        		var c = cardNumber.charAt(i);


            		if( c < '0' || c > '9' ) {
            			alert("Please enter a valid card number. Use only digits. do not use spaces or hyphens.");
            			return false;
            		}
            	}
            	var length = cardNumber.length;			//perform card specific length and prefix tests


                	switch( cardType ) {
                		case 'a':


                    			if( length != 15 ) {
                    				alert("Please enter a valid American Express Card number.");
                    				return;
                    			}
                    			var prefix = parseInt( cardNumber.substring(0,2));


                        			if( prefix != 34 && prefix != 37 ) {
                        				alert("Please enter a valid American Express Card number.");
                        				return;
                        			}
                        			break;
                        		case 'd':


                            			if( length != 16 ) {
                            				alert("Please enter a valid Discover Card number.");
                            				return;
                            			}
                            			var prefix = parseInt( cardNumber.substring(0,4));


                                			if( prefix != 6011 ) {
                                				alert("Please enter a valid Discover Card number.");
                                				return;
                                			}
                                			break;
                                		case 'm':


                                    			if( length != 16 ) {
                                    				alert("Please enter a valid MasterCard number.");
                                    				return;
                                    			}
                                    			var prefix = parseInt( cardNumber.substring(0,2));


                                        			if( prefix < 51 || prefix > 55) {
                                        				alert("Please enter a valid MasterCard Card number.");
                                        				return;
                                        			}
                                        			break;
                                        		case 'v':


                                            			if( length != 16 && length != 13 ) {
                                            				alert("Please enter a valid Visa Card number.");
                                            				return;
                                            			}
                                            			var prefix = parseInt( cardNumber.substring(0,1));


                                                			if( prefix != 4 ) {
                                                				alert("Please enter a valid Visa Card number.");
                                                				return;
                                                			}
                                                			break;
                                                	}
                                                	if( !mod10( cardNumber ) ) { 		// run the check digit algorithm
                                                		alert("The credit card you entered is not valid. Please verify your number.");
                                                		return false;
                                                	}
                                                	if( expired( cardMonth, cardYear ) ) {							// check if entered date is already expired.
                                                		alert("You have entered an expired credit card, please check the expiration date.");
                                                		return false;
                                                	}
                                                	
                                                	return true; // at this point card has not been proven to be invalid
                                            }



//---------

function intFilter(objV,ordermin) {

	var val = "";
	var len = objV.value.length;

	//Remove all non-numeric

    	for(i=0;i<len;i++)
        		{
			charatA = objV.value.charAt(i);
			charat0 = objV.value.charCodeAt(i);
        		if(charat0 >= 48 && charat0 <= 57) { val=val+charatA; }
			}
	
	if(val.length>0) { objV.value=val; } else { objV.value=[ordermin]; }
		
	//Now check for minimum allowable order qty

	if([ordermin] != '')
	{

	if(!isNaN(parseInt(val))) 
		{
		
		if (parseInt(val)>0 && parseInt(val)<[ordermin]) 
			{
			
			alert('You have requested a quantity that is less than the minimum required order amount.\rYour quantity will be reset to the minimum.');
				if([ordermin]==0) {
				objV.value="";
				}
				else {
				objV.value=[ordermin];
				}
				return false;
	
		}
		else if(parseInt(val)!=0 && (parseInt(val)%[ordermin])!=0) {

			alert('Quantity ordered must be a multiple of '+[ordermin]+".");
			objV.value=[ordermin];
			objV.focus();
			return true;

		}
		else { return true; }

	}
	}
}


