var reg            = /\w/;
var regInt         = /^\d+$/;
var currentPage    = '';
var mnuSelected    = '';

/* the following js path settings have been moved to common.php*/
// SiteAction settings...
var site            = 'http://www.wheelsabroad.com/';
var secure          = 'https://www.wheelsabroad.com/secure/';
// SB local settings...
//var site            = 'http://localhost/wheelsabroad/';
//var secure          = 'http://localhost/wheelsabroad/secure/';

// code to disguise email addresses...
var name1 = "service";
var name2 = "bookdesk";
var name3 = "webmaster";
var name4 = "sbicknell";
var at = "&#064";
var dom = "wheelsabroad.";
var ext = "com";

function openHelpPage(pageName,country_code){	// for example = goOneWays, goCDW

	var goToPage = site + pageName +'.php?country_code='+country_code;

	pageName=window.open(goToPage,pageName,"toolbar=no,top=20,left=20,width=820,status=no,scrollbars=yes,resizable=yes,menubar=no");
	pageName.focus();
}

function openWebLinks()
{
	webLinks=window.open("web_links.php", 
	"web_links","top=20,left=20,width=800,height=200,status=no,scrollbars=yes,resizable=yes,toolbar=yes,menubar=yes");
	webLinks.focus();
}

var sitemap        = 'n';

function showSiteMap()
{
   document.getElementById('siteMap').style.visibility = "visible";
   sitemap = 'y';
}

function hideSiteMap()
{
   document.getElementById('siteMap').style.visibility = "hidden";
   sitemap = 'n';
}

function toggleSiteMap()
{
   if   ( sitemap == 'n')
        { showSiteMap(); }
   else { hideSiteMap(); }
}

function priceBookLink(country,region,location)
{
   if   ( document.getElementById('priceBookPrice').checked )
        { var linkPage = 'price' }
   else { var linkPage = 'book'; }
   if   ( linkPage == 'price' )
        {
        if      ( country < 0 )
                { var tempLink = site + 'price_check.php?country_id=1'; }					 
        else if ( region < 0 )
                { var tempLink = site + 'price_check.php?country_id=' + country; }
        else if ( location < 0 )
                { var tempLink = site + 'price_check.php?country_id=' + country + '&region_id=' + region; }			
        else    { var tempLink = site + 'price_check.php?country_id=' + country + '&region_id=' + region + '&location_id=' + location; }
        }
   else {
        if      ( country < 0 )
                { var tempLink = secure + 'new_booking.php?country_id=1'; }			
        else if ( region < 0 )
                { var tempLink = secure + 'new_booking.php?country_id=' + country; }			
        else if ( location < 0 )
                { var tempLink = secure + 'new_booking.php?country_id=' + country + '&sub_section=region&region_id=' + region; }				
        else    { var tempLink = secure + 'new_booking.php?country_id=' + country + '&sub_section=location&location_id=' + location; }
        }												
   document.location = tempLink; 
}	

function bookmark()
{
   var url         = location.href;
   var description = 'WheelsAbroad.com';
   if   ( navigator.appName=='Microsoft Internet Explorer')
        { window.external.AddFavorite(url, description); }
   else { if (navigator.appName=='Netscape') { alert("Click OK and then hit CTRL+D to add a bookmark to this site."); } }
}

function mailpage()
{
mail_str = "mailto:?subject=Check out WheelsAbroad.com car rental service";
mail_str += "&body=I thought this car rental site (http://www.wheelsabroad.com) might be of interest for your trip.";
location.href = mail_str;
}

function openBePatient()
{
bePatient=window.open("bePatient.html","bePatient","toolbar=no,top=20,left=20,width=330,height=180,status=no,scrollbars=no,resizable=no,menubar=no");
   bePatient.focus();
}

function changeCountry()
{
   if ( document.country.countryPage.options[document.country.countryPage.selectedIndex].value != 'currentPage' )
      { document.location = document.country.countryPage.options[document.country.countryPage.selectedIndex].value; }
}

function showMenu(menu)
   {
   hideMenu(mnuSelected);
   document.getElementById(menu).style.visibility = 'visible';
   mnuSelected = menu;
   }

function hideMenu(menu)
   {
   if (mnuSelected!='') { document.getElementById(menu).style.visibility = 'hidden'; }
   }

function init()
   {
//   document.getElementById('mnuAbout').style.left = document.getElementById('index').style.left;
   }

// Sandeep V. Tamhankar (stamhankar@hotmail.com)

