﻿window.onload = function(){
    setDeliveryFields()
    setVAT(document.getElementById('selCountry').value);
    showFields('cboChequePayment','chequeText')
    showFields('cboPO','poText')
    showFields('cboOther','otherText')
    showFields('cboOther','otherTextDetails')
    
    prepareInputsForHints();
    showFields('cboShopperNewsletter','newsletters');

}

function prepareInputsForHints() {

  var inputs = document.getElementsByClassName("hintLink");
  for (var i=0; i<inputs.length; i++){
    inputs[i].onclick = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
      return(false);
    }
    inputs[i].onblur = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "none";
    }
  }
}


// Sets the country information (i.e. dialling codes) and checks if the 
// Country is okay to use
function setCountryCode(country,codeField,mobileField,warningField){
   
    var codeField = document.getElementById(codeField);
    var mobileField = document.getElementById(mobileField);
    if (country!=""){
        countryArray = country.split(",")
        
        if (warningField==""){
                codeField.value = countryArray[1];
                mobileField.value = countryArray[1];
       
        }else{
            // Commented out to allow all countries in the list to be submitted
            // IMPORTANT!!!!!
            // ---------------------
            // Needs to be uncommented if going with Euroconex
            
            //if (countryArray[2] != "1"){
            //    document.getElementById(warningField).innerHTML = "Unfortunately we are not able to take registrations from this country";
            //    document.getElementById('btnSubmit').disabled = true;
            //}else{
                 document.getElementById(warningField).innerHTML = "";
                 document.getElementById('btnSubmit').disabled = false;
          
                codeField.value = countryArray[1];
                mobileField.value = countryArray[1];
            //}
         }
    }else{
        codeField.value = "";
        mobileField.value = "";
    }
}

function setVAT(selCountry){
    if (selCountry != ""){
        selCountry = selCountry.split(",")
        
        if (selCountry[4] == 'True'){
            document.getElementById("vatRowDT").style.display = 'block';
            document.getElementById("vatRowDD").style.display = 'block';
        }else{
            document.getElementById("vatRowDT").style.display = 'none';
            document.getElementById("vatRowDD").style.display = 'none';
        }
    }else{
        document.getElementById("vatRowDT").style.display = 'none';
        document.getElementById("vatRowDD").style.display = 'none';
    }
}

function setDeliveryFields(){
	if (document.getElementById('rboDeliveryNo').checked){
        document.getElementById("delAddressDetails").style.display = 'block';
	}else{
        document.getElementById("delAddressDetails").style.display = 'none';
	}
}

function showFields(cbo,textDiv){
    var cbo = document.getElementById(cbo);
    var textDiv = document.getElementById(textDiv);
    if(cbo.checked){
        textDiv.style.display = 'block';
    }else{
        textDiv.style.display = 'none';
    }
}


