// JavaScript Library
// @version $Id$
// @package CryoStar 0.0.1
// @copyright (C) 2006-2010 CryoStar Co
// <meta http-equiv="Content-Type" content="text/html; charset=windows-1251"/>
/*==========================================================================#
# * Function for adding a Filter to an Input Field                          #
# * @param  : [filterType  ] Type of filter 0=>Alpha, 1=>Num, 2=>AlphaNum   #
# * @param  : [evt         ] The Event Object                               #
# * @param  : [allowDecimal] To allow Decimal Point set this to true        #
# * @param  : [allowCustom ] Custom Characters that are to be allowed       #
#==========================================================================*/
function filterInput(filterType, evt, allowDecimal, allowCustom){
    var keyCode, Char, inputField, filter = '';
    var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var num   = '0123456789.';
    // Get the Key Code of the Key pressed if possible else - allow
    if(window.event){
        keyCode = window.event.keyCode;
        evt = window.event;
    }else if (evt)keyCode = evt.which;
    else return true;
    // Setup the allowed Character Set
    if(filterType == 0) filter = alpha;
    else if(filterType == 1) filter = num;
    else if(filterType == 2) filter = alpha + num;
    if(allowCustom)filter += allowCustom;
    if(filter == '')return true;
    // Get the Element that triggered the Event
    inputField = evt.srcElement ? evt.srcElement : evt.target || evt.currentTarget;
    // If the Key Pressed is a CTRL key like Esc, Enter etc - allow
    if((keyCode==null) || (keyCode==0) || (keyCode==8) || (keyCode==9) || (keyCode==13) || (keyCode==27) )return true;
    // Get the Pressed Character
    Char = String.fromCharCode(keyCode);
    // If the Character is a number - allow
    if((filter.indexOf(Char) > -1)) return true;
    // Else if Decimal Point is allowed and the Character is '.' - allow
    else if(filterType == 1 && allowDecimal && (Char == '.') && inputField.value.indexOf('.') == -1)return true;
    else return false;
}
// AJAX functions to calculate FromSum and ToSum
// Get the HTTP Object
function getHTTPObject(){
    if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
    else if (window.XMLHttpRequest) return new XMLHttpRequest();
    else {
        alert("Your browser does not support AJAX.");
        return null;
    }
}
// Change the value of the outputText field
function setTo(){
    if (httpObject.readyState == 4) {
        ToSum = Number(httpObject.responseText);
        FromSum = Number(document.getElementById('FromSum').value);
        document.getElementById('ToSum').value = ToSum;
        FromCurrency = Number(document.getElementById('From').value);
        MaxComission = 1500;
        if (FromCurrency == 5) { MaxComission = 50; }
        if (FromCurrency == 6) { MaxComission = 250; }
        if (FromCurrency == 7) { MaxComission = 1500; }
        if (FromCurrency == 8) { MaxComission = 50; }
        Comission = FromSum * 0.008; if (Comission > MaxComission) { Comission = MaxComission; }
        document.getElementById('FromSumFull').value = (FromSum + Comission).toFixed(2);
        document.getElementById('FromSumFullForm').value = (FromSum + Comission).toFixed(2);
    }
}
// Change the value of the outputText field
function setFrom(){
    if (httpObject.readyState == 4) {
        FromSum = Number(httpObject.responseText);
        document.getElementById('FromSum').value = FromSum;
        FromCurrency = Number(document.getElementById('From').value);
        MaxComission = 1500;
        if (FromCurrency == 5) { MaxComission = 50; }
        if (FromCurrency == 6) { MaxComission = 250; }
        if (FromCurrency == 7) { MaxComission = 1500; }
        if (FromCurrency == 8) { MaxComission = 50; }
        Comission = FromSum * 0.008; if (Comission > MaxComission) { Comission = MaxComission; }
        document.getElementById('FromSumFull').value = (FromSum + Comission).toFixed(2);
        document.getElementById('FromSumFullForm').value = (FromSum + Comission).toFixed(2);
    }
}
function doCalcFrom(){
    httpObject = getHTTPObject();
    if (httpObject != null) {
        httpObject.open("GET", "/include/calculate.inc.php?f="
            +document.getElementById('From').value
            +"&t="
            +document.getElementById('To').value
            +"&ts="
            +document.getElementById('ToSum').value
            +"&p="
            +document.getElementById('Page').value
            +"&w="
            +document.getElementById('Way').value,
        true);
        httpObject.send(null);
        httpObject.onreadystatechange = setFrom;
    }
}
function doCalcTo(){
    httpObject = getHTTPObject();
    if (httpObject != null) {
        httpObject.open("GET", "/include/calculate.inc.php?f="
            +document.getElementById('From').value
            +"&t="
            +document.getElementById('To').value
            +"&fs="
            +document.getElementById('FromSum').value
            +"&p="
            +document.getElementById('Page').value
            +"&w="
            +document.getElementById('Way').value,
        true);
            httpObject.send(null);
        httpObject.onreadystatechange = setTo;
    }
}

var httpObject = null;

