﻿// trim the front and end
function trim(str) {
  return str.replace(/^\s+|\s+$/g, "");
}

// is this string an empty string
function isEmptyString(str) {
  if (trim(str) == "") {
    return true;
  }
  else return false;
}

//does this str has minmum required length,lenght has to bigger then 0
function hasMinimumRequiredLength(str, len) {
  if (isEmptyString(str)) {
    return false;
  }
  if (trim(str).length >= len) {
    return true;
  }
  else return false;
}

//does the str contain white space inside
function hasWhiteSpace(str) {

  if (str.indexOf(' ') != -1) {
    return true;
  }
  else return false;
}

//is this a valid email address
function isValidEmail(str) {
  var newStr = trim(str);
  var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w-]+\\.)+[\\w]+[\\w]$";
  // \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
  var regex = new RegExp(emailReg);
  return regex.test(newStr);
}

//is this a valid URL
function isValidUrl(str) {
  var newStr = trim(str);
  var urlReg = "http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
  var regex = new RegExp(urlReg);
  return regex.test(newStr);
}

//is this a valid US phone number
function isValidUsPhone(str) {
  var newStr = trim(str);
  var phoneReg = /((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}$/;
  return phoneReg.test(newStr);
}

//is this a valid us zip code
function isValidUsZip(str) {
  var newStr = trim(str);
  var zipReg = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
  return zipReg.test(newStr);
}

//is this a valid number
function isValidNumber(str) {
  var newStr = trim(str);
  var iNum = new Number(newStr);
  return (!isNaN(iNum));
}

//is this a positive number
function isPositive(str) {
  return (!isNaN(str) && parseInt(str) > 0);
}

//is this a negative number
function isNegative(str) {
  return (!isNaN(str) && parseInt(str) < 0);
}

//is this a negative number or zero
function isNonPositive(str) {
  return (!isNaN(str) && parseInt(str) <= 0);
}

//is this a positive number or zero
function isNonNegative(str) {
  return (!isNaN(str) && parseInt(str) >= 0);
}


//is this a valid date in mm/dd/yyyy format
function isValidDate(str) {
  if (isEmptyString(str)) return false;
  str = trim(str);
  var monthfield = str.split("/")[0]
  var dayfield = str.split("/")[1]
  var yearfield = str.split("/")[2]
  var dayobj = new Date(yearfield, monthfield - 1, dayfield)
  if ((dayobj.getMonth() + 1 != monthfield) || (dayobj.getDate() != dayfield) || (dayobj.getFullYear() != yearfield))
    return false;
  else
    return true;
}
//strDate must be in mm/dd/YYYY format;
function createDate(strDate) {
  return new Date(strDate.split('/')[2], strDate.split('/')[0] - 1, strDate.split('/')[1]);
}

//both date in mm/dd/YYYY format;
function dateCompare(date1, date2) {
  var d1 = createDate(date1);
  var d2 = createDate(date2);
  if (d1 < d2) { //d2 > d1
    return 1;
  }
  else if (d1 == d2) { //d2==d1
    return 0;
  }
  else   //d2 < d1
    return -1;
}

function cboeCheckTextFieldEmpty(txtFieldName, txtErrorMessage) {
  txtField = document.getElementById(txtFieldName);

  if (isEmptyString(txtField.value)) {
    alert(txtErrorMessage);
    txtField.focus();
    return false;
  }
  else {
    return true;
  }
}

function cboeCheckTextFieldPhoneUS(txtFieldName, txtErrorMessage) {
  txtField = document.getElementById(txtFieldName);

  if (isValidUsPhone(trim(txtField.value))) {
    return true;
  }
  else {
    alert(txtErrorMessage);
    txtField.focus();
    return false;
  }
}

function cboeCheckTextFieldZipUS(txtFieldName, txtErrorMessage) {
  txtField = document.getElementById(txtFieldName);

  if (isValidUsZip(trim(txtField.value))) {
    return true;
  }
  else {
    alert(txtErrorMessage);
    txtField.focus();
    return false;
  }
}

function cboeCheckComboBoxField(txtFieldName, txtDefaultText, txtErrorMessage) {
  txtField = document.getElementById(txtFieldName);

  if (txtField.value == txtDefaultText) {
    alert(txtErrorMessage);
    txtField.focus();
    return false;
  }
  else {
    return true;
  }
}
/*  get the value of a key in the query string if not there returns 
empty string or the _default value passed in
*/
function getQuerystringValue(key, default_) {
  if (default_ == null) default_ = "";
  key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if (qs == null)
    return default_;
  else
    return qs[1];
}

function formatFieldForAjaxPost(val) {
  return escape(val);
}