// Form Validation
function validateForm(){

    
    var errMsg = "";
    var ppPaymentVal = "";
    var chequePaymentVal = "";
    var poPaymentVal = "";
    var otherPaymentVal = "";
    var otherPaymentTypeVal = "";
    var totalPaymentMethods = 0;
    var paymentMsg = "";

    var txtPassword = document.getElementById('txtPassword');
    var txtPasswordConfirm = document.getElementById('txtPasswordConfirm');
    var txtEmail = document.getElementById('txtEmail');
    var txtEmailConfirm = document.getElementById('txtEmailConfirm');

    var firstNameVal = validate('txtFirstName','Please enter your first name','starFirstName');
    var surnameVal = validate('txtSurname','Please enter your surname','starSurname');
    var screenNameVal = validate('txtScreenName','Please choose a screen name','starScreenName');
   
   
    var passwordVal = validate('txtPassword','Please enter a password','starPassword');
    var passwordConfirmVal = validate('txtPasswordConfirm','Please choose a screen name','starPassword');
    
    var emailVal = echeck(txtEmail.value,'Please enter your email address','starEmail');

    var emailCompare = compare('txtEmail','txtEmailConfirm','Your email addresses don\'t match','starEmail','starEmailConfirm');
    
    var addressVal = validate('txtAddress1','Please enter your address','starAddress');
    var countryVal = validate('selCountry','Please select your country','starCountry');
    var telAreaVal = validate('txtCountryCode','Please enter your area dialling code','starTelephone');
    var telVal     = validate('txtTelephoneNumber','Please enter your telephone number','starTelephone');
    
    if (document.getElementById('starLabel')!=null){
        var labelVal   = validate('txtLabel','Please enter a name for your label','starLabel');
    }else{
        var labelVal = "";  
    }
    var currencyVal   = validate('selCurrency','Please select the currency you would like to add products with and receive payments in','starCurrency');
    
    var DelcountryVal = "";
    var DeltelAreaVal = "";
    var DeltelVal = "";

    if (document.getElementById('rboDeliveryNo').checked){
        DelcountryVal = validate('selCountryDel','Please select your country for returns etc.','starDelCountry');
        DeltelAreaVal = validate('txtDelCountryCode','Please enter your area dialling code for returns etc.','starDelTelephone');
        DeltelVal     = validate('txtDelTelephoneNumber','Please enter your telephone number for returns etc.','starDelTelephone');
    }
    
    
    if (document.getElementById('cboPayPalPayment').checked){
        totalPaymentMethods++;
        ppPaymentVal = validate('txtPayPalEmailAccount','Please enter your PayPal e-mail address for receiving PayPal payments','starPPPayment');
    }
    if (document.getElementById('cboChequePayment').checked){
        totalPaymentMethods++;
        chequePaymentVal = validate('txtChequeName','Please enter the name cheques should be made payable to','starChequePayment');
    }
    if (document.getElementById('cboPO').checked){
        totalPaymentMethods++;
        poPaymentVal = validate('txtPOName','Please enter the name Postal Orders should be made payable to','starPOPayment');
    }
    if (document.getElementById('cboOther').checked){
        totalPaymentMethods++;
        otherPaymentTypeVal = validate('txtOtherPaymentType','Please enter the type of other payment method you accept','starOtherPaymentType');
        otherPaymentVal = validate('txtOtherPayment','Please enter the details of other payment methods you accept','starOtherPayment');
    }
    
   
    //if (totalPaymentMethods==0){
    //    paymentMsg = "\u00BB Please select at least one method of receiving payments \n\n";     
    //}
    
    
    errMsg = errMsg + firstNameVal;
    errMsg = errMsg + surnameVal;
    errMsg = errMsg + screenNameVal;
    errMsg = errMsg + emailVal;

    errMsg = errMsg + emailCompare;

    if (document.getElementById('txtPassword').value == ""){
        errMsg = errMsg + "\u00BB Please enter a password \n\n";
        document.getElementById('starPassword').className = 'error';
    }else if (document.getElementById('txtPassword').value != document.getElementById('txtPasswordConfirm').value){
       errMsg = errMsg + "\u00BB Your passwords don\'t match \n\n";
       document.getElementById('starPassword').className = 'error';
       document.getElementById('starPasswordConfirm').className = 'error';
    }else if (document.getElementById('txtPassword').value.length < 5 || document.getElementById('txtPassword').value.length > 10){
       errMsg = errMsg + "\u00BB Your password should be between 5 and 10 characters \n\n";
       document.getElementById('starPassword').className = 'error';
    }else{
        document.getElementById('starPassword').className = 'nonError';
        document.getElementById('starPasswordConfirm').className = 'nonError';
    }
    
    errMsg = errMsg + addressVal;
    errMsg = errMsg + countryVal;
    errMsg = errMsg + telAreaVal;
    errMsg = errMsg + telVal;

    errMsg = errMsg + DelcountryVal;
    errMsg = errMsg + DeltelAreaVal;
    errMsg = errMsg + DeltelVal;
    
    

    errMsg = errMsg + labelVal;
    errMsg = errMsg + currencyVal;

    errMsg = errMsg + ppPaymentVal;
    errMsg = errMsg + chequePaymentVal;
    errMsg = errMsg + poPaymentVal;
    errMsg = errMsg + otherPaymentTypeVal;
    errMsg = errMsg + otherPaymentVal;
    errMsg = errMsg + paymentMsg; // No Payment methods selected
    
    if (document.getElementById('rboAcceptYes')!=null){
        if (document.getElementById('rboAcceptYes').checked == false){
           errMsg = errMsg + "\u00BB Please confirm that you have read and agree to our terms and conditions \n\n";
        }
    }
    
    if (errMsg == ""){
        document.fmRegister.submit();
    }else{
        var alertmsg = "Sorry, but there were problems submitting the form. Please review the following elements before resubmitting \n\n";
        alertmsg = alertmsg + errMsg
        alert(alertmsg);
        //document.fmRegister.submit();
        return false;
    }

}
