/***
 * Options used in various functions
 */

var delay_time = 5000;
var fadeout_time = 1000;

var cf_option = {
    repeat: 0,
    params: [
    {
        fadeout:100,
        stayout:300,
        opout:0,
        fadein:100,
        stayin:3000,
        opin:0.5
    },

    {
        fadeout:100,
        stayout:300,
        opout:0,
        fadein:100,
        stayin:300,
        opin:0.75
    },

    {
        fadeout:100,
        stayout:300,
        opout:0,
        fadein:100,
        stayin:300,
        opin:1.0
    }]
};

jQuery.validator.addMethod("complete_url", function(val, elem) {
    // if no url, don't do anything
    if (val.length == 0) {
        return true;
    }
 
    // if user has not entered http:// https:// or ftp:// assume they mean http://
    if(!/^(https?|ftp):\/\//i.test(val)) {
        val = 'http://'+val; // set both the value
        $(elem).val(val); // also update the form element
    }
    // now check if valid url
    // http://docs.jquery.com/Plugins/Validation/Methods/url
    // contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/
    return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(val);
});

function redirect(sUrl)
{
    //alert(sUrl);
    if(sUrl!="")
        window.location=sUrl;
}

function confirmDelete(){
    var sMessage = "Are you sure to delete this item?";

    return confirm(sMessage);
}

function confirmPause(){
    var sMessage = "Are you sure to pause this campaign?";

    return confirm(sMessage);
}
function confirmResume(){
    var sMessage = "Are you sure to resume this campaign?";

    return confirm(sMessage);
}

function confirmBanned(){
    var sMessage = "Are you sure to Banned this user?";

    return confirm(sMessage);
}
function confirmUnBanned(){
    var sMessage = "Are you sure to Unbanned this user?";

    return confirm(sMessage);
}
function confirmResend(){
    var sMessage = "Are you sure to resend activation email to this user?";

    return confirm(sMessage);
}
function isDefined(myObj)
{
    retVar = false;

    if(typeof(myObj)!='undefined')
        retVar = true;

    return retVar;
}

function isNull(myObj)
{
    retVar = false;

    if(myObj==null)
        retVar = true;

    return retVar;
}

function handleException(dataSet)
{
    alert(dataSet.message);
    return true;
}

function printMessage(msg, typ){
    alert(msg);
}

$.fn.clearForm = function() {
    // iterate each matching form
    return this.each(function() {
        // iterate the elements within the form
        $(':input', this).each(function() {
            var type = this.type, tag = this.tagName.toLowerCase();
            if (type == 'text' || type == 'password' || tag == 'textarea')
                this.value = '';
            else if (type == 'checkbox' || type == 'radio')
                this.checked = false;
            else if (tag == 'select')
                this.selectedIndex = -1;
        });
    });
};

function registerMe(frm){
    $("#regAjax").html("Processing ...").show();
    $(frm).hide();

    $.post(site_url+'api/user/register',
        $(frm).serialize(),
        function (data){
            if(isDefined(data) && !isNull(data))
            {
                if(typeof(data)=="string")
                    data = JSON.parse(data);

                if(isDefined(data.ets_response) && !isNull(data.ets_response))
                {
                    if(isDefined(data.ets_response.reg) && data.ets_response.reg == true)
                    {
                        var ifrmTracking = $("<iframe>").attr("src","./conversion_tracking.html").attr("height","0").attr("width","0");
                        $("#regAjax").html(data.ets_response.message);
                        $("#regAjax").append(ifrmTracking);

                    //window.location = site_url+'user/redirect';
                    }
                    else
                    {
                        $("#regAjax").html("");
                        pMessage = $("<p/>").html("");
                        aReg = $("<a/>").attr("href","javascript:;")
                        .html("Click here").css("margin","0 4px")
                        .bind("click",function(){
                            $(this).parent().hide();
                            $(frm).show();
                        });
                        pMessage.html(data.ets_response.message + " To change your sign up information");
                        pMessage.append(aReg);
                        //alert(data.ets_response.message);
                        $("#regAjax").append(pMessage);
                    }
                }
            }
        },
        "json");
}



function logMeIn(frm){
    $("#loginAjax").html("Authenticating ...").show();
    $(frm).hide();

    $.post(site_url+'api/user/login',
        $(frm).serialize(),
        function (data){
            if(isDefined(data) && !isNull(data))
            {
                if(typeof(data)=="string")
                    data = JSON.parse(data);

                if(isDefined(data.ets_response) && !isNull(data.ets_response))
                {
                    if(isDefined(data.ets_response.auth) && data.ets_response.auth == true)
                    {
                        $("#loginAjax").html("Redirecting to your location ...");
                        window.location = site_url+'user/redirect';
                    }
                    else
                    {
                        aLogin = $("<a/>").attr("href","javascript:;")
                        .html("Login Again").css("margin","0 4px")
                        .bind("click",function(){
                            $(this).parent().hide();
                            $(frm).show();
                        });
                        //alert(data.ets_response.message);
                        $("#loginAjax").html(data.ets_response.message).append(aLogin);
                    }
                }
            }
        },
        "json");
}


// Private function to load the data
$.fn.showFormView = function(){
    //alert(1)
    placeHolder = $(this);
    viewName = placeHolder.attr("id");

    $(placeHolder).show(0,function(){
        myForm = placeHolder.children('form');

        if(myForm.attr('action') != ""){
            myForm.html("Loading ...");

            $.post(myForm.attr('action'), {
                "v":viewName
            }, function(data){
                myForm.html("");

                if(typeof(data)=="string")
                    data = JSON.parse(data);

                if (data.success === true) {
                    myForm.html(data.message);
                } else {
                    printMessage(data.message);
                }
            }, "json")
        }

    //alert(placeHolder.children('form').attr('action'));
    });
}

$(document).ready(function(){
    $(".closeIco").live("click", function(){
        $(this).parent().hide();
    });

    $(".trCloseIco").live("click", function(){
        $(this).parents("tr").hide();
    });
    
});
