// ===================================================================
// Author: Matt Kruse <matt@ajaxtoolbox.com>
// WWW: http://www.AjaxToolbox.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. 
// If you wish to share this code with others, please just point them
// to the URL instead.
// Please DO NOT link directly to my .js files from your site. Copy
// the files to your server and use them there. Thank you.
// ===================================================================

function AjaxRequest() {
    var req = new Object();
    req.timeout = null;
    req.generateUniqueUrl = false;
    req.url = window.location.href;
    req.method = "GET";
    req.async = true;
    req.username = null;
    req.password = null;
    req.parameters = new Object();
    req.requestIndex = AjaxRequest.numAjaxRequests++;
    req.responseReceived = false;
    req.groupName = null;
    req.queryString = "";
    req.responseText = null;
    req.responseXML = null;
    req.status = null;
    req.statusText = null;
    req.aborted = false;
    req.xmlHttpRequest = null;
    req.onTimeout = null;
    req.onLoading = null;
    req.onLoaded = null;
    req.onInteractive = null;
    req.onComplete = null;
    req.onSuccess = null;
    req.onError = null;
    req.onGroupBegin = null;
    req.onGroupEnd = null;
    req.xmlHttpRequest = AjaxRequest.getXmlHttpRequest();
    if (req.xmlHttpRequest == null) { return null; } req.xmlHttpRequest.onreadystatechange =
function() { if (req == null || req.xmlHttpRequest == null) { return; } if (req.xmlHttpRequest.readyState == 1) { req.onLoadingInternal(req); } if (req.xmlHttpRequest.readyState == 2) { req.onLoadedInternal(req); } if (req.xmlHttpRequest.readyState == 3) { req.onInteractiveInternal(req); } if (req.xmlHttpRequest.readyState == 4) { req.onCompleteInternal(req); } };
    req.onLoadingInternalHandled = false;
    req.onLoadedInternalHandled = false;
    req.onInteractiveInternalHandled = false;
    req.onCompleteInternalHandled = false;
    req.onLoadingInternal =
function() {
    if (req.onLoadingInternalHandled) { return; } AjaxRequest.numActiveAjaxRequests++;
    if (AjaxRequest.numActiveAjaxRequests == 1 && typeof (window['AjaxRequestBegin']) == "function") { AjaxRequestBegin(); } if (req.groupName != null) {
        if (typeof (AjaxRequest.numActiveAjaxGroupRequests[req.groupName]) == "undefined") { AjaxRequest.numActiveAjaxGroupRequests[req.groupName] = 0; } AjaxRequest.numActiveAjaxGroupRequests[req.groupName]++;
        if (AjaxRequest.numActiveAjaxGroupRequests[req.groupName] == 1 && typeof (req.onGroupBegin) == "function") { req.onGroupBegin(req.groupName); } 
    } if (typeof (req.onLoading) == "function") { req.onLoading(req); } req.onLoadingInternalHandled = true;
};
    req.onLoadedInternal =
function() { if (req.onLoadedInternalHandled) { return; } if (typeof (req.onLoaded) == "function") { req.onLoaded(req); } req.onLoadedInternalHandled = true; };
    req.onInteractiveInternal =
function() { if (req.onInteractiveInternalHandled) { return; } if (typeof (req.onInteractive) == "function") { req.onInteractive(req); } req.onInteractiveInternalHandled = true; };
    req.onCompleteInternal =
function() {
    if (req.onCompleteInternalHandled || req.aborted) { return; } req.onCompleteInternalHandled = true;
    AjaxRequest.numActiveAjaxRequests--;
    if (AjaxRequest.numActiveAjaxRequests == 0 && typeof (window['AjaxRequestEnd']) == "function") { AjaxRequestEnd(req.groupName); } if (req.groupName != null) {
        AjaxRequest.numActiveAjaxGroupRequests[req.groupName]--;
        if (AjaxRequest.numActiveAjaxGroupRequests[req.groupName] == 0 && typeof (req.onGroupEnd) == "function") { req.onGroupEnd(req.groupName); } 
    } req.responseReceived = true;
    req.status = req.xmlHttpRequest.status;
    req.statusText = req.xmlHttpRequest.statusText;
    req.responseText = req.xmlHttpRequest.responseText;
    req.responseXML = req.xmlHttpRequest.responseXML;
    if (typeof (req.onComplete) == "function") { req.onComplete(req); } if (req.xmlHttpRequest.status == 200 && typeof (req.onSuccess) == "function") { req.onSuccess(req); } else if (typeof (req.onError) == "function") { req.onError(req); } delete req.xmlHttpRequest['onreadystatechange'];
    req.xmlHttpRequest = null;
};
    req.onTimeoutInternal =
function() {
    if (req != null && req.xmlHttpRequest != null && !req.onCompleteInternalHandled) {
        req.aborted = true;
        req.xmlHttpRequest.abort();
        AjaxRequest.numActiveAjaxRequests--;
        if (AjaxRequest.numActiveAjaxRequests == 0 && typeof (window['AjaxRequestEnd']) == "function") { AjaxRequestEnd(req.groupName); } if (req.groupName != null) {
            AjaxRequest.numActiveAjaxGroupRequests[req.groupName]--;
            if (AjaxRequest.numActiveAjaxGroupRequests[req.groupName] == 0 && typeof (req.onGroupEnd) == "function") { req.onGroupEnd(req.groupName); } 
        } if (typeof (req.onTimeout) == "function") { req.onTimeout(req); } delete req.xmlHttpRequest['onreadystatechange'];
        req.xmlHttpRequest = null;
    } 
};
    req.process =
function() {
    if (req.xmlHttpRequest != null) {
        if (req.generateUniqueUrl && req.method == "GET") { req.parameters["AjaxRequestUniqueId"] = new Date().getTime() + "" + req.requestIndex; } var content = null;
        for (var i in req.parameters) { if (req.queryString.length > 0) { req.queryString += "&"; } req.queryString += encodeURIComponent(i) + "=" + encodeURIComponent(req.parameters[i]); } if (req.method == "GET") { if (req.queryString.length > 0) { req.url += ((req.url.indexOf("?") > -1) ? "&" : "?") + req.queryString; } } req.xmlHttpRequest.open(req.method, req.url, req.async, req.username, req.password);
        if (req.method == "POST") { if (typeof (req.xmlHttpRequest.setRequestHeader) != "undefined") { req.xmlHttpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); } content = req.queryString; } if (req.timeout > 0) { setTimeout(req.onTimeoutInternal, req.timeout); } req.xmlHttpRequest.send(content);
    } 
};
    req.handleArguments =
