﻿///	<changes>
/// 2008-08-26  EF      Fix BUG in height calculations (';' character was included in height);
/// 2008-09-15  SS      Added logic to disable the context menu 
/// 2008-10-24  SS      Added logic to check valid credit card number
///	2008-12-11	EF		Add and remove a non visible iframe. Helps to render certifica control in ajax calls.
/// 2009-04-22  FF      Add function to generate phones
/// 2009-12-08  FF      Bring LimitChars from AgregarTexto.aspx
///	</changes>
var cInicio = 1;
var cBienesRaices = 2;
var cVehiculos = 3;
var cEmpleos = 4;
var cServicios = 5;
var cOtros = 6;
var cSubastalo = 7;

version = navigator.appVersion;
/*if (version.toLowerCase().indexOf("mac")!=-1){
document.write('<link href="css/mac.css" rel="stylesheet" type="text/css" />');
}*/

var agt = navigator.userAgent.toLowerCase();
if (agt.indexOf("safari") != -1) {
    document.write('<link href="css/mac.css" rel="stylesheet" type="text/css" />');
}

// CSS Browser Selector   v0.2.5
var css_browser_selector = function() {
    var 
		ua = navigator.userAgent.toLowerCase(),
		is = function(t) { return ua.indexOf(t) != -1; },
		h = document.getElementsByTagName('html')[0],
		b = (!(/opera|webtv/i.test(ua)) && /msie (\d)/.test(ua)) ? ('ie ie' + RegExp.$1) : is('gecko/') ? 'gecko' : is('opera/9') ? 'opera opera9' : /opera (\d)/.test(ua) ? 'opera opera' + RegExp.$1 : is('konqueror') ? 'konqueror' : is('applewebkit/') ? 'webkit safari' : is('mozilla/') ? 'gecko' : '',
		os = (is('x11') || is('linux')) ? ' linux' : is('mac') ? ' mac' : is('win') ? ' win' : '';
    var c = b + os + ' js';
    h.className += h.className ? ' ' + c : c;
} ();

function IsIE8Browser() {
    var rv = -1;
    var ua = navigator.userAgent;
    var re = new RegExp("Trident\/([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null) {
        rv = parseFloat(RegExp.$1);
    }
    return (rv == 4);
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}
function KW_autoClear(obj, def) {
    if (obj.value == def) obj.value = ""
}

function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; } 
    }
}

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.osrc; i++) x.src = x.osrc;
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.1
    if (typeof (nPaginaActual) == "undefined") nPaginaActual = cInicio;
    var i, j = 0, x, a = MM_swapImage.arguments; if (a[4] != nPaginaActual) {
        document.MM_sr = new Array; for (i = 0; i < (a.length - 3); i += 3)
            if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.osrc) x.osrc = x.src; x.src = a[i + 2]; } 
    }
}
/*TOGGLER*/
function showBox(obj) {
    var el = document.getElementById(obj);

    if (el.style.display != 'none') {
        el.style.display = 'none';
    } else {
        el.style.display = 'block';
    }
}
/*TOGGLER CLASS*/
function toggleNextById(el) {
    //var ccn="Rojo";
    var clicker = document.getElementById(el);
    clicker.className = ((clicker.className == "Marked") ? "Active" : "Marked");
}

function P7_autoLayers() { //v1.4 by PVII
    var g, b, k, f, args = P7_autoLayers.arguments; a = parseInt(args[0]); if (isNaN(a)) a = 0;
    if (!document.p7setc) {
        p7c = new Array(); document.p7setc = true; for (var u = 0; u < 10; u++) {
            p7c[u] = new Array();
        } 
    } for (k = 0; k < p7c[a].length; k++) {
        if ((g = MM_findObj(p7c[a][k])) != null) {
            b = (document.layers) ? g : g.style; b.visibility = "hidden";
        } 
    } for (k = 1; k < args.length; k++) {
        if ((g = MM_findObj(args[k])) != null) {
            b = (document.layers) ? g : g.style; b.visibility = "visible"; f = false;
            for (var j = 0; j < p7c[a].length; j++) { if (args[k] == p7c[a][j]) { f = true; } }
            if (!f) { p7c[a][p7c[a].length++] = args[k]; } 
        } 
    }
}

