﻿///  Global Definite
matchPhone = /\+[0-9]+\s\d+(?=\s\d+)/;
matchEmail = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
matchDate = /^\d{2}\/\d{2}\/\d{4}/; //Match date: mm/dd/yy
matchNumber = /^\d+$/;

argExp = /^(?:[\?]cat=(.*)&name=(.*))/;
///---------------------------------------------------------------

// use JSON

arg = window.location.search;
if(argExp.test(arg) && arg !="")
{
    var url = "http://www.welcomevietnamtours.com/xml2json/TourService.php";
        url += arg;
    getRequest(url);
}
/*else{
    
    var url = "http://www.welcomevietnamtours.com/xml2json/TourService.php";
    
    // send request to xml2jsonService
    if(window.XMLHttpRequest){
    var xml = new XMLHttpRequest();
    }else{
        var xml = new ActiveXObject("MSXML2.XMLHTTP.3.0");
    }

    xml.onreadystatechange = function(){
        if(xml.readyState==4)
        {
            if(xml.responseText){
                addTourList(xml.responseText);
            }else { alert('error'); }
        }
    }
    
    xml.open("GET", url, true);
    xml.send(null);
    
}*/
function getRequest(url)
{
    var xml;
      try
        {
        // Firefox, Opera 8.0+, Safari
        xml=new XMLHttpRequest();
        }
      catch (e)
        {
        // Internet Explorer
        try
          {
          xml=new ActiveXObject("Msxml2.XMLHTTP");
          }
        catch (e)
          {
          try
            {
            xml=new ActiveXObject("Microsoft.XMLHTTP");
            }
          catch (e)
            {
            alert("Your browser does not support AJAX!");
            }
          }
        }

    xml.onreadystatechange = function(){
        if(xml.readyState==4)
        {
            if(xml.responseText){
                addEntries(xml.responseText);
                //addTourList(xml.responseText);
            }else { alert('error'); }
        }
    }
    xml.open("GET", url, true);
    xml.send(null);
}
function getTour(theSelection)
{
    optionCollection = theSelection.options;
    // get selectedindex item
    selectedItem = theSelection.options[theSelection.selectedIndex];
    //alert(selectedItem.text);
    // move to parent node and get category name
    inCategory = selectedItem.parentNode.label;
    var url = "http://www.welcomevietnamtours.com/xml2json/TourService.php";
        url = url + "?cat="+inCategory+"&name="+selectedItem.text;
    getRequest(url);
}

function addEvent(obj, evType, func)
{
    if(obj.addEventListener){ obj.addEventListener(evType, func, false); return true; }
    else if(obj.attachEvent){ var r = obj.attachEvent("on"+evType, func); return r;} else{ return false;}
}
function addTourList(jsondata)
{
    //alert(jsondata);
    jsonObject = eval('('+jsondata+')');
    tourSelection = document.getElementById('gtourname');
    //tourSelection.innerHTML = "";
    if(document.getElementById('gtourname')) // remove now
        document.getElementById('tourlist').removeChild(document.getElementById('gtourname'));
    
    tourSelection = document.createElement("select");
    tourSelection.id = 'gtourname';
    tourSelection.name = 'gtourname';
    
    // feed tour list
    var categories = jsonObject.tourCatalog.tourCategory;
    //alert('jsonObject: '+categories);
    //var str = "<option selected=\"selected\" value=\"No selection\">No selecttion</option>";
    
    noSelectElement = document.createElement("option");
    noSelectElement.selected = true;
    noSelectElement.value = "nonSelection";
    if(noSelectElement.textContent) noSelectElement.textContent = "Select here...";
    else noSelectElement.text = "Select here...";
    
    tourSelection.appendChild(noSelectElement);
    
    for(var cindex = 0; cindex < categories.length; cindex++)
    {
        opGroupElement = document.createElement("optgroup");
        opGroupElement.label = categories[cindex].Name;
        //str += "<optgroup label=\""+categories[cindex].Name+"\">";
        for(var tindex = 0; tindex < categories[cindex].Tour.length; tindex++)
        {
            optionElement = document.createElement('option');
            optionElement.value = categories[cindex].Tour[tindex].Name;
            /*if(optionElement.textContent)
                optionElement.textContent = categories[cindex].Tour[tindex].Name;
            else
                optionElement.innerText = categories[cindex].Tour[tindex].Name;
            */
            optionElement.text = categories[cindex].Tour[tindex].Name;
            opGroupElement.appendChild(optionElement);
        }
            //str += "<option value=\""+categories[cindex].Tour[tindex].Name+"\">"+categories[cindex].Tour[tindex].Name+"</option>";
        tourSelection.appendChild(opGroupElement);
    }
    
    //document.getElementById('gtourname').innerHTML = str;
    //alert(document.getElementById('gtourname').innerHTML);
    document.getElementById('tourlist').appendChild(tourSelection);
}
function addEntries(jsondata)
{
    jsonObject = eval('('+jsondata+')');
    //document.getElementById('tourBooked').innerHTML = "";
    // disable selecttion
        theSelection = document.getElementById('gtourname');
        optionCollection = theSelection.options;
        for(var oindex = 0; oindex < optionCollection.length; oindex ++)
            if(optionCollection[oindex].text.indexOf(jsonObject.tourCatalog.tourCategory.Tour.Name)!=-1) break;
        optionCollection[oindex].selected = true;
        //theSelection.disabled = true; 
    //-------------------
    if(document.getElementById('jsonObjectElement'))
        document.getElementById('tourBooked').removeChild(document.getElementById('jsonObjectElement'));
        
    // create jsonObjectElement
    jsonElement = document.createElement("DIV");
    jsonElement.id = "jsonObjectElement";
    document.getElementById('tourBooked').appendChild(jsonElement);
    document.getElementById('tourBooked').style.border = "1px dashed blue";
    
    var str ="<table cellpadding='2' cellspacing='0' algin='100%'>";
    str += "<tr><td colspan='2'><span style='font-size: 12pt; color: blue; font-weight: bold;'>"+jsonObject.tourCatalog.tourCategory.Name+" / </span><span style='font-size: 10pt; color: highlight;'>"+jsonObject.tourCatalog.tourCategory.Tour.Name+"</span></td><tr>";
    str += "<tr><td rowspan='3' valign='middle' style='width: 130px;' align='center'><img src='"+jsonObject.tourCatalog.tourCategory.Tour.Image+"' width='140' /></td>";
    str += "<td valign='top' ><div class='itinerary'><strong>Itinerary: </strong><span class='itinerary'>"+jsonObject.tourCatalog.tourCategory.Tour.Itinerary+"</span></td></tr>";
    str += "<tr><td></td></tr>";
    str += "<tr><td><strong>Length: </strong><span class='itinerary'>"+jsonObject.tourCatalog.tourCategory.Tour.Length+"</span></div></td></tr>";
    str += "</table>";
    
    document.getElementById('jsonObjectElement').innerHTML = str;
    document.getElementById('categoryname').value = jsonObject.tourCatalog.tourCategory.Name;
    document.getElementById('tourname').value = jsonObject.tourCatalog.tourCategory.Tour.Name;
    document.getElementById('tourimage').value = jsonObject.tourCatalog.tourCategory.Tour.Image;
    document.getElementById('touritinerary').value = jsonObject.tourCatalog.tourCategory.Tour.Itinerary;
    document.getElementById('tourlength').value = jsonObject.tourCatalog.tourCategory.Tour.Length;
}
function formValidation(theForm)
{
    //if(document.getElementById('gtourname'))
        //alert(document.getElementById('gtourname').value);
    //alert(theForm.gtourname.value);
    if(theForm.gtourname.value == "nonSelection"){
        alert('Please select tour program');
        return false;
    }
    if(theForm.gname.value =="")
    {
        alert('Please Enter Your Name');
        theForm.gname.focus();
        return false
    }
    if(theForm.gaddress.value == "")
    {
        alert('Please Enter Your Address');
        theForm.gaddress.focus();
        return false;
    }
    if(theForm.gworkphone.value == "")
    {
        alert('Please Enter Your Workphone');
        theForm.gworkphone.focus();
        return false;
    }//else{
     //   var thePhone = theForm.gworkphone.value;
     //   if(!thePhone.match(matchPhone)){
     //       alert('Please Enter Valid Phone Number');
     //       theForm.gworkphone.focus();
     //       return false;    
     //   }
    //}
    //if(theForm.ghandphone.value != "")
    //{
    //    var thePhone = theForm.ghandphone.value;
    //    if(!thePhone.match(matchPhone)){
    //        alert('Please Enter Valid Phone Number');
    //        theForm.ghandphone.focus();
    //        return false;    
    //    }
    //}
    //if(theForm.gfax.value != "")
    //{
    //    var thePhone = theForm.gfax.value;
    //    if(!thePhone.match(matchPhone)){
    //        alert('Please Enter Valid Fax Number');
    //        theForm.gfax.focus();
    //        return false;
    //    }
    //}
    if(theForm.gemail.value == "")
    {
        alert('Please Enter Your Email');
        theForm.gemail.focus();
        return false;
    }else{
        var theEmail = theForm.gemail.value;
        //alert(theEmail.match(matchEmail));
        if(!theEmail.match(matchEmail)){
            alert('Please Enter Valid Email Address');
            theForm.gemail.focus();
            return false;
        }
    }
/*    if(theForm.gduration.value =="")
    {
        alert('Please Enter Duration');
        theForm.gduration.focus();
        return false;
    }else{
        if(!matchNumber.test(theForm.gduration.value)){
            alert('Please Enter Valid Number');
            theForm.gduration.focus();
            return false;
        }
    }*/
    if(theForm.gadult.value =="")
    {
        alert('Please Enter Adult');
        theForm.gadult.focus();
        return false;
    }else{
        if(!matchNumber.test(theForm.gadult.value)){
            alert('Please Enter Valid Number');
            theForm.gadult.focus();
            return false;
        }
    }
    if(theForm.gchildren.value =="")
    {
        alert('Please Enter Children');
        theForm.gchildren.focus();
        return false;
    }else{
        if(!matchNumber.test(theForm.gchildren.value)){
            alert('Please Enter Valid Number');
            theForm.gchildren.focus();
            return false;
        }
    }
    if(theForm.gachildren.value =="")
    {
        alert('Please Enter Age of Children');
        theForm.gachildren.focus();
        return false;
    }else{
        if(!matchNumber.test(theForm.gachildren.value)){
            alert('Please Enter Valid Number');
            theForm.gachildren.focus();
            return false;
        }
    }
    return true;
}