/*
**************************************************************************
*                                                                        *
* WHMCS Addon - Ajax Domain Search                                       *
* http://www.whmcsaddon.com                                              *
*                                                                        *
**************************************************************************
*                                                                        *
* In accordance with our terms of service:                               *
* Redistribution of this addition to the software is strictly forbidden  *
* without permission directly from a WHMCS Addon official representative.*
* No title to and ownership of the additions to the code presented here  *
* is hereby transferred.                                                 *
*                                                                        *
**************************************************************************
*/

// Configuration
var cartLocation = '/myaccount/cart.php'; // Where is your cart located?
var orderFormLocation = '/myaccount/order/ajaxdomain.php'; // Where is your order form located?

// -- Special Elements :: Wrap <a></a> around text/elements you wish to link to the cart:
var transferString = '<img src="/myaccount/images/statusok.gif" /> <a>Taken - Available for transfer...</a>' // What would you like to say if the domain is eligible for a transfer? 
var registerString = '<img src="/myaccount/images/statusok.gif" /> <a>Available - Register this domain...</a>' // What would you like to say if the domain is eligible for a registration?
// -- End of Special Elements
var unavailable = '<img src="/myaccount/images/statusfailed.gif" /> This domain is unavailable. <br /><a>Search for another domain...</a>';

var tableMode = false; // Would you like to check all available tlds? Displays the order/templates/ajaxdomain.tpl template file. Ignores transferString/registerString/unavailable variables above.

/*
	Regular Expression to determine if the domain is properly formatted. Add any additional tld's that are not included in the list below by adding |tld after the word museum.
	-- Any two letter tld is already included by defualt.
*/
// domainReg = /^\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
domainReg = /^\w+([\.-]?\w+)*\.(\w{2}|(com.au|net.au|org.au|asn.au|id.au|com|net|org|co.nz|ne.nz|tv|co.uk|me|info|mobi|org.uk|asia|co))$/

// End of configurations

var adcurrentcheckcontent,adlastcheckcontent,ld,checkdomaintimer;

function ajaxdomainvalidatedomain() {
		
		if (tableMode == false) {
			jQuery.post(orderFormLocation, "a=ajaxRegister&domain="+jQuery("#ajaxdomain").val(),
			function(data){
				data = jQuery.parseJSON(data);
                jQuery("#ajaxdomainloader").slideUp();
				if (data.status == "unavailable") {
				    ld = adcurrentcheckcontent.split(".");
                    fulldomain = adcurrentcheckcontent;
					if (data.transferenabled == "on" && data.transferoptionscount) {
						// Transfer Now../
						// jQuery("#ajaxdomainstatus").html(transferString.replace(/<a>/gi, '<a href="'+cartLocation+'?a=add&domain=transfer&sld='+ld[0]+'&tld=.'+ld[1]+'">')).slideDown();
                        jQuery("#ajaxdomainstatus").html(transferString.replace(/<a>/gi, '<a href="https://www.newsprout.com.au/myaccount/cart.php?carttpl=ajaxcart&a=add&domain=transfer&prefilldomain='+fulldomain+'">')).slideDown();
					} else {
						jQuery("#ajaxdomainstatus").html(unavailable.replace(/<a>/gi, '<a href="https://www.newsprout.com.au/myaccount/cart.php?carttpl=ajaxcart&a=add&domain=register&prefilldomain='+fulldomain+'">')).slideDown();
						// Not Available...	
					}
				}
				
				if (data.status == "available") {
					if (data.regenabled == "on" && data.regoptionscount) {
						// Register Now...
						ld = adcurrentcheckcontent.split(".");
                        fulldomain = adcurrentcheckcontent;
						// jQuery("#ajaxdomainstatus").html(registerString.replace(/<a>/gi, '<a href="'+cartLocation+'?a=add&domain=register&sld='+ld[0]+'&tld=.'+ld[1]+'">')).slideDown();
                        // jQuery("#ajaxdomainstatus").html(registerString.replace(/<a>/gi, '<a href="https://www.newsprout.com.au/myaccount/order/?prefilldomain='+fulldomain+'&pid=29&skip=1">')).slideDown();
                        jQuery("#ajaxdomainstatus").html(registerString.replace(/<a>/gi, '<a href="https://www.newsprout.com.au/myaccount/cart.php?carttpl=ajaxcart&a=add&domain=register&prefilldomain='+fulldomain+'">')).slideDown();
					} else {
						jQuery("#ajaxdomainstatus").html(unavailable).slideDown();
					}
				}
			});
		} else {
			jQuery.post(orderFormLocation, "a=ajaxRegisterAll&domain="+jQuery("#ajaxdomain").val(),
			function(data){
				jQuery("#ajaxdomainstatus").html(data.replace(/\[cartLocation\]/gi, cartLocation)).slideDown();
			});

		}
}

function ajaxdomaincheckdomainentry() {
    adcurrentcheckcontent = jQuery("#ajaxdomain").val();
    if (adcurrentcheckcontent!=adlastcheckcontent && domainReg.test(adcurrentcheckcontent)) {
        ajaxdomainvalidatedomain();
		jQuery("#ajaxdomainstatus").slideUp();
        adlastcheckcontent = adcurrentcheckcontent;
	} else if (adcurrentcheckcontent!=adlastcheckcontent) {
		jQuery("#ajaxdomainstatus").slideUp();
		adlastcheckcontent = adcurrentcheckcontent;
	}
    checkdomaintimer = setTimeout('ajaxdomaincheckdomainentry();', 3000);
}

jQuery("#ajaxdomain").live("change", function() {    
	clearTimeout(checkdomaintimer);	
	//jQuery("#ajaxdomainstatus").slideUp();
	checkdomaintimer = setTimeout('ajaxdomaincheckdomainentry();', 3000);
}).live("keypress", function(event){
	if (event.which == '13') {
	 clearTimeout(checkdomaintimer);	
	 ajaxdomaincheckdomainentry();
   }
	
});

ajaxdomaincheckdomainentry();
