﻿var form_submitted = false;

function showSubForm(divName)
{
  var cntrl = document.getElementById(divName);        
  if (cntrl)
  {    
    cntrl.style.display = 'block';   
  };       
};

function SubmitForm()
{
  if (form_submitted)
  {
    alert ( "Your request has already been submitted. Please wait..." );
    return false;
//  }
//  else
//  {
//    //form_submitted = true;
//    return true;
  };
};

function isValidEmail(str) {    
  var iPos = str.indexOf("@");
  var iPos2;

  if (str.search(/[^A-Za-z0-9@._\- ]/) != -1) return false;
  
  if (str.indexOf("@",iPos + 1) != -1)
  {
    return false;
  };
  
  if (iPos > 0){
    iPos2 = str.lastIndexOf(".") 
    if (iPos2 > iPos){
      if (str.length - iPos2 > 2)
      {
        return true;
      } else {
        return false;
      };
    };
  };  

return false;
};

function rightTrim(strText){
  return(strText.replace(new RegExp("[\\s]+$", "gm"), ""));
}

/* 0 = dates are the same, 1 = date1 > date2, -1 = date1 < date2 */
function compareDates(_date1,_date2)
{
  var one_day=1000*60*60*24;
  var days = (_date1.getTime()-_date2.getTime());
  days = Math.round(days/one_day);
  
  if (days == 0){
    return 0;
  } else if (days > 0){
    return 1;
  } else if (days < 0){
    return -1;
  };
};

function checkDateSelectRows(index, colorid, msg) {
  var ccntrl = document.getElementById(colorid);
  var dobMonth = document.getElementById('slDOBMonth' + index);
  var dobDay = document.getElementById('slDOBDay' + index);
  var dobYear = document.getElementById('slDOBYear' + index);
  var cnt = 0;

  if (dobMonth && dobDay && dobYear) {
    if (dobMonth.options[dobMonth.selectedIndex].value == 'DEF') cnt++;
    if (dobDay.options[dobDay.selectedIndex].value == 'DEF') cnt++;
    if (dobYear.options[dobYear.selectedIndex].value == 'DEF') cnt++;

    if (cnt > 0) {
      if (ccntrl) ccntrl.style.color = 'red';
      if (msg) alert(msg);

      return 1;
    }

    if (ccntrl) ccntrl.style.color = '';
    return 0;
  };
  return 0;
};

function checkDateField(id, colorid, len, msg) 
{
  var cntrl = document.getElementById(id);
  var ccntrl = document.getElementById(colorid);
  var dtBuf = null;
  var buf = '';

  if (cntrl) {
    buf = rightTrim(cntrl.value);
    if (cntrl.disabled == true) return 0;

    if (buf == "") {
      if (ccntrl) {
        ccntrl.style.color = 'red';
        if (msg) alert(msg);
      };
      return 1;
    }    
    else {
      if (len != null) {
        if (buf.length < parseInt(len)) {
          if (ccntrl) {
            ccntrl.style.color = 'red';
            if (msg) alert(msg);
          };
          return 1;
        };
      }
    
      try {
        dtBuf = Date.parse(cntrl.value);
        if (isNaN(dtBuf)) {
          if (ccntrl) {
            ccntrl.style.color = 'red';
            if (msg) alert(msg);
          };
          return 1;
        }
        else {
          if (compareDates(new Date(cntrl.value), new Date()) == 1) {
            if (ccntrl) {
              ccntrl.style.color = 'red';
              if (msg) alert(msg);
            };
            return 1;
          };
        };
      }
      catch (ex) {
        if (ccntrl) {
          ccntrl.style.color = 'red';
          if (msg) alert(msg);
        };
        return 1;
      };
            
      if (ccntrl) ccntrl.style.color = '';
      return 0;     
    };
  }
  else {
    if (ccntrl) ccntrl.style.color = '';
    return 0;
  };  
};

function checkField(id,colorid,len,msg)
{  
  var cntrl = document.getElementById(id);
  var ccntrl = document.getElementById(colorid);
  var buf = '';

  if (cntrl)
  {    
    buf = rightTrim(cntrl.value);
    if (cntrl.disabled == true) return 0;
    
    if (buf == "")
    {  
      if(ccntrl){
        ccntrl.style.color = 'red';
        if(msg)ccntrl.value = msg;
      };
      return 1;
    }
    else
    {
      if (len != null){        
        if (buf.length >= parseInt(len)){
          if(ccntrl)ccntrl.style.color = '';
          return 0;                
        }
        else
        {
          if(ccntrl){
            ccntrl.style.color = 'red';
            if(msg)ccntrl.value = msg;
          };
          return 1;
        };      
      }
      else
      {
        if(ccntrl)ccntrl.style.color = '';
        return 0;
      };
    }; 
  }
  else {
    if (ccntrl) ccntrl.style.color = '';
    return 0;       
  };  
};

function autoMask(field, event, sMask) {
  var KeyCode = getKeyCode(event);
  var KeyTyped = String.fromCharCode(KeyCode);
  var targ = getTarget(event);
  var isValid = false;
  keyCount = targ.value.length;
  
  if (KeyCode < 32)
  {
    return true;
  };
  
	if(keyCount == sMask.length)
	{
	  return false;
	};
	
	if ((sMask.charAt(keyCount) == '#') && isNumeric(KeyTyped))
  {
    isValid = true;
  }  
  else if ((sMask.charAt(keyCount) == 'A') && isAlpha(KeyTyped))
  {
    isValid = true;
  };
	
	if ((sMask.charAt(keyCount+1) != '#') && (sMask.charAt(keyCount+1) != 'A' ) && isValid)
  {    
    field.value = field.value + KeyTyped + sMask.charAt(keyCount+1);
    return false;
  }
  else if (isValid)
  {
    return true;
  };
  	
  if (sMask.charAt(keyCount) == '*')
  {    
    return true;
  };
   
  if ((sMask.charAt(keyCount+1) == '?') )
  {
    field.value = field.value + KeyTyped + sMask.charAt(keyCount+1);
    return true;
  };
    
  return false;
};

function getTarget(e) {
 if (e.srcElement) {
      return e.srcElement;
 };
 if (e.target) {
      return e.target;
 };
};

function getKeyCode(e) {
  if (e.srcElement) {  
   return e.keyCode
  };

  if (e.target) {  
   return e.which
  };
};

 function isNumeric(c)
{
  var sNumbers = "01234567890";
  if (sNumbers.indexOf(c) == -1){
          return false;
  }
  else {return true;
  };
};

function isAlpha(c)
{
  var lCode = c.charCodeAt(0);
  if (lCode >= 65 && lCode <= 122 )
    {
          return true;
   }
  else{
    return false;
  };
};

function isPunct(c)
{
  var lCode = c.charCodeAt(0);
  if (lCode >= 32 && lCode <= 47 )
    {
          return true;
   }
  else{  
    return false;
  };
};

function createCookie(name,value,days,mins)
{
  var now = new Date();
	
	if (days != 0)
	{
		now.setTime(now.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+now.toGMTString();		
	};
 
  if (mins != 0)
	{	  
	  var minutes = now.getMinutes();
	  minutes = minutes + mins;
	  now.setMinutes(minutes);
	  var expires = "; expires="+now.toGMTString();	  
	};
	
	if (mins == 0 && days == 0){	
	  var expires = "";
	};
	
	document.cookie = name+"="+value+expires+"; path=/";	
};

function readCookie(name)
{
	var nameEQ = name + "=";	
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	};
	return null;
};

function eraseCookie(name)
{
	createCookie(name,"",0,0);
};

function CallCalendar(tripType,ele,sourceEle,dateOffsetInDays,setFocusTo)
{
  var cntrl;  
  var ttype;

  for( i = 0; i < tripType.length; i++ )
  {
    if( tripType[i].checked == true )
    ttype = tripType[i].value;
  }
    
  if (ttype != '')
  {
    if (ttype == 'openJaw')
    {        
      cntrl = document.frmGui.txtDepartOn2;
    }
    else if (ttype == 'oneWay')
    {
      setFocusTo = document.frmGui.selNumberOfAdults;
    }
    else if (ttype == 'roundTrip')
    {      
      cntrl = document.frmGui.txtReturnOn;
    };   
    scwShowSetOffsetNext(ele, sourceEle,cntrl,dateOffsetInDays,setFocusTo,ttype,'departOnYear1');
  };
};

function formatToTwoDecimals(value)
{
  var pos = value.indexOf('.');
  
  if (pos == -1) return value + '.00';
  
  var valueLen = value.substring(pos + 1).length;
                                        
  if (valueLen == 1)
  {
    value = value + '0';
  }
  else if (valueLen > 2)
  {
    value = value.substring(0,pos + 3);  
  };
  
  return value;

};

function addLeadingZero(value)
{  
  value = value + '';
  var len = 2 - value.length;
  
  for (i=0;i<=len-1;i++)
  {    
    value = "0" + value;  
  };
  
  return value;

};