﻿/*
* Contains standard Tradelink javascript
*/

/*
----------------------
* "Global" variables 
*/
var cityControl;


/*** SHOW/HIDE HELP TEXTS ***/
$j(document).ready(function() {
	$j(".HelpLink").hover(
      function () {
		$j(this).siblings(".HelpText").show("fast");
      }, 
      function () {
        $j(this).siblings(".HelpText").hide("fast");
      }
    );
});


/* AUTOTAB */
$j.fn.autotab=function(){$j(this).keyup(function(e){switch(e.keyCode){case 9:return false;case 16:return false;case 20:return false;default:var maxlength=$j(this).attr('maxlength');var inputlength=$j(this).val().length;if(inputlength>=maxlength){$j(this).siblings('input[type="text"]').focus();}}});}



/* Client side functionality for the CombinedValidator serverside control */
function CombinedValidatorIsValid(val)
{    
    var value = ValidatorGetValue(val.controltovalidate); 
    if ( val.required == 'True' || val.required == 'true' ) 
    {
        if (ValidatorTrim(value).length == 0) 
            return false;     
    }
    // Custom validation end, allow standard ASP .NET clientscript to do the rest
    if ( val.validationexpression ) 
        return RegularExpressionValidatorEvaluateIsValid(val);
    else
        return true;
}

/* Client side functionality for the CheckBoxValidator serverside control */
function CheckBoxValidatorEvaluateIsValid(arg)
{
    var value = document.getElementById(arg.controltovalidate);
    if ( value ) 
        return value.checked;
    else
        return false;
}

/* Client side functionality for validating a radio button list */
function RadioButtonListValidatorEvaluateIsValid(arg)
{
    var value = $j(arg.controltovalidate);
    var count = 0;
    for (i = 0; i < value.all.length; i++)
    {
        if (value.all[i].checked)
            return true;
    }
    return false;
}

/* Client side functionality for the RequiredOrValidator serverside control */
function RequiredOrValidatorIsValid(arg)
{
    var val1 = ValidatorGetValue(arg.controltovalidate);
    var val2 = ValidatorGetValue(arg.secondcontrol);
    if ( ValidatorTrim(val1).length + ValidatorTrim(val2).length > 0 ) 
        return true;
        
    return false;    
}

/* Clietn side functionality for the CreditCardValidator serverside control */
function CreditCardValidatorEvaluateIsValid(arg)
{
    var cardNumber = ValidatorGetValue(arg.controltovalidate);
    var min = arg.minwidth;
    var max = arg.maxwidth;
    var len = cardNumber.length 
    if ( len < min || len > max ) 
        return false;
    
    var modv = (len - 1) % 2;
    var sum = 0;
    var digit = 0;
    var i = 0;
    for( i = cardNumber.length - 1 ; i >= 0 ; i-- )
    {        
        digit = cardNumber.charAt(i) * 1;
        if ( i % 2 != modv )
            digit = digit * 2;
        sum += digit % 10;
        if ( digit >= 10 ) 
            sum += 1;
    }        
    return sum % 10 == 0 && sum > 0;
}

/* Client side functionality for the ExpiryDateValidator serverside control */  
function ExpiryDateValidatorEvaluateIsValid(arg)
{
    var year = ValidatorGetValue(arg.controltovalidate);    // control value must be full, 4-digit year
    var month = ValidatorGetValue(arg.monthcontrol);        // month index, 1 = January, etc.
    var date = new Date();
    var yearNow = date.getFullYear();
    var monthNow = date.getMonth() + 1;
    return year > yearNow || ( year >= yearNow && month >= monthNow )
}

/* Makes a hidden element visible */
/* elementId : The ID of the hidden element to make visible */
function makeVisible(elementId)
{
    var el = $j(elementId);
    if ( el ) 
    {
        el.style.visibility = "visible";
    }      
}

