var REGEX = {"valid_email":"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,4})$","valid_phone":"^[\\*\\(\\)\\ \\+0-9-]{3,50}$","valid_promotion_code":"^[A-Za-z]{1,3}[0-9]{2,7}$","valid_zip":"^[\\ A-Za-z0-9-\\\/]{1,20}$","valid_name":"^[A-Za-z '-]{2,50}$","valid_reservnum":"^[0-9]{8}$","valid_password":"^(?=[_A-Za-z0-9-]{3,20}$).*[^0-9]","valid_address":"^[A-Za-z0-9\\.,:;\\(\\)'\"\\\/ \\u0400-\\u04FF\\u0080-\\u00FF\\u0100-\\u017F\\u0180-\\u024F-]{3,50}$","valid_date":"^(19|20)[0-9][0-9][- ](0?[1-9]|1[012])[- ](0?[1-9]|[12][0-9]|3[01])$","valid_year":"^(19|20)[0-9][0-9]$","valid_month":"^(0?[1-9]|1[012])$","valid_day":"^(0?[1-9]|[12][0-9]|3[01])$","valid_age":"^[1-9][0-9]$","valid_cvv2":"^[0-9]{3,4}$","empty":"^$"};

function isArray(testObject) {
    return testObject && !(testObject.propertyIsEnumerable('length')) && typeof testObject === 'object' && typeof testObject.length === 'number';
}

function isValidRegex(input, regex) {
  return input.match(new RegExp(regex)) != null;
}

function isNameValid(nameId) {
  return isValidRegex($('#' + nameId).val(), REGEX['valid_name']);
}

function isEmailValid(emailId) {
  return isValidRegex($('#' + emailId).val(), REGEX['valid_email']);
}

function isPhoneValid(phoneId) {
  return isValidRegex($('#' + phoneId).val(), REGEX['valid_phone']);
}

function isPromotionCodeValid(codeId) {
  return isValidRegex($('#' + codeId).val(), REGEX['valid_promotion_code']);
}

function isReservNumberValid(numberId) {
  return isValidRegex($('#' + numberId).val(), REGEX['valid_reservnum']);
}

function isPasswordValid(passwordId) {
  return isValidRegex($('#' + passwordId).val(), REGEX['valid_password']);
}

function isAddressValid(addressId) {
  return isValidRegex($('#' + addressId).val(), REGEX['valid_address']);
}

function isZipValid(zipId) {
  return isValidRegex($('#' + zipId).val(), REGEX['valid_zip']);
}

function isAgeValid(ageId) {
  return isValidRegex($('#' + ageId).val(), REGEX['valid_age']);
}

function isCvv2Valid(id) {
  return isValidRegex($('#' + id).val(), REGEX['valid_cvv2']);
}

function equalValues(id, id2) {
  return $('#' + id).val() == $('#' + id2).val();
}

function isEmpty(id) {
  return $('#' + id).val() == '';
}

function isNonZero(id) {
  return $('#' + id).val() != '0';
}



function isChecked(id) {
  return $('#' + id + ':checked').length;
}

function fixPromotionCodeValue(id) {
  if (!isPromotionCodeValid(id.value)) {
    id.value = "";
    document.getElementById('promovalid').innerHTML = "Vigane PromoCode!";
  } else {
    document.getElementById('promovalid').innerHTML = "";
  }
}

function isCreditCardNumberValid(numberId) {
  var number = $('#' + numberId).val();
  if(number.length == 0) {
    return false;
  }
  var length = number.length;
  var idx = length - 2;
  var sum = 0;
  var digit = 0;
  var result = 0;

  for (var i = length - 1;i >= 0; i--) {
    digit = parseInt(number.substring(i, i+1), 10);
    if (i == idx) {
      idx -= 2;
      result = digit * 2;
      if (result >= 10) {
        sum += (result - 9);
      } else {
        sum += result;
      }
    } else {
      sum += digit;
    }
  }

  return sum % 10 == 0;
}

/*
function colorize(labelId, inputId, regexTags) {
  var color = matchAny(inputId, regexTags) ? 'black' : 'red';
  $('#' + labelId).css('color', color);
}

function matchAny(inputId, regexTags) {
  regexTagList = regexTags.split(':');
  for (var i = 0; i < regexTagList.length; i++) {
    if (isValidRegex($('#' + inputId).val(), REGEX[regexTagList[i]])) {
      return true;
    }
  }
  return false;
}

function matchAnyF(labelId, inputId, callbackList) {
  var ok = false;
  for (var i=0; i<callbackList.length; i++) {
    if (callbackList[i](inputId)) {
      ok = true;
    }
  }
  var color = ok ? 'black' : 'red';
  $('#' + labelId).css('color', color);
}

function validate(labelId, inputId, callback) {
  if(!callback(inputId)) {
    $('#' + labelId).css('color', 'red');
  } else {
    $('#' + labelId).css('color', 'black');
  }
}

*/

function userMessage(message) {
  alert(message);
}

function validateFunction(labelId, callback) {
  if (!callback) {
    $('#' + labelId).css('color', 'red');
  } else {
    $('#' + labelId).css('color', 'black');
  }
}


function colorize (labelId, inputId, callbacks) {
  var color = validate(inputId, callbacks) ? 'black' : 'red';
  $('#' + labelId).css('color', color);
}

/* return true, if any callback returns true */
function validate(inputId, callback) {
  if (isArray(callback)) {
    for (var i=0; i<callback.length; i++) {
      if (callback[i](inputId)) return true;
    }
    return false;
  } else {
    return callback(inputId);
  }
}

