var modalWindow2 = {
  parent:"body",
  windowId:null,
  content:null,
  width:null,
  height:null,
  close:function()
  {
    $(".modal-window").remove();
    $(".modal-overlay").remove();
  },
  open:function()
  {
    var modal = "";
    modal += "<div class=\"modal-overlay\"></div>";
    modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
    modal += this.content;
    modal += "</div>";  

    $(this.parent).append(modal);

    $(".modal-window").append(""); //<a class=\"close-window\"></a>
    $(".close-window").click(function(){modalWindow.close();}); //
    $(".modal-overlay").click(function(){modalWindow.close();}); //
  }
};


var modalWindow = {
  parent:"body",
  windowId:null,
  content:null,
  width:null,
  height:null,
  close:function()
  {
    $(".modal-window").remove();
    $(".modal-overlay").remove();
  },
  open:function()
  {
    var modal = "";
    modal += "<div class=\"modal-overlay\"></div>";
    modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
    modal += this.content;
    modal += "</div>";  

    $(this.parent).append(modal);

    $(".modal-window").append("<a class=\"close-window\"></a>"); //
    $(".close-window").click(function(){modalWindow.close();}); //
    $(".modal-overlay").click(function(){modalWindow.close();}); //
  }
};

var openMyModal = function(source)
{
  modalWindow.windowId = "myModal";
  modalWindow.width = 340;
  modalWindow.height = 240;
  modalWindow.content = "<iframe border='10' style='border:10px sold #000000;' width='340' height='240' frameborder='0' scrolling='no' allowtransparency='true' src='" + source + "'>&lt/iframe>";
  modalWindow.open();
};

var openUnknownRecordModal = function()
{
  modalWindow2.windowId = "unknownModal";
  modalWindow2.width = 320;
  modalWindow2.height = 240;
  modalWindow2.content = "<iframe border='10' style='border:10px sold #000000;' width='320' height='240' frameborder='0' scrolling='no' allowtransparency='true' src='UnknownRecord.cfm'>&lt/iframe>";
  modalWindow2.open();
};

var openDynaModal = function(source, ht, wd)
{
  modalWindow.windowId = "myModal";
  modalWindow.width = wd;
  modalWindow.height = ht;
  modalWindow.content = "<iframe border='10' style='border:10px sold #000000;' width='" + wd + "' height='" + ht + "' frameborder='0' scrolling='no' allowtransparency='true' src='" + source + "'>&lt/iframe>";
  modalWindow.open();
};

var openEditConfirmModal = function(s)
{
	w= 320;
	h=240;
	if(s.length > 80)
	{
		w= 400;
    h=300;
	}
	
	if(s.length > 120)
  {
    w= 440;
    h=330;
  }
  modalWindow2.windowId = "editConfirmModal";
  modalWindow2.width = w;
  modalWindow2.height = h;
  modalWindow2.content = "<iframe border='10' style='border:10px sold #000000;' width='" + w + "' height='" + h + "' frameborder='0' scrolling='no' allowtransparency='true' src='EditConfirm.cfm?s=" + s + "'>&lt/iframe>";
  modalWindow2.open();
};
  
function setFocus(s)
{
  try
  {
    document.getElementById(s).select();
    document.getElementById(s).focus();
  }catch(err)
  {
    alert("exception caught")
  }
}

// initiaze helper functions
function _isNumber(s){
  var i;
  for (i = 0; i < s.length; i++){   
      // Check that current character is number.
      var c = s.charAt(i);
      if (((c < "0") || (c > "9"))) return false;
  }
  // All characters are numbers.
  return true;
}

  
function isAlphaNumeric(param)
{
  var valid = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var charIdx;
  var result = true;
  if (param.length == 0) return false;
  for (i = 0; i < param.length && result == true; i++)
  {
    charIdx = param.charAt(i);
    if (valid.indexOf(charIdx) == -1)
      result = false;
  }
    
  return result;
}

function isUpperAlpha(param)
{
  var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var charIdx;
  var result = true;
  if (param.length == 0) return false;
  for (i = 0; i < param.length && result == true; i++)
  {
    charIdx = param.charAt(i);
    if (valid.indexOf(charIdx) == -1)
      result = false;
  }
    
  return result;
}

