var visibleCartItems = 0;

function replaceText(sId, sText)
{
    var el;
    if (document.getElementById && (el = document.getElementById(sId)))
	{
	    while (el.hasChildNodes()){
		el.removeChild(el.lastChild);}
	    //el.appendChild(document.createTextNode(sText));
	    el.innerHTML = sText;
	}
}
function select_all()
{
    var currentform = window.document.form1;
    for (var i = 0; i < currentform.elements.length;i++) {
        if (currentform.elements[i].name.indexOf("domain_name") != -1){
            if(currentform.elements[i].checked === false){
                currentform.elements[i].checked = true;}
	    if(currentform.elements[i].onclick){
		currentform.elements[i].onclick();}
        }
    }
}
function unselect_all()
{
    var currentform = window.document.form1;
    for (var i = 0; i < currentform.elements.length;i++) {
        if (currentform.elements[i].name.indexOf("domain_name") != -1){
            if(currentform.elements[i].checked === true){
                currentform.elements[i].checked = false;}
	    if(currentform.elements[i].onclick){
		currentform.elements[i].onclick();}
        }
    }
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
    window.open(theURL,winName,features);
}
function selectall(form, name) {
    //var currentform = window.document.form2;
    for (var i = 0; i < form.elements.length;i++) {
        if (form.elements[i].name.indexOf(name) != -1){
            if(form.elements[i].checked === false && form.elements[i].disabled === false){
                form.elements[i].checked = true;
	        if(form.elements[i].onclick){
		    form.elements[i].onclick('selectall');}
            }
        }
    }
}
function unselectall(form, name) {
    //var currentform = window.document.form2;
    for (var i = 0; i < form.elements.length;i++) {
        if (form.elements[i].name.indexOf(name) != -1){
            if(form.elements[i].checked === true){
                form.elements[i].checked = false;
	        if(form.elements[i].onclick){
		    form.elements[i].onclick('unselectall');}
            }
        }
    }
}
function selectOnly(form, name, tldArray, doSelect)
{
    for (var i = 0; i < form.elements.length;i++)
	{
	    if(form.elements[i].name.indexOf(name) != -1)
		{
		    if(tldArray.indexOf(form.elements[i].value) != -1 && doSelect)
			{
			    form.elements[i].checked = true;
			} 
		    else if(tldArray.indexOf(form.elements[i].value) != -1 && !doSelect) 
			{
			    form.elements[i].checked = false;
			} 
		    else if(tldArray.indexOf(form.elements[i].value) == -1 && doSelect) 
			{
			    form.elements[i].checked = false;
			} 
		}
	}
}
function cb_check(id)
{
    var checkbox = document.getElementById(id);
    if(typeof checkbox.focus == "function"){
	checkbox.focus();}
    checkbox.checked = true;
}
function cb_uncheck(id)
{
    var checkbox = document.getElementById(id);
    if(typeof checkbox.focus == "function"){
	checkbox.focus();}
    checkbox.checked = false;
}
function selectDiv(domain,ptype, value)
{
    var changeStr;
    var theObj;
    var dispStr;
    var blocking;
    var domainstatus = ptype+"_"+value;
    
    var value1 = "true";
    if (value === true){
	value1 = false;}
    else{
	value1 = true;}
    var domainstatus1 = ptype+"_"+value1;
    
    if (document.getElementById)
	{
	    changeStr = "document.getElementById('status_" + domain + "_" + domainstatus + "')";
	    theObj = eval(changeStr);
	    if (theObj)
                {
		    blocking = "1";
		    theObj.style.display = 'block';
                }
	    changeStr = "document.getElementById('status_" + domain + "_" + domainstatus1 + "')";
	    theObj = eval(changeStr);
	    if (theObj)
                {
		    if (blocking == "1"){
			theObj.style.display = 'none';
		    } else {
			theObj.style.display = 'block';
		    }
                }
	    
        }
}
function changeDisplay(sId, sText)
{
    var el;
    if(document.getElementById && (el = document.getElementById(sId)))
	{
	    el.style.display = sText;
	}
}
function rowLight(box,table_id)
{
    if(document.getElementById)
	{
	    var table = document.getElementById(table_id);
	    if(box.checked){
		table.className = 'selected';}
	    else{
		table.className = 'unselected';}
	}
}
function deleteTableRow(rowId, tableId)
{
    if(document.getElementById(tableId) && document.getElementById(rowId))
	{
	    var rowIndex = document.getElementById(rowId).rowIndex;
	    document.getElementById(tableId).deleteRow(rowIndex);
	    if(tableId == "cartTable")
		visibleCartItems--;
	}
}
function insertCartRow(referenceNode, name, type, duration, privateWhois, googleApps, price, cartRowId, tag, displayType, displayDomain)
{
    if(!referenceNode){	return;}
    
    if(document.getElementById("tr_"+type+"_"+name))
	{
	    deleteTableRow("tr_"+type+"_"+name, "cartTable");
	    return;
	}
    
    var parent = referenceNode.parentNode.parentNode.getElementsByTagName("TBODY")[0];
    var originalName = name;

//    var lastRow = referenceNode.rows.length;
    var lastRow = visibleCartItems + 1;
    var newRow = referenceNode.insertRow(lastRow);

//    var newRow = document.createElement("TR");
    newRow.setAttribute("id", "tr_"+type+"_"+name);
    newRow.setAttribute("class", "even");
    newRow.style.height = "25px";
    newRow.style.borderBottom = "1px solid #E0E0E0";
    
    var td1 = document.createElement("TD");
    var td2 = document.createElement("TD");
    var td3 = document.createElement("TD");
    var td4 = document.createElement("TD");
    var td5 = document.createElement("TD");
    var td6 = document.createElement("TD");
    var td7 = document.createElement("TD");
    var td8 = document.createElement("TD");
    
    newRow.appendChild(td1);
    newRow.appendChild(td8);
    newRow.appendChild(td2);
    newRow.appendChild(td3);
    newRow.appendChild(td4);
    newRow.appendChild(td5);
    newRow.appendChild(td6);
    newRow.appendChild(td7);
    
    var privateWHoisHtml = "";
    var googleAppsHtml = "";
    var privateWhoisHtml = "";
    
    var priceHtml = "<span id=\"span_checkoutProductPriceHidden_"+type+"_"+name+"\" style=\"display: none;\">"+price+"</span><span id=\"span_checkoutProductPrice_"+type+"_"+name+"\" style=\"color: rgb(0, 0, 0);\">"+price+"</span>";
    var removeHtml = "<span id=\"span_removeLink_"+cartRowId+"\" style=\"color: blue; text-decoration: underline; cursor: pointer;\" onmouseover=\"this.style.cursor='pointer'\" onclick=\"this.style.display = 'none';document.getElementById('span_removeLoading_"+cartRowId+"').style.display = '';productCartSwap(false, '"+name+"', 1, '"+type+"', '"+tag+"', '');\">remove</span><span id=\"span_removeLoading_"+cartRowId+"\" style=\"display: none;\"><img src='/javascript/images/loading.gif' width='15'></span>";
    var durationControlsHtml = " year(s) <span id=\"span_durationLoading_"+cartRowId+"\" style=\"display: none;\"><img width=\"15\" src=\"/javascript/images/loading.gif\"/></span><span id=\"span_durationControls_"+cartRowId+"\"> [ <span style=\"color: blue;\" onmouseover=\"this.style.cursor='pointer'\" onclick=\"adjustCartDuration('minus', "+cartRowId+");\"><b>-</b></span> / <span style=\"color: blue;\" onmouseover=\"this.style.cursor='pointer'\" onclick=\"adjustCartDuration('plus', "+cartRowId+");\"><b>+</b></span> ] </span>";
    
    if(privateWhois && type == "registration")
    {
	privateWhoisHtml = "<input id=\"whois_"+name+"\" type=\"checkbox\" onclick=\"productCartSwap(this.checked, '"+name+"', ((this.checked) ? 1 : 0), 'private_whois', 'hidden_product', 'ALL');\"/>";
    }
    else if(type == "whois_privacy")
    {
	privateWhoisHtml = "";
    }
    else
    {
	privateWhoisHtml = "n/a";
    }
    
    if(googleApps && type == "registration")
    {
	googleAppsHtml = "<input id=\"google_apps_"+name+"\" type=\"checkbox\" onclick=\"productCartSwap(true, '"+name+"', ((this.checked) ? 1 : 0), 'google_apps', 'hidden_product', 'ALL', googleAppsSwapHandler);\"/>";
    }
    else if(type == "whois_privacy")
    {
	googleAppsHtml = "";
    }
    else
    {
	googleAppsHtml = "n/a";
    }
    
    if(!displayType){ displayType = type;}
    
    if(type == "google_apps_support_level_1")
	{ 
	    durationControlsHtml = " domain(s)";
	}
    if(type == "rapid_blog")
	{ 
	    durationControlsHtml = " year";
	    originalName = originalName+"rap_y"
	}
    else if(type == "rapid_blog_monthly")
	{ 
	    durationControlsHtml = " month";
	    originalName = originalName+"rap_m";
	}
    else if(type == "google_apps")
	{ 
	    durationControlsHtml = " domain(s)";
	}
    
    td1.innerHTML = displayDomain;
    td8.innerHTML = "";
    td2.innerHTML = removeHtml;
    td3.style.whiteSpace = "nowrap";
    td3.innerHTML = displayType;
    td4.innerHTML = "<span id='span_qty_"+originalName+cartRowId+"'>"+duration+"</span>"+durationControlsHtml;
    td5.innerHTML = privateWhoisHtml;
    td6.innerHTML = googleAppsHtml;
    td7.innerHTML = priceHtml;
    
    td5.style.textAlign = "center";
    td6.style.textAlign = "center";
    td7.style.textAlign = "right";

    visibleCartItems++;
//    parent.insertBefore(newRow, referenceNode.nextSibling);
}
function processPromoCode(event)
{
    if(event && event.keyCode != "13"){	return;}
    
    if(!document.getElementById("promo_code")){	return;}
    
    var promoCode = document.getElementById("promo_code").value.trim();
    
    document.getElementById("span_promo_code").style.display = "none";
    document.getElementById("span_processing_promo").style.display = "";
    document.getElementById("span_checkoutSavings").style.color = "#000000";
    document.getElementById("tr_checkoutSavings").style.display = "none";
    
    getURL('/products-pricing/services/productCart.php?action=processpromocode&promoCode='+promoCode, processPromoCodeHandler);
}

function formatCurrency(num, nosign) 
{
    num = num.toString().replace(/\$|\,/g,'');
    
    if(isNaN(num)){ num = "0";}
    
    var sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    var cents = num%100;
    num = Math.floor(num/100).toString();
    
    if(cents<10) { cents = "0" + cents;}
    
    for(var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	{
	    num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	}
    
    return (((sign)?'':'-') + ((nosign) ? '' : '$') + num + '.' + cents);
}
function setShoppingCartInfo(productTotalPrice, productCount)
{
    $('#Cart-number').html(productCount);
    $('#Cart-price').html(formatCurrency(productTotalPrice, true));
}

function getSetShoppingCartInfoHandler(status, content, contentType)
{
    if(status != 200 && status !== 0)
	{
	    alert("Something went wrong. Please refresh the page and try again. ("+status+")");
	    return;
	}
    
    if(!content){ return;}
    
    var results = eval('('+content+')');
    
    if(!results.status)
	{
	    alert(results.message);
	    return;
	}
    
    var cartItems = results.cartItems;
    var cartItemsLength = cartItems.length;
    
    for(var i = 0; i < cartItemsLength; i++)
	{
	    if(document.getElementById("check_"+cartItems[i]))
		{
		    document.getElementById("check_"+cartItems[i]).checked = true;
		}
	    
	    if(document.getElementById("div_"+cartItems[i]))
		{
		     document.getElementById("div_"+cartItems[i]).innerHTML = "Added to Cart";
		     document.getElementById("div_"+cartItems[i]).style.color = "#245FA5";
		    
		    if(document.getElementById('div_fast_'+cartItems[i])){
			document.getElementById('div_fast_'+cartItems[i]).style.display = 'none';}
		}
	    
	    domainsInCart.push(cartItems[i]);
	}
    
    setShoppingCartInfo(results.productTotalPrice, results.productCount);
}
function getSetShoppingCartInfo()
{
// owen
    getURL('/products-pricing/services/productCart.php?action=getTotals', getSetShoppingCartInfoHandler);
}
function handleTLDResponse(status,content,contentType)
{
    var results = content.split(",");
    var AVAIL;
    if(results[1] > 0)
	{
	    AVAIL[results[0]] = 1;
	    
	    if(document.getElementById("td_"+results[0]).innerHTML != "Added to Cart")
		{
		    replaceText("td_" + results[0], "Available");
		    document.getElementById("td_" + results[0]).style.color = '#00cc00';
		}
	    
	    document.getElementById('check_' + results[0]).disabled = false;
	}
    else
	{
	    AVAIL[results[0]] = 0;
	    replaceText("td_" + results[0], "Taken");
	    document.getElementById("td_" + results[0]).style.color = '#ff0000';
	}
}
function handleCartResponse(status,content,contentType)
{
    var results = content.split(",");
    if(results[1] == 1 && document.getElementById("div_" + results[0]))
	{
	    replaceText("div_" + results[0], "Added to Cart");
	    document.getElementById("div_" + results[0]).style.color = '#245fa5';
	}
    else if(document.getElementById("div_" + results[0]))
	{
	    replaceText("div_" + results[0], "Available");
	    document.getElementById("div_" + results[0]).style.color = '#00cc00';
	}
    
    var sctext='Shopping Cart';
    if(results[2] > 0){
	sctext += ' (' + results[2] + ')';}
    replaceText("shopping_count", "<a href='/billing_info_paypal.php'>" + sctext + "</a>");
}
var start_time = null;
var timeout = 20;
var noreply_time  = new Date();
var post_search_handler = null;

function handleTLDResponseG(status,content,contentType)
{
    var search_code = 0;
    var wait = 500;
    
    var results = content.split(";");
    
    for ( var i in results )
	{
	    if(results[i] == 'done')
		{
		    if(typeof(post_search_handler) == 'function'){
			post_search_handler();}
		    return 0;
		}
	    
	    var result = results[i].split(",");
	    
	    
	    if(result[0] == 'code'){
		search_code = result[1];}
	    else
		{
		    noreply_time = new Date();
		    if(result[1] == 1)
			{
			    if(document.getElementById("div_"+result[0]).innerHTML != "Added to Cart")
				{
				    replaceText("div_" + result[0], "Available");
				    document.getElementById("div_" + result[0]).style.color = '#00cc00';
				    if(document.getElementById('div_fast_' + result[0]) !== null){
					document.getElementById('div_fast_' + result[0]).style.display = 'inline';}
				}
			    
			    document.getElementById('check_' + result[0]).disabled = false;
			}
		    else if(result[1] == 2)
			{
			    changeDisplay("div_fast_" + result[0], "block");
			    replaceText("div_" + result[0], "Added to Cart");
			    document.getElementById("td_" + result[0]).style.color = '#245fa5';
			    document.getElementById('check_' + result[0]).disabled = false;
			    document.getElementById('check_' + result[0]).checked = true;
			}
		    else
			{
			    replaceText("td_" + result[0], "Taken");
			    if(document.getElementById("td_" + result[0])){
				document.getElementById("td_" + result[0]).style.color = '#ff0000';}
			}
		}
	}
    
    if(start_time === null){
	start_time = new Date();}
    else
	{
	    var current_time = new Date();
	    var elapsed_time = ((current_time.getTime() / 1000) - (start_time.getTime() / 1000));
	    var elapsed_noreply_time = ((current_time.getTime() / 1000) - (noreply_time.getTime() / 1000));
	    
	    if(elapsed_time > 3){ wait = 750;}
	    if(elapsed_time > 10){ wait = 1000;}
	    if(elapsed_time > 20){ wait = 2000;}
	    if(elapsed_noreply_time > 20){ return(1);}
	}
    setTimeout("getURL('/javascript/search/old_search.php?get_results="+search_code+"',handleTLDResponseG);", wait);
}
if (!document.getElementById){
    document.getElementById = function() { return null; };}
function productCartSwap(add, name, duration, type, tag, tld, handler, notasync, interval, languageTag)
{
    var action = "";
    if(!tag){ tag = "";}
    if(!tld){ tld = "";}
    if(!handler){ handler = productCartSwapHandler;}
    if(!notasync){ notasync = 0;}
    
    if(tag == "addon_product"){
	handler = productCartSwapAddOnHandler;}

    if(add){
	action = "add";}
    else{
	action = "remove";}
    
    getURL('/products-pricing/services/productCart.php?action='+action+'&name='+name+'&duration='+duration+'&type='+type+'&tld='+tld+'&tag='+tag+'&billing_interval='+interval+'&languageTag='+languageTag, handler, notasync);
}

function productCartSwapHandler(status, content, contentType, minimal)
{
    if(status != 200 && status !== 0)
	{
	    alert("Something went wrong. Please refresh the page and try again. ("+status+")");
	    return;
	}

    if(!content){ return;}
    
    var results = eval('('+content+')');
    var action = results.action;
    var displayDomains = results.displayDomains;
    
    if(!results.status)
	{
	    alert(results.message);
	    if(document.getElementById("check_"+results.name)){ document.getElementById("check_"+results.name).checked = false;}
	    return;
	}
    if(action == "remove")
	{
	    if(document.getElementById("check_addon_"+results.name+results.type)){ document.getElementById("check_addon_"+results.name+results.type).checked = false;}
	    if(document.getElementById("check_addon_"+results.name)){ document.getElementById("check_addon_"+results.name).checked = false;}
	    if(document.getElementById("check_"+results.name)){ document.getElementById("check_"+results.name).checked = false;}
	}
    else if(action == "add")
	{
	    if(document.getElementById("check_addon_"+results.name)){ document.getElementById("check_addon_"+results.name).checked = true;}
	    if(document.getElementById("check_"+results.name)){ document.getElementById("check_"+results.name).checked = true;}
	}
    if(document.getElementById("whois_"+results.name) && document.getElementById("check_"+results.name))
	{
/*
	    if(document.getElementById("whois_"+results.name).disabled){
		document.getElementById("whois_"+results.name).disabled = false;}
	    else{
		document.getElementById("whois_"+results.name).disabled = true;}
*/
	    
	    if(!document.getElementById("check_"+results.name).checked){ 
		document.getElementById("whois_"+results.name).checked = false;}
	}
    if(document.getElementById("div_"+results.name)) 
	{
	    document.getElementById("check_"+results.name).checked = (results.action == "add") ? true : false;
	    document.getElementById("div_"+results.name).innerHTML = (results.action == "add") ? "Added to Cart" : "Available";
	    document.getElementById("div_"+results.name).style.color = (results.action == "add") ? "#245FA5" : "#00CC00";
	    
	    if(document.getElementById('div_fast_'+results.name)){
		document.getElementById('div_fast_'+results.name).style.display = (results.action == "add") ? 'none' : '';}
	}

    setShoppingCartInfo(results.productTotalPrice, results.productCount);
    
    deleteTableRow("tr_"+results.type+"_"+results.name, "cartTable");
    deleteTableRow("tr_alttld_"+results.type+"_"+results.name, "cartTable");
    deleteTableRow("tr_show_names_"+results.name, "cartTable");

    var removedItems = results.removedItems;

    if(removedItems)
	{
	    var removedItemsLength = removedItems.length;
	    
	    if(removedItemsLength > 0)
		{
		    for(var i = 0; i < removedItemsLength; i++)
			{
			    if(document.getElementById("tr_"+removedItems[i].type+"_"+removedItems[i].name))
				{
				    deleteTableRow("tr_"+removedItems[i].type+"_"+removedItems[i].name, "cartTable");
				}
			}
		}
	}

    if(results.action == "add") 
	{
	    domainsInCart.push(results.name);
	}
    else if(results.action == "remove") 
	{
	    if(Array.indexOf){ delete(domainsInCart[domainsInCart.indexOf(results.name)]);}
	    
	    if(document.getElementById("span_checkoutProductPrice_"+results.type+"_"+results.name))
		{
		    document.getElementById("span_checkoutProductPrice_"+results.type+"_"+results.name).innerHTML = formatCurrency(document.getElementById("span_checkoutProductPriceHidden_"+results.type+"_"+results.name).innerHTML, true);
		    document.getElementById("span_checkoutProductPrice_"+results.type+"_"+results.name).style.color = '#000000';
			}
	    if(document.getElementById("span_checkoutProductPriceAlternative_"+results.name))
		{
		    document.getElementById("span_checkoutProductPriceAlternative_"+results.name).innerHTML = formatCurrency(document.getElementById("span_checkoutProductPriceHiddenAlternative_"+results.name).innerHTML, true);
		    document.getElementById("span_checkoutProductPriceAlternative_"+results.name).style.color = '#000000';
			}
	}
    
    if(results.tag != "addon_product" && document.getElementById("div_addonDomainsInCart"))
	{ 
	    if(document.getElementById("div_addonDomainsInCart").style.display != "none"){
		showAddonDomainList("cart" , globalCurrentAddOnProductName, globalCurrentAddOnProductType);}
	}
    if(results.action == "add" || results.action == "addaddon")
	{
     	    var displayDomain = results.name;
            if(displayDomains) displayDomain = displayDomains[results.name];
	    handleProductTypeLevelOverride(true, results.type, results.name);
	    insertCartRow(document.getElementById("cartTable"), results.name, results.type, results.duration, results.privateWhois, results.googleApps, results.price, results.cartRowId, results.tag, results.displayType, displayDomain);
	}
    else
	{
	    handleProductTypeLevelOverride(false, results.type, results.name);
	    if(document.getElementById("check_"+results.name)) {
		document.getElementById("check_"+results.name).checked = false;}
	}
    
    if(document.getElementById("tr_addOnProducts"))
	{  
	    processAddOnServices(processAddOnServicesInlineHandler);
	}
    
    processPromoCode();
}

function emptyCart()
{
    var confirmation = confirm("Empty all the contents of your shopping cart?");
    
    if(!confirmation){ return;}
    
//    document.getElementById("LoadingDiv").style.display='block';
    $('#JqueryLoadingDiv').dialog('open');
    getURL('/products-pricing/services/productCart.php?action=empty', emptyCartHandler);
}

function emptyCartHandler(status, content, contentType)
{
    if(status != 200 && status !== 0)
	{
	    alert("Something went wrong. Please refresh the page and try again. ("+status+")");
	    resetLoadingDiv();
	    return;
	}
    
    var results = eval('('+content+')');
    
    if(!results.status)
	{
	    alert(results.message);
	    resetLoadingDiv();
	    return;
	}
    
    var removedItems = results.removedItems;
    var removedItemsLength = removedItems.length;
    
    for(var i = 0; i < removedItemsLength; i++)
	{
	    if(document.getElementById("check_"+removedItems[i]))
		{
		    document.getElementById("check_"+removedItems[i]).checked = false;
		}
	    if(document.getElementById("div_"+removedItems[i]))
		{
		    document.getElementById("div_"+removedItems[i]).innerHTML = "Available";
		    document.getElementById("div_"+removedItems[i]).style.color = "#00CC00";
		    
		    if(document.getElementById('div_fast_'+removedItems[i])){ 
			document.getElementById('div_fast_'+removedItems[i]).style.display = '';}
		}
	    if(document.getElementById("new_check_"+removedItems[i]))
		{
		    document.getElementById("new_check_"+removedItems[i]).click();
		}
	}
    
    domainsInCart = new Array();
    
    setShoppingCartInfo(results.productTotalPrice, results.productCount);
    if(document.getElementById("cartTable")){ window.location = location.href;}
    resetLoadingDiv();
}

function productCartSwapSimpleHandler(status, content, contentType)
{
    if(status != 200 && status !== 0)
	{
	    alert("Something went wrong. Please refresh the page and try again. ("+status+")");
//	    resetLoadingDiv();
	    return;
	}
    
    if(!content){ return;}
    
    var results = eval('('+content+')');
    var action = results.action;
    
    if(!results.status)
	{
	    alert(results.message);
//	    resetLoadingDiv();
	    if(document.getElementById("check_"+results.name)){
		document.getElementById("check_"+results.name).checked = false;}
	    return;
	}
 
    if(action == "remove")
	{
	    if(document.getElementById("check_"+results.name)){
		document.getElementById("check_"+results.name).checked = false;}
	}
    else if(action == "add")
	{
	    if(document.getElementById("check_"+results.name)){
		document.getElementById("check_"+results.name).checked = true;}
	}
    
    if(document.getElementById("div_"+results.name)) 
	{
	    document.getElementById("div_"+results.name).innerHTML = (results.action == "add") ? "Added to Cart" : "Available";
	    document.getElementById("div_"+results.name).style.color = (results.action == "add") ? "#245FA5" : "#00CC00";
	    
	    if(document.getElementById('div_fast_'+results.name)){
		document.getElementById('div_fast_'+results.name).style.display = (results.action == "add") ? 'none' : '';}
	}
    
    setShoppingCartInfo(results.productTotalPrice, results.productCount);
    
//    resetLoadingDiv();
}
function prepareCheckoutLoggedIn(ignorePrepareCheckoutLoggedIn)
{
    if(!ignorePrepareCheckoutLoggedIn){ ignorePrepareCheckoutLoggedIn = "";}
//    document.getElementById("LoadingDiv").style.display = 'block';
//Commented out loading div per sean request    $('#JqueryLoadingDiv').dialog('open');
    getURL('/products-pricing/services/productCart.php?action=checkSpecialTldRequirements&ignorePrepareCheckoutLoggedIn='+ignorePrepareCheckoutLoggedIn, checkSpecialTldRequirementsHandler);
}
function checkSpecialTldRequirementsHandler(status, content, contentType)
{
    if(status != 200 && status !== 0)
	{
	    alert("Something went wrong. Please refresh the page and try again. ("+status+")");
//	    document.getElementById("LoadingDiv").style.display='none';
            resetLoadingDiv();
	    return;
	}
    
    var results = eval('('+content+')');
    var haveIncomplete = 0;
    
    if(results.status)
	{
	    var html = "";
	    var specialTldArrayLength = results.specialTldArray.length;
	    
	    html += "<br><br><b>Special TLD Requirements</b><br><br>";
	    html += "The following TLDs have special requirements. Before continuing you must complete the requirements for each of them if you have not already done so. Items with an orange border require completion.<br><br>";
	    
	    html += "<table width='100%'>";
	    html += "<tr style='padding-top:3px; background-color: #FE6A00; color: #FFFFFF;'><td><b>TLD</b></td><td><b>Requirements</b></td><td><b>Action</b></td></tr>";
	    
	    for(var i = 0; i < specialTldArrayLength; i++)
		{
		    var tldType = results.specialTldArray[i].split(":");
		    var tld = tldType[0];
		    var type = tldType[1];
		    var editRequirementsLink = "";
		    var borderHtml = "";
		    
		    editRequirementsLink = (results.specialTldInfoArray[results.specialTldArray[i]].complete) ? "Edit Requirements" : "Complete Requirements";
		    
		    if(!results.specialTldInfoArray[results.specialTldArray[i]].complete)
			{
			    borderHtml = "border:2px solid #FF6800;";
			    haveIncomplete = 1;
			}
		    
		    html += "<tr style='"+borderHtml+"'><td width='10%' valign='top' style='border-left:1px solid #E0E0E0; border-bottom:1px solid #E0E0E0;'>"+tld+"<br>"+type+"</td><td width='65%' valign='top' style='border-left:1px solid #E0E0E0; border-bottom:1px solid #E0E0E0; border-right:1px solid #E0E0E0;'>"+results.specialTldInfoArray[results.specialTldArray[i]].requirements+"<br><br></td><td width='25%' valign='top' style='border-bottom:1px solid #E0E0E0; border-right:1px solid #E0E0E0;'><span id='span_view' onclick='specialRequirementsForm(\""+tld+"\", \""+type+"\");'onmouseover=\"this.style.cursor='pointer'\" style='color:blue;text-decoration:underline'>"+editRequirementsLink+"</span></td></tr>";
		}
	    
	    html += "</table>";
	    
	    if(!results.ignorePrepareCheckoutLoggedIn && document.getElementById("div_loggedInContainer"))
		{
		    if(document.getElementById("agreement_checkbox_bottom")) document.getElementById("agreement_checkbox_bottom").style.display = 'none';
		    if(document.getElementById("div_checkoutButton_bottom")) document.getElementById("div_checkoutButton_bottom").style.display = 'none';
		    if(document.getElementById("div_checkoutButton_top")) document.getElementById("div_checkoutButton_top").style.display = 'none';
		    document.getElementById("div_loggedInContainer").style.display = 'none';
		    document.getElementById("div_loggedOutContainer").style.display = 'none';
		    document.getElementById("div_loggedOutContainerSecondary").style.display = 'none';
		    if(document.getElementById("div_loggedOutContainerSecondaryBottom")) document.getElementById("div_loggedOutContainerSecondaryBottom").style.display = 'none';
		}
	    
	    if(document.getElementById("div_specialTldRequirementsContainer"))
		{
		    document.getElementById("div_specialTldRequirementsContainer").innerHTML = html;
		    document.getElementById("div_specialTldRequirementsContainer").style.display = 'block';
		    document.getElementById("div_specialTldRequirementsNotice").style.display = 'block';
		}
	    
	    resetLoadingDiv();
	    
	    if(haveIncomplete){	return;}
	}
    else
	{
	    if(document.getElementById("div_specialTldRequirementsContainer"))
		{
		    document.getElementById("div_specialTldRequirementsContainer").innerHTML = "";
		    document.getElementById("div_specialTldRequirementsContainer").style.display = '';
		    document.getElementById("div_specialTldRequirementsNotice").style.display = 'none';
		}
	}
    
    if(!results.ignorePrepareCheckoutLoggedIn)
	{
	    getURL('/account/services/account.php?action=check', prepareCheckoutLoggedInHandler);
	}
    else{
	resetLoadingDiv();
        }
}
function prepareCheckoutLoggedInHandler(status, content, contentType)
{
    if(status != 200 && status !== 0)
	{
	    alert("Something went wrong. Please refresh the page and try again. ("+status+")");
   	    resetLoadingDiv();
	    return;
	}
    
    var results = eval('('+content+')');
    
    if(!results.status)
	{
	    if(document.getElementById("div_loggedInContainer") && document.getElementById("div_loggedOutContainer"))
		{
		    if(document.getElementById("agreement_checkbox_bottom")) document.getElementById("agreement_checkbox_bottom").style.display = 'none';
		    if(document.getElementById("div_checkoutButton_bottom")) document.getElementById("div_checkoutButton_bottom").style.display = 'none';
		    if(document.getElementById("div_checkoutButton_top")) document.getElementById("div_checkoutButton_top").style.display = 'none';
		    document.getElementById("div_loggedInContainer").style.display = 'none';
		    document.getElementById("div_loggedOutContainer").style.display = 'block';
		    document.getElementById("div_loggedOutContainerSecondary").style.display = 'block';
		    if(document.getElementById("div_loggedOutContainerSecondaryBottom")) document.getElementById("div_loggedOutContainerSecondaryBottom").style.display = 'block';
		}
	    
	    if(results.message)
		{
		    alert(results.message);
		    
		    if(results.redirUrl)
			{
			    window.location = (results.redirUrl);
			    return;
			}	
		}
	    
	    resetLoadingDiv();
	    return;
	}
    
    if(document.getElementById("div_loggedInContainer") && document.getElementById("div_loggedOutContainer"))
	{
	    if(document.getElementById("agreement_checkbox_bottom")) document.getElementById("agreement_checkbox_bottom").style.display = '';
	    if(document.getElementById("div_checkoutButton_bottom"))document.getElementById("div_checkoutButton_bottom").style.display = '';
	    if(document.getElementById("div_checkoutButton_top"))document.getElementById("div_checkoutButton_top").style.display = '';
	    document.getElementById("div_loggedInContainer").style.display = 'block';
	    document.getElementById("div_loggedOutContainer").style.display = 'none';
	    document.getElementById("div_loggedOutContainerSecondary").style.display = 'none';
	    if(document.getElementById("div_loggedOutContainerSecondaryBottom"))document.getElementById("div_loggedOutContainerSecondaryBottom").style.display = 'none';
	    prepareCheckoutLoggedInDisplay(results.accountId, results.accountCredit);
	}
    
    resetLoadingDiv();
}
function handleProductTypeLevelOverride(checked, productType, name)
{
    if(checked)
	{
	    getURL('/products-pricing/services/productCart.php?action=removetype&type='+productType+"&name="+name, handleProductTypeLevelOverrideHandler);
	}
    else
	{
	    getURL('/products-pricing/services/productCart.php?action=getaddonextendedinfo&type='+productType+"&name="+name, handleProductTypeLevelOverrideHandler);
	}
}
function handleProductTypeLevelOverrideHandler(status, content, contentType)
{
    if(status != 200 && status !== 0)
	{
	    return;
	}
    
    var results = eval('('+content+')');
    
    if(!results.status)
	{
	    return;
	}
    
    if(!results.addonExtendedInfo){ return;}
    
    var levelOverride = results.addonExtendedInfo.levelOverride;
    var name = results.name;
    
    for(var i = 0; i < levelOverride.length; i++)
	{
	    var elementId = levelOverride[i]+"_"+name;
	    
	    if(document.getElementById(elementId))
		{
		    document.getElementById(elementId).checked = false;
		    document.getElementById(elementId).disabled = (document.getElementById(elementId).disabled) ? false : true ;
		}
	}
}
