﻿
function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function createXMLDocument(string) {
    var browserName = navigator.appName;
    var doc;
    if (browserName == 'Microsoft Internet Explorer') {
        doc = new ActiveXObject('Microsoft.XMLDOM');
        doc.async = 'false'
        doc.loadXML(string);
    } else {
        doc = (new DOMParser()).parseFromString(string, 'text/xml');
    }
    return doc;
}


function SendMessage(method, params, callback, errorHandler) {
    $.ajax({
        url: method,
        data: params,
        type: "POST",
        //processData: false,
        //contentType: "application/json",
        timeout: 10000,
        dataType: "html", //"json",
        success: function(res) {
            if (!callback)
                return;
            callback(res);
        }//,
//        error: function(xhr, status) {
//            var err = null;
//            if (xhr.readyState == 4) {
//                var res = xhr.responseText;
//                alert("errore: " + res);
//                if (res && res.charAt(0) == '{')
//                    var err = JSON.parseWithDate(res);
//                if (!err) {
//                    if (xhr.status && xhr.status != 200)
//                        err = new CallbackException(xhr.status + " " + xhr.statusText);
//                    else
//                        err = new CallbackException("Callback Error: " + status);
//                    err.detail = res;
//                }
//            }
//            if (!err)
//                err = new CallbackException("Callback Error: " + status);
//            if (errorHandler)
//                errorHandler(err, _I, xhr);
//        }
    });
}

function SendMessageSync(method, params) {
    return $.ajax({
        url: method,
        data: params,
        type: "POST",
        timeout: 10000,
        dataType: "html",
        async: false
    }).responseText;
}

function ReplaceAll(Source, stringToFind, stringToReplace) {
    var temp = Source;
    var index = temp.indexOf(stringToFind);
    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}

function encHTML(sHtml) {
    return sHtml.replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/&/g, "&amp;").replace(/'/g, "&#39;");
};

function decHTML(sHtml) {
    return sHtml.replace(/&quot;/g, '"').replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, '&').replace(/&#39;/g, "'");
};

function ToHex(string) {
    var new_string = '';
    for (var i = 0; i < string.length; i++) {
        new_string += Convert.toHex(String.fromCharCode(string.charCodeAt(i)));
    }
    return new_string;
}

function FromHex(string) {
    var new_string = '';
    for (var i = 0; i <= (string.length - 2); i += 2) {
        new_string += Convert.toChar(string.substring(i, i + 2));
    }
    return new_string;
}

var Convert = {
    chars: " !\"#$%&'()*+'-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
    hex: '0123456789ABCDEF', bin: ['0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111'],

    decToHex: function(d) {
        return (this.hex.charAt((d - d % 16) / 16) + this.hex.charAt(d % 16));
    },
    hexToDec: function(d) {
        var DecVal = this.hex.indexOf(d.substring(0, 1)) * 16;
        DecVal += this.hex.indexOf(d.substring(1));
        return DecVal;
    },
    toBin: function(ch) {
        var d = this.toDec(ch);
        var l = this.hex.charAt(d % 16);
        var h = this.hex.charAt((d - d % 16) / 16);

        var hhex = "ABCDEF";
        var lown = l < 10 ? l : (10 + hhex.indexOf(l));
        var highn = h < 10 ? h : (10 + hhex.indexOf(h));
        return this.bin[highn] + ' ' + this.bin[lown];
    },
    toHex: function(ch) {
        return this.decToHex(this.toDec(ch));
    },
    toDec: function(ch) {
        var p = this.chars.indexOf(ch);
        return (p <= -1) ? 0 : (p + 32);
    },
    toChar: function(ch) {
        return String.fromCharCode(this.hexToDec(ch));
    }
};