function isWholeNumber(param)
{

  var valid = "0123456789";
  var charIdx;
  var result = true;

  if (param.length == 0) return false;
  
  for (i = 0; i < param.length && result == true; i++){
    charIdx = param.charAt(i);
    if (valid.indexOf(charIdx) == -1)
      result = false;
  }
  
  return result;
}

function isValidEmail(str){
  // slice into pieces based on the @ sign
  // second piece should contain only 1 period  
  var strAr = new Array();
  strAr= str.split('@');
  if(strAr.length != 2)
    return false;
  else
  {
    // domain section must be at least 4 characters long
    if(strAr[1].length < 4)
      return false;
    else
     return true;
  }
} // end function



function getInternetExplorerVersion()
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function isCrap()
{
  IEVersion = getInternetExplorerVersion();
  if(IEVersion == -1)
    return false;
    
  if(getInternetExplorerVersion() < 7)
    return true;
  else
    return false;
}

function reportError(s)
{
  message = escape(s);
  source = "alert.cfm?s=" + message;
  openMyModal(source);
}

function reportErrorDynamicallySized(s, ht, wd)
{
  message = escape(s);
  source = "alert.cfm?s=" + message;
  openDynaModal(source,ht, wd);
}

function cleanPhone(s)
{
  var cleanChars = "()- ";
  var newString = "";
  for(var i =0; i < s.length;i++ ){
    if (cleanChars.indexOf(s.charAt(i)) == -1) {
       newString += s.charAt(i); 
    }
  }
  return newString;
}
  
function cleanAmount(s){
  var cleanChars = "$, ";
  var newString = "";
  for(var i =0; i < s.length;i++ ){
    if (cleanChars.indexOf(s.charAt(i)) == -1) {
       newString += s.charAt(i); 
    }
  }
  return newString;
}


function stripCharsInBag(s, bag){
  var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function _daysInFeb (year){
// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
  return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
  
function isDate(val){
  return _isDate(val);
}
function _isDate(val){
 
  
 // intialize VARS
  var dtCh= "/";
  var minYear=1920;
  var maxYear=2100;
  
  var daysInMonth = new Array();
  daysInMonth[0] = 0;
  daysInMonth[1] = 31;
  daysInMonth[2] = 29;
  daysInMonth[3] = 31;
  daysInMonth[4] = 30;
  daysInMonth[5] = 31;
  daysInMonth[6] = 30;
  daysInMonth[7] = 31;
  daysInMonth[8] = 31;
  daysInMonth[9] = 30;
  daysInMonth[10] = 31;
  daysInMonth[11] = 30;
  daysInMonth[12] = 31;
  
  var pos1=val.indexOf(dtCh)
  var pos2=val.indexOf(dtCh,pos1+1)
  var strMonth=val.substring(0,pos1)
  var strDay=val.substring(pos1+1,pos2)
  var strYear=val.substring(pos2+1)
  strYr=strYear
  
  if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
  if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
  for (var i = 1; i <= 3; i++) {
    if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
  }
  
  month=parseInt(strMonth)
  day=parseInt(strDay)
  year=parseInt(strYr)
  
  if (pos1==-1 || pos2==-1){
    return false
  }
  if (month<1 || month>12){
    return false
  }
  if (day<1 || day>31 || (month==2 && day> _daysInFeb(year)) || day > daysInMonth[month]){
    return false
  }
  if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
    return false
  }
  if (val.indexOf(dtCh,pos2+1)!=-1 || _isNumber(stripCharsInBag(val, dtCh))==false){
    return false
  }
return true
}

// common error message
var requiredHCADCodeMessage = "HCAD ID is a required field.";
var invalidHCADCodeMessage ="HCAD ID appears to be invalid.[br]   HCAD IDs should be 9 digits.";
var requiredVINMessge = "VIN is a required Field.";
var dupMessage = 'Our records indicate that a redemption has already been received for your VIN or HCAD ID.[br] If you have not submitted a previous redemption, please check your information closely and re-enter it, or use the "Click here if you do not know your HCAD ID" link to enter all your information. [br]If you have already submitted a previous request, please allow four to eight weeks for processing.';
var inProcessingMessage = "We are currently processing your claim.  Please allow four to eight weeks for delivery.";
var badRODate = "Repair Order Date appears to be invalid. Service must be completed in October of 2011.";
var badVInMessage = 'Your VIN does not match the HCAD ID we have on record.[br]Please check your VIN and re-enter your information, or use the "Click here if you do not know your HCAD ID" link to enter all your information.';