function checkStep3Form() {
  var message = "";
  var ok = true;

  function report(errMessage) {
    message += errMessage + "\n";
    ok = false;
  }

  if (!document.getElementById('terms').checked) report("You must accept terms and conditions in order to continue");
  if (!validate('firstname_input', isNameValid)) report("Firstname must be 2-50 characters long and contain only letters!");
  if (!validate('lastname_input', isNameValid)) report("Last name must be 2-50 characters long and contain only letters!");
  if (!validate('email_input', isEmailValid)) report ("E-mail is not valid!");
  if (!validate('phone_input', isPhoneValid)) report ("Phone number is not valid!");

  $('#firstname_input').keyup();
  $('#lastname_input').keyup();
  $('#email_input').keyup();
  $('#phone_input').keyup();
  $('#terms').change();

  if (!ok) {
        userMessage(message);
  }
  return ok;
}

function checkSearchboxForm() {
  var message = "";
  var ok = true;

  function report(errMessage) {
    message += errMessage + "\n";
    ok = false;
  }
  var d = $('#pickup_day').val();
  var m = $('#pickup_month').val().split('.')[0];
  var y = $('#pickup_month').val().split('.')[1];
  var pd = new Date(y,m-1,d);
  var cd = new Date();

  if (!validate('driver_age', isAgeValid)) report(invalid_driver_age);
  if ($('#pickup_country').val() == "0") report(invalid_pickup_country);
  if ($('#pickup_city').val() == "0") report(invalid_pickup_city);
  if ($('#pickup_location').val() == "0") report(invalid_pickup_location);
  if ($('#dropoff_country').val() == "0") report(invalid_dropoff_country);
  if ($('#dropoff_city').val() == "0") report(invalid_dropoff_city);
  if ($('#dropoff_location').val() == "0") report(invalid_dropoff_location);
  if(pd.getTime() <  cd.getTime() ) 	report(invalid_date);
  

 
  

  
  

  var inputs = ['driver_age', 'pickup_country', 'pickup_city', 'pickup_location', 'dropoff_country', 'dropoff_city', 'dropoff_location'];
  for (var i=0; i<inputs.length; i++) $('#' + inputs[i]).keyup();
  if (!ok) {
        userMessage(message);
  }
  return ok;
}

function checkBookForm(form) {
  var message = "";
  var ok = true;

  function report(errMessage) {
    message += errMessage + "\n";
    ok = false;
  }

  if (!validate('firstname_input', isNameValid)) report("Firstname must be 2-50 characters long and contain only letters!");
  if (!validate('lastname_input', isNameValid)) report("Last name must be 2-50 characters long and contain only letters!");
  if (!validate('email_input', isEmailValid)) report("E-mail is not valid!");
  if (!validate('phone_input', isPhoneValid)) report("Phone number is not valid!");
  if (!validate('address_input', isAddressValid)) report("Address is not valid!");
  if (!validate('city_input', isAddressValid)) report("City is not valid!");
  if (!validate('zip_input', isZipValid)) report("Postal Zip code not valid");
  if (validate('cc_holder', isEmpty)) report("Please enter the cardholders name");
  else if (!validate('cc_holder', isNameValid)) report("Cardholder name is not valid");
  if (validate('cc_number_input', isEmpty)) report("Please enter the credit card number");
  else if (!validate('cc_number_input', isCreditCardNumberValid)) report("Credit card number is not valid");
  //if (validate('cc_cvv2_input', isEmpty)) report("Please enter CVV2");
  //else if (!validate('cc_cvv2_input', isCvv2Valid)) report("CVV2 is not valid");

  if (ok) {
    form.submit();
    return true;
  }

  var inputs = ['firstname_input', 'lastname_input', 'email_input', 'phone_input', 'address_input', 'city_input',
      'zip_input', 'cc_holder', 'cc_number_input'];
  for (var i=0; i<inputs.length; i++) $('#' + inputs[i]).keyup();
  userMessage(message);
  return false;
}

function checkCustomerDataForm() {
  var message = "";
  var ok = true;

  function report(errMessage) {
    message += errMessage + "\n";
    ok = false;
  }
  if (!validate('firstname_input', isNameValid)) report("Firstname must be 2-50 characters long and contain only letters!");
  if (!validate('lastname_input', isNameValid)) report("Last name must be 2-50 characters long and contain only letters!");
  if (!validate('address', isAddressValid)) report("Address is not valid!");
  if (!validate('city', isAddressValid)) report("City is not valid!");
  if (!validate('state', [isAddressValid,isEmpty])) report("State is not valid!");
  if (!validate('zip', [isZipValid,isEmpty])) report("Postal Zip code not valid");
  //if (!validate('country', isAddressValid)) report("Country is not valid!");
  if (!validate('email_input', isEmailValid)) report("E-mail is not valid!");
  if (!validate('telephone_input', [isPhoneValid,isEmpty])) report("Phone number is not valid!");
  if (!validate('cellphone_input', [isPhoneValid,isEmpty])) report("Cell phone is not valid!");
  if (!validate('fax_input', [isPhoneValid,isEmpty])) report("Fax is not valid!");

  if (isEmpty('telephone_input') && isEmpty('cellphone_input')) report("Please provide your contact phone!");

  var inputs = ['firstname_input', 'lastname_input', 'address', 'city', 'state', 'zip', 'country', 'email_input',
        'telephone_input', 'cellphone_input', 'fax_input'];
  for (var i=0; i<inputs.length; i++) $('#' + inputs[i]).keyup();

  if (!ok) {
    userMessage(message);
  }
  return ok;
}