// JavaScript Document

$(document).ready(function(){
						   
			var inputArr = $('.rBox').find('input');	 //Array of billing/shipping/contact inputs	 - used in validate		   
			var selectArr = $('.rBox').find('select'); //Array of billing/shipping selections - used in validate
			var radioArr = $('.shippingRates').find('input'); //Array of shipping radio inputs - used in validate1()
			var cardArr = $('.cardType').find('input'); //Array of card type radio inputs - used in validate2()
			
			var check = null; //Used in all validation()
			var checked = null; //Used in validation1() - validation5()
			var warning = 0;  //Used in validationInit
			var selectedCard = null; //Used in validation2() and validationInit
						   
			$('.submitForm').bind('click', validate); //Check Out page 1 submit button - onclick
			$('.submitForm2').bind('click', validateInit); //Check Out page 2 submit button - onclick
			
						   
			// VALIDATION SCRIPT FOR CHECK OUT PAGE 1		
					
			function validate(){
				
				$('.cInputs').find('span').css('left', '-999em'); //re-hides all 'required'
				

				for(i=0; i<inputArr.length; i++){		//loops through all inputs
					
					if(inputArr[i].value ==''){			//if input is empty
						
						$(inputArr).eq(i).next().css('left', 'auto');	//shows required
						
						check = 1;
						
					}

				}
				
				for(i=0; i<selectArr.length; i++){ //loops through all selects
					
					if(selectArr[i].selectedIndex == 0){ //if select is on the initial selection option
						
						$(selectArr).eq(i).next().css('left', 'auto'); //shows required
						
						check = 1;
						
					}

				}
				
				if (check == 1){ //if any of the above, do not go to next page
				
					//------ BROWSER FIXES -----------------------/
					
					if(navigator.appName == 'Opera'){
					
						$('.rBox').find('span').css('margin-left', '8em');
						$('.cSpan').css('margin-left', '-3.3em');
					
					}
					
					var theApp = navigator.appVersion;
					var chrome = new RegExp('Chrome');
					var result = theApp.match(chrome);
					var safari = new RegExp('AppleWebKit');
					var result2 = theApp.match(safari);
					
					
					if(result == 'Chrome'){
					
						$('.cSpan').css('padding-left', '7.6em');
						
					}
					
					if(result2 == 'AppleWebKit'){
						
						$('.cSpan').css('padding-left', '6.9em');	
					}
					
					
					//---------------------------------------------/
					
					return false;
					event.returnValue = false;
					
				}
						   
			}//end validate
			
			// END VALIDATION SCRIPT FOR CHECK OUT PAGE 1
			
			//VALIDATION SCRIPT FOR CHECK OUT PAGE 2
			
			function validateInit(){ // This function is the heart of CheckOut Page 2 Validation, calls multiple functions and tests them
									// will ultimately determine if user proceeds to next page or not
				var trigger = 0;

				hideWarnings();			//hides all warning messages
				removeMessage();		//hides message box

				validate1();			//validates shipping radio boxes
				
				if (warning != 0){		//if radio boxes not selected
					
					trigger += 1;
				
				}
				
				
				
				validate2();			//validates credit card type
				
				if (warning != 0){		//if no credit card selected
					
					trigger += 1;
				
				}
				
				
				// temporarily removed until we resolve the cardnum / ccnumber question
				//
				// validate3();			// validates credit card number
				// 
				// if (warning != 0){		//if credit card number is wrong length
				// 	
				// 	trigger += 1;
				// 	
				// }
				
				validate4();			//validates expiration date
				
				if (warning != 0){		//if initial values selected
					
					trigger += 1;
				
				}
				
				validate5();			//validates security code
				
				if (warning != 0){		//if code is not long enough
					
					trigger += 1;
				
				}
				
				if (trigger != 0){

					try{ selectedCard.checked = true}// Bug fix so the credit card doesn't get reset
					catch(e){}; //Using Try/Catch in the event the credit card hasn't been selected (thus making selectedCard Null)
					
					return false;
					event.returnValue = false;
				
				}

			} //end validateInit
			
			function validate1(){ //tests shipping radio boxes
				
				warning = 0;
				
				for (i=0; i<radioArr.length; i++){ //loops through radio boxes

					checked = 0;
					
					if(radioArr[i].checked){  //if a radio button is selected, terminate function

						checked = 1;
						return checked;
						
					}
					

				}

				if(checked == 0){	//if no button selected, show warning
					
					showWarning1();
					message();
	
					warning = 1;
					
				}

			} //end validate1
			
			function validate2(){
				
				warning = 0;
				
				for (i=0; i<cardArr.length; i++){ //loop through credit card radio buttons

					checked = 0;
					
					if(cardArr[i].checked){ //if the current button is selected

						checked = 1;
						selectedCard = cardArr[i];  //Memorizes which card is selected to fix a bug in validateInit
						return checked; //terminate function
						
					}
					

				}

				if(checked == 0){ // if no card is selected show warning
					
					showWarning2();
					message();
	
					warning = 1;
					
				}

			} //end validate2
			
			function validate3(){ //validates credit card number length
				
				warning = 0;
				theCard = $('.cardNum').val(); //value of the card input
				cardLength = null; //length of card number set to null
				
				for(i=0; i<cardArr.length; i++){ //loops through credit card types
				
					if(cardArr[i].checked){ //if one is checked 
				
						if(cardArr[i] == cardArr[0] || cardArr[i] == cardArr[1] || cardArr[i] == cardArr[2]){ //is it Visa/Mastercard/Discover?
										 
							cardLength = 16; //card length is 16 digits				 
							break;					 
						}
						
						else if(cardArr[i] == cardArr[3]){ // is it American Express?
												 
							cardLength = 15;	//card length is 15 digits			 
							break;					 
						}
						
					}
					
				}

				if(theCard.length == cardLength){	//if value inputted is equal to expected length terminate function
				
					return;
					
				}
				
				if(theCard.length != cardLength){ //else show warning
					
					showWarning3();
					message();
	
					warning = 1;
					
				}

			} //end validate3
			
			function validate4(){ //validates expiration date
				
				warning = 0;
				var expiration = $('.xDate'); //array of <select>
				
				for (i=0; i<expiration.length; i++){ //loops through <select>

					checked = 0;
				
					if(expiration[i].selectedIndex != 0){ //if either box is not set to initial value
						
						checked = 1;
						return checked; //terminate function
						
					}
					

				}

				if(checked == 0){ //if both are still on the initial value show warning
					
					showWarning4();
					message();
	
					warning = 1;
					
				}

			} //end validate4
			
			function validate5(){
				
				warning = 0;
				checked = 0;
				theCode = $('.secureCode').val(); //value of security code iput
				
				if(theCode.length > 2){
					
					return; //if code length is greater than 2 digits
						
				}

				if(checked == 0){ //else show warning
					
					showWarning5();
					message();
	
					warning = 1;
					
				}

			} //end validate5
			
			function message(){ //shows message box
				
				$('.validWarning').css('position', 'static');

			}//end message
			
			function removeMessage() { //hides message box
				
				$('.validWarning').css('position', 'absolute');
				
				
			} //end removeMessage
			
			function hideWarnings(){ //hides all warnings
				
				$('.validWarning').find('p').hide();
				
			} //end hideWarnings
			
			function showWarning1(){ //warning for shipping radio buttons
				
				$('.validWarning').find('p').eq(0).show();
			
			} //end showWarning1
			
			function showWarning2(){ //warning for no credit card selected
				
				$('.validWarning').find('p').eq(1).show();
			
			}//end showWarning2
			
			function showWarning3(){//warning for not enough digits for credit card
				
				$('.validWarning').find('p').eq(2).show();
			
			}//end showWarning3
			
			function showWarning4(){//warning for no expiration date selected
				
				$('.validWarning').find('p').eq(3).show();
			
			}//end showWarning4
			
			function showWarning5(){//warning for no security code entered
				
				$('.validWarning').find('p').eq(4).show();
			
			}//end showWarning5
						   
});

//END VALIDATION SCRIPT FOR CHECK OUT PAGE 2