// Opens the browser's print window
/*function printWindow() {
	window.print();
	return false;
}
*/

// Open popup window
function openWindow(theURL) { //v2.0
	var oWin = window.open(theURL, "","toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=800,height=600");
	if (oWin==null || typeof(oWin)=='undefined') {
		alert('You have a popup blocker installed. Please, deactivates it in order to execute iWeb correctly.');
	}
	else
	{
		oWin.focus();
	}
	//window.showModalDialog(theURL, null,"toolbar:no;status:no;menubar:no;scrollbars:yes;resizable:yes;width:700;height:470");
}
function openWindow(theURL, winName) { //v2.0
	var oWin = window.open(theURL, winName,"toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=800,height=600");
	if (oWin==null || typeof(oWin)=='undefined') {
		alert('You have a popup blocker installed. Please, deactivates it in order to execute iWeb correctly.');
	}
	else
	{
		oWin.focus();
	}
}

function openFileUploadWindow(destinationFolder) { //v2.0
	var width = 550;
	var height = 160;
	var left = Math.floor((screen.availWidth - width) / 2);
	var top = Math.floor((screen.availHeight - height) / 2);

	var oWin = window.open('../FileUpload.aspx?folder=' + destinationFolder, 'FileUpload','toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,width=' + width + ',height=' + height + ', left=' + left + ', top=' + top);
	if (oWin==null || typeof(oWin)=='undefined') {
		alert('You have a popup blocker installed. Please, deactivates it in order to execute iWeb correctly.');
	}
	else
	{
		oWin.focus();
	}
}

// Open popup window including escape
function openWindowEscape(theURL) { 
	theURL = escape(theURL);
	window.open(theURL, "","toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=800,height=600");
}

function IsDigit()
{
	return ((event.keyCode >= 48) && (event.keyCode <= 57));
}

function IsNumericSymbol()
{
	return ((event.keyCode == 36) || (event.keyCode == 46));
}
// Validate only numeric fields
function OnlyNumeric()
{
	if (!IsDigit() && !IsNumericSymbol()) 
		event.returnValue = false;
}

// TextArea length restriction
function textareaMaxLength(Object)
{
  return (Object.value.length <= 2000);
}

function AddressValidation(address1ControlId, address2ControlId, cityControlId, stateControlId, zipControlId, countryControlId)
{
	var address1Control = document.getElementById(address1ControlId);
	var address2Control = document.getElementById(address2ControlId);
	var cityControl = document.getElementById(cityControlId);
	var stateControl = document.getElementById(stateControlId);
	var zipControl = document.getElementById(zipControlId);
	var countryControl = document.getElementById(countryControlId);

	var address =	"<Record>";
		address +=	"	<Address>" + escapeForXml(address1Control.value) + "</Address>";
		address +=	"	<Address2>" + escapeForXml(address2Control.value) + "</Address2>";
		address +=	"	<City>" + escapeForXml(cityControl.value) + "</City>";
		address +=	"	<State>" + stateControl.value + "</State>";
		address +=	"	<Zip>" + escapeForXml(zipControl.value) + "</Zip>";
		address +=	"	<Plus4></Plus4>";
		address +=	"	<Country>" + countryControl.value + "</Country>";
		address +=	"	<CongressionalDistrict></CongressionalDistrict>";
		address +=	"	<Latitude></Latitude>";
		address +=	"	<Longitude></Longitude>";
		address +=	"</Record>";

	var resultSet = PostAndReceiveAddressXml(address, '100919984');
	var oXml = new ActiveXObject("Microsoft.XMLDOM");
	oXml.async = false;
	oXml.loadXML(resultSet);

	if (oXml.getElementsByTagName("RecordSet/Record/Address").length > 0)
		address1Control.value = oXml.selectSingleNode("RecordSet/Record/Address").text;

	if (oXml.getElementsByTagName("RecordSet/Record/Address2").length > 0)
		address2Control.value = oXml.selectSingleNode("RecordSet/Record/Address2").text;

	if (oXml.getElementsByTagName("RecordSet/Record/City").length > 0)
		cityControl.value = oXml.selectSingleNode("RecordSet/Record/City").text;

	if (oXml.getElementsByTagName("RecordSet/Record/State").length > 0)
		stateControl.value = oXml.selectSingleNode("RecordSet/Record/State").text;

	if (oXml.getElementsByTagName("RecordSet/Record/Zip").length > 0)
	{
			var plus4=oXml.selectSingleNode("RecordSet/Record/Zip").text;
			if (oXml.getElementsByTagName("RecordSet/Record/Plus4").length > 0) 
			{
				if(oXml.selectSingleNode("RecordSet/Record/Plus4").text!="")
				plus4 = plus4+"-"+ oXml.selectSingleNode("RecordSet/Record/Plus4").text; 
			}
			zipControl.value  = plus4;
	}

	if (oXml.getElementsByTagName("RecordSet/Record/Country").text != 'US')
		countryControl.value = oXml.selectSingleNode("RecordSet/Record/Country").text;
}

function PostAndReceiveAddressXml(XmlString, Key)
{
	var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

	var xmlText = "<?xml version='1.0'?>";
		xmlText = xmlText + "<RecordSet><CustomerID>"+ Key +"</CustomerID>"+ XmlString +"</RecordSet>";

	var submitUrl = "http://xml.melissadata.com/xml.asp";

	xmlhttp.Open("POST", submitUrl, false);
	xmlhttp.Send(xmlText);

	return xmlhttp.responseXML.xml;
}

function escapeForXml(value)
{
	value = value.replace("&", "&amp;");
	value = value.replace("<", "&lt;");
	value = value.replace(">", "&gt;");
	value = value.replace("\\", "&quot;");
	value = value.replace("'", "&apos;");
	return value;
}
