function combineUrl(path) {
    var str = window.location.href.replace("http://", "");
    var arr = str.split("/");
    var siteurl = "http://" + arr[0];

    if (path.substring(0, 1) != '/')
        path = "/" + path;

    return siteurl+path;
}

function getHTTPObject() {
  var xmlhttp;
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  } 
  return xmlhttp;
}

var httpObj = getHTTPObject();

var isExecuting = false;
function addItinerary(cid) {
    if (uid == null || uid == '') {
        window.location = combineUrl("register.php");
    } else if (!isExecuting && httpObj) {
        var url = combineUrl("user_edited.php?add=1&cid="+cid+"&uid="+uid);

        httpObj.open("GET", url, true);
        isExecuting = true;
        httpObj.onreadystatechange = function(){
            if (httpObj.readyState == 4) {
                isExecuting = false;
                if (httpObj.responseText == "ok")
                    alert('Added to Custom Itinerary List.');
                else
                    alert('Cannot add item.');
            }
        };
        httpObj.send(null);
    }
}

function deleteItinerary(eid) {
    
    if (uid == null || uid == '') {
        window.location = combineUrl("register.php");
    } else if (!isExecuting && httpObj) {
        var url = combineUrl("user_edited.php?del=1&id="+eid);

        httpObj.open("GET", url, false);
        isExecuting = true;
        httpObj.onreadystatechange = function(){
            if (httpObj.readyState == 4) {
                isExecuting = false;
                if (httpObj.responseText == "no")
                    alert('Cannot delete item.');
                else{
                    window.location.reload(true);
                }
            }
        };
        httpObj.send(null);
    }
}

function printItinary() {
    if (uid == null || uid == '') {
        window.location = combineUrl("register.php");
    } else {
        var url = combineUrl("printitin.php");
        window.open(url, "Print", "width=640,height=750,resizable=0,status=0,toolbar=0,scrollbars=1")
    }
}

function resetPass() {
    var email = document.getElementById("fldlogin");

     if (!isExecuting && httpObj) {
        if (email.value.length == 0) {
            alert('Enter email.')
            return;
        }
        var url = combineUrl("user_edited.php?reset=1&em="+email.value);
        httpObj.open("GET", url, true);
        isExecuting = true;
        httpObj.onreadystatechange = function(){
            if (httpObj.readyState == 4) {
                isExecuting = false;
                if (httpObj.status == 200) {
                    if (httpObj.responseText == "ok")
                        alert("Email with new password was sent to your email address.");
                    else
                        alert("Cannot send email.");
                } else
                    alert('Unable to change password.');
            }
        };
        httpObj.send(null);
     }
}

function setPass() {
    var pass = document.getElementById("pass");
    var pass1 = document.getElementById("pass1");
    var email = document.getElementById("email");
    if (pass.value != pass1.value) {
        alert('Password does not match the confirm password.');
        return;
    }

    var len = pass.value.replace(/\s+/g, '').length*1;
    if (len > 0 && len < 6){
        alert("Password must be minimum 6 leters.");
        return;
    }
    
    if (!isExecuting && httpObj) {
        var url = combineUrl("user_edited.php?set=1&em="+email.innerHTML+"&pwd="+pass.value);

        httpObj.open("GET", url, true);
        isExecuting = true;
        httpObj.onreadystatechange = function(){
            if (httpObj.readyState == 4) {
                isExecuting = false;
                if (httpObj.status == 200) {
                    var text = httpObj.responseText;
                    if (text == "ok") {
                        alert("Password changed successfully.");
                        pass.value = "";
                        pass1.value = "";
                    }else if (text == "6") {
                        alert("Password must be minimum 6 leters.");
                    }else
                        alert("Unable to change password.");
                } else
                    alert('Unable to change password.');
            }
        };
        httpObj.send(null);
    }
}

function printCoupon(cpid){
    var url = combineUrl("printcoupon.php?cpid="+cpid);
    var w = window.ActiveXObject?"740":"720";
    var wnd = window.open(url, "Coupon", "width=" + w + ",height=750,resizable=0,status=0,toolbar=0,scrollbars=1");
    if (wnd && win.focus)
        wnd.focus();


    return false;
}
