var sessionId = "";

var curSection = "PROFILE";
var nextSection = "";

var aoSaveProfile = null;
var aoSaveRecommendation = null;
var aoSavePhotoCap = null;
var aoRemovePhoto = null;
var aoSaveTraveler = null;
var aoRemoveTravPhoto = null;
var aoCheckPastProfile = null;
var aoLoadPastProfile = null;
var aoRefreshCity = null; 


var lastCity = "";

function showLabel(s, t) {
  document.getElementById("divSaveDraft" + t).style.display = (s==1? "" : "none");
}

function showCover(s) {
  document.getElementById("divCover").style.display = (s==1? "" : "none"); 
}

function validate() {
  //profile information check
  if (!validate_field(document.getElementById("pName"), "Name", TEXT) ||
      !validate_field(document.getElementById("pAge"), "Age", TEXT) ||
      !validate_field(document.getElementById("pEmail"), "Email", EMAIL) ||
      !validate_field(document.getElementById("pEmail2"), "Confirm Email", EMAIL))  {
    if (curSection != "PROFILE")  {
      showProfile();
    }
    return false;      
  }
  
  //email and confirm email
  if (document.getElementById("pEmail").value != document.getElementById("pEmail2").value) {
    alert("Please type in correct confirmed email");
    if (curSection != "PROFILE")  {
      showProfile();
    }
    document.getElementById("pEmail2").focus();
    return false; 
  }
  
  //recommendation check
  if (!validate_field(document.getElementById("rSubject"), "recommendation", TEXT) ||
      !validate_field(document.getElementById("rLocation"), "recommendation", TEXT))  {
    showRecommendation();
    return false;        
  }
  
  //at least 1 recommendations been filled up
  var rCount = 0;
  for (i=1; i<=5; i++) {
    if (document.getElementById("rField" + i + "Body").value.search(/\S/) >= 0) {
      rCount++;  
    }
  }
  if (rCount < 1) {
    alert("Please fill in at least 1 recommendation");
    showRecommendation();
    return false; 
  }
  
  return true;
}

function validateBeforeSubmit() {
  if (validate()) {
    if (confirm("Submit to TakeMeToAsia's Editor now?")) {
      location.href = "recommendations-update.php?update_type=SUBMIT";
    }
  }
}

function saveProfile() {
  var pName = toUtfSafeUrlStr(document.getElementById("pName").value);
  var pNationality = toUtfSafeUrlStr(document.getElementById("pNationality").value);
  var pCountry = toUtfSafeUrlStr(document.getElementById("pCountry").value);
  var pAge = document.getElementById("pAge").value;
  var pMarital = toUtfSafeUrlStr(document.getElementById("pMarital").value);
  var pEmail = toUtfSafeUrlStr(document.getElementById("pEmail").value);
  var gNews = document.getElementById("gNews").checked? 1 : 0;
  
  if (aoSaveProfile == null) {
    aoSaveProfile = GetXmlHttpObject();
  }

  if (aoSaveProfile != null)  {
    if (aoSaveProfile.readyState == 4 || aoSaveProfile.readyState == 0) {
      showLabel(1, "Profile");
      var url = "recommendations-update.php?PHPSESSID=" + sessionId;
      var post = "update_type=SAVE_PROFILE&" + 
        "pName=" + pName + "&pNationality=" + pNationality + "&pCountry=" + pCountry +
        "&pAge=" + pAge + "&pMarital=" + pMarital + "&pEmail=" + pEmail + "&gNews=" + gNews;
      aoSaveProfile.open("POST",url,true);
      aoSaveProfile.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      aoSaveProfile.setRequestHeader("Content-length", post.length);
      aoSaveProfile.setRequestHeader("Connection", "close");
      aoSaveProfile.send(post);
      aoSaveProfile.onreadystatechange=saveProfileStateChanged;
    }
  }
}
function saveProfileStateChanged()   {
  if (aoSaveProfile != null)  {
    if (aoSaveProfile.readyState==4 && aoSaveProfile.status == 200)  {
      showLabel(0, "Profile");
      if (aoSaveProfile.responseText == "OK") {
        jumptoSection();
      }
      else  {
        alert(aoSaveProfile.responseText);
      }
    }
    else if (aoSaveProfile.readyState==4 && aoSaveProfile.status != 200) {
      showLabel(0, "Profile");
      alert("Error calling processing script. Please try again."); 
    }
  }
}

