var xmlHttp = false;

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try
{
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
  try
  {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch (e2)
  {
    xmlHttp = false;
  }
}
@end @*/

if (!xmlHttp && typeof XMLHttpRequest != "undefined")
{
  xmlHttp = new XMLHttpRequest();
}

function LoadMenu()
{
  timerID = "";
  navRoot = document.getElementById("ddm");
  for (i = 0; i < navRoot.childNodes.length; i++)
  {
    node = navRoot.childNodes[i];
    if (node.nodeName == "LI")
    {
      node.onmouseover = function()
      {
        if (this.id != "end_li")
        {
          this.style.backgroundImage = "url(/system/media/menu_1.jpg)";
          this.className = "over";
          
          if (this.id == timerID)
          {
            clearTimeout(timer);
          }
          else if (this.id != timerID && timerID != "")
          {
            clearTimeout(timer);
            document.getElementById(timerID).style.backgroundImage = "url(/system/media/menu.jpg)";
            document.getElementById(timerID).className = "out";
          }
        }
      }
      node.onmouseout = function()
      {
        timerID = this.id;
        timer = setTimeout("UnloadMenu('" + this.id + "')", 200);
      }
    }
  }
}
window.onload = LoadMenu;

function UnloadMenu(id)
{
  document.getElementById(id).style.backgroundImage = "url(/system/media/menu.jpg)";
  document.getElementById(id).className = "out";
}

function ToggleFeatures()
{
  if (document.getElementById("hosting_features").style.display == "none")
  {
    document.getElementById("hosting_features").style.display = "block";
  }
  else
  {
    document.getElementById("hosting_features").style.display = "none";
  }
}

SelectedCategory = "1";

function LoadCategory(id, category_count)
{
  for (i = 1; i <= category_count; i++)
  {
    if (i == id)
    {
      document.getElementById("category_" + SelectedCategory).className = "hosting_category";
      document.getElementById("category_" + i).className = "hosting_category_selected";
      document.getElementById("category_data_" + i).style.display = "block";
      SelectedCategory = i;
    }
    else
    {
      document.getElementById("category_data_" + i).style.display = "none";
    }
  }
}

timerID2 = "";

function LoadInfoData(id)
{
  if (id == timerID2)
  {
    clearTimeout(timer2);
  }
  else if (timerID2 != "")
  {
    clearTimeout(timer2);
    document.getElementById(timerID2).className = "info_data";
  }
  document.getElementById(id).className = "info_data_over";
}

function UnloadInfoData(id)
{
  timerID2 = id;
  timer2 = setTimeout("UnloadInfoNow('" + id + "')", 200);
}

function UnloadInfoNow(id)
{
  document.getElementById(id).className = "info_data";
}

function OpenWin(url, width, height, location, menubar, status, toolbar, scrollbars, resizable)
{
  var left = (screen.width - width) / 2;
  var top = (screen.height - height) / 2;
  window.open(url, "title", "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + ", location=" + location + ", menubar=" + menubar + ", status=" + status + ", toolbar=" + toolbar + ", scrollbars=" + scrollbars + ", resizable=" + resizable);
}

var current_form_name;

function OnSend (form_file, form_name, form_option)
{
  LockForm (form_name, 0);
  var get_string = "?local_directive=" + form_option;
  current_form_name = form_name;

  for (i = 0; i < document.forms[form_name].elements.length; i++)
  {
    if (document.forms[form_name].elements[i].type != "button" && document.forms[form_name].elements[i].type != "reset" && document.forms[form_name].elements[i].type != "submit")
    {
      get_string += "&" + document.forms[form_name].elements[i].name + "=" + document.forms[form_name].elements[i].value;
    }
  }
  xmlHttp.open("GET", form_file + get_string, true);
  xmlHttp.onreadystatechange = OnSendReceive;
  xmlHttp.send(null);
}

function OnSendReceive ()
{
  if (xmlHttp.readyState == 4)
  {
    var message = xmlHttp.responseText.split("|");
    if (message[0] == 1)
    {
      document.forms[current_form_name].submit();
    }
    else
    {
      alert("Please verify the following:\n\n" + message[1]);
      LockForm (current_form_name, 1);
    }
  }
}

function LockForm (form_name, option)
{
  if (option == 0)
  {
    for (i = 0; i < document.forms[form_name].elements.length; i++)
    {
      if ((document.forms[form_name].elements[i].type == "button" || document.forms[form_name].elements[i].type == "reset" || document.forms[form_name].elements[i].type == "submit") && (document.forms[form_name].elements[i].value == "Submit" || document.forms[form_name].elements[i].value == "Clear"))
      {
        document.forms[form_name].elements[i].disabled = true;
      }
    }
  }
  else if (option == 1)
  {
    for (i = 0; i < document.forms[form_name].elements.length; i++)
    {
      if ((document.forms[form_name].elements[i].type == "button" || document.forms[form_name].elements[i].type == "reset" || document.forms[form_name].elements[i].type == "submit") && (document.forms[form_name].elements[i].value == "Submit" || document.forms[form_name].elements[i].value == "Clear"))
      {
        document.forms[form_name].elements[i].disabled = false;
      }
    }
  }
}

function SetAsCurrency (id)
{
  X = document.getElementById(id).value;
  document.getElementById(id).value = (S = new String(Math.round(X * 100))).substr(0, T = S.length - 2) + "." + S.substr(T, 2);
}

// PIN //

function OnSendPIN ()
{
  LockForm (1, 0);
  var get_string = "?local_directive=validate";

  for (i = 0; i < document.forms[1].elements.length; i++)
  {
    if (document.forms[1].elements[i].type != "button" && document.forms[1].elements[i].type != "reset" && document.forms[1].elements[i].type != "submit")
    {
      get_string += "&" + document.forms[1].elements[i].name + "=" + document.forms[1].elements[i].value;
    }
  }
  xmlHttp.open("GET", "payitnow.ajax.php" + get_string, true);
  xmlHttp.onreadystatechange = OnSendReceivePIN;
  xmlHttp.send(null);
}

function OnSendReceivePIN ()
{
  if (xmlHttp.readyState == 4)
  {
    alert("* THIS FORM IS IN A TESTING MODE. NO MONEY TRANSACTIONS WILL OCCUR WITH YOUR CREDIT CARD. *");
    var message = xmlHttp.responseText.split("|");
    if (message[0] == 1)
    {
      LockForm (1, 1);
      document.forms[1].trnId.value = message[1];
      document.forms[1].authCode.value = message[3];
      document.forms[1].submit();
    }
    else
    {
      alert("Please verify the following:\n\n" + message[2]);
      LockForm (1, 1);
    }
  }
}

// SUN (Sign Up Now) //

function OnCheckDomain ()
{
  domain = document.getElementById("domain_name").value;
  domain_extension = document.getElementById("domain_extension").value;
  full_domain = domain + "." + domain_extension;

  if (domain != "" && domain_extension != "")
  {
    LockForm (1, 0);
    document.getElementById("check_domain").style.display = "none";
    document.getElementById("check_domain_checking").style.display = "block";
    document.getElementById("check_domain_available").style.display = "none";
    document.getElementById("check_domain_taken").style.display = "none";

    xmlHttp.open("GET", "order.ajax.php?local_directive=check_domain&domain_name=" + full_domain, true);
    xmlHttp.onreadystatechange = OnCheckDomainReceive;
    xmlHttp.send(null);
  }
  else
  {
    alert("Please enter a domain name and extension.");
  }
}

function OnCheckDomainReceive ()
{
  if (xmlHttp.readyState == 4)
  {
    message = xmlHttp.responseText.split("|");
    if (message[0] == 1)
    {
      document.getElementById("check_domain").style.display = "none";
      document.getElementById("check_domain_checking").style.display = "none";
      document.getElementById("check_domain_available").style.display = "block";
      document.getElementById("check_domain_taken").style.display = "none";
      document.getElementById("domain_check").value = 1;
    }
    else if (message[0] == 0)
    {
      document.getElementById("check_domain").style.display = "none";
      document.getElementById("check_domain_checking").style.display = "none";
      document.getElementById("check_domain_available").style.display = "none";
      document.getElementById("check_domain_taken").style.display = "block";
      document.getElementById("domain_check").value = 0;
    }
    else
    {
      ResetDomainCheck ();
      alert (message[3]);
    }
    LockForm (1, 1);
  }
}

function ResetDomainCheck ()
{
  document.getElementById("domain_check").value = 0;
  document.getElementById("check_domain").style.display = "block";
  document.getElementById("check_domain_checking").style.display = "none";
  document.getElementById("check_domain_available").style.display = "none";
  document.getElementById("check_domain_taken").style.display = "none";
}

// SUN (Sign Up Now) //

function OnCheckPromotion ()
{
	/*
		Get code. send it.
	*/
	var promotional_code = document.getElementById("promotional_code").value;

	if (promotional_code != "")
	{
		LockForm (1, 0);
		document.getElementById("check_promotion").style.display = "none";
		document.getElementById("check_promotion_checking").style.display = "block";
		document.getElementById("check_promotion_available").style.display = "none";
		document.getElementById("check_promotion_taken").style.display = "none";
		xmlHttp.open("GET", "promotion.ajax.php?promotional_code=" + promotional_code, true);
		xmlHttp.onreadystatechange = OnCheckPromotionReceive;
		xmlHttp.send(null);
	}
	else
	{
		alert("Please enter a promotional code.");
  	}
}

function OnCheckPromotionReceive ()
{
	if (xmlHttp.readyState == 4)
	{
		message = xmlHttp.responseText.split("|");
		if (message[0] == 1)
		{
			document.getElementById("hosting_package_container").innerHTML = message[1];
			document.getElementById("check_promotion").style.display = "none";
			document.getElementById("check_promotion_checking").style.display = "none";
			document.getElementById("check_promotion_available").style.display = "block";
			document.getElementById("check_promotion_taken").style.display = "none";
		}
		else if (message[0] == 0)
		{
			document.getElementById("check_promotion").style.display = "none";
			document.getElementById("check_promotion_checking").style.display = "none";
			document.getElementById("check_promotion_available").style.display = "none";
			document.getElementById("check_promotion_taken").style.display = "block";
		}
		LockForm (1, 1);
	}
}

function ResetPromotionCheck ()
{
	document.getElementById("check_promotion").style.display = "block";
	document.getElementById("check_promotion_checking").style.display = "none";
	document.getElementById("check_promotion_available").style.display = "none";
	document.getElementById("check_promotion_taken").style.display = "none";
}

function ToggleAccountOption (option)
{
	if (option == "0")
	{
		document.getElementById("new_user_information").style.display = "block";
		document.getElementById("row_username").style.visibility = "hidden";
		document.getElementById("row_password").style.visibility = "hidden";
	}
	else
	{
		document.getElementById("new_user_information").style.display = "none";
		document.getElementById("row_username").style.visibility = "visible";
		document.getElementById("row_password").style.visibility = "visible";
	}
}

function ToggleDomainOption (option)
{
  if (option == "register")
  {
    document.getElementById("btn_check_domain").disabled = false;
    ResetDomainCheck ();
  }
  else
  {
    document.getElementById("btn_check_domain").disabled = true;
    ResetDomainCheck ();
  }
}

function OnSendON ()
{
	LockForm (1, 0);
	var get_string = "?local_directive=validate";

	for (i = 0; i < document.forms[1].elements.length; i++)
	{
		if (document.forms[1].elements[i].type != "button" && document.forms[1].elements[i].type != "reset" && document.forms[1].elements[i].type != "submit")
		{
			get_string += "&" + document.forms[1].elements[i].name + "=" + document.forms[1].elements[i].value;
		}
	}
	xmlHttp.open("GET", "order.ajax.php" + get_string, true);
	xmlHttp.onreadystatechange = OnSendReceiveON;
	xmlHttp.send(null);
}

function OnSendReceiveON ()
{
	if (xmlHttp.readyState == 4)
	{
		var message = xmlHttp.responseText.split("|");
		if (message[0] == 1)
		{
			document.forms[1].submit ();
			LockForm (1, 1);
		}
		else
		{
			alert ("Please verify the following:\n\n" + message[1]);
			LockForm (1, 1);
		}
	}
}

/*
	Soon to be deprecated.
*/
function OnSendSUN ()
{
  LockForm (1, 0);
  var get_string = "?local_directive=validate";

  for (i = 0; i < document.forms[1].elements.length; i++)
  {
    if (document.forms[1].elements[i].type != "button" && document.forms[1].elements[i].type != "reset" && document.forms[1].elements[i].type != "submit")
    {
      get_string += "&" + document.forms[1].elements[i].name + "=" + document.forms[1].elements[i].value;
    }
  }
  xmlHttp.open("GET", "signup.ajax.php" + get_string, true);
  xmlHttp.onreadystatechange = OnSendReceiveSUN;
  xmlHttp.send(null);
}

function OnSendReceiveSUN ()
{
  if (xmlHttp.readyState == 4)
  {
    var message = xmlHttp.responseText.split("|");
    if (message[0] == 1)
    {
      document.forms[1].trnId.value = message[1];
      document.forms[1].authCode.value = message[3];
      document.forms[1].rbAccountId.value = message[4];
      document.forms[1].submit();
      LockForm (1, 1);
    }
    else
    {
      alert("Please verify the following:\n\n" + message[3]);
      LockForm (1, 1);
    }
  }
}

function UpdatePaymentMethod (option)
{
	if (option == "1")
	{
		document.getElementById("row_name_on_card").style.visibility = "visible";
		document.getElementById("row_card_number").style.visibility = "visible";
		document.getElementById("row_card_expiry").style.visibility = "visible";
	}
	else
	{
		document.getElementById("row_name_on_card").style.visibility = "hidden";
		document.getElementById("row_card_number").style.visibility = "hidden";
		document.getElementById("row_card_expiry").style.visibility = "hidden";
	}
}

function ValidatePhoneNumber (element)
{
	var chars = element.value.split("");
	var return_string = "";

	// Loop through each and reassemble.
	for (i = 0; i < chars.length; i++)
	{
		// If this char is an invalid char, skip it.
		if (!isNaN (chars[i]) && chars[i] != " " && return_string.length < 14)
		{
			// get current string length.
			if (return_string.length == 0)
			{
				return_string += "(";
			}
			if (return_string.length == 4)
			{
				return_string += ") ";
			}
			if (return_string.length == 9)
			{
				return_string += "-";
			}
			return_string += chars[i];
		}
	}
	element.value = return_string;
}