//2008-08-20    SS      Added support for safari browser

function resizeIframe(frameid) {

    var getFFVersion = navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1];
    var FFextraHeight = parseFloat(getFFVersion) >= 0.1 && parseFloat(getFFVersion) < 3 ? 16 : 0; //extra height in px to add to iframe in FireFox 1.0+ browsers
    //2008-08-20    SS      Added support for safari browser
    var safari = navigator.userAgent.indexOf("Safari");
    var currentfr = document.getElementById(frameid);

    if (currentfr && typeof (arraySelecciones[0]) == 'string') //currentfr.Document.getElementById("frmCamposLoaded") != null)
    {
        //2008-08-20    SS      Added support for safari browser    
        if (!window.opera && safari == -1) {

            //alert('IE o FX');
            //backgroundColor
            //alert('currentfr.style.backgroundColor ' + currentfr.style.backgroundColor);
            currentfr.style.display = "block";
            //alert('currentfr.style.backgroundColor 2 ' + currentfr.style.backgroundColor);

            if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) //ns6 syntax
            {
                //  2008-08-26  EF      Fix BUG in height calculations (';' character was included in height);
                //                currentfr.style.height = currentfr.contentDocument.body.offsetHeight+FFextraHeight + 'px;'; 
                currentfr.style.height = currentfr.contentDocument.body.offsetHeight + FFextraHeight + 'px';
                //alert('currentfr.style.backgroundColor 3.1 ' + currentfr.style.backgroundColor);
            }
            else if (currentfr.Document && currentfr.Document.body.scrollHeight) //ie5+ syntax
            {
                var altura = '' + currentfr.Document.body.scrollHeight + 'px';
                currentfr.style.height = altura;
                //alert('currentfr.style.backgroundColor 3.2 ' + currentfr.style.backgroundColor);
            }
            //2008-08-20    SS      Added support for safari browser and opera 
        }
        else {
            var height = window.innerHeight;
            //            if (typeof(arraySelecciones[0]) == 'string')
            //            {
            height = currentfr.contentDocument.body.offsetHeight;
            currentfr.style.display = "block";
            currentfr.style.height = height + "px";
            //            }
        }
        //alert('currentfr.style.backgroundColor 4 ' + currentfr.style.backgroundColor);
    }
    else {
        currentfr.style.height = "0px";
        currentfr.style.display = "none";
    }
}
// 2008-09-15   SS      Added logic to disable the context menu 
function DisableContextMenu() {
    document.body.oncontextmenu = function() { return false; };
}

// 2008-10-24   SS      Added logic to check valid credit card number

function IsMasterCard(sCreditCardNumber) {
    return /^5[1-5][0-9]{14}$/.test(sCreditCardNumber);
}
function IsVisaCard(sCreditCardNumber) {
    return /^4[0-9]{12}(?:[0-9]{3})?/.test(sCreditCardNumber);
}
function IsAmericanCard(sCreditCardNumber) {
    return /^3[47][0-9]{13}$/.test(sCreditCardNumber);
}

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + num + '.' + cents);
}

//2008-09-16    AP  :Change the width  of the paginator div, depending the number of pages.
function CalcWidth(nPages) {

    //	var nRet = 16;
    var nRet = 22;
    nPages = nPages / 100;
    while (nPages >= 1) {
        nRet = nRet + 5;
        nPages = nPages / 10;
    }
    nRet = nRet + "px";
    return nRet;
}

function bestFit(divContainer, image, scalingFactor) {
//    try {

        //		alert('');
        //var iz = document.getElementById(divContainer);
        var iz = $("#" + divContainer).get(0);
        //var img = document.getElementById(image);
        var img = $("#" + image).get(0);
        //var cw = iz.clientWidth;
        var cw = $(iz).width();

        // sometimes the clientWidth is 0 in IE,
        // so look at the parent elements until
        // a valid clientWidth is found
        while (cw == 0) {
            //iz = iz.parentElement;
            iz = $(iz).parent().get(0);
            cw = $(iz).width();

            //cw = iz.clientWidth;
        }
        //var ch = iz.clientHeight;
        var ch = $(iz).height();


        // return the image back to its
        // original dimensions
        img.removeAttribute('width');

        // calculate the aspect ratio
        var ar = img.width / img.height;
//        alert("aspect ratio: " + ar);
        var newWidth;
        if (ar >= 1.0) {
            newWidth = Math.floor(cw / scalingFactor);
        }
        else {
            newWidth = Math.floor((ch * ar) / scalingFactor);
        }
        //alert(newWidth + " " + cw);
        img.setAttribute("width", newWidth > cw ? cw : newWidth);

        img.style.visibility = "visible";
//    }
//    catch (e) {
//        alert('bestFit error: ' + e.message + ' izid ' + divContainer);
//        alert($("#" + divContainer).get(0).id);
//    }
}


function bestFitWidth(containerWidth, containerHeight, image, scalingFactor) {
    var img = $("#" + image).get(0);
    var cw = containerWidth;

    var ch = containerHeight;


    // return the image back to its
    // original dimensions
    img.removeAttribute('width');

    // calculate the aspect ratio
    var ar = img.width / img.height;
    //        alert("aspect ratio: " + ar);
    var newWidth;
    if (ar >= 1.0) {
        newWidth = Math.floor(cw / scalingFactor);
    }
    else {
        newWidth = Math.floor((ch * ar) / scalingFactor);
    }
    //alert(newWidth + " " + cw);
    img.setAttribute("width", newWidth > cw ? cw : newWidth);

    img.style.visibility = "visible";
    //    }
    //    catch (e) {
    //        alert('bestFit error: ' + e.message + ' izid ' + divContainer);
    //        alert($("#" + divContainer).get(0).id);
    //    }
}


function queryStringList(name) {



    // // get the current URL
    // var url = window.location.toString();
    // //get the parameters
    // url.match(/\?(.+)$/);
    // var params = RegExp.$1;
    // // split up the query string and store in an
    // // associative array
    // var params = params.split("&");
    // var qsList = {};
    // 
    // for(var i=0;i<params.length;i++)
    // {
    //	
    // 	var tmp = params[i].split("=");
    // 	if (isEncript)
    // 	{
    // 		qsList[tmp[0]] = unescape(tmp[1]) + "=";
    // 	}
    // 	else
    //	{
    //		qsList[tmp[0]] = unescape(tmp[1]);
    //	}		
    // }
    // 
    //  return qsList;

    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];

}

///	2008-12-11	EF		Add and remove a non visible iframe. Helps to render certifica control in ajax calls.


function AddIFrame(id, url) {
    var ifrm = document.createElement("IFRAME");
    ifrm.setAttribute("src", url);
    if (ifrm != null) {
        ifrm.id = id;
    }
    //    ifrm.style.display = "none"; 
    //    ifrm.style.width = 800+"px"; 
    //    ifrm.style.height = 600+"px";
    ifrm.style.visibility = "hidden";
    ifrm.style.width = 0 + "px";
    ifrm.style.height = 0 + "px";

    document.body.appendChild(ifrm);
}

function RemoveIFrame(id) {
    var ifrm = document.getElementById(id);

    if (ifrm != null) {
        document.body.removeChild(ifrm);
    }
}

//var prm = false;
//var sdo = false;

//function mask(f, keyCode)
//{
//    alert('hola' + keyCode);
//    tel='';
//    var val = f.value.split('');
//    
//    if((keyCode>=48)&&(keyCode<=57))
//    {
//        if(val.length < 3)
//        {
//            prm = false;
//        }
//        if(val.length < 7)
//        {
//            sdo = false;
//        }
//        for(var i=0; i < val.length && i < 11; i++)
//        {
//            if(isNumber(val[i]) || val[i] == '-')
//            {
//                if(i==2 && !prm)
//                {
//                    val[i]=val[i]+'-';
//                    prm = true;
//                }
//                if(i==6 && !sdo)
//                {
//                    val[i]=val[i]+'-'
//                    sdo = true;
//                }
//                tel=tel+val[i]
//            }
//        }
//        f.value = tel;
//    }
//    else if((keyCode>=65)&&(keyCode<=90))
//    {
//        alert(f.value);
//        var s = f.value;
//        s = s.substring(0,s.length-1);
//        f.value = s;
//        f.value[f.value.length - 1] = '';
//        alert(f.value);
//    }
//}

//function isNumber(val)
//{
//    if(val == 0 || val == 1 || val == 2 || val == 3 || val == 4 || val == 5 || val == 6 || val == 7 || val == 8 || val == 9)
//    {
//        return true;
//    }
//    else
//    {
//        return false;
//    }
//}

// str is value
// textbox is html control
// loc is location
// delim is delimiter symbol
function mask(str, textbox, loc, delim) {
    var locs = loc.split(',');
    for (var i = 0; i <= locs.length; i++) {
        for (var k = 0; k <= str.length; k++) {
            if (k == locs[i]) {
                if (str.substring(k, k + 1) != delim) {
                    str = str.substring(0, k) + delim + str.substring(k, str.length);
                }
            }
        }
    }
    textbox.value = str
}

function LimitChars(textid, limit, infodiv) {

    var text = $('#' + textid).val();
    var textlength = text.length;

    if (textlength > limit) {

        //$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
        $('#' + textid).val(text.substr(0, limit));

        return false;
    } else {

        $('#' + infodiv).html('Car&aacute;cteres restantes: ' + (limit - textlength));

        return true;
    }
}

function Browser_IE8() {
    var is8 = false;
    jQuery.each(jQuery.browser, function(i, val) {
        //check browser and version
        if (i == "msie" && jQuery.browser.version.substr(0, 3) == "8.0") {
            is8 = true;
        }       
    });

    return is8;
}

function Browser_IE9() {
    var is9 = false;
    jQuery.each(jQuery.browser, function(i, val) {
        //check browser and version
        if (i == "msie" && jQuery.browser.version.substr(0, 3) == "9.0") {
            is9 = true;
        }
    });

    return is9;
}

function Browser_IE6() {
    var is6 = false;
    jQuery.each(jQuery.browser, function(i, val) {
        //check browser and version
        if (i == "msie" && jQuery.browser.version.substr(0, 3) == "6.0") {
            is6 = true;
        }
    });

    return is6;
}
function Browser_IE7() {
    var is7 = false;
    jQuery.each(jQuery.browser, function(i, val) {
        //check browser and version
        if (i == "msie" && jQuery.browser.version.substr(0, 3) == "7.0") {
            is7 = true;
        }
    });

    return is7;
}
function Browser_FF() {
    var isFF = false;
    jQuery.each(jQuery.browser, function(i, val) {
        //check browser
        if (i == "mozilla" && val) {
            isFF = true;
        }
    });

    return isFF;
}
function Browser_Chrome() {
    var isChrome = false;
    jQuery.each(jQuery.browser, function(i, val) {
        //check browser
        if (i == "safari" && val) {//Chrome uses Safari engine
            isChrome = true;
        }
    });

    return isChrome;
}
function Browser_Safari() {
    var isSafari = false;
    jQuery.each(jQuery.browser, function(i, val) {
        //check browser
        if (i == "safari" && val) {
            isSafari = true;
        }
    });

    return isSafari;
}
