// Non-application (and non-page) specific utilities

function newWindow(URL) {
	var newWindow = this.open(URL,"CoreMedia","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,marginwidth=0,marginheight=0,rightmargin=0");
}

function showSecurityKey() {
	var newWindow = this.open("securityKey.asp","CoreMedia","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,copyhistory=0,marginwidth=0,marginheight=0,rightmargin=0,height=385,width=267")
}

function descriptionWindow(productId) {
	var newWindow = this.open("description.asp?idProduct=" + productId,"CoreMedia","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,marginwidth=0,marginheight=0,rightmargin=0,height=300,width=300")
}

function closeWindow() {
	window.close();
}

function show(theObject) {
	// IE only
	theObject.style.visibility = "inherit"; // visible
}

function hide(theObject) {
	// IE only
	theObject.style.visibility = "hidden";
}

function writeSpan(spanObject,theString) {
// Write theString into the contents of a span with the ID of spanObject
	spanObject.innerHTML=theString;
}

function setCookie(cookieName,cookieValue) {
	// alert("Debug: " + cookieName + "=" + cookieValue);
	document.cookie = cookieName + "=" + escape(cookieValue) + "; ";
}

function getCookie(cookieName) {
	thisCookie = document.cookie.split("; ");
	for (i = 0; i <  thisCookie.length; i++) {
		if (cookieName == thisCookie[i].split("=")[0]) {
			return thisCookie[i].split("=")[1];
		}
	}
	return 0;
}

function getCookieDefault(cookieName,defaultValue) {
	thisCookie = document.cookie.split("; ");
	for (i = 0; i <  thisCookie.length; i++) {
		if (cookieName == thisCookie[i].split("=")[0]) {
			return thisCookie[i].split("=")[1];
		}
	}
	return defaultValue;
}

function killAllCookies()
{
	if (document.cookie != "") {
		thisCookie = document.cookie.split("; ")
		expireDate = new Date
		expireDate.setDate(expireDate.getDate()-1)
		                               
		for (i=0; i<thisCookie.length; i++) {
		    cookieName = thisCookie[i].split("=")[0]
		    document.cookie = cookieName + "=;expires=" + expireDate.toGMTString()
		}
	}
}

function killCookie(cookieName)
{
	expireDate = new Date
	expireDate.setDate(expireDate.getDate()-1)
	
	document.cookie = cookieName + "=;expires=" + expireDate.toGMTString()
}


function getSelectStr(selectField) {
// Returns value of a select field/dropdown
	var str = String(selectField.options[selectField.selectedIndex].value);
	return str;
}

function validEmail(email) {
	invalidChars = "/:,;";
	
	if (email == "") {
		return false;
	}

	for (i=0; i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false;
		}
	}
	
	// Check postion of @
	atPos = email.indexOf("@",1);
	if (atPos == -1) {
		return false;
	}
	if (email.indexOf("@",atPos + 1) > -1) {
		return false;
	}

	// Check postion of period
	periodPos = email.indexOf(".",atPos);
	if (periodPos == -1) {
		return false;
	}
	if (periodPos + 3 > email.length) {
		return false;
	}

	return true;
}

function percent(reached,fromBase)
{
	return reached / fromBase * 100;
}


// Image Handling
function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; 
	for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
    for (i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0) { 
			d.MM_p[j]=new Image; 
			d.MM_p[j++].src=a[i];
		}
    }
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  
	if(!d) d=document; 
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}
	if	(!(x=d[n])&&d.all) x=d.all[n]; 
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for (i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if (!x && d.getElementById) x=d.getElementById(n); 

	return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; 
	document.MM_sr=new Array; 
	for(i=0;i<(a.length-2);i+=3) 
	{
		if ((x=MM_findObj(a[i]))!=null)
		{
			document.MM_sr[j++]=x; 
			if(!x.oSrc) 
				x.oSrc=x.src; 
			x.src=a[i+2];
		}
	}
}

function MM_findObj(n, d) { //v3.0
	var p,i,x;  
	if(!d) d=document; 
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) { 
		x=d.all[n]; 
	}
	for (i=0;!x&&i<d.forms.length;i++) {
		x=d.forms[i][n];
	}
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) {
		x=MM_findObj(n,d.layers[i].document); 
	}

	return x;
}

function MM_showHideLayers() { //v3.0
	var i,p,v,obj,args=MM_showHideLayers.arguments;
	for (i=0; i < (args.length-2); i+=3) 
	{
		if ((obj=MM_findObj(args[i]))!=null) 
		{ 
			v=args[i+2];
			if (obj.style) 
			{ 
				obj=obj.style; 
				v = (v=='show')?'visible':(v='hide')?'hidden':v; 
			}
			obj.visibility=v; 
		}
	}
}


function randomElement(theArray)
{
	// returns random element from given array
	return theArray[Math.floor(Math.random() * theArray.length)];
}


function randomInt(lower,upper)
{
	return Math.ceil(Math.random() * (upper-lower) + lower);
}


function gibber(paragraphs)
{
	var theString = "";
	var theWord = "";
	var oldWord = "";
	var theSentence = "";
	var theParagraph = "";
	var pCount = 0;
	var wCount = 0;
	var sCount = 0;
	var pSize = 0;

	var words = new Array("lorem","ipsum","dolor","sic","fnord","belles","syrtis",
						"ceret","voci","ficat","cogitus","callime","loc","quat",
						"nil","bonum","est","nisi","go","charon","boron");

	for (pCount = 0; pCount < paragraphs; pCount++)
	{
		// Get the size of this paragraph
		pSize = randomInt(1,6);
		theParagraph = "";
		for (sCount = 0; sCount < pSize; sCount++)
		{
			// Get the size of this sentence
			sSize = randomInt(5,15);
			theSentence = "";
			for (wCount = 0; wCount < sSize; wCount++)
			{
				// Don't repeat words
				while (theWord = randomElement(words) == oldWord)
				{
					theWord = randomElement(words);
				}
				theSentence = theSentence + randomElement(words) + " ";
			}
			theParagraph = theParagraph + 
						theSentence.substring(0,1).toUpperCase() + 
						theSentence.substring(1,theSentence.length - 1) + 
						".  ";
		}
		theString = theString + "<p>" +  theParagraph + "</p>\r\r";
	}

	return theString;
}


function writeStatus(theText)
{
	window.status = theText;

	return true;
}

function shippingAddress()
{
	document.orderform.shippingAddress.value = document.orderform.address.value;
	document.orderform.shippingCity.value = document.orderform.city.value;
	document.orderform.shippingStateCode.value = document.orderform.stateCode.value;
	//document.orderform.shippingState.value = document.orderform.state.value;
	document.orderform.shippingZip.value = document.orderform.zip.value;
	document.orderform.shippingCountryCode.value = document.orderform.countryCode.value;
	//alert(document.orderform.countryCode.value);
	//return true;
}

function modifyTotal(subTotal,total,shipping)
{
	var temp;
	temp = subTotal + shipping;
	writeSpan(subTotalSpan,"$"+temp.toString());
	
	temp = total + shipping;
	writeSpan(totalSpan,"$"+temp.toString());
}

function writeSpan(spanObject,theString) {
// Write theString into the contents of a span with the ID of spanObject
	spanObject.innerHTML=theString;
}

function jsMail(recipient,domainName,extension, subject) {
	window.location = 'mai' + 'lto:' + recipient + '@' + domainName + '.' + extension + '?sub' + 'ject=' + subject;
}

function enableDisable(contactMethod){
	if(contactMethod == "phone"){
		document.form1.phone.disabled = false;
		document.form1.email.value = "";
	}else{
		document.form1.email.disabled = false;
		document.form1.phone.value = "";
	}
}

function newWindowSized(URL, strWidth, strHeight) {
	var newWindow = this.open(URL,"CoreMedia","width="+strWidth+", height="+strHeight+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,copyhistory=0,marginwidth=0,marginheight=0,rightmargin=0");
}