var to;
var qdelay = 600;
var email_check_delay = 800;

function devnull()
{
//
}

function replaceAll( str, from, to ) {
    var idx = str.indexOf( from );


    while ( idx > -1 ) {
        str = str.replace( from, to );
        idx = str.indexOf( from );
    }

    return str;
}

function array_unique(arr) {
    var newArray = [];
    var existingItems = {};
    var prefix = String(Math.random() * 9e9);
    for (var ii = 0; ii < arr.length; ++ii) {
        if (!existingItems[prefix + arr[ii]]) {
            newArray.push(arr[ii]);
            existingItems[prefix + arr[ii]] = true;
        }
    }
    return newArray;
}

function sl(v) {
return replaceAll(v,'/','%2F');
}

//Basic Ajax Routine- Author: Dynamic Drive (http://www.dynamicdrive.com)
//Last updated: Jan 15th, 06'

function createAjaxObj(){
var httprequest=false;
if (window.XMLHttpRequest){ // if Mozilla, Safari etc
httprequest=new XMLHttpRequest();
if (httprequest.overrideMimeType)
httprequest.overrideMimeType('text/xml');
}
else if (window.ActiveXObject){ // if IE
try {
httprequest=new ActiveXObject("Msxml2.XMLHTTP");
} 
catch (e){
try{
httprequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){}
}
}
return httprequest;
}

var ajaxpack=new Object();
ajaxpack.basedomain="http://"+window.location.hostname;
ajaxpack.ajaxobj=createAjaxObj();
ajaxpack.filetype="txt";
ajaxpack.addrandomnumber=0; //Set to 1 or 0. See documentation.

ajaxpack.getAjaxRequest=function(url, parameters, callbackfunc, filetype){
ajaxpack.ajaxobj=createAjaxObj(); //recreate ajax object to defeat cache problem in IE
if (ajaxpack.addrandomnumber==1) //Further defeat caching problem in IE?
var parameters=parameters+"&ajaxcachebust="+new Date().getTime();
if (this.ajaxobj){
this.filetype=filetype;
this.ajaxobj.onreadystatechange=callbackfunc;
this.ajaxobj.open('GET', '/rpc/'+sl(escape(url))+'/'+sl(escape(parameters)), true);
this.ajaxobj.send(null);
}
}

ajaxpack.postAjaxRequest=function(url, parameters, callbackfunc, filetype){
ajaxpack.ajaxobj=createAjaxObj(); //recreate ajax object to defeat cache problem in IE
if (this.ajaxobj){
this.filetype=filetype;
this.ajaxobj.onreadystatechange = callbackfunc;
this.ajaxobj.open('POST', '/rpc/'+sl(escape(url)), true);
this.ajaxobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.ajaxobj.setRequestHeader("Content-length", parameters.length);
this.ajaxobj.setRequestHeader("Connection", "close");
this.ajaxobj.send(parameters);
}
}
///////////END OF ROUTINE HERE////////////////////////


//////EXAMPLE USAGE ////////////////////////////////////////////

//Define call back function to process returned data
function processGetPost(){
var myajax=ajaxpack.ajaxobj;
var myfiletype=ajaxpack.filetype;
if (myajax.readyState == 4){ //if request of file completed
if (myajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
if (myfiletype=="txt")
alert(myajax.responseText);
else
alert(myajax.responseXML);
}
}
}

function checkDomain(){
reqspam = document.getElementById('checkdomainreq');
domainname = document.getElementById('dname').value;
var myajax=ajaxpack.ajaxobj;
var myfiletype=ajaxpack.filetype;
if (myajax.readyState == 4){ //if request of file completed
//if (myajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
if (myfiletype=="txt")
c = myajax.responseText;
else
c = myajax.responseXML;
//}
}

        if(c == 'OK')
        {
        //Ok
        reqspam.innerHTML = '<img src="/images/tick.png" alt="Tick icon" />';
        //document.getElementById('oktosubmit').value = 1;
        }
        else
        {
        reqspam.innerHTML = '<img src="/images/cross.png" alt="Cross icon" />';
		}
}


function checkdomainform () {
	reqspam = document.getElementById('checkdomainreq');
	domainname = document.getElementById('dname').value;
	var y = '';

	if(document.getElementById('dext-com').checked)
	{
		y += '&com=1';
	}
	if(document.getElementById('dext-com.au').checked)
	{
		y += '&com.au=1';
	}
	if(document.getElementById('dext-net').checked)
	{
		y += '&net=1';
	}
	if(document.getElementById('dext-net.au').checked)
	{
		y += '&net.au=1';
	}
	if(document.getElementById('dext-org').checked)
	{
		y += '&org=1';
	}
	if(document.getElementById('dext-org.au').checked)
	{
		y += '&org.au=1';
	}
	if(document.getElementById('dext-biz').checked)
	{
		y += '&biz=1';
	}
	if(document.getElementById('dext-info').checked)
	{
		y += '&info=1';
	}
	if(document.getElementById('dext-id.au').checked)
	{
		y += '&id.au=1';
	}
	if(document.getElementById('dext-asn.au').checked)
	{
		y += '&asn.au=1';
	}
	
	
	if(domainname.length >= 1 && domainname.length <= 63 && !domainname.match(/[^A-Za-z0-9-]/)) //RFC 883
	{
       	//ajaxpack.getAjaxRequest('check_username', v, checkUsername, 'txt');
		reqspam.innerHTML = '<img src="/images/ajax_ind.gif" alt="Ajax Indicator" />';
		x = 'domain='+sl(escape(domainname))+y;
		/*window.clearTimeout(to);
		to = window.setTimeout("*/ajaxpack.postAjaxRequest('check_domain', x, checkDomain, 'txt');//",qdelay);
		//ajaxpack.postAjaxRequest('check_username', x, checkUsername, 'txt');
	    //to = window.setTimeout("*/reqspam.innerHTML = '&nbsp;<img src=\"/images/tick.png\" alt=\"Tick icon\" />';//",400);
        //document.getElementById('oktosubmit').value = 1;
        
    }
    else
    {
        reqspam.innerHTML = '&nbsp;<img src="/images/cross.png" alt="Cross icon" />';
    }
}