function(args) { for (var i in args) { if (typeof (req[i]) == "undefined") { req.parameters[i] = args[i]; } else { req[i] = args[i]; } } };
    req.getAllResponseHeaders =
function() { if (req.xmlHttpRequest != null) { if (req.responseReceived) { return req.xmlHttpRequest.getAllResponseHeaders(); } alert("Cannot getAllResponseHeaders because a response has not yet been received"); } };
    req.getResponseHeader =
function(headerName) { if (req.xmlHttpRequest != null) { if (req.responseReceived) { return req.xmlHttpRequest.getResponseHeader(headerName); } alert("Cannot getResponseHeader because a response has not yet been received"); } };
    return req;
} AjaxRequest.getXmlHttpRequest = function() {
    if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if (window.ActiveXObject) {/*@cc_on@*/
        /*@if(@_jscript_version >=5)
        try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { return null; } } @end@*/
    } else { return null; } 
};
AjaxRequest.isActive = function() { return (AjaxRequest.numActiveAjaxRequests > 0); };
AjaxRequest.get = function(args) { AjaxRequest.doRequest("GET", args); };
AjaxRequest.post = function(args) { AjaxRequest.doRequest("POST", args); };
AjaxRequest.doRequest = function(method, args) {
    if (typeof (args) != "undefined" && args != null) {
        var myRequest = new AjaxRequest();
        myRequest.method = method;
        myRequest.handleArguments(args);
        myRequest.process();
    } 
};
AjaxRequest.submit = function(theform, args) {
    var myRequest = new AjaxRequest();
    if (myRequest == null) { return false; } var serializedForm = AjaxRequest.serializeForm(theform);
    myRequest.method = theform.method.toUpperCase();
    myRequest.url = theform.action;
    myRequest.handleArguments(args);
    myRequest.queryString = serializedForm;
    myRequest.process();
    return true;
};
AjaxRequest.serializeForm = function(theform) {
    var els = theform.elements;
    var len = els.length;
    var queryString = "";
    this.addField =
function(name, value) { if (queryString.length > 0) { queryString += "&"; } queryString += encodeURIComponent(name) + "=" + encodeURIComponent(value); };
    for (var i = 0; i < len; i++) {
        var el = els[i];
        if (!el.disabled) {
            switch (el.type) {
                case 'text': case 'password': case 'hidden': case 'textarea':
                    this.addField(el.name, el.value);
                    break;
                case 'select-one':
                    if (el.selectedIndex >= 0) { this.addField(el.name, el.options[el.selectedIndex].value); } break;
                case 'select-multiple':
                    for (var j = 0; j < el.options.length; j++) { if (el.options[j].selected) { this.addField(el.name, el.options[j].value); } } break;
                case 'checkbox': case 'radio':
                    if (el.checked) { this.addField(el.name, el.value); } break;
            } 
        } 
    } return queryString;
};
AjaxRequest.numActiveAjaxRequests = 0;
AjaxRequest.numActiveAjaxGroupRequests = new Object();
AjaxRequest.numAjaxRequests = 0;