function saveRecommendation() {
  var rSubject = toUtfSafeUrlStr(document.getElementById("rSubject").value);
  var rLocation = toUtfSafeUrlStr(document.getElementById("rLocation").value);
  var rQuote = toUtfSafeUrlStr(document.getElementById("rQuote").value);
  var rCountry = toUtfSafeUrlStr(document.getElementById("rCountry").value);
  var rCity = toUtfSafeUrlStr(document.getElementById("rCity").value);
  var rCity2 = toUtfSafeUrlStr(document.getElementById("rCity2").value);
  var rTitle = new Array();
  var rBody = new Array();
  for (i=1; i<=6; i++) {
    rTitle[i] = toUtfSafeUrlStr(document.getElementById("rField" + i + "Title").value);
    rBody[i] = toUtfSafeUrlStr(document.getElementById("rField" + i + "Body").value);
  }
  
  if (aoSaveRecommendation == null) {
    aoSaveRecommendation = GetXmlHttpObject();
  }

  if (aoSaveRecommendation != null)  {
    if (aoSaveRecommendation.readyState == 4 || aoSaveRecommendation.readyState == 0) {
      showLabel(1, "Recommendation");
      var url = "recommendations-update.php?PHPSESSID=" + sessionId;
      var post = "update_type=SAVE_RECOMMENDATION&" + 
        "rSubject=" + rSubject + "&rLocation=" + rLocation + "&rQuote=" + rQuote +
        "&rCountry=" + rCountry + "&rCity=" + rCity + "&rCity2=" + rCity2;
      for (i=1; i<=6; i++) {
        post += "&rField" + i + "Title=" + rTitle[i] + "&rField" + i + "Body=" + rBody[i];
      }
      aoSaveRecommendation.open("POST",url,true);
      aoSaveRecommendation.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      aoSaveRecommendation.setRequestHeader("Content-length", post.length);
      aoSaveRecommendation.setRequestHeader("Connection", "close");
      aoSaveRecommendation.send(post);
      aoSaveRecommendation.onreadystatechange=saveRecommendationStateChanged;
    }
  }
}
function saveRecommendationStateChanged()   {
  if (aoSaveRecommendation != null)  {
    if (aoSaveRecommendation.readyState==4 && aoSaveRecommendation.status == 200)  {
      showLabel(0, "Recommendation");
      if (aoSaveRecommendation.responseText == "OK") {
        jumptoSection();
      }
      else  {
        alert(aoSaveRecommendation.responseText);
      }
    }
    else if (aoSaveRecommendation.readyState==4 && aoSaveRecommendation.status != 200)  { 
      showLabel(0, "Recommendation");
      alert("Error calling processing script. Please try again.");  
    }
  }
}

function savePhotoCaptions() {
  var post = "update_type=SAVE_PHOTOCAPTIONS";
  for (i=1; i<=3; i++) {
    post += "&iCaption" + i + "=" + document.forms["frmPhoto" + i].elements["iCaption" + i].value;
  }
  if (aoSavePhotoCap == null) {
    aoSavePhotoCap = GetXmlHttpObject();
  }

  if (aoSavePhotoCap != null)  {
    if (aoSavePhotoCap.readyState == 4 || aoSavePhotoCap.readyState == 0) {
      url = "recommendations-update.php?PHPSESSID=" + sessionId;
      showLabel(1, "Photo");
      aoSavePhotoCap.open("POST",url,true);
      aoSavePhotoCap.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      aoSavePhotoCap.setRequestHeader("Content-length", post.length);
      aoSavePhotoCap.setRequestHeader("Connection", "close");
      aoSavePhotoCap.send(post);
      aoSavePhotoCap.onreadystatechange=savePhotoCaptionsStateChanged;
    }
  }
  
  jumptoSection();
}
function savePhotoCaptionsStateChanged() {
  if (aoSavePhotoCap != null)  {
    if (aoSavePhotoCap.readyState==4 && aoSavePhotoCap.status == 200)  {
      showLabel(0, "Photo");
      if (aoSavePhotoCap.responseText == "OK")  {
        jumptoSection();
      }
      else  {
        alert(aoSavePhotoCap.responseText);
      }
    }
    else if (aoSavePhotoCap.readyState==4 && aoSavePhotoCap.status != 200) {
      showLabel(0, "Photo");
      alert("Error calling processing script. Please try again.");  
    }
  }
}

