document.write('<script language="javascript" src="ajax/lithos.js"></script>');

// validate URL
function jsValidateURL()
{
	var xmlHttp = GetXmlHttpObject()
	
	if (xmlHttp == null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	
	var txtURL = document.frmRegister.txtURL.value;
	
	SetHTMLColor("divURLDesc", "&nbsp;", colorNormal);
	SetHTML("divURLEx", "&nbsp;");
	
	if (txtURL.length == 0)
		return;
	
	SetHTMLColor("divURLDesc", "Validating URL...", colorNormal);
		
	// ver si tiene protocol:// y si lo tiene QUITARLO!
	var hasProtocol = /^[A-Za-z]+:\/\//;
	
	txtURL = txtURL.replace(hasProtocol, "");
	
	// si solo tiene un punto entonces es invaliDIsimo
	if (txtURL.indexOf('.') == -1)
	{
		SetHTMLColor("divURLDesc", "The URL you entered is invalid.", colorInvalid);
		return;
	}
	
	// tiene que tener al menos dos puntos, sino quita el ultimo
	if (txtURL.indexOf('.') == txtURL.lastIndexOf('.'))
		txtURL = "www." + txtURL;
		
	// si tiene un SLASH al final, se lo quita....
	if (txtURL.charAt(txtURL.length -1) == "/")
		txtURL = txtURL.substring(0, txtURL.length - 1);
		
	document.frmRegister.txtURL.value = txtURL;
	
	// si tiene <= 5 caracteres probablemente es invalida (menor = a.bc.de)
	if (txtURL.length <= 5)
	{
		SetHTMLColor("divURLDesc", "The URL you entered is too short.", colorInvalid);
		return;
	}
	
	if (txtURL.length > 64)
	{
		SetHTMLColor("divURLDesc", "The URL you entered is too long. URL's must be less than 64 characters long.", colorInvalid);
		return;
	}
	
	// como el REGEX es complicado y feo, entonces mejor dejarle a PHP para que revise el website
	// hacerlo URLEncode
	//return;
	
	var url = "ajax/new_website.php?q=validateURL" + "&URL=" + (URLEncode(txtURL)) + "&sid=" + NumberMS();
	
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			switch (xmlHttp.responseText)
			{
				case "1": // success!
					SetHTMLColor("divURLDesc", "Your entered URL appears to be valid!", colorValid);
					break;
				
				case "-1":
					SetHTMLColor("divURLDesc", "Your entered URL is already in our website database.", colorInvalid);
					break;
					
				case "-2":
					SetHTMLColor("divURLDesc", "Your entered URL is invalid and does not resolve.", colorInvalid);
					break;
			}
		}
	}
	
	//jsValidateUsernameChanged
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

// validate website title
function jsValidateTitle()
{
	var txtTitle = document.frmRegister.txtTitle.value;
	
	SetHTMLColor("divTitleDesc", "&nbsp;", colorNormal);
	SetHTML("divTitleEx", "&nbsp;");
	
	if (txtTitle.length == 0)
		return;

	if (txtTitle.length > 64)
	{
		SetHTMLColor("divTitleDesc", "The website title you entered is too long.", colorInvalid);
		return;
	}
	
	SetHTMLColor("divTitleDesc", "The website title you entered is valid!", colorValid);
}

function jsUpdateTitle(boolUpdateEx)
{
	var txtTitle = document.frmRegister.txtTitle.value;
	
	SetHTMLColor('divTitleDesc', 'Enter the website title (without the slogan). You have ' + (64 - txtTitle.length) + ' characters left.', colorNormal);
	
	if (boolUpdateEx)
		SetHTMLColor('divTitleEx', '<u>Example:</u> WebSoda.net', colorValid);
}

// validate website slogan
function jsValidateSlogan()
{
	var txtSlogan = document.frmRegister.txtSlogan.value;
	
	SetHTMLColor("divSloganDesc", "&nbsp;", colorNormal);
	SetHTML("divSloganEx", "&nbsp;");
	
	if (txtSlogan.length == 0)
		return;

	if (txtSlogan.length > 64)
	{
		SetHTMLColor("divSloganDesc", "The website slogan you entered is too long.", colorInvalid);
		return;
	}
	
	SetHTMLColor("divSloganDesc", "The website slogan you entered is valid!", colorValid);
}

function jsUpdateSlogan(boolUpdateEx)
{
	var txtSlogan = document.frmRegister.txtSlogan.value;
	
	SetHTMLColor('divSloganDesc', 'Enter the website slogan. You have ' + (64 - txtSlogan.length) + ' characters left. You may leave it blank.', colorNormal);
	
	if (boolUpdateEx)
		SetHTMLColor('divSloganEx', '<u>Example:</u> Vote for the best websites and add your own!', colorValid);
}