/* Asynchronous method for getting a city name from its zip code 
** zipCodeId : ID of the control containing the zip code
** targetID  : ID of the control that shall receive the city name.
* ----------------------------------------------------------------- */ 
function GetCityBegin(zipCodeId, targetId)
{
    var zipCode = $j(zipCodeId);
    var target = $j(targetId);
    if ( target && zipCode ) 
    {
        cityControl = target;      
        GetCityEnd(getPostalCode(zipCode.attr('value')));
    }        
}

/* GetCity callback */
function GetCityEnd(result)
{
    if ( cityControl )
    {    
        if ( result && result.length > 0 ) 
        {
            cityControl.attr('value',result); 
        }
    }    
}

/* Silently handle an AJAX timeout */
function ajaxTimeoutSilent(result) {}

/* --------- Search ----------- */ 
function searchRedirect(queryControl)
{
    if ( queryControl ) 
    {
        var query = queryControl.value;
        var cCategoryDropDown = $j(cCategoryDropDownId);
        if ( query && cCategoryDropDown ) 
        {    
            searchUrl = searchUrl + "&category=" + cCategoryDropDown.value;
            searchUrl = searchUrl + "&extra=" + escape(query);            
            window.location.href = searchUrl;
        }
    }
    return false;
}

function searchKeyPress(obj,ev)
{
    var keyCode = ev.which ? ev.which : ev.keyCode;
    if ( keyCode == 13 ) 
    {   
        ev.returnValue = false;
        ev.cancel  = true;     
        searchRedirect(obj);
    }
    return false;
}

/**************************************************************
/* General function for setting the CSS class on an
/* element, when a checkbox is checked or not.
/* -------------------------------------------------------
/* elementId : ID of the element on which to set a class.
/* checkBoxId : ID of the checkbox
/* classChecked : Class to set on the element if the checkbox is checked
/* classUnchecked : Class to set on the element if the checkbox is unchecked
/***************************************************************/
function setElementClassOnChecked(elementId,checkBoxId,classChecked,classUnchecked)
{
    var el = $j(elementId);
    var checkbox = $j(checkBoxId);
    if ( el && checkbox ) 
    {
        var css = checkbox.attr('checked') ? classChecked : classUnchecked;
        el.attr('class',css);
    }
}


Sys_Readonly_DefaultReadonlyBackgroundColor = "#E2E0D9 url('/Frontend/Graphics/Tradelink/bg_disabled.gif') repeat"; // Can be #hex or Css colornames
Sys_Readonly_DefaultReadWriteBackgroundColor = "#ffffff"; // Can be #hex or Css colornames

/* Sætter et elements readonly attribut

    elm = elementet, ex elm = document.getElementById("postnummer");, Må ikke være tom.
    isReadOnly = om den skal være readonly eller ej, kan være tom men skifter så bare værdi.
    bgColor = Baggrundsfarve, kan være tom, men tager så bare default værdi.
    
*/    
function setReadonly(elm, isReadOnly, bgcolor)
{
    //Sætter isReadOnly hvis det ikke i forvejen er en bool
    if(isReadOnly.toLower != "false" || isReadOnly.toLower != "true")
    {
        
        isReadOnly = elm.disabled ? false : true;   
    }

    //Sætter baggrundsfarve fra sys værdier, hvis den er tom.
    if(bgcolor == "" && isReadOnly)
    {
       bgcolor = Sys_Readonly_DefaultReadonlyBackgroundColor;       
    }
    else if(bgcolor == "" && !isReadOnly)
    {
        bgcolor = Sys_Readonly_DefaultReadWriteBackgroundColor;
    }
    
    //Sætter elementer
    elm.disabled = isReadOnly;
    /*
    // Gøres browserkompatibel. Fejl konstateret i FF.
    elm.style.background = bgcolor;
    */
}

/* Tager et array af elementer */
/* DEPRECATED */
function setReadonlyArr(elms, isReadOnly, bgColor)
{
    for (var j = 0; j < elms.length; j++) 
    {
       setReadonly(elms[j], isReadOnly, bgColor);          
    } 
}