function savePhotos() {
  var toup = 0;
  var objAgree = document.getElementById("iAgree");
  
  if (objAgree.checked) {
    for (i=1; i<=3; i++) {
      if (document.forms["frmPhoto" + i].elements["iUpload" + i].value.search(/\S/)>=0) {
        toup++;
        savePhoto(i);
      } 
    }
    objAgree.checked = 0;
    if (toup == 0) {
      alert("No file selected to upload"); 
    }
  }
  else  {
    alert("Please agree the photo uploading declaration");
    objAgree.focus(); 
  }
}

function savePhoto(i) {
  var objAgree = document.getElementById("iAgree");
  if (!objAgree.checked) {
    alert("Please agree the photo uploading declaration");
    objAgree.focus();   
    return; 
  }
  
  var objForm = document.forms["frmPhoto" + i];
  if (objForm.elements["iUpload" + i].value.search(/\S/)>=0) {
    var objContainer = document.getElementById("divPhoto" + i);
    var objLoading = document.getElementById("divPhotoLoading" + i);
    objLoading.style.display = "";
    objContainer.style.display = "none";
    objForm.submit();   
  }
  else  {
    alert("Please select photo to upload"); 
  }
}

function displayPhoto(i, tf, aff, af) {
  var objContainer = document.getElementById("divPhoto" + i);
  var objLoading = document.getElementById("divPhotoLoading" + i);
  var objImg = document.getElementById("iPhoto" + i);
  var objLnk = document.getElementById("iPhotoLink" + i);
  var objField = document.getElementById("iPFile" + i);
  var objRmv = document.getElementById("iRemove" + i);
  var objFile = document.forms["frmPhoto" + i].elements["iUpload" + i];
  var objBtn = document.forms["frmPhoto" + i].elements["iBtnUpload" + i];
  
  objLoading.style.display = "none";
  objContainer.style.display = "";
  objImg.src = tf;
  objLnk.href = aff;
  objLnk.target = "_photo";
  objField.value = af;
  objRmv.style.display = "";
  objFile.value = "";
  objFile.style.display = "none";
  objBtn.style.display = "none";
}

function savePhotoError(i, msg) {
  var objContainer = document.getElementById("divPhoto" + i);
  var objLoading = document.getElementById("divPhotoLoading" + i); 
  
  objLoading.style.display = "none";
  objContainer.style.display = "";
  
  alert(msg);
}

