﻿// JScript File
// variable declaration
var txtboxid; //Used to store id of focused textbox
var fzone = "", tzone = ""; // store zoneid of current focus

// trim value
function trim(str) {
    return str.replace(/^\s+|\s+$/g, "");
}

//Search box on mouse hover and blur
function txthover(txtid,text)
{
    var txt = document.getElementById(txtid);
    if(txt.value == text)
    {
        txt.value="";
        txt.style.color="Black";
    }
}

function txtblur(txtid,text)
{
    var txt = document.getElementById(txtid);
    if(txt.value=="")
    {
        txt.value = text;
        txt.style.color="Gray";
    }
}

function check()
{
    if (document.getElementById('ddlCityS').selectedIndex == 0) {
        alert("First select a city.");
        document.getElementById('ddlCityS').focus();
    }
}
// Set default keypress for search carpool in header
//document.getElementById('ddlCityS').setAttribute('onkeydown', 'return keypressforsubmit(event)');
//document.getElementById('txtSourceS').setAttribute('onkeydown', 'return keypressforsubmit(event)');
//document.getElementById('txtDestinationS').setAttribute('onkeydown', 'return keypressforsubmit(event)');
//To Enable default Enter Key press in LOGIN module 
function keypressforsubmit(e) {
    if ([e.keyCode || e.which] == 13) {
        return searchbar();
    }
}
function keypressforsubmit1(e) {
    if ([e.keyCode || e.which] == 13) {
        return searchbar1();
    }
}
function callsearch()
{
    if (document.getElementById('ddlCityS').selectedIndex > 0) {
        var sloc = document.getElementById('txtSourceS').value;
        var dloc = document.getElementById('txtDestinationS').value;
        if (sloc == '' || sloc == "Source location") {
            alert('Please enter your source location.');
            document.getElementById('txtSourceS').focus();
        }
        else if (dloc == '' || dloc == "Destination location") {
            alert('Please enter your destination location.');
            document.getElementById('txtDestinationS').focus();
        }
        else {
            //redirect to searchresult with sloc and dloc as querystring.
            var f = document.forms[0];
            var e = f.elements["rblType"];
            var e1 = f.elements["rblSmoke"];
            var e2 = f.elements["rblGender"];

            var type = "";
            if (e[0].checked == true)
                type = "1";
            else if (e[1].checked == true)
                type = "2";
            else
                type = "3";

            //rblSmoke Non-Smoker Smoker Anyway
            //rblGender Male Carpoolers Female Carpoolers Anyway
            var smoke = "";
            if (e1[0].checked == true)
                smoke = "1";
            else if (e1[1].checked == true)
                smoke = "2";
            else
                smoke = "3";

            var gender = "";
            if (e2[0].checked == true)
                gender = "1";
            else if (e2[1].checked == true)
                gender = "2";
            else
                gender = "3";

            var city = document.getElementById('ddlCityS').value;
            var source = document.getElementById('txtSourceS').value;
            var dest = document.getElementById('txtDestinationS').value;

            var loc = "carpoolers.aspx?cap=carpool-from-" + source + "-to-" + dest + "&t=" + type + "&s=" + smoke + "&g=" + gender;
            window.location = loc;
        }
    }
    else {
        alert('Please select a city.');
        document.getElementById('ddlCityS').focus();
    }
}
/* root pages */
function searchbar() {
    if (document.getElementById('ddlCityS').selectedIndex > 0) {
        var sloc = document.getElementById('txtSourceS').value;
        var dloc = document.getElementById('txtDestinationS').value;
        if (sloc == '' || sloc == "Source location") {
            alert('Please enter your source location.');
            document.getElementById('txtSourceS').focus();
        }
        else if (dloc == '' || dloc == "Destination location") {
            alert('Please enter your destination location.');
            document.getElementById('txtDestinationS').focus();
        }
        else {
            //redirect to searchresult with sloc and dloc as querystring.
            var city = document.getElementById('ddlCityS').value;
            var source = document.getElementById('txtSourceS').value;
            var dest = document.getElementById('txtDestinationS').value;

            var loc = "carpoolers.aspx?cap=carpool-from-" + source + "-to-" + dest;
            window.location = loc;
        }
    }
    else {
        alert('Please select a city.');
        document.getElementById('ddlCityS').focus();
    }
}
/* internal pages */
function searchbar1() {
    if (document.getElementById('ddlCityS').selectedIndex > 0) {
        var sloc = document.getElementById('txtSourceS').value;
        var dloc = document.getElementById('txtDestinationS').value;
        if (sloc == '' || sloc == "Source location") {
            alert('Please enter your source location.');
            document.getElementById('txtSourceS').focus();
        }
        else if (dloc == '' || dloc == "Destination location") {
            alert('Please enter your destination location.');
            document.getElementById('txtDestinationS').focus();
        }
        else {
            //redirect to searchresult with sloc and dloc as querystring.
            var city = document.getElementById('ddlCityS').value;
            var source = document.getElementById('txtSourceS').value;
            var dest = document.getElementById('txtDestinationS').value;

            var loc = "../carpoolers.aspx?cap=carpool-from-" + source + "-to-" + dest;
            window.location = loc;
        }
    }
    else {
        alert('Please select a city.');
        document.getElementById('ddlCityS').focus();
    }
}

/*  Route page - On selection of popular routes */
function getlist() {
    var from = document.getElementsByName('rblS');
    var to = document.getElementsByName('rblD');
    var flength = from.length;
    var tlength = to.length;
    var fval = "", tval = "";

    for (var i = 0; i < flength; i++) {
        if (document.getElementById('rblS' + i).checked)
            fval = document.getElementById('rblS' + i).value;
    }

    for (var i = 0; i < tlength; i++) {
        if (document.getElementById('rblD' + i).checked)
            tval = document.getElementById('rblD' + i).value;
    }

    if (fval == "")
        alert("Please select your start location");
    else if (tval == "")
        alert("Please select your Destination location");
    else if (fval == tval)
        alert("Well! Well! \n\n May be I am wrong but I guess, your start location and destination location can't be same.\n Please make a valid selection and try again.");
    else {
        //alert("Thanks! Please wait while we search for carpool in selected route.");
        var loc = "carpoolers.aspx?cap=carpool-from-" + fval + "-to-" + tval;
        window.location = loc;
    }
}






function loginshowhide(id)
{
    document.getElementById('city').style.backgroundColor="";
    document.getElementById(id).style.backgroundColor="#4e7304";
    var objLogin=document.getElementById('txtlogin');
    var objPwd=document.getElementById('txtpwd');
    document.getElementById('txtlogin').value="";
    document.getElementById('txtpwd').value="";
    document.getElementById('spnerr').innerHTML="";
    objLogin.setAttribute('onkeydown','return keypressforlogin(event)');
    objPwd.setAttribute('onkeydown','return keypressforlogin(event)');
        
    var objbtn = document.getElementById(id);
    var y=0;
    var x=0;
    while (objbtn)
    {
        y+=objbtn.offsetTop;
        x+=objbtn.offsetLeft;
        objbtn = objbtn.offsetParent;
    }
    document.getElementById("dvSignin").style.top=(y+30)+"px";
    document.getElementById("dvSignin").style.left=(x-147)+"px";
    animatedcollapse.hide('dvCities');
    animatedcollapse.show('dvSignin'); 
    document.getElementById('txtlogin').focus();
    return false;
} 
function closeLDv()
{
    document.getElementById('log').style.backgroundColor="";
    animatedcollapse.hide('dvSignin');
    clickstatus=0; 
}  
/*==== Login validation ====*/
function submitlogin1(response)
{
    var subres = response.value;
    
    if(subres == "fail")
    {
        document.getElementById('dvLogin').style.display="block";
        document.getElementById('dvloader').style.display="none";
        alert("Error in login process. Please check back soon.");
    }
    else if(subres == "0")
    {
        document.getElementById('dvLogin').style.display="block";
        document.getElementById('dvloader').style.display="none";
        document.getElementById('spnerr').innerHTML="Invalid Login/Password";
    }
    else if (subres == "1")
    {
        document.getElementById('dvLogin').style.display="block";
        document.getElementById('dvloader').style.display="none";
        document.getElementById('spnerr').innerHTML="Please activate your account first.";
    }
    else if (subres == "2")
    {
        document.getElementById('dvLogin').style.display="block";
        document.getElementById('dvloader').style.display="none";
        document.getElementById('spnerr').innerHTML="This ID has been blocked by Admin.";
    }
    else if(subres !=null)
    {
        var arr=new Array();
        arr=subres.split('-');
        document.getElementById('dvPreLogin').style.display="none";
        document.getElementById('dvPostLogin1').style.display="block";
        animatedcollapse.hide('dvSignin');
        animatedcollapse.show('dvPostLogin');
        document.getElementById('spnDname').innerHTML=arr[0];
        document.getElementById('spnLastvisit').innerHTML=arr[1];
        document.getElementById('hdnLstatus').value=subres;
    }
}

function submitlogin2(response)
{
    var subres = response.value;
    
    if(subres == "fail")
    {
        document.getElementById('dvLogin').style.display="block";
        document.getElementById('dvloader').style.display="none";
        alert("Error in login process. Please check back soon.");
    }
    else if(subres == "0")
    {
        document.getElementById('dvLogin').style.display="block";
        document.getElementById('dvloader').style.display="none";
        document.getElementById('spnerr').innerHTML="Invalid Login/Password";
    }
    else if (subres == "1")
    {
        document.getElementById('dvLogin').style.display="block";
        document.getElementById('dvloader').style.display="none";
        document.getElementById('spnerr').innerHTML="Please activate your account first.";
    }
    else if (subres == "2")
    {
        document.getElementById('dvLogin').style.display="block";
        document.getElementById('dvloader').style.display="none";
        document.getElementById('spnerr').innerHTML="This ID has been blocked by Admin.";
    }
    else if(subres !=null)
    {
        var loc="index.aspx";
        window.location=loc;
    }
}

function checkLogin()
{   
    var lstatus = document.getElementById('hdnLstatus').value;
    if(lstatus != '0')
    {
        document.getElementById('dvPreLogin').style.display='none';
        document.getElementById('dvPostLogin1').style.display="block";
        animatedcollapse.show('dvPostLogin');
        var arr=new Array();
        arr=lstatus.split('-');
        document.getElementById('spnDname').innerHTML=arr[0];
        document.getElementById('spnLastvisit').innerHTML=arr[1];
    }
    else
    {
        document.getElementById('dvPreLogin').style.display='block';
    }
}

function keypressforlogin(e)
{
    if ([e.keyCode||e.which]==13)
    {
        return submitlogin();
    }
}

function closeDv(id)
{
    animatedcollapse.hide(id);
    clickstatus=0; 
}

function imgMouseOver(id,img)
{
    document.getElementById(id).src = ""+img+"";
}
function imgMouseOut(id,img)
{
    document.getElementById(id).src = ""+img+"";
}
function visitdetail(rownum) {
    var city = document.getElementById('hdnCity' + rownum).value;
    var title = document.getElementById('hdnTitle' + rownum).value;
    var cpid = document.getElementById('hdncid' + rownum).value;
    var detailURL = "../details.aspx?city=" + city + "&cap=" + title + "&id=" + cpid;
    window.open(detailURL);
}
