/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Debabrata Rana
 */

var receiveReq = getXmlHttpRequestObject();

window.history.forward();

function getLoginForm(lukupURL, username, userType) {
    //For development, testing, staging & production server
    //document.forms[0].action = lukupURL + "/jsp/signin.jsp?username="+username+"&userType="+userType;
    //For localhost server
    //document.forms[0].action = "UserAction.do?method=getLoginForm&focus=login";
    document.forms[0].action = "Welcome.do";
    document.forms[0].submit();
}

function goLukupSearch(lukupURL) {
    //For development, testing, staging & production server
    //document.forms[0].action = lukupURL + "/jsp/search.jsp";
    //For localhost server
    document.forms[0].action = "Welcome.do";
    document.forms[0].submit();
}

function logout(lukupURL) {
    //For development, testing, staging & production server
    //document.forms[0].action = lukupURL + "/jsp/SignOut.jsp";
    //For localhost server
    document.forms[0].action = "Welcome.do";
    document.forms[0].submit();
}

function goRelHome(userType, anchor) {
    var userInSes = document.forms[0].userInSes.value;
    var sessionId = document.forms[0].sessionId.value;
    if(anchor)
    {
        anchor.setAttribute("href","UserAction.do?method=goHome&userType="+userType+"&username="+userInSes+"&sessionId="+sessionId);
        anchor.setAttribute("target","_blank");
    }
    else
    {
        document.forms[0].action = "UserAction.do?method=goHome&userType="+userType+"&username="+userInSes+"&sessionId="+sessionId;
        document.forms[0].submit();
    }
}

function showTutorial(userType) {
    var userInSes = document.forms[0].userInSes.value;
    var sessionId = document.forms[0].sessionId.value;
    document.forms[0].action = "UserAction.do?method=goHome&userType="+userType+"&username="+userInSes+"&showTutorial=yes&sessionId="+sessionId;
    document.forms[0].submit();
}

function goHome() {
    document.forms[0].action = "Welcome.do";
    document.forms[0].submit();
}

function checkEmpty(val) {
    var len = val.length;
    var counter = 0;
    for(var i = 0; i < len; i++) {
        if(val.charAt(i) == " ") {
            counter++;
        }
    }

    if(counter == len) {
        return false;
    } else {
        return true;
    }
}

function checkEnterKey(event) {
    var keycode;
    if (window.event) {
        keycode = window.event.keyCode;
    } else if (event) {
        keycode = event.which;
    } else {
        return true;
    }

    if (keycode == 13) {
        login();
        return false;
    } else {
        return true;
    }
}

function getDevForm() {
    var sessionId = document.forms[0].sessionId.value;
    document.forms[0].action = "DevAction.do?method=getDevForm&approvalStatus=submitted&searchedAppType=all&pageNum=1&sessionId="+sessionId;
    document.forms[0].submit();
}

function getACBForm() {
    var sessionId = document.forms[0].sessionId.value;
    document.forms[0].action = "DevAction.do?method=getACBForm&approvalStatus=submitted&searchedAppType=all&pageNum=1&sessionId="+sessionId;
    document.forms[0].submit();
}

function getMCBForm() {
    var sessionId = document.forms[0].sessionId.value;
    document.forms[0].action = "DevAction.do?method=getMCBForm&approvalStatus=submitted&searchedAppType=all&pageNum=1&sessionId="+sessionId;
    document.forms[0].submit();
}

function getListOfPosts(section) {
    var sessionId = document.forms[0].sessionId.value;
    document.forms[0].action = "PostAction.do?method=getListOfPosts&category=all&section="+section+"&sortBy=none&when=noPref&pageNum=1&sessionId="+sessionId;
    document.forms[0].submit();
}

function signOut() {
    document.forms[0].action = "UserAction.do?method=logout";
    document.forms[0].submit();
}

function getProfile(lukupURL) {
    document.forms[0].action = lukupURL + "/jsp/my_profile.jsp";
    document.forms[0].submit();
}

function getSettings(lukupURL) {
    document.forms[0].action = lukupURL + "/jsp/settings.jsp";
    document.forms[0].submit();
}

function getAdminHome() {
    var sessionId = document.forms[0].sessionId.value;
    document.forms[0].action = "AdminSubmissionAction.do?method=getAdminHome&approvalStatus=pending&widgetCat=all&pageNum=1&sessionId="+sessionId;
    document.forms[0].submit();
}

function validateEmail(email) {
    var regExp_email=/^([\w]+)(\.[\w]+)*@([\w]+)(\.[\w]{2,3}){1,2}$/;
    if(email != "") {
        if(regExp_email.test(email) == false) {
            return false;
        }
    }
    return true;
}

function validateURL(url) {
    var regExp_url=/https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/;
    if(url != "") {
        if(regExp_url.test(url) == false) {
            return false;
        }
    }
    return true;
}

function validateName(name) {
    var regExp_name=/^(([a-zA-Z]+)( [a-zA-Z]+)*)$/;
    if(name != "") {
        if(regExp_name.test(name) == false) {
            return false;
        }
    }
    return true;
}

function validatePhone(phone) {
    var regExp_phone=/^[0-9]{10,15}$/;
    if(phone != "") {
        if(regExp_phone.test(phone) == false) {
            return false;
        }
    }
    return true;
}

function valCardNo(cardNo) {
    var regExp_cardNo=/^[0-9]{16}$/;
    if(cardNo != "") {
        if(regExp_cardNo.test(cardNo) == false) {
            return false;
        }
    }
    return true;
}

function valCvvNo(cvvNo) {
    var regExp_cvvNo=/^[0-9]{3}$/;
    if(cvvNo != "") {
        if(regExp_cvvNo.test(cvvNo) == false) {
            return false;
        }
    }
    return true;
}

function valCreditPeriod(creditPeriod) {
    var regExp_creditPeriod=/^[0-9]{1,3}$/;
    if(creditPeriod != "") {
        if(regExp_creditPeriod.test(creditPeriod) == false) {
            return false;
        }
    }
    return true;
}

function validatePrice(price) {
    var regExp_price = /^(([1-9]{1}[0-9]{0,}(\.[0-9]{1,2})?)|((0\.((0[1-9]{1})|([1-9]{1}[0-9]{0,1})))?))$/;
    if(price != "") {
        if(regExp_price.test(price) == false) {
            return false;
        }
    }
    return true;
}

function validateNumber(number) {
    var regExp_number = /^[0-9]+$/;
    if(number != "") {
        if(regExp_number.test(number) == false) {
            return false;
        }
    }
    return true;
}

function getDoubleDigitDay(singleDigitDay) {
    var doubleDigitDay;
    var dayArray = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09"];
    doubleDigitDay = dayArray[singleDigitDay];
    return doubleDigitDay;
}

function getMonthName(mon) {
    var monthArray = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
    return(monthArray[mon]);
}

function getToday() {
    var currentDate = new Date();
    var day = currentDate.getDate();
    var month = (currentDate.getMonth() * 1) + 1;
    var year = currentDate.getFullYear();
    if(parseInt(day) < 10) {
        day = getDoubleDigitDay(day);
    }
    var today = day + "-" + month + "-" + year;
    return today;
}

function getAdvertiser() {
    var statType = "graphicalStat";
    var sessionId = document.forms[0].sessionId.value;
    document.forms[0].action = "AdvAction.do?method=getAdvertiser&statType="+statType+"&selPeriod=last7Days&sessionId="+sessionId;
    document.forms[0].submit();
}

function getPublisher() {
    var statType = "graphicalStat";
    var sessionId = document.forms[0].sessionId.value;
    document.forms[0].action = "PubAction.do?method=getPublisher&statType="+statType+"&selPeriod=last7Days&sessionId="+sessionId;
    document.forms[0].submit();
}

//This function returns true if date1 is greater than date2
function firstDateGreaterThanSecond(date1, date2) {
    if((date1 != "") && (date2 != ""))  {
        var from = date1.split("-");
        var fromDay = parseInt(from[0] * 1);
        var fromMonth = parseInt(from[1] * 1);
        var fromYear = parseInt(from[2] * 1);

        var to = date2.split("-");
        var toDay = parseInt(to[0] * 1);
        var toMonth = parseInt(to[1] * 1);
        var toYear = parseInt(to[2] * 1);

        if(fromYear > toYear) {
            return true;
        } else if((fromYear == toYear) && (fromMonth > toMonth)) {
            return true;
        } else if((toYear == fromYear) && (toMonth == fromMonth) && (fromDay > toDay)) {
            return true;
        }
    }

    return false;
}

function getXmlHttpRequestObject() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }else if(window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } else{
        alert("Your browser doesn't support the XmlHttpRequest object.");
        return false;
    }
}

function validateSession() {
    var sessionId = document.forms[0].sessionId.value;    
    if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
        var url = "/portal/UserAction.do?method=validateSession&sessionId="+sessionId;
        receiveReq.open("GET", url, true);
        receiveReq.onreadystatechange = function () {
            getResultFromXML();
        };
        receiveReq.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
        receiveReq.send(null);
    }
}


function getResultFromXML() {
    if (receiveReq.readyState == 4) {
        if (receiveReq.status == 200) {
            var xmlDoc = receiveReq.responseXML;
            var session = xmlDoc.getElementsByTagName("session");
            var result   = "";
            for(var i = 0; i < session.length; i++) {
                result = session[i].getElementsByTagName("result")[0].childNodes[0].nodeValue;
            }
            if(result == "invalid") {
                window.history.forward();
            }
        }
    }
}

function customSearch() {
    return false;
}

function callAction(actionPath) {
    document.forms[0].action = actionPath;
    document.forms[0].submit();
}

function setWidthNHeight() {
    var standardSize = document.forms[0].standardSize.value;
    switch(standardSize) {
        case "":
            document.forms[0].width.value = "";
            document.forms[0].height.value = "";
            break;
        case "IABFullBanner":
            document.forms[0].width.value = 468;
            document.forms[0].height.value = 60;
            break;
        case "IABSkyscraper":
            document.forms[0].width.value = 120;
            document.forms[0].height.value = 600;
            break;
        case "IABLeaderboard":
            document.forms[0].width.value = 728;
            document.forms[0].height.value = 90;
            break;
        case "IABButton":
            document.forms[0].width.value = 120;
            document.forms[0].height.value = 90;
            break;
        case "IABButton2":
            document.forms[0].width.value = 120;
            document.forms[0].height.value = 60;
            break;
        case "IABHalfBanner":
            document.forms[0].width.value = 234;
            document.forms[0].height.value = 60;
            break;
        case "IABMicroBar":
            document.forms[0].width.value = 88;
            document.forms[0].height.value = 31;
            break;
        case "IABSquareButton":
            document.forms[0].width.value = 125;
            document.forms[0].height.value = 125;
            break;
        case "IABVerticalBanner":
            document.forms[0].width.value = 120;
            document.forms[0].height.value = 240;
            break;
        case "IABRectangle":
            document.forms[0].width.value = 180;
            document.forms[0].height.value = 150;
            break;
        case "IABMediumRectangle":
            document.forms[0].width.value = 300;
            document.forms[0].height.value = 250;
            break;
        case "IABLargeRectangle":
            document.forms[0].width.value = 336;
            document.forms[0].height.value = 280;
            break;
        case "IABVerticalRectangle":
            document.forms[0].width.value = 240;
            document.forms[0].height.value = 400;
            break;
        case "IABSquarePopUp":
            document.forms[0].width.value = 250;
            document.forms[0].height.value = 250;
            break;
        case "IABWideSkyscraper":
            document.forms[0].width.value = 160;
            document.forms[0].height.value = 600;
            break;
    }
}

function setStandardSizeAsCustom() {
    document.forms[0].standardSize.value = "custom";
}

function setWidgetTypeNCategory(widgetType, widgetCat) {
    document.forms[0].searchWidgetType.value = widgetType;
    document.forms[0].searchWidgetCategory.value = widgetCat;
}

function checkEnterKeypress(event) {
    var keycode;
    if (window.event) {
        keycode = window.event.keyCode;
    } else if (event) {
        keycode = event.which;
    } else {
        return true;
    }

    if (keycode == 13) {
        setSearchWidgetWord();
        getWidgetListBySearchWord();
        return false;
    } else {
        return true;
    }
}

function callSearch() {
    setSearchWidgetWord();
    getWidgetListBySearchWord();
    return false;
}

function setSearchWidgetWord() {
    document.forms[0].searchWidgetWord.value = document.forms[0].search.value;
}

function getWidgetListBySearchWord() {
    var searchWidgetWord = document.forms[0].searchWidgetWord.value;
    var searchWidgetType = document.forms[0].searchWidgetType.value;
    var searchWidgetCategory = document.forms[0].searchWidgetCategory.value;
    document.forms[0].action = "DevAction.do?method=getWidgetList&widgetType="+searchWidgetType+"&widgetCat="+searchWidgetCategory+"&sortBy=none&when=noPref"+
    "&srchWord="+searchWidgetWord+"&pageNum=1";
    document.forms[0].submit();
    return true;
}

function getFeedbackForm() {
    //    var sessionId = document.forms[0].sessionId.value;
    //    document.forms[0].action = "FeedbackAction.do?method=getFeedbackForm&sessionId="+sessionId;
    document.forms[0].action = "FeedbackAction.do?method=getFeedbackForm";
    document.forms[0].submit();
}

function validateImg(imgObj) {
    var image = imgObj.value;
    if(image != "") {
        var fileExt = image.substring(image.lastIndexOf(".")+1);

        // Checking Extension
        if(!(fileExt == "jpg" || fileExt == "JPG" || fileExt == "jpeg" || fileExt == "JPEG" ||
            fileExt == "png" || fileExt == "PNG" || fileExt == "gif" || fileExt == "GIF")) {
            alert("Upload  jpg, jpeg, png or gif images only.");
            imgObj.value = "";
            return false;
        }
    }
    
    return true;
}

function validateJSONArr(jsonArr) {
    var jsonValid = true;
    var counter = 0;

    for(var i = 0; i < jsonArr.length; i++) {
        var val = eval(jsonArr[i][1]);

        if(val == 0) {
            counter++;
        }

        if(counter == jsonArr.length) {
            jsonValid = false;
        }
    }

    return jsonValid;
}

function validateChart(jsonArr) {
    var jsonValid = true;
    var counter = 0;
    if(jsonArr)
    {
        for(var i = 0; i < jsonArr[0].length; i++) {
            var val = eval(jsonArr[0][i]);

            if(val == 0) {
                counter++;
            }

        
        }
        if(counter == jsonArr[0].length) {
            jsonValid = false;
        }
    }else
        jsonValid = false;
    return jsonValid;
}

function getWidgetsList() {
   /* document.forms[0].action = "DevAction.do?method=getWidgetList&widgetType=all&widgetCat=all&sortBy=none&when=noPref"+
    "&srchWord=&widgets=8&pageNum=1";*/
    document.forms[0].action = "jsp/RedirectUser.jsp";
    document.forms[0].submit();
}

$(document).ready(function(){
    $(".dropdown").click(function() {
        $(this).find("ul.subdrop").slideToggle('fast');
    });
});

function changeAll(data, place){ 
    var res = data.innerHTML;
    document.getElementById(place).innerHTML = res;
}