var lastPhotoToRemove = 0;
function removePhoto(i) {
  if (confirm("Remove photo #" + i)) {
    if (aoRemovePhoto == null) {
      aoRemovePhoto = GetXmlHttpObject();
    }    
      
    if (aoRemovePhoto != null)  {
      if (aoRemovePhoto.readyState == 4 || aoRemovePhoto.readyState == 0) {  
        lastPhotoToRemove = i;
        var objContainer = document.getElementById("divPhoto" + i);
        var objRemoving = document.getElementById("divPhotoDeleting" + i);
        objContainer.style.display = "none";
        objRemoving.style.display = "";
        
        var url = "recommendations-update.php?PHPSESSID=" + sessionId + "&update_type=REMOVE_PHOTO&pid=" + i;
        aoRemovePhoto.open("POST",url,true);
        aoRemovePhoto.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        aoRemovePhoto.send(null);
        aoRemovePhoto.onreadystatechange=removePhotoStateChanged; 
      }
    }
  } 
}
function removePhotoStateChanged()   {
  if (aoRemovePhoto != null)  {
    var objRemoving = document.getElementById("divPhotoDeleting" + lastPhotoToRemove);
    if (aoRemovePhoto.readyState==4 && aoRemovePhoto.status == 200)  {
      objRemoving.style.display = "none";
      
      if (lastPhotoToRemove != 0 && aoRemovePhoto.responseText == "OK") {
        var i = lastPhotoToRemove;
        var objContainer = document.getElementById("divPhoto" + i);
        var objImg = document.getElementById("iPhoto" + i);
        var objLnk = document.getElementById("iPhotoLink" + i);
        var objField = document.getElementById("iPFile" + i);
        var objRmv = document.getElementById("iRemove" + i);
        var objFile = document.forms["frmPhoto" + i].elements["iUpload" + i];
        var objBtn = document.forms["frmPhoto" + i].elements["iBtnUpload" + i];
        
        objContainer.style.display = "";
        objImg.src = "images/nophoto.gif";
        objLnk.href = "javascript:void(0)";
        objLnk.target = "_self";
        objField.value = "";
        objField.style.display = "none";
        objRmv.style.display = "none";
        objFile.value = "";
        objFile.style.display = "";
        objBtn.style.display = "";
      }
      else  {
        alert(aoRemovePhoto.responseText);
      }
    }
    else if (aoRemovePhoto.readyState==4 && aoRemovePhoto.status != 200) {
      objRemoving.style.display = "none";
      alert("Error calling processing script. Please try again.");  
    }
  }
}

function saveTraveler() {
  var aCountry = toUtfSafeUrlStr(document.getElementById("aCountry").value);
  var aFavDest = toUtfSafeUrlStr(document.getElementById("aFavDest").value);
  var aFavHotel = toUtfSafeUrlStr(document.getElementById("aFavHotel").value);
  var aTravType = toUtfSafeUrlStr(document.getElementById("aTravType").value);
  var aIdeal = toUtfSafeUrlStr(document.getElementById("aIdeal").value);
  var aPOS = toUtfSafeUrlStr(document.getElementById("aPOS").value);
  
  if (aoSaveTraveler == null) {
    aoSaveTraveler = GetXmlHttpObject();
  }

  if (aoSaveTraveler != null)  {
    if (aoSaveTraveler.readyState == 4 || aoSaveTraveler.readyState == 0) {
      showLabel(1, "Traveler");
      var url = "recommendations-update.php?PHPSESSID=" + sessionId;
      var post = "update_type=SAVE_TRAVELER&" + 
        "aCountry=" + aCountry + "&aFavDest=" + aFavDest + "&aFavHotel=" + aFavHotel +
        "&aTravType=" + aTravType + "&aIdeal=" + aIdeal + "&aPOS=" + aPOS;
      
      aoSaveTraveler.open("POST",url,true);
      aoSaveTraveler.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      aoSaveTraveler.setRequestHeader("Content-length", post.length);
      aoSaveTraveler.setRequestHeader("Connection", "close");
      aoSaveTraveler.send(post);
      aoSaveTraveler.onreadystatechange=saveTravelerStateChanged;
    }
  }
}
function saveTravelerStateChanged()   {
  if (aoSaveTraveler != null)  {
    if (aoSaveTraveler.readyState==4 && aoSaveTraveler.status == 200)  {
      showLabel(0, "Traveler");
      if (aoSaveTraveler.responseText == "OK") {
        jumptoSection();
      }
      else  {
        alert(aoSaveTraveler.responseText);
      }
    }
    else if (aoSaveTraveler.readyState==4 && aoSaveTraveler.status != 200) {
      showLabel(0, "Traveler");
      alert("Error calling processing script. Please try again.");  
    }
  }
}

function uploadTravelerPhoto() {
  var objContainer = document.getElementById("divTravPhoto");
  var objLoading = document.getElementById("divTravPhotoLoad");
  objLoading.style.display = "";
  objContainer.style.display = "none";
  document.frmTravPhoto.submit(); 
}

