﻿/********
* Author: Scott Underhill 
* Contact: underhill.scott at the gmail 
* Script for supporting junta42 web site 
data: { type: "'" + cat_type + "'" },
*/

function ContentLookup(cat_type) {
    $.ajax({
        type: "POST",
        url: "../AjaxHelper.asmx/GetContentType",
        data: "{ type: '" + cat_type + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            // alert(msg.d);
            $('#catTypes').empty();
            
            $('#catTypes').append(msg.d);
        },
        error: function (e) {
            alert(e);
        }
    });
};

function ContentLookup2(cat_type) {
    $.ajax({
        type: "POST",
        url: "../AjaxHelper.asmx/GetContentType2",
        data: "{ type: '" + cat_type + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            // alert(msg.d);
            $('#catTypes').empty();

            $('#catTypes').append(msg.d);
        },
        error: function (e) {
            alert(e);
        }
    });
};


function sendpassword(email, control) {
    $.ajax({
        type: "POST",
        url: "../AjaxHelper.asmx/sendpassword",
        data: "{ username: '" + email + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            // alert(msg.d);
            $(control).empty();

            $(control).append(msg.d);
        },
        error: function (e) {
            alert(e);
        }
    });
};

// Removes an uploaded image from session 
function DeleteImage(name,control) {
    $.ajax({
        type: "POST",
        url: "../AjaxHelper.asmx/DeleteImageSession",
        data: "{ imageName: '" + name + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            
            // Remove delete span 
            $(control).next().remove();

            
            $(control).remove();
            // $('#indTypes').append(msg.d);
        },
        error: function (e) {
            // alert(e);
        }
    });
}

function TagertLookup(cat_type) {
    $.ajax({
        type: "POST",
        url: "../AjaxHelper.asmx/GetTargetType",
        data: "{ type: '" + cat_type + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            // alert(msg.d);
            $('#indTypes').empty();

            $('#indTypes').append(msg.d);
        },
        error: function (e) {
            alert(e);
        }
    });
};


function SubmitContactUs(message) {
    $.ajax({
        type: "POST",
        url: "../AjaxHelper.asmx/SubmitContactUs",
        data: "{ message: '" + message + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
     
        },
        error: function (e) {
     
        }
    });
};

function DeleteFile(file) {
    $.ajax({
        type: "POST",
        url: "../AjaxHelper.asmx/delFile",
        data: "{ file: '" + file + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {

        },
        error: function (e) {

        }
    });
};

var last_errorMessage = "";

function Validation() {
    return Validation("");
}
function Validation(form) {

    last_errorMessage = "";
    var p1 = "";
    var error = false;
    $(form + ' input[validation],' + form + ' textarea[validation]').each(function (e) {
        var arry = $(this).attr('validation').split(":");
        // Required field 
        if (inArray("r", arry)) {
            if ($(this).text().length == 0 && $(this).val().length == 0) {
                // Assuming label and br. There is probably lot better way of doing this 
                var label = $(this).prev().prev().text();
                try {
                    if ($(this).prev()[0].tagName == "LABEL")
                        label = $(this).prev().text();
                } catch (xx) { }
                try {
                    if ($(this).parent()[0].tagName == "TD") {
                        label = $(this).parent().prev().children().text();
                    }
                } catch (xx) { }
                last_errorMessage += "<li>" + label + " (required)</li>";
                error = true;
            }
        }
        if (inArray("p1", arry)) {
            if (typeof (p1) == "undefined" || p1 == "") {
                p1 = $(this).val();
            } else if (p1 != $(this).val()) {
                last_errorMessage += "<li> Password mismatch</li>";
                error = true;
            }
        }
    });
    if (error) {
        if (form == undefined) {
            form = "";
        }
            $(form + ' #error').empty();
            $(form + ' #error').append("<ul>" + last_errorMessage + "</ul>");
            try {
                $('body').animate({ scrollTop: 250 }, 'slow');
            } catch (ex) { }
            try {
                $('html').animate({ scrollTop: 250 }, 'slow');
            } catch (ex) { }
        
        
    }
    return error;

}

function inArray(needle, haystack) {
    var length = haystack.length;
    for (var i = 0; i < length; i++) {
        if (haystack[i] == needle) return true;
    }
    return false;
}

function AddForm(formName){
    if($("#formtype").length == 0){
        $("form").append("<input id='formtype' name='formtype' type='hidden' value='"+formName+"' />");
      }else{
        $("#formtype").val(formName);
      }
}
