﻿var isDOM = (document.getElementById ? true : false); 
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);
var venueTypes = new Array(
     new Array('Restaurant', 1)
    ,new Array('Bar/Pub', 3)
    ,new Array('Café', 9)
    ,new Array('Conference/Meeting Room', 6)
    ,new Array('Hotel', 2)
    ,new Array('Bed & Breakfast', 7)
    ,new Array('Cinema', 5)
    );

var yourAddrInput = null;
var theirAddrInput = null;
var venTypeDropDown = null;
var modeInput = null;
var submitLnk = null;
var requestURL = 'http://localhost/meethalfway_com/index.aspx';

function getRef(id) 
{
    if (isDOM) return document.getElementById(id);
    if (isIE4) return document.all[id];
    if (isNS4) return document.layers[id];
}

function PopulateVenueTypes()
{
    venTypeDropDown = getRef('mh_vt');
    yourAddrInput = getRef('mh_ya');
    theirAddrInput = getRef('mh_ta');
    modeInput = getRef('mh_mode');
    submitLnk = getRef('mh_submitLnk');
    
    for (var i = 0; i < venueTypes.length; i++)
    {        
        venTypeDropDown.options[i] = new Option(venueTypes[i][0], venueTypes[i][1]);
    } 
}

function ValidateField(obj)
{
    if (obj && obj.value.length > 0) return true;
    else return false;
}

function MeethalfwaySubmit()
{
    var yourInputValid = ValidateField(yourAddrInput);
    var theirInputValid = ValidateField(theirAddrInput);    
    
    if (!isNaN(venTypeDropDown.value))
    {
        if (!yourInputValid && !theirInputValid) alert('Please enter your and their address.');
        else if (!yourInputValid) alert('Please enter your address.');
        else if (!theirInputValid) alert('Please enter their address.');
    }
    else alert('Please select venue type');
    
    if (yourInputValid && theirInputValid)
    {   
        var sURL = requestURL + '?ya='+ yourAddrInput.value + '&ta=' + theirAddrInput.value + '&vt=' + venTypeDropDown.value + '&mode=' + modeInput.value;
        submitLnk.href = sURL;
        submitLnk.target = '_blank';        
        try { submitLnk.click(); }
        catch(e) { window.open(sURL); }
    }
}