/*******************************************************************************
**
** FileName: pop.js
**
** Version 1.0
**
*******************************************************************************/

function OpenWindow(jURL,jNm,jW,jH,jSX,jSY,jLc,jMb,jSt,jTb,jSb,jRs)
{

// abreJanela(URL,wName,width,height,left,top,location,menubar,status,toolbar,scrollbars,resizable)
// Variaveis de entrada
// jImg	Imagem a ser editada
// jURL	Pagina a ser aberta na janela
// jNm	Nome da janela a ser aberta
// jW	Largura da janela
// jH	Altura da janela
// jSX	Posicao horizontal da janela
// jSY	Posicao vertical da janela
// jLc	location=
// jMb	menubar=
// jSt	status=
// jTb	toolbar=
// jSb	scrollbars=
// jRs	resizable=
	if (jNm == '') jNm = '_blank';
//	Montagem da sequencia de parametros
	sParam = "width=" + jW + ",";
	sParam = sParam + "height=" + jH + ",";
	sParam = sParam + "left=" + jSX + ",";
	sParam = sParam + "top=" + jSY + ",";
	sParam = sParam + "location=" + jLc + ",";
	sParam = sParam + "menubar=" + jMb + ",";
	sParam = sParam + "status=" + jSt + ",";
	sParam = sParam + "toolbar=" + jTb + ",";
	sParam = sParam + "scrollbars=" + jSb + ",";
	sParam = sParam + "resizable=" + jRs;
//	Abertura da janela
	var aJanela = null;
	aJanela = window.open(jURL,jNm,sParam);
	if (aJanela != null ) {
		if (aJanela.opener == null ) {
			aJanela.opener = self;
			self.location.href=document.location;
		}
		//aJanela.location.href=jURL;	
		aJanela.focus();
	}
}

function OpenWindowSimple(jURL) {
    OpenWindow(jURL, 'Printer', '800', '600', '100', '100', '0', '1', '0', '0', '1', '1');
}

function limitLength(name, max) {

     try {
         var field = document.getElementById(name);
         if (field.value != null) {
             var len = field.value.length;

             if (parseInt(len) > parseInt(max)) {
                 field.value = field.value.substring(0, max);
             }
         }
     } catch (error) {
     }
 }

 function limitLengthSimple(field, max) {

     try {         
         if (field.value != null) {
             var len = field.value.length;

             if (parseInt(len) > parseInt(max)) {
                 field.value = field.value.substring(0, max);
             }
         }
     } catch (error) {
     }
 }

//used in email tasks/comments
function PopUp_ViewEmail(a, ID) {

   // var a = document.getElementById(href);

    a.href = "/admin/popup/ViewEmail.aspx?SalesEmailID=" + ID + "&TB_iframe=true&amp;height=680&amp;width=805&amp;modal=true";
}


function removeUserCookie() {

    document.cookie = "wc_user=;path=/;expires=Thu, 01-Jan-70 00:00:01 GMT;";
    window.location = window.location;
}

//Get cookie routine by Shelley Powers 
function get_cookie(Name) {
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        // if cookie exists
        if (offset != -1) {
            offset += search.length
            // set index of beginning of value
            end = document.cookie.indexOf(";", offset);
            // set index of end of cookie value
            if (end == -1) end = document.cookie.length;
            returnvalue = unescape(document.cookie.substring(offset, end))
        }
    }
    return returnvalue;
}

//simple helper function to return a value from a cookie.Returns null if no value
function reva_getCookieValue(cookieName) {
    var nameEQ = cookieName + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function reva_GetCookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}


function reva_SetCookie(name, value, expires) {
    var expires_date = new Date();
    if (expires) {
        expires_date.setDate(expires_date.getDate() + expires);
    }

    document.cookie = name + "=" + value +
	((expires) ? ";expires=" + expires_date.toGMTString() : "") + ";path=/";
    //	( ( path ) ? ";path=" + path : "" ) +
    //	( ( domain ) ? ";domain=" + domain : "" );
}


function wc_GetFcid(runAfter) {
    gvAjaxManager.ajaxStart();
    $.ajax({
        url: "/consumer/ajax/detectusercountry.aspx",
        data: {},
        async: true,
        dataType: "json",
        success: function (_data) {

            reva_SetCookie("fcid", _data.fcid, 30);

            wc_SetFcidValues(_data.fcid.split("|"));
            
                
            if (runAfter)
                runAfter();

            gvAjaxManager.ajaxStop();
        },
        onerror: function (errorMsg) {
            gvAjaxManager.ajaxStop();
        }
    });




}

function wc_SetFcidValues(splitParams) {
    providersListParams.fcid = splitParams[0];
    var tmpCurrency = splitParams[1];
    switch (tmpCurrency) {
        case '0':
            revaGV_Currency = "&euro;";
            break;
        case '1':
            revaGV_Currency = "&pound;";
            break;
        case '2':
            revaGV_Currency = "&yen;";
            break;
        case '3':
            revaGV_Currency = "&#376;";
            break;
        default:
            revaGV_Currency = tmpCurrency;
            break;

    }
    revaGV_CurrencyRate = splitParams[2];
    lastCharCode = revaGV_Currency.charCodeAt(revaGV_Currency.length - 1);
    revaGV_PrefixSymbol = (lastCharCode > 64 && lastCharCode < 91) || (lastCharCode > 96 && lastCharCode < 123) ? false : true;

    if (providersListParams.fcid > 0)
        wc_setCountryFlag(splitParams);
        //wc_setCountryFlag($(".dropdown ul li.cid_" + providersListParams.fcid.split("|")[0] + " .flag").css("background-position"), $(".dropdown ul li.cid_" + providersListParams.fcid.split("|")[0] + " .flag").css("background-image"));
}


function wc_setCountryFlag(splitParams) {
    var image = splitParams.length > 3 ? splitParams[3] : (splitParams[0] != '0' ? $(".dropdown ul li.cid_" + splitParams[0] + " .value").text().split("|")[3] : "ie");    
    $(".dropdown dt .flag").css({ "background-position": "0px 0px", "background-image": "url(/images/countries/" + image.toLowerCase() + ".png)" });
}

function isNumberKey(event) {
    if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 32 || event.keyCode == 109) {

    }
    else {
        if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
            event.preventDefault();
        }
    }
}

/*
used on new consultation form popup to resize itself depending on content
created: 03-jan-2012 [damien]
*/
function wc_SetPopupSize(elmID,newHeight) {
    document.getElementById(elmID).style.height = newHeight + "px";
    
}

