﻿/*
* Contains standard Tradelink javascript
*/

/*
----------------------
* "Global" variables 
*/
var cityControl;

/* 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 = document.getElementById(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 = document.getElementById(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 = document.getElementById(zipCodeId);
    /*
    var target = document.getElementById(targetId);
    if ( target && zipCode ) 
    {
        cityControl = target;
        Webdanmark.Tradelink.Output.AjaxService.GetCityFromPostalCode(zipCode.value,GetCityEnd,ajaxTimeoutSilent);
    }
    */
    GetCityEnd(getPostalCode(zipCode.value));
}

/* GetCity callback */
function GetCityEnd(result)
{
    if ( cityControl )
    {
        if ( result && result.length > 0 ) 
        {
            cityControl.value = result;            
        }
    }    
}

/* Silently handle an AJAX timeout */
function ajaxTimeoutSilent(result) {}

/* --------- Search ----------- */ 
function searchRedirect(queryControl)
{
    alert('argh');
    if ( queryControl ) 
    {
        var query = queryControl.value; 
        var cCategoryDropDown = document.getElementById(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 = document.getElementById(elementId);
    var checkbox = document.getElementById(checkBoxId);
    if ( el && checkbox ) 
    {
        var css = checkbox.checked ? classChecked : classUnchecked;
        el.className    = css;
    }
}