// validate website slogan
function jsValidateDesc()
{
	var txtDesc = document.frmRegister.txtDesc.value;
	
	SetHTMLColor("divDescDesc", "&nbsp;", colorNormal);
	
	if (txtDesc.length == 0)
		return;

	if (txtDesc.length > 255)
	{
		SetHTMLColor("divDescDesc", "The website description you entered is too long.", colorInvalid);
		return;
	}
	
	// si tiene comillas al principio o al final, quitarselas...
	if (txtDesc.charAt(0) == "\"")
		txtDesc = txtDesc.substring(1, txtDesc.length);
	
	if (txtDesc.charAt(txtDesc.length-1) == "\"")
		txtDesc = txtDesc.substring(0, txtDesc.length-1);
	
	document.frmRegister.txtDesc.value = txtDesc;
	
	SetHTMLColor("divDescDesc", "The website description you entered is valid!", colorValid);
}

function jsUpdateDesc(boolUpdateEx)
{
	var txtDesc = document.frmRegister.txtDesc.value;
	
	SetHTMLColor('divDescDesc', 'Enter the website description. You have ' + (255 - txtDesc.length) + ' characters left. Website descriptions are usually inside the &lt;meta name="Description"&gt; tag.', colorNormal);

}

function jsValidateImageURL()
{
	var xmlHttp = GetXmlHttpObject()
	
	if (xmlHttp == null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	
	var txtURL = document.frmRegister.txtImageURL.value;
	
	SetHTMLColor("divImageURLDesc", "&nbsp;", colorNormal);
	//SetHTML("divImageURLEx", "&nbsp;");
	
	if (txtURL.length == 0)
		return;
	
	SetHTMLColor("divImageURLDesc", "Validating Image URL...", colorNormal);
		
	// esta vez si NO tiene mas bien hay que agregarselo!
	var hasProtocol = /^[A-Za-z]+:\/\//;
	
	if (!txtURL.match(hasProtocol))
		txtURL = "http://" + txtURL; //txtURL.replace(hasProtocol, "");
	
	// si no tiene un punto entonces es invaliDIsimo
	if (txtURL.indexOf('.') == -1)
	{
		SetHTMLColor("divImageURLDesc", "The Image URL you entered is invalid.", colorInvalid);
		return;
	}
	
	document.frmRegister.txtImageURL.value = txtURL;
	
	if (txtURL.length > 128)
	{
		SetHTMLColor("divImageURLDesc", "The Image URL you entered is too long. Image URL's must be less than 128 characters long.", colorInvalid);
		return;
	}
	
	// como el REGEX es complicado y feo, entonces mejor dejarle a PHP para que revise el website
	// hacerlo URLEncode
	//return;
	
	var url = "ajax/new_website.php?q=validateImageURL" + "&URL=" + (URLEncode(txtURL)) + "&sid=" + NumberMS();
	
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			switch (xmlHttp.responseText)
			{
				case "1": // success!
					SetHTMLColor("divImageURLDesc", "Your entered Image URL appears to be valid!", colorValid);
					break;
				
				/*
				case "-1":
					SetHTMLColor("divURLDesc", "Your entered URL is already in our website database.", colorInvalid);
					break;
				*/	
				case "-1":
					SetHTMLColor("divImageURLDesc", "Your entered Image URL is invalid and does not resolve.", colorInvalid);
					break;
			}
		}
	}
	
	//jsValidateUsernameChanged
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);	
}

function jsPreviewWebsite()
{
	var txtURL = "www.url.com";
	var txtTitle = "Your Title"
	var txtSlogan = "Your slogan";
	var txtDesc = "This is the website description.";
	var txtImageURL = "http://www.websoda.net/images/layout/blank.gif";
	
	if (document.frmRegister.txtURL.value != "")
		txtURL = document.frmRegister.txtURL.value;
	
	if (document.frmRegister.txtTitle.value != "")
		txtTitle = document.frmRegister.txtTitle.value;
	
	if (document.frmRegister.txtSlogan.value != "")
		txtSlogan = document.frmRegister.txtSlogan.value;
	
	if (document.frmRegister.txtDesc.value != "")
		txtDesc = document.frmRegister.txtDesc.value;
	
	if (document.frmRegister.txtImageURL.value != "")
		txtImageURL = document.frmRegister.txtImageURL.value;
	
	// set title
	SetHTML("divPrevTitle", "<a href=\"http://" + txtURL + "\" target='_blank'>" + txtTitle + "</a></div>");
	SetHTML("divPrevAbout", txtTitle);
	
	// set slogan
	SetHTML("divPrevSlogan", "&#8212; " + txtSlogan);
	
	// URL
	SetHTML("divPrevURL", "<span class=\"style95\">" + 
                          "<a href=\"http://" + txtURL + "\" target='_blank'>http://" + txtURL + "</a></span>" + 
                          "<a href=\"http://" + txtURL + "\" target='_blank'><img src=\"images/layout/outer_link.gif\" width=\"14\" height=\"14\" border=\"0\" /></a>");
	
	// Preview image
	SetHTML("divPrevImage", "<a href=\"http://" + txtURL + "\" target=\"_blank\">" +
			"<img src=\"" + txtImageURL + "\" width=\"320\" height=\"240\" border=\"0\" /></a>");
	
	// Description
	SetHTML("divPrevDesc", "<span class=\"style86\">&quot;" + txtDesc + "&quot;</span>");
	
}