///////////////////////////////////////////////////////////
// XXTEA / JSON Page Encryption Include                  //
// Copyright Serif (Europe) Ltd. 2007                    //
///////////////////////////////////////////////////////////

var bodyCode;
function handleResponse(data) {if (data.Response.Code!="failed_security") document.write(TEAdecrypt(UnHex(bodyCode),data.Response.Code));}
function processSecurity(resourceID) {document.write("<script src='http://www.serifwebresources.com/secure/key.php?uid="+resourceID+"'></script>");}
function UnHex(hex) {out=""; for (i=0; i<hex.length; i+=2) out+=String.fromCharCode(parseInt(hex.substring(i,i+2),16)); return out;}
function TEAdecrypt(ciphertext, password)
{
    if (ciphertext.length == 0) return('');
    var v = strToLongs(unescCtrlCh(ciphertext));
    var k = strToLongs(password.slice(0,16)); 
    var n = v.length;

    var z = v[n-1], y = v[0], delta = 0x9E3779B9;
    var mx, e, q = Math.floor(6 + 52/n), sum = q*delta;

    while (sum != 0) {
        e = sum>>>2 & 3;
        for (var p = n-1; p >= 0; p--) {
            z = v[p>0 ? p-1 : n-1];
            mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z);
            y = v[p] -= mx;
        }
        sum -= delta;
    }
    var plaintext = longsToStr(v);
    plaintext = plaintext.replace(/\0+$/,'');
    return unescape(plaintext);
}
function strToLongs(s)
{
    var l = new Array(Math.ceil(s.length/4));
    for (var i=0; i<l.length; i++) {
        l[i] = s.charCodeAt(i*4) + (s.charCodeAt(i*4+1)<<8) + 
               (s.charCodeAt(i*4+2)<<16) + (s.charCodeAt(i*4+3)<<24);
    }
    return l;   
}              
function longsToStr(l) 
{
    var a = new Array(l.length);
    for (var i=0; i<l.length; i++) {
        a[i] = String.fromCharCode(l[i] & 0xFFFF, l[i]>>>16 & 0xFFFF);
    }
    return a.join('');  
}
function unescCtrlCh(str) 
{  
    return str.replace(/!\d\d?\d?!/g, function(c) { return String.fromCharCode(c.slice(1,-1)); });
}