function emailCheck (emailStr)
{
var emailPat     = /^(.+)@(.+)$/;
var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
var validChars   = "\[^\\s" + specialChars + "\]";
var quotedUser   = "(\"[^\"]*\")";
var ipDomainPat  = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom         = validChars + '+';
var word         = "(" + atom + "|" + quotedUser + ")";
var userPat      = new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat    = new RegExp("^" + atom + "(\\." + atom +")*$");

var matchArray = emailStr.match(emailPat);
if ( matchArray == null )
   {
   alert("Email address seems incorrect (check @ and .'s)");
   return false;
   }
   
var user   = matchArray[1];
var domain = matchArray[2];

if ( user.match(userPat) == null)
   {
   alert("The username doesn't seem to be valid.");
   return false;
   }

var IPArray = domain.match(ipDomainPat);
if ( IPArray != null )
   {
   for ( var i = 1; i <= 4; i++ )
       {
       if ( IPArray[i] > 255 )
          {
          alert("Destination IP address is invalid!");
          return false;
          }
       }
   return true;
   }

var domainArray=domain.match(domainPat)
if (domainArray==null)
   {
   alert("The Email domain name doesn't seem to be valid.");
   return false;
   }

return true;
}

function isValidDate(year,month)
{
   var now              = new Date();
   var m                = 1 * now.getMonth() + 1;
   var y                = 1 * now.getYear();
   if ( y < 1000 ) { y += 1900; }
   var currentYearMonth = y * 100 + m;
   var expiryYearMonth  = '20' + year + '' + month;
   currentYearMonth     = 1 * currentYearMonth;
   expiryYearMonth      = 1 * expiryYearMonth;
   if ( currentYearMonth > expiryYearMonth ) { return false; }
   return true;
}

function CheckCC(CC_Num,CC_Type)
    {
    var CCN_digits = CC_Num;
    var validcard  = false;
    var msgind     = 0;

    switch(CC_Type)
       {
       case "A":
          if   ( CCN_digits.length == 15 )
               {
               if   ( ( CCN_digits.substring (0, 2) == "34" ) || ( CCN_digits.substring (0, 2) == "37" ) )
                    { validcard = true; }
               else { msgind = 1; }
               }
          else { msgind = 2; }
          CC_Type = 'American Express';
       break;
       case "M":
          if   ( CCN_digits.length == 16 )
               {
               if   ( ( CCN_digits.substring (0, 2) >= "51" ) && ( CCN_digits.substring (0, 2) <= "55" ) )
                    { validcard = true; }
               else { msgind = 1; }
               }
          else { msgind = 2; }
          CC_Type = 'Master Card';
       break;
       case "V":
          if   ( ( CCN_digits.length == 16 ) || ( CCN_digits.length ==13 ) )
               {
               if   ( CCN_digits.substring (0, 1) == "4" )
                    { validcard = true; }
               else { msgind = 1; }
               }
          else { msgind = 2; }
          CC_Type = 'Visa';
       break;
       default:
           msgind = 3;
       break;
       }

    if ( !validcard )
       {
       switch(msgind)
          {
          case 1:
             alert ("The Card Number ("+CC_Num + ") and the Card Type (" + CC_Type + ") do not match.");
          break;
          case 2:
             alert ("The Card Number (" + CC_Num + ") is not the right length for the Card Type (" + CC_Type + ").");
          break;
          default:
             alert ("Sorry, "+ CC_Type + " is not currently being accepted - please use American Express, Master Card or Visa.");
          break;
          }
       return validcard;
       }
    var CheckSum = 0;
    for ( var x = 1; x <= CCN_digits.length; x++ )
        {
        var CurrentDigit = CCN_digits.charAt(CCN_digits.length - x);
        if   ( x % 2 == 0 )
             {
             var WorkDigit = CurrentDigit * 2;    
             if   ( WorkDigit > 9 )
                  { 
                  CheckSum = CheckSum + (1 - 0);
                  CheckSum = CheckSum + (WorkDigit % 10);
                  }
             else { CheckSum = CheckSum + (WorkDigit - 0); }     
             }
        else { CheckSum = CheckSum + (CurrentDigit - 0); }
        }

    if ( CheckSum % 10 ) 
       { 
       validcard = false; 
       alert ("I'm sorry, the Card Number ("+ CC_Num +") is not correct - perhaps there is a typo or two numbers are reversed?"); 
       } 
    return validcard; 
    }

function isEmpty(formName,field,name)
   {
   var docForm      = document[formName];
   var docFormField = docForm[field];
   if ( docFormField )
      {
      if ( !docFormField.value.match(reg) )
         {
         alert(name + ' must be entered.' );
         docFormField.focus();
         docFormField.select();
         return true;
         }
      }
   return false;
   }

function isInt(formName,field,name)
   {
   var docForm      = document[formName];
   var docFormField = docForm[field];
   if ( docFormField )
      {
      if ( !docFormField.value.match(regInt) )
         {
         alert(name + ' must be an integer.' );
         docFormField.focus();
         docFormField.select();
         return false;
         }
      }
   return true;
   }

function isSelected(formName,field,name)
{
   var docForm      = document[formName];
   var docFormField = docForm[field];
   if ( docFormField )
      {
      if ( docFormField.options[docFormField.selectedIndex].value == '' )
         {
         alert(name + ' must be selected.' );
         docFormField.focus();
         return false;
         }
      }
   return true;
}