function displayTravPhoto(i, tf, aff, af) {
  var objContainer = document.getElementById("divTravPhoto");
  var objLoading = document.getElementById("divTravPhotoLoad");
  var objImg = document.getElementById("aTravPhoto");
  var objLnk = document.getElementById("aTravPhotoLnk");
  var objRmv = document.getElementById("aRemovePhoto");
  var objUploader = document.getElementById("divTravPhoto");
  var objFile = document.getElementById("aPhoto");
  
  objLoading.style.display = "none";
  objContainer.style.display = "";
  objImg.src = tf;
  objLnk.href = aff;
  objLnk.target = "_photo";
  objRmv.style.display = "";
  objFile.value = "";
  objUploader.style.display = "none";
}

function removeTravelerPhoto() {
  if (aoRemoveTravPhoto == null) {
    aoRemoveTravPhoto = GetXmlHttpObject();
  }    
    
  if (aoRemoveTravPhoto != null)  {
    if (aoRemoveTravPhoto.readyState == 4 || aoRemoveTravPhoto.readyState == 0) {  
      var objContainer = document.getElementById("divTravPhoto");
      var objRemoving = document.getElementById("divTravPhotoDel");
      objContainer.style.display = "none";
      objRemoving.style.display = "";
      
      var url = "recommendations-update.php?update_type=REMOVE_TRAVELERPHOTO";
      aoRemoveTravPhoto.open("POST",url,true);
      aoRemoveTravPhoto.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      aoRemoveTravPhoto.send(null);
      aoRemoveTravPhoto.onreadystatechange=removeTravelerPhotoStateChanged; 
    }
  }
}
function removeTravelerPhotoStateChanged()   {
  if (aoRemoveTravPhoto != null)  {
    var objRemoving = document.getElementById("divTravPhotoDel");
      
    if (aoRemoveTravPhoto.readyState==4 && aoRemoveTravPhoto.status == 200)  {
      if (aoRemoveTravPhoto.responseText == "OK") {
        var i = lastPhotoToRemove;
        var objContainer = document.getElementById("divTravPhoto");
        var objImg = document.getElementById("aTravPhoto");
        var objLnk = document.getElementById("aTravPhotoLnk");
        var objRmv = document.getElementById("aRemovePhoto");
        var objFile = document.frmTravPhoto.aPhoto;
        var objUpload = document.getElementById("lnkUploadTravPhoto");
         
        objRemoving.style.display = "none";
        objContainer.style.display = "";
        objImg.src = "images/nophoto.gif";
        objLnk.href = "javascript:void(0)";
        objLnk.target = "_self";
        objRmv.style.display = "none";
        objFile.value = "";
        objFile.style.display = "";
        objUpload.style.display = "";
      }
      else  {
        objRemoving.style.display = "none";
        alert(aoRemoveTravPhoto.responseText);
      }
    }
    else if (aoRemoveTravPhoto.readyState==4 && aoRemoveTravPhoto.status != 200) {
      objRemoving.style.display = "none";
      alert("Error calling processing script. Please try again."); 
    }
  }
}

function saveTravPhotoError(i, msg) {
  var objContainer = document.getElementById("divTravPhoto");
  var objLoading = document.getElementById("divTravPhotoLoad");
  
  objLoading.style.display = "none";
  objContainer.style.display = "";
  
  alert(msg);
}


function hideAll() {
  document.getElementById("divProfile").style.display = "none";
  document.getElementById("divRecommendation").style.display = "none";
  document.getElementById("divPhotos").style.display = "none";
  document.getElementById("divTraveler").style.display = "none";
  document.getElementById("tabProfile").className = "tabGrey60";
  document.getElementById("tabRecommendation").className = "tabGrey120";
  document.getElementById("tabPhotos").className = "tabGrey60";
  document.getElementById("tabTraveler").className = "tabGrey100";
}

function profileNext() {
  nextSection = "RECOMMENDATION";
  saveLastInfo();
}

function recommendationNext() {
  nextSection = "PHOTOS";
  saveLastInfo();
}

function photosNext() {
  nextSection = "TRAVELER";
  saveLastInfo();
}

function travelerNext() {
  nextSection = "PREVIEW";
  saveLastInfo();
}

function clickProfile() {
  nextSection = "PROFILE";
  saveLastInfo();
}

