﻿
function countText(adtxt) {
    document.getElementById("countAdText").innerHTML = document.getElementById(adtxt).value.length;
    document.getElementById("AdPreview").innerHTML = document.getElementById(adtxt).value;
}

function CheckValidAdinStep1(npmcat, npsubcat, mcast, sby, pubid, state, city, tsitems, emailid, pass, email, fname, lname) {

    if ($("#" + npmcat + "").val() == 0) {
        alert("Please Select Main Category ");
        $("#" + npmcat + "").focus();
        return false;
    }

    if ($("#" + npsubcat + "").val() == 0 & $("#" + npmcat + "").val() != 7) {
        alert("Please Select Sub Category ");
        $("#" + npsubcat + "").focus();
        return false;
    }

    //? Check Preferred Classification fot Matrimonial Category.
//    var mcat = $("#" + npmcat + " option:selected").text();
//    if (mcat == "matrimonial") {

//        if ($("#" + mcast + "").val() == '') {

//            alert("Please select Preferred Classification");
//            $("#" + mcast + "").focus();
//            return false;

//        }

//    }

    //? Check Ad Type
    var c = TotalSelectedAdType();

    if (c == 0) {

        alert("Please select ad Type");
        return false;
    }

    //? Check if Total selected items is zero.
    if ($("#" + tsitems + "").val() == "0") {

        if ($("#SelectBy").val() == "sByNp") {

            if ($("#" + pubid + "").val() == 0) {
                alert("Please Select Edition or Any Package");
                $("#" + pubid + "").focus();
                return false;
            }



            if ($("#ChEdition :checkbox:checked").length == 0 & $("#DivSprSvrPackage :checkbox:checked").length == 0) {

                alert("Please select any Edition or Package");
                return false;
            }

            // alert($("#SprSvrPackage :checkbox:checked").length);

        }
        if ($("#SelectBy").val() == "sByL") {

            if ($("#" + state + "").val() == 0) {
                alert("Please Select State ");
                $("#" + state + "").focus();
                return false;


            }

            if ($("#" + city + "").val() == 0) {
                alert("Please Select City ");
                $("#" + city + "").focus();
                return false;
            }

            if ($("#DivSprSvrPackage1 :checkbox:checked").length == 0) {

                alert("Please select Package");
                return false;
            }

        }
    }

    if ($("#ValiCusPanel").val() == "CheckEmail") {

        if ($("#" + emailid + "").val().trim().length == 0) {

            alert("Please enter Email ID");
            $("#" + emailid + "").focus();
            return false;
        }
        else {

            return emailCheck($("#" + emailid + "").val().trim());


        }

    }

    if ($("#ValiCusPanel").val() == "CheckEmailPass") {

        if ($("#" + emailid + "").val().trim().length == 0) {

            alert("Please enter Email ID");
            $("#" + emailid + "").focus();
            return false;
        }

        if ($("#" + pass + "").val().trim().length == 0) {

            alert("Please enter Password");
            $("#" + pass + "").focus();
            return false;
        }
    }

    if ($("#ValiCusPanel").val() == "CheckEmailForNewUser") {

        if ($("#" + fname + "").val().trim().length == 0) {

            alert("Please enter First Name");
            $("#" + fname + "").focus();
            return false;
        }

        if ($("#" + lname + "").val().trim().length == 0) {

            alert("Please enter Last Name");
            $("#" + lname + "").focus();
            return false;
        }

        if ($("#" + email + "").val().trim().length == 0) {

            alert("Please enter Email ID");
            $("#" + email + "").focus();
            return false;
        }

        if ($("#" + email + "").val().trim().length > 0) {

            //            if (!emailCheck($("#" + email + "").val())) {
            //                $("#" + email + "").val() = "";
            //                return false;
            //            }

            return emailCheck($("#" + email + "").val());
        }

    }
}

function CheckValidAdinStep2(adname, adtext, tsdates) {

    if ($("#" + adname + "").val().trim().length == 0) {
        alert("Please enter Advertisement Name");
        $("#" + adname + "").focus();
        return false;
    }

    if ($("#" + adtext + "").val().trim().length == 0) {
        alert("Please enter Advertisement Text");
        $("#" + adtext + "").focus();
        return false;
    }

    //    if ($("#" + tsdates + "").val() <= 0) {
    //        alert("Please select insertion date");
    //        return false;
    //    }

    //======== For Select Release Date ==============// 

    var arrayResult = ($("#ListofItems").val()).split("|");

    if (arrayResult.length > 0) {
        var resultType = arrayResult[0];

        if (resultType.length > 1) {

            alert("Please select date for, " + resultType);
            return false;

        }

    }

}

//==================================== Ad Type ========================================//


function IsSelectedAdType() {

    //? Check Ad Type

    var c = TotalSelectedAdType();       

    if (c == 0) {

        alert("Please select the type of advertisement first from above, to filter the packages.");
        return false;
    }    

}

//? Return Total Selected Ad Type.
function TotalSelectedAdType() {

    var c = 0;
    for (i = 0; i < document.forms[0].elements.length; i++) {

        elm = document.forms[0].elements[i]

        if (elm.type == 'radio') {

            if (elm.checked == true) {
                c++;
            }
        }
    }

    return c;
}



//==================================== /Ad Type ========================================//


function MessageForPendingAds() {

    alert("You can pay for this ad, when ad will be Approved By Admin.");
    return flase;
}

function SureBookThisAd() {

    return confirm("Are you Sure want to Book This Ad ?");
}

function SureDeleteThisAd() {

    return confirm("Are you Sure want To Delete This Ad ?");

}

/*================= Trouble Tickets =======================================*/

function checkValidTicket(emailid, subject, message) {

    if ($("#IsShowEmail").val() == "1") {

        if ($("#" + emailid + "").val().trim().length == 0) {

            alert("Please enter Email ID");
            $("#" + emailid + "").focus();
            return false;
        }
        if ($("#" + emailid + "").val().trim().length > 0) {

            if (!emailCheck($("#" + emailid + "").val().trim())) {

                $("#" + emailid + "").val("");
                return false;
            }
        }
    }
    if ($("#" + subject + "").val().trim().length == 0) {

        alert("Please enter Subject");
        $("#" + subject + "").focus();
        return false;
    }
    if ($("#" + message + "").val().trim().length == 0) {

        alert("Please enter Message");
        $("#" + message + "").focus();
        return false;
    }

}

/*================= /Trouble Tickets =======================================*/

/*================= News Letter =======================================*/

function CheckValidEmailID(email) {

    var emailid = $("#" + email + "").val();
    if (Trim(emailid)=='' || emailid == "Email") {

        alert("Please enter Email ID");
        $("#" + email + "").focus();
        return false;
    }

    if (Trim(emailid) != '') {

        if (!emailCheck(emailid)) {

            $("#" + email + "").val('');
            $("#" + email + "").focus();
            return false;
        }

    }
}


/*================= /News Letter =======================================*/

/*================= News Papers ========================================*/

function ValidatePubContactUs(pubname, lang, publfrom, noeditions, edition, mname, designation, phone, emailid, website) {

    if (Trim($("#" + pubname + "").val()) == '') {

        alert("Please enter Publication Name");
        $("#" + pubname + "").focus();
        return false;

    }
    if (Trim($("#" + lang + "").val()) == '') {

        alert("Please enter Language");
        $("#" + lang + "").focus();
        return false;

    }
    if (Trim($("#" + publfrom + "").val()) == '') {

        alert("Please enter Published From");
        $("#" + publfrom + "").focus();
        return false;

    }
    if (Trim($("#" + noeditions + "").val()) == '') {

        alert("Please enter No of Editions");
        $("#" + noeditions + "").focus();
        return false;

    }
    if (Trim($("#" + noeditions + "").val()) != '') {

        if (isNaN($("#" + noeditions + "").val())) {

            alert("No of Editions must be Digit");
            $("#" + noeditions + "").val('');
            $("#" + noeditions + "").focus();
            return false;


        }

    }


    if (Trim($("#" + edition + "").val()) == '') {

        alert("Please enter Edition Name");
        $("#" + edition + "").focus();
        return false;

    }
    if (Trim($("#" + mname + "").val()) == '') {

        alert("Please enter Name");
        $("#" + mname + "").focus();
        return false;

    }
    if (Trim($("#" + designation + "").val()) == '') {

        alert("Please enter Designation");
        $("#" + designation + "").focus();
        return false;

    }
    if (Trim($("#" + phone + "").val()) == '') {

        alert("Please enter Phone");
        $("#" + phone + "").focus();
        return false;

    }
    if (Trim($("#" + emailid + "").val()) == '') {

        alert("Please enter Email ID");
        $("#" + emailid + "").focus();
        return false;

    }

    if (Trim($("#" + emailid + "").val()) != '') {


        if (!emailCheck($("#" + emailid + "").val())) {

            $("#" + emailid + "").val('');
            $("#" + emailid + "").focus();
            return false;
        }

    }
    if (Trim($("#" + website + "").val()) != '') {

        if (checkURL($("#" + website + "").val()) == false) {

            alert("Please enter Valid url of Website");
            return false;

        }

    }
}

/*================= /News Papers ========================================*/

/*================= Special Packages ========================================*/

function CheckIteminCart(count) {

    if (count > 0) {

        return confirm("If You select other Category then selected packages will be losed");

        } 

}

/*================= /Special Packages ========================================*/


//? For checking of valid Email ID

function emailCheck(str) {
    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)

    // check if '@' is at the first position or  at last position or absent in given email 
    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        alert("Invalid E-mail ID")
        return false
    }
    // check if '.' is at the first position or at last 
    //   position or absent in given email
    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        alert("Invalid E-mail ID")
        return false
    }
    // check if '@' is used more than one times in given email
    if (str.indexOf(at, (lat + 1)) != -1) {
        alert("Invalid E-mail ID")
        return false
    }

    // check for the position of '.'
    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        alert("Invalid E-mail ID")
        return false
    }
    // check if '.' is present after two characters 
    //  from location of '@'
    if (str.indexOf(dot, (lat + 2)) == -1) {
        alert("Invalid E-mail ID")
        return false
    }

    // check for blank spaces in given email
    if (str.indexOf(" ") != -1) {
        alert("Invalid E-mail ID")
        return false
    }
    return true
}

//? For Checking Valid Url
function checkURL(value) {
    var urlregex = new RegExp(
        "^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
    if (urlregex.test(value)) {
        return (true);
    }
    return (false);
}


// Removes leading whitespaces
function LTrim(value) {

    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");

}

// Removes ending whitespaces
function RTrim(value) {

    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");

}

// Removes leading and ending whitespaces
function Trim(value) {

    return LTrim(RTrim(value));

}





