﻿//Modify the jquery function name
var j = jQuery.noConflict();


function isWebkit() {
    var flag = true;

    if (!j.browser.webkit) {

        flag = false;

    }


    return flag;

}


function getVar(name) {
    get_string = document.location.search;
    return_value = '';

    do { //This loop is made to catch all instances of any get variable.
        name_index = get_string.indexOf(name + '=');

        if (name_index != -1) {
            get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);

            end_of_value = get_string.indexOf('&');
            if (end_of_value != -1)
                value = get_string.substr(0, end_of_value);
            else
                value = get_string;

            if (return_value == '' || value == '')
                return_value += value;
            else
                return_value += ', ' + value;
        }
    } while (name_index != -1)

    //Restores all the blank spaces.
    space = return_value.indexOf('+');
    while (space != -1) {
        return_value = return_value.substr(0, space) + ' ' +
              return_value.substr(space + 1, return_value.length);

        space = return_value.indexOf('+');
    }

    return (return_value);
}


function showUserFeedback(theTitle, theText, feedBackType) {
    j.pnotify({
        pnotify_title: theTitle,
        pnotify_text: theText,
        pnotify_type: feedBackType,
        pnotify_hide: false,
        pnotify_opacity: .8,
        pnotify_before_open: function(pnotify) {
            pnotify.css({
                "top": 5,
                "left": (j(window).width() / 2) - (pnotify.width() / 2)
            });
        }
    });
}

function toogleNotice() {
    if (permanotice) {
        permanotice.pnotify_display();
    }
    else {
        permanotice = $.pnotify({
            pnotify_title: 'Now Look Here',
            pnotify_text: 'There\'s something you need to know, and I won\'t go away until you come to grips with it.',
            pnotify_nonblock: true,
            pnotify_hide: false,
            pnotify_closer: false
        });
    }

    //remove
    if (permanotice.pnotify_remove)
        permanotice.pnotify_remove();

}

function gup(name) {// function to get any value from a GET parameter of the current page
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

function OnEditorInit(s, e) {
    InitializeInvalidStyle(s);
}
function InitializeInvalidStyle(editor) {
    editor.Validation.AddHandler(function(s, e) {
        if (s.GetIsValid() && e.isValid)
            ApplyValidStyle(s, e);
        else
            ApplyInvalidStyle(s, e);
    });
}

//Valid Style
//TODO: use styles defined in the main StyleSheet
function ApplyValidStyle(editor, e) {
    editor.GetMainElement().style.border = "1px solid #9f9f9f";
    editor.GetInputElement().style.color = "black";
    editor.GetInputElement().style.background = "white";

    var editorid = editor.GetMainElement().id.toString();
    var msgid = "msg_" + editorid;

    j("#" + msgid).remove();
    j("#" + editorid).unbind("focusin");
    j("#" + editorid).unbind("focusout");

}
// Invalid style
//TODO: use styles defined in the main StyleSheet
function ApplyInvalidStyle(editor, e) {
    editor.GetMainElement().style.border = "1px solid #A0A0A0";
    editor.GetInputElement().style.color = "black";
    //#FFFFCC
    //#FFEBE8
    editor.GetInputElement().style.background = "#FFFFCC";


    var editorid = editor.GetMainElement().id.toString();
    var msgid = "msg_" + editorid;

    j("#" + editorid).focusin(function() {
        j(this).feedback(e.errorText, msgid, { type: "error", right: true });
    });

    j("#" + editorid).focusout(function() {
        j("#" + msgid).remove();
    });

}

function minimumPhoneNumberLength(s, e) {
    e.isValid = true;

    if (s.GetText() != "" && s.GetText().length < 8) {
        e.isValid = false;
    }
}



function autoCompleteOff() {
    j(document).find("input[type=text]").attr('autocomplete', 'off');
}


//esta funcion nos lleva al metodo establecido en companyParameter para formatear los nombres
function nameFormat(metodo, nameText) {
    
    var result='';
    switch (metodo) {
        case "Capitalizar":
            result=Capitalizar(nameText);
            break;

        case "Mayuscula":
            result=Mayuscula(nameText);
            break;
    }

    return result;

}

//Capitaliza los nombres
function Capitalizar(nombre) {

    // definimos un array de articulos (en minuscula)    
    var articulos = ['a', 'de', 'del', 'la', 'los', 'las', 'A', 'DE', 'DEL', 'LA', 'LOS', 'LAS'];

    // separamos el nombre
    var palabras = j.trim(nombre).split(' ');


    // creamos la variable que contendra el nombre
    // formateado
    var nuevoNombre = '';

    // parseamos cada palabra
    var i;
    for (i = 0; i < palabras.length; i++) {
        // si la palabra es un articulo
        if (j.inArray(palabras[i], articulos) > -1) {
            // concatenamos seguido de un espacio
            nuevoNombre = nuevoNombre.concat(' ' + palabras[i].toLowerCase());
        }
        else {
            // sino, es un nombre propio, por lo tanto aplicamos
            // las funciones y concatenamos seguido de un espacio
            nuevoNombre = nuevoNombre + ' ' + palabras[i].charAt(0).toUpperCase() + palabras[i].substring(1).toLowerCase();
        }
    }

    return j.trim(nuevoNombre);
}

//Esta funcion pone en mayusculas los nombres
function Mayuscula(nameText) {
    return nameText.toUpperCase();
}