var dateFormat = function() {
    var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
		timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
		timezoneClip = /[^-+\dA-Z]/g,
		pad = function(val, len) {
		    val = String(val);
		    len = len || 2;
		    while (val.length < len) val = "0" + val;
		    return val;
		};

    // Regexes and supporting functions are cached through closure
    return function(date, mask, utc) {
        var dF = dateFormat;

        // You can't provide utc if you skip other args (use the "UTC:" mask prefix)
        if (arguments.length == 1 && (typeof date == "string" || date instanceof String) && !/\d/.test(date)) {
            mask = date;
            date = undefined;
        }

        // Passing date through Date applies Date.parse, if necessary
        date = date ? new Date(date) : new Date();
        if (isNaN(date)) throw new SyntaxError("invalid date");

        mask = String(dF.masks[mask] || mask || dF.masks["default"]);

        // Allow setting the utc argument via the mask
        if (mask.slice(0, 4) == "UTC:") {
            mask = mask.slice(4);
            utc = true;
        }

        var _ = utc ? "getUTC" : "get",
			d = date[_ + "Date"](),
			D = date[_ + "Day"](),
			m = date[_ + "Month"](),
			y = date[_ + "FullYear"](),
			H = date[_ + "Hours"](),
			M = date[_ + "Minutes"](),
			s = date[_ + "Seconds"](),
			L = date[_ + "Milliseconds"](),
			o = utc ? 0 : date.getTimezoneOffset(),
			flags = {
			    d: d,
			    dd: pad(d),
			    ddd: dF.i18n.dayNames[D],
			    dddd: dF.i18n.dayNames[D + 7],
			    m: m + 1,
			    mm: pad(m + 1),
			    mmm: dF.i18n.monthNames[m],
			    mmmm: dF.i18n.monthNames[m + 12],
			    yy: String(y).slice(2),
			    yyyy: y,
			    h: H % 12 || 12,
			    hh: pad(H % 12 || 12),
			    H: H,
			    HH: pad(H),
			    M: M,
			    MM: pad(M),
			    s: s,
			    ss: pad(s),
			    l: pad(L, 3),
			    L: pad(L > 99 ? Math.round(L / 10) : L),
			    t: H < 12 ? "a" : "p",
			    tt: H < 12 ? "am" : "pm",
			    T: H < 12 ? "A" : "P",
			    TT: H < 12 ? "AM" : "PM",
			    Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
			    o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
			    S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
			};

        return mask.replace(token, function($0) {
            return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
        });
    };
} ();

// Some common format strings
dateFormat.masks = {
    "default": "ddd mmm dd yyyy HH:MM:ss",
    shortDate: "m/d/yy",
    mediumDate: "mmm d, yyyy",
    longDate: "mmmm d, yyyy",
    fullDate: "dddd, mmmm d, yyyy",
    shortTime: "h:MM TT",
    mediumTime: "h:MM:ss TT",
    longTime: "h:MM:ss TT Z",
    isoDate: "yyyy-mm-dd",
    isoTime: "HH:MM:ss",
    isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
    isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

// Internationalization strings
dateFormat.i18n = {
    dayNames: [
		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
	],
    monthNames: [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
		"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
	]
};

// For convenience...
Date.prototype.format = function(mask, utc) {
    return dateFormat(this, mask, utc);
};

function gmobj(mtxt) { if (document.getElementById) { m = document.getElementById(mtxt) } else if (document.all) { m = document.all[mtxt] } else if (document.layers) { m = document[mtxt] } return m; }

function getNodeValue(o) {
    try {
        return o.item(0).firstChild.nodeValue;
    }
    catch (err) {
        return '';
    }
}

function getINFO(d, e, obj) {
	getContent('http://pavietnam.vn/whois.php?domain=' + d + e, d, e, obj)
}

function getContent(sUrl, d, e, target) {
	var fulldomain = d + e
    AjaxRequest.get(
	{
	    "url": "AjaxProxy.asp?sUrl=" + sUrl
	    , 'onLoading': function() {gmobj(target).innerHTML = "<div class='fleft' style='width:100%; padding-top:10px'><strong>"+ fulldomain +"</strong> <img src='/image/theme/check-loading.gif' class='fleft'></div>"}
		, 'onSuccess': function(req) {
			var strWrite = "";
			strWrite += "<div style='float:left; width:100%; padding-top:10px'>"
			strWrite += "<span style='font-size:15px'>"+ fulldomain + "</span>";
			if (req.responseText == '0') {
				strWrite += "<img src='/image/theme/false-check.jpg' class='fleft' style='padding-right:10px'>"	
				strWrite += "<span style='color:red'> &nbsp; Đã đăng ký</span>"
				strWrite += " <a style='cursor:pointer' target='_blank' onClick=\"clickwhois('" + fulldomain + "')\">"
				if (e.indexOf(".vn") == -1 ){
					strWrite += "<img src='/image/theme/info.gif'></a>"
				}
			}
			else
			{
				strWrite += "<img src='/image/theme/true-check.jpg' class='fleft' style='padding-right:10px'>"
				strWrite += "<span style='color:blue'> &nbsp; Chưa đăng ký</span>"
			}
			
			strWrite += "</div>"
			gmobj(target).innerHTML = strWrite;
			
		}
		, 'onError': function(req) {gmobj(target).innerHTML = "<div class='fleft' style='width:100%; padding-top:10px'><strong>"+ fulldomain +"</strong> Lỗi kết nối tới máy chủ</div>"}
	});
}

function clickwhois(d){
	var Whois = document.getElementById('Whois');
	Whois.style.display = '';
	GetContentDomain(Whois, d)
}

function GetContentDomain(target, domain){
    AjaxRequest.get(
    {
        'url': "AjaxXML.asp?sUrl=http://www.matbao.net/whoisXML.aspx?domain=" + domain + PreventCaching()
        ,'onLoading':function() { target.innerHTML = "<img src='/image/theme/loading.gif'>"; }
        ,'onSuccess':function(req){
			if (navigator.appVersion.indexOf("MSIE") != -1) {
				var doc = new ActiveXObject("MSXML2.DOMDocument"); 
				doc.loadXML(req.responseText);				
			} else {
				var doc = req.responseXML;
			}
	        var Item = doc.getElementsByTagName('Item');
	        var description = "", strWrite = ""

	        description = getNodeValue(doc.getElementsByTagName('item').item(0).getElementsByTagName('description'));
			
			strWrite = "<div style='padding-top:10px' class='fleft'><p>Thông tin whois <strong>" +domain+ "</strong></p>"
			strWrite +=  description + "</div>"
			target.innerHTML = strWrite		
			 }
        ,'onError':function(req){ target.innerHTML = "Lỗi kết nối với máy chủ" ;}
      }
    );
}

function PreventCaching(){
	var d = new Date();
	return '&t='+d.getTime();
}
