/*dw_main.js*/
/*shared functions for site*/

var currentIndx = 0;

function buildStateSelect()
{
	/*this function creates the United States dropdown list*/
	var stateList = ["AK","AL","AR","AZ","CA","CO","CT","DC","DE","FL",
						  "GA","HI","IA","ID","IL","IN","KS","KY","LA","MA",
						  "MD","ME","MI","MN","MO","MS","MT","NC","ND","NE",
						  "NH","NJ","NM","NY","NV","OH","OK","OR","PA","RI",
						  "SC","SD","TN","TX","UT","VA","VT","WA","WI","WV",
						  "WY"];
	document.write("<option value=\"\"></option>");
	for (x = 0; x < stateList.length; x++)
	{
		document.write("<option value=\""+stateList[x]+"\">"+stateList[x]+"</option>");
	}
	
	return;
}

function passvalues()
{
	/* this function passes the contact email address and order id to the payment form*/
	/* set the unique order key */
	document.dw_orderform.elements['orderkey'].value =  Math.floor(Math.random()* 999999999 );

	/* create redirect query string to pass*/
	var urlnew = baseurl +
		"_secure_pay.html?orderkey=" +
		document.dw_orderform.elements['orderkey'].value +
		"&ServiceOrdered=" +
		escape(document.dw_orderform.elements['ServiceOrdered'].value) +
		"&ThanksPage=" +
		document.dw_orderform.elements['ThanksPage'].value +
		"&ThanksText=" +
		document.dw_orderform.elements['ThanksText'].value +
		"&DisplayPrice=" +
		document.dw_orderform.elements['DisplayPrice'].value;

	if (document.dw_orderform.elements['_req,from_Contactemail'])
	{
		urlnew = urlnew + "&_from_Contactemail=" +
			document.dw_orderform.elements['_req,from_Contactemail'].value;
	}
	/* exceptions added for services with no email required */
	else if (document.dw_orderform.elements['from_Contactemail'].value != '' )
	{
		urlnew = urlnew + "&_from_Contactemail=" +
			document.dw_orderform.elements['from_Contactemail'].value;
	}
	else 
	{
		urlnew = urlnew + "&_from_Contactemail=none";
	}

	/*pass it along*/
	document.dw_orderform.elements['_redirectURL'].value = urlnew;
	
	return;
}
function getParams() {
/*this function passes the values posted to the form into an array named params*/
var idx = document.URL.indexOf('?');
var params = new Array();
if (idx != -1) {
var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
for (var i=0; i<pairs.length; i++) {
nameVal = pairs[i].split('=');
params[nameVal[0]] = nameVal[1];
   }
}
return params;
}
/* change the thumbnail of the web site style selector*/
function changeStyle()
{
	/* split the template number from the file name */
	var splitArray = document.dw_orderform._req_SiteStyle.options[document.dw_orderform._req_SiteStyle.selectedIndex].value.split('|');

	document.styleImage.src = 'images/' + splitArray[1] + '_small.gif';

	/*required to keep the slideshow functions up to date*/
	currentIndx=document.dw_orderform._req_SiteStyle.selectedIndex;
	
	return;
}

/* slideshow next function for the site styles on the order form*/
function nextStyle()
{
	if (currentIndx<document.dw_orderform._req_SiteStyle.length-1)
	{
		currentIndx=currentIndx+1;
		document.dw_orderform._req_SiteStyle.selectedIndex = currentIndx;
		changeStyle();
	}
	else
	{
		currentIndx=1;
		document.dw_orderform._req_SiteStyle.selectedIndex = currentIndx;
		changeStyle();
	}
	
	return;
}

/* slideshow previous function for the site styles on the order form*/
function prevStyle()
{
	if (currentIndx>1)
	{
		currentIndx=currentIndx-1;
		document.dw_orderform._req_SiteStyle.selectedIndex = currentIndx;
		changeStyle();
	}
	else
	{
		currentIndx=document.dw_orderform._req_SiteStyle.length-1;
		document.dw_orderform._req_SiteStyle.selectedIndex = currentIndx;
		changeStyle();
	}
	
	return;
}

/* pop open the appropriate large version from the web site style selector*/
function popupStyle()
{
	/* split the template number from the file name */
	var splitArray = document.dw_orderform._req_SiteStyle.options[document.dw_orderform._req_SiteStyle.selectedIndex].value.split('|');

	if (splitArray[1] != 'none_selected')
	{
		whereToGo = 'images/' + splitArray[1] + '_large.gif' ;
		myWin = window.open(whereToGo, 'dw_styleWin', 'width=690,height=490,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=0,resizable=0');
		myWin.focus();
	}
	else
	{
		alert('You must select a style from the drop-down menu on the left.');
	}
	
	return;
}

function openWin(URL,w,h,WindowName)
{
	var build;
	var left = 0;
	var top = 0;
	var NewWindow = null;
	
	if (document.layers) //NS4
	{
		left = window.screenX + ((window.outerWidth - w) / 2);
		top = window.screenY + ((window.outerHeight - h) / 2);
		build = "screenX=" + left + ",screenY=" + top + ",resizable=yes,scrollbars=yes,width=" + w + ",height=" + h;
	}
	else if (document.all) //IE4
	{
		left = (screen.width - w) / 2;
		top = (screen.height - h) / 2;
		build = "left=" + left + ",top=" + top +
			",resizable=yes,scrollbars=yes,titlebar=no,status=no,width=" + w + ",height=" + h;
	}
	else
	{
		build = 'width='+w+',height='+h+',resizable=yes,scrollbars=yes';
	}

	NewWindow = window.open(URL, WindowName, build);
	NewWindow.focus();
	NewWindow.opener = self;
	
	return;
}

function CheckDomainAvail(fName)
{
	document.dw_orderform.DomainChecked.value = 1;
	appDest = "http://preview.i-2.com/:domaincheck?CustomerID=" +
		document.dw_orderform.elements[fName].value;
	openWin(appDest , 300, 200, "_dw_domain_check");
	
	return;
}

//calculate cookie expiration - pass it a numbe of days
function getexpirydate(nodays)
{
	var UTCstring;

	Today = new Date();
	nomilli=Date.parse(Today);
	Today.setTime(nomilli+nodays*24*60*60*1000);
	UTCstring = Today.toUTCString();

	return UTCstring;
}

//set the longterm cookie - currently only the referral source
function setLongCookie(doc)
{
	if (QueryString ("source") && QueryString ("source") != "null")
	{
		setCookie (doc, "source", unescape(QueryString("source")), getexpirydate(60));
	}
}

//get the longterm cookie - currently only the referral source
function getLongCookie()
{
	var the_cookie = null;
	var the_cookie = null;
	var broken_cookie = null;
	var retval = null;

	if (!document.cookie || document.cookie == '')
	{
		// there's no cookie, so go no further
		return 'none';
	}

	// there is a cookie
	the_cookie = document.cookie;
	the_cookie = unescape(the_cookie);
	broken_cookie = the_cookie.split(":");
	retval = broken_cookie[1];

	return retval;
}

setLongCookie(document);