function clickRecommendation() {
  nextSection = "RECOMMENDATION";
  saveLastInfo();
}

function clickPhotos() {
  nextSection = "PHOTOS";
  saveLastInfo();
}

function clickTraveler() {
  nextSection = "TRAVELER";
  saveLastInfo();
}

function clickPreview() {
  nextSection = "PREVIEW";
  saveLastInfo();  
}

function clickSubmit() {
  nextSection = "SUBMIT";
  saveLastInfo(); 
}

function saveLastInfo() {
  switch(curSection) {
    case "PROFILE":
      saveProfile();
    break;
    
    case "RECOMMENDATION":
      saveRecommendation();
    break;
    
    case "PHOTOS":
      savePhotoCaptions();
    break;
    
    case "TRAVELER":
      saveTraveler();
    break;
  }  
}

function jumptoSection() {
  if (nextSection != "")  {
    switch(nextSection) {
      case "PROFILE":
        showProfile();
      break;
      
      case "RECOMMENDATION":
        showRecommendation();
      break;
      
      case "PHOTOS":
        showPhotos();
      break;
      
      case "TRAVELER":
        showTraveler();
      break;
      
      case "PREVIEW":
        showPreview();
      break;
      
      case "SUBMIT":
        validateBeforeSubmit();
      break;
    } 
    nextSection = "";
  }
}



function showProfile() {
  if (curSection != "PROFILE") {
    hideAll();
    document.getElementById("tabProfile").className = "tabBlue60";
    document.getElementById("divProfile").style.display = "";
    curSection = "PROFILE";
  }
}

function showRecommendation() {
  if (curSection != "RECOMMENDATION") {
    hideAll();
    document.getElementById("tabRecommendation").className = "tabBlue120";
    document.getElementById("divRecommendation").style.display = "";
    curSection = "RECOMMENDATION";
  }
}

function showPhotos() {
  if (curSection != "PHOTOS") {
    hideAll();
    document.getElementById("tabPhotos").className = "tabBlue60";
    document.getElementById("divPhotos").style.display = "";
    curSection = "PHOTOS";
  }
}

function showTraveler() {
  if (curSection != "TRAVELER") {
    hideAll();
    document.getElementById("tabTraveler").className = "tabBlue100";
    document.getElementById("divTraveler").style.display = "";
    curSection = "TRAVELER";
  }
}

function showPreview() {
  if (validate()) {
    location.href = "recommendations-preview.php";
  }
}

function checkPastProfile() {
  var pEmail = toUtfSafeUrlStr(document.getElementById("pEmail").value);
  var pEmail2 = toUtfSafeUrlStr(document.getElementById("pEmail2").value);
  if (pEmail == pEmail2 &&
      pEmail.search(/^[^@ ]+@[^@ ]+\.[^@ \.]+$/) >= 0 &&
      pEmail2.search(/^[^@ ]+@[^@ ]+\.[^@ \.]+$/) >= 0) {
    
    if (aoCheckPastProfile == null) {
      aoCheckPastProfile = GetXmlHttpObject();
    }
  
    if (aoCheckPastProfile != null)  {
      if (aoCheckPastProfile.readyState == 4 || aoCheckPastProfile.readyState == 0) {
        showLabel(1, "CheckProfile");
        var url = "recommendations-update.php?PHPSESSID=" + sessionId + "&update_type=CHECK_PROFILE&pEmail=" + pEmail;
        aoCheckPastProfile.open("GET",url,true);
        aoCheckPastProfile.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        aoCheckPastProfile.send(null);
        aoCheckPastProfile.onreadystatechange=checkPastProfileStateChanged;
      }
    }
  }
}
function checkPastProfileStateChanged()   {
  if (aoCheckPastProfile != null)  {
    if (aoCheckPastProfile.readyState==4)  {
      showLabel(0, "CheckProfile");
      var reply = aoCheckPastProfile.responseText;
      if (reply.substring(0,3) == "YES") {
        var parts = reply.split("__:__");
        var pName = parts[1];
        var pDate = parts[2];
        if (confirm("Retrieve profile and traveler's info of " + pName + " submitted on " + pDate + "?\n(Note: This might overwrite the information you have typed in.)")) {
          loadPastProfile();
        }
      }
      else  {
        alert("Past profile not found"); 
      }
    }
  }
}

function loadPastProfile() {
  var pEmail = toUtfSafeUrlStr(document.getElementById("pEmail").value);
  if (aoLoadPastProfile == null) {
    aoLoadPastProfile = GetXmlHttpObject();
  }
  
  if (aoLoadPastProfile != null)  {
    if (aoLoadPastProfile.readyState == 4 || aoLoadPastProfile.readyState == 0) {
      showLabel(1, "ReadProfile");
      var url = "recommendations-update.php?PHPSESSID=" + sessionId + "&update_type=LOAD_PROFILE&pEmail=" + pEmail;
      aoLoadPastProfile.open("GET",url,true);
      aoLoadPastProfile.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      aoLoadPastProfile.send(null);
      aoLoadPastProfile.onreadystatechange=loadPastProfileStateChanged;
    }
  }
}
function loadPastProfileStateChanged()   {
  if (aoLoadPastProfile != null)  {
    if (aoLoadPastProfile.readyState==4)  {
      showLabel(0, "ReadProfile");
      var reply = aoLoadPastProfile.responseText;
      if (reply == "OK") {
        alert("Past profile loaded"); 
        location.reload();
      }
      else {
        alert(reply);
      }
    }
  }
}



function refreshCity(cname) {
  var objsel = document.getElementById("rCity");
  document.getElementById("rCity2").style.display = "none"; 
  
  //remove all old record
  while (objsel.length > 0) {
    objsel.options[objsel.length-1] = null;
  }
  
  if (cname != "")  {
    //add loading selection
    var objopt = new Option('loading...', '', false, false);
    objsel.options[0]=objopt;
    objsel.disabled = true;
    
    if (aoRefreshCity == null) {
      aoRefreshCity = GetXmlHttpObject();
    }
  
    if (aoRefreshCity != null)  {
      if (aoRefreshCity.readyState == 4 || aoRefreshCity.readyState == 0) {
        
        var url = "recommendations-update.php?PHPSESSID=" + sessionId + "&update_type=GET_CITIES&country=" + cname;
        aoRefreshCity.open("GET",url,true);
        aoRefreshCity.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        aoRefreshCity.send(null);
        aoRefreshCity.onreadystatechange=refreshCityStateChanged;
      }
    }
  }
  else  {
    var objopt = new Option('- Select City -', '', false, false);
    objsel.options[0]=objopt;   
  }
}
function refreshCityStateChanged()   {
  var objsel = document.getElementById("rCity");
    
  if (aoRefreshCity != null)  {
    if (aoRefreshCity.readyState==4 && aoRefreshCity.status == 200)  {
      if (aoRefreshCity.responseText.substring(0,2) == "OK") {
        var vals = aoRefreshCity.responseText.substring(3);
        
        var objopt = new Option('- Select City -', '', false, false);
        objsel.options[0] = objopt;
        var idx = 0;
        
        if (vals != "") {
          var arrCities = vals.split("\n");
          for (c=0; c<arrCities.length; c++) {
            idx++;
            var sel = false;
            if (arrCities[c] == lastCity) {
              sel = true;
            }
            objopt = new Option(arrCities[c], arrCities[c], false, sel); 
            objsel.options[idx] = objopt;
          }
        }
        
        idx++;
        objopt = new Option('- Other City -', '__OTHERS__', false, false);
        objsel.options[idx] = objopt;
        
        objsel.disabled = false;
      }
      else  {
        alert(aoRefreshCity.responseText);
      }
    }
    else if (aoRefreshCity.readyState==4 && aoRefreshCity.status != 200) {
      alert("Error calling processing script. Please try again."); 
    }
  }
}


function changeCity(cname) {
  if (cname == "__OTHERS__") {
    document.getElementById("rCity2").value = "";
    document.getElementById("rCity2").style.display = ""; 
  }
  else  {
    document.getElementById("rCity2").value = "";
    document.getElementById("rCity2").style.display = "none"; 
    lastCity = cname;
  }
}

