//Function to show the page once it is loaded completly into the browser
function showPage(){
document.getElementById("showPage").style.visibility="visible";
}

//Remove from cart function
function removeItem(id,prodName){
  doit = confirm("Do you wish to remove " + prodName + " item from your cart?");
  
  if(doit){
    window.location = "?page=cart&action=removeItem&prodID=" + id;
  }
} 

//Confirm logout before doing it.
function confirmLogout(){
	logOut = confirm("Do you wish to Logout now?");
    if(logOut){
    	window.location = "?page=login&action=logout";
	}
}            

//Function to disable card number and date fields if paypal is choosen pay method
function paypalMenuDisable(x){
	if (x=="paypal"){
       	document.getElementById("CardNumber").disabled = !(document.getElementById("CardNumber").disabled); 
       	document.getElementById("expYear").disabled = !(document.getElementById("expYear").disabled); 
       	document.getElementById("expMonth").disabled = !(document.getElementById("expMonth").disabled);       	
       	}
       
	if(x!="paypal"){ 
		document.getElementById("CardNumber").disabled = false;
       	document.getElementById("expYear").disabled = false;
       	document.getElementById("expMonth").disabled = false;
       	}
} 

//Function to change state to zip code input for the cart/shipping
function epostSwitch(country){
	
    if(country=="CA"){
		document.getElementById("epostSwitch").innerHTML = 'Postal Code:&nbsp;<input type="text" name="ship2"><input type="hidden" name="ship2Value" value="postCode">';
    }
    if(country!="CA"){
    	document.getElementById("epostSwitch").innerHTML = '<input type="hidden" name="ship2Value" value="international">'; 
        }
        
	if(country=="US"){
    	document.getElementById("epostSwitch").innerHTML = '<select name="ship2"><option value="AL">Alabama</option><option value="AR">Arkansas</option><option value="AS">American Samoa</option><option value="AZ">Arizona</option><option value="CA">California</option><option value="CO">Colorado</option><option value="CT">Connecticut</option><option value="DC">District of Columbia</option><option value="DE">Delaware</option><option value="FL">Florida</option><option value="FM">Micronesia</option><option value="GA">Georgia</option><option value="GU">Guam</option><option value="HI">Hawaii</option><option value="IA">Iowa</option><option value="ID">Idaho</option><option value="IL">Illinois</option><option value="IN">Indiana</option><option value="KS">Kansas</option><option value="KY">Kentucky</option><option value="LA">Louisiana</option><option value="MA">Massachusetts</option><option value="MD">Maryland</option><option value="ME">Maine</option><option value="MI">Michigan</option><option value="MN">Minnesota</option><option value="MO">Missouri</option><option value="MP">Northern Marianas</option><option value="MS">Mississippi</option><option value="MT">Montana</option><option value="NC">North Carolina</option><option value="ND">North Dakota</option><option value="NE">Nebraska</option><option value="NH">New Hampshire</option><option value="NJ">New Jersey</option><option value="NM">New Mexico</option><option value="NV">Nevada</option><option value="NY">New York</option><option value="OH">Ohio</option><option value="OK">Oklahoma</option><option value="OR">Oregon</option><option value="PA">Pennsylvania</option><option value="PR">Puerto Rico</option><option value="RI">Rhode Island</option><option value="SC">South Carolina</option><option value="SD">South Dakota</option><option value="TN">Tennessee</option><option value="TX">Texas</option><option value="UT">Utah</option><option value="VA">Virginia</option><option value="VI">Virgin Islands</option><option value="VT">Vermont</option><option value="WA">Washington</option><option value="WI">Wisconsin</option><option value="WV">West Virginia</option><option value="WY">Wyoming</option></select><input type="hidden" name="ship2Value" value="province">'; 
	}         
} 

//Small function to check the form from the cart page for a postal code
function validateCartForm(){
   	
    document.cartForm.ship2.value = document.cartForm.ship2.value.toUpperCase();
    
    if(window.document.cartForm.ship2.value == ""){ 
    alert("Please enter a valid Canadian Postal Code.");
    return false;
    }
}  

//Validation of the Create Account Form

function validateCreateAccountForm(){

	 postName = window.document.createAccount.name.value
	 postEmail = window.document.createAccount.email.value
	 postConfirmEmail =  window.document.createAccount.confirmemail.value
	 postPass = window.document.createAccount.pass.value
	 postConfirmPass = window.document.createAccount.confirmPass.value

	 if(postName == ""){
	 alert("Please enter your first name");
	 return false;
	 }
	 	 
	 if(postEmail == ""){
	 alert("Please enter a valid Email Address");
	 return false;
	 }
	 
	 if(postConfirmEmail == ""){
	 alert("Please confirm your Email Address");
	 return false;
	 }
	 
	 if(postPass == ""){
	 alert("Please enter a Password");
	 return false;
	 }
	 
	 if(postConfirmPass == ""){
	 alert("Please confirm your Password");
	 return false;
	 }
	 
	 if(postPass != postConfirmPass){
	 alert("Your passwords do not match");
	 return false;
	 }  
	 
	 if(postEmail != postConfirmEmail){
	 alert("Your Email address does not match");
	 return false;
	 } 
}

//Validate Login
function validateLogin(){
	 
	 postEmail = window.document.loginForm.email.value
	 postPass = window.document.loginForm.pass.value
	 
	 if(postEmail == ""){
	 alert("Please enter your email address");
	 return false;
	 }
	 
	 if(postPass == ""){
	 alert("Please enter your password");
	 return false;
	 }
}

//Validate Update for required fields

function validateAccountUpdate(){
  postName = window.document.accountUpdate.name.value
    if(postName == ""){
    alert("Firt Name is a required field");
    return false;
  }
}

function getDownload(keyID,platform){
window.open("/pickup/?keyID="+keyID+"&platform="+platform+"","myDownload","toolbar=0,scrollbars=0,location=0,statusbar=no,menubar=0,resizable=no,width=1,height=1,left = 10,top = 10");
}

function validateAccountUpdateEmail(){
  postEmail = window.document.accountUpdateEmail.email.value
  postRetypeEmail = window.document.accountUpdateEmail.retypeEmail.value
  if(postEmail == ""){
    alert("Both E-Mail fields are required");
    return false;
  }
  if(postRetypeEmail == ""){
    alert("Both E-Mail fields are required");
    return false;
  }	
  if(postEmail != postRetypeEmail){
    alert("Your E-Mail address do not match");
  	return false;
  }
}

function validateAccountUpdatePass(){
  postOldPass = window.document.updatePass.oldPass.value
  postNewPass = window.document.updatePass.newPass.value
  postretypeNewPass = window.document.updatePass.retypeNewPass.value
  
  if(postOldPass == "" || postNewPass == "" || postretypeNewPass == ""){
    alert("All password fields are required");
    return false;
  }
  
  if(postNewPass != postretypeNewPass){
    alert("Your Passwords do not match!");
    return false;
  }
}

//Validation check for the shipping form
function validateShippingForm(){

    postName = window.document.shippingForm.name.value
    postEmail = window.document.shippingForm.email.value 
    postAddress = window.document.shippingForm.address.value
    postCity = window.document.shippingForm.city.value
    postPostCode = window.document.shippingForm.postCode.value    
    postPhone = window.document.shippingForm.phone.value
	
    if(postName == "" || postEmail == "" || postAddress == "" || postCity == "" || postPhone == "" || postPostCode == ""){
    alert("One or more of the required fields are not completed. Please complete them, then submit again!");
    return false;
    }
}  

//Function to check reset password field
function resetPassFormCheck(){
  if(window.document.forgotPassForm.email.value == ""){
  alert("Please enter the email address associated with your account");
  return false;
  }
	if(window.document.forgotPassForm.email.value != ""){
	var agree=confirm("Please confirm that a new password will be mailed to "+window.document.forgotPassForm.email.value+"");
			if (agree)
			return true;
			else
			window.document.forgotPassForm.email.value = "";
			return false;
	}
}

//Function to ensure they are not selecting a province/state for outside Canada or the U.S
function billFormProvinceCheck(){
		if(window.document.billingForm.country.value != "CA" && window.document.billingForm.country.value != "US"){
		window.document.billingForm.province.value = "--";
		}

}
function validateBillingForm(){ 

		if(window.document.billingForm.country.value != "CA"  && window.document.billingForm.country.value != "US"){
		window.document.billingForm.province.value = "--";
		}
		 
    //The cardNumber field
    if(document.billingForm.paymentMethod.value != "paypal"){ 
	cardNumber = window.document.billingForm.cardNumber.value
	
		if(cardNumber == ""){
			alert("Please Enter a valid credit card number!");
			return false; 
		}
    }

    postEmail = window.document.billingForm.email.value;    
    
    //Now let's see if they are using a Free mail service and warn them
	if(postEmail.match("hotmail") || postEmail.match("yahoo") || postEmail.match("msn") || postEmail.match("gmail") || postEmail.match("aol")){
	  doit = confirm("Warning: You have entered an email address that belongs to a Free Mail Service.  Please be advised that we use your billing email address to send correspondence regarding this order including download links for download purchases.  These services often filter these mails as junk or SPAM - This may result in mail from us not being delivered to you.  We recommend you choosing an alternative email address.\n\nDo you wish to continue?");
	  	if (!doit){
		return false;  
		}
	}
	
	//Turn the posted values into varibles
    postName = window.document.billingForm.name.value   
    postAddress = window.document.billingForm.address.value
    postCity = window.document.billingForm.city.value
    postPhone = window.document.billingForm.phone.value
    postPostCode = window.document.billingForm.postCode.value
        
    if(postName == "" || postEmail == "" || postAddress == "" || postCity == "" || postPhone == "" || postPostCode == ""){
    	alert("One or more of the required fields are not completed. Please complete them, then submit again!");
        return false;
        }
} 

function internationalShip(payMethod){

	if(payMethod == "creditCard"){
    alert("Processing this transaction may take a few seconds, please wait till the transaction is complete.  Do not submit the order a second time, doing so may cause errors with your order.");
    }        
	if(window.document.submitOrder.dutiesAgree.checked == false){
    	alert("You need to agree to duties, taxes and fees for international shipments");
        return false;
	}
    
}          
  
//function to check contact form
function contactValidate(){
	postName = window.document.contact.docName.value
    postEmail = window.document.contact.docEmail.value
    postMessage = window.document.contact.docMessage.value
    
    if(postName == "" || postEmail == "" || postMessage == ""){
    	alert("All fields are required to submit a contact request");
        return false;
	}
}            