//Copyright 2011 Virtual Interconnect Software, LLC.  All rights reserved.

var publicURL = "http://www.data.virtualinterconnect.com/";
var secureURL = "https://www.virtualinterconnect.com//common/";

function MoveLoginBox()
{
	//move the menu banner to the top of the screen
	var obj = document.getElementById("LoginBoxBackground");
	//get the current top value
	var scrollTop = ((document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop);
	obj.style.top = scrollTop + "px";
	window.onscroll = MoveLoginBox;
	//setTimeout("MoveLoginBox()", 1000);
}

function DisableFlash()
{
	var objects = document.getElementsByTagName("object");
	
	for(var i=0; i<objects.length; ++i)
	{
		objects[i].style.visibility = "hidden";
	}
	
	//also hide any select/option tags (for IE)
	objects = document.getElementsByTagName("select");
	
	for(var i=0; i<objects.length; ++i)
	{
		objects[i].style.visibility = "hidden";
	}
}

function EnableFlash()
{
	var objects = document.getElementsByTagName("object");
	
	for(var i=0; i<objects.length; ++i)
	{
		objects[i].style.visibility = "visible";
	}

	//also hide any select/option tags (for IE)
	objects = document.getElementsByTagName("select");
	
	for(var i=0; i<objects.length; ++i)
	{
		objects[i].style.visibility = "visible";
	}
}

function GalleryActivate()
{
	var allLists = document.getElementsByTagName("div");
	var galleryDivs;
	var expandedDivs;
	
	for(var i=0; i<allLists.length; ++i)
	{
		if(allLists[i].className == "Gallery")
		{
			galleryDivs = allLists[i].getElementsByTagName("div");
			for(var j=0; j<galleryDivs.length; ++j)
			{
				if(galleryDivs[j].className == "GalleryExpanded")
				{
					galleryDivs[j].style.display = "block";
					expandedDivs = galleryDivs[j].getElementsByTagName("div");
					for(var k=0; k<expandedDivs.length; ++k)
					{
						switch(expandedDivs[k].className)
						{
							case "GalleryLargePictureTitle":
							{
								expandedDivs[k].innerHTML = "Picture (large view)";
							} break;
							case "GalleryDescriptionTitle":
							{
								expandedDivs[k].innerHTML = "Description";
							} break;
							case "GalleryLargePicture":
							{
								expandedDivs[k].innerHTML = "No picture selected";
							} break;
							case "GalleryDescription":
							{
								if(expandedDivs[k].innerHTML == "")
								{
									expandedDivs[k].innerHTML = "Please click an image below to see an expanded view to the left";
								}
							} break;
						}
					}
				}
			}
		}
	}
}

function GalleryExpand(thumbEl, url, description)
{
	var parentEl;
	var galleryDivs;
	var inputEls;
	var pricingInfo = new Array();
	
	description = decodeURIComponent(description);
	//expand the given url into the upper pane
	//look through the hierarchy above this element and look for a div with a className of "Gallery"
	parentEl = thumbEl;
	do
	{
		parentEl = parentEl.parentNode;
	} while(parentEl && parentEl.className != "Gallery");
	if(parentEl && parentEl.className)
	{
		galleryDivs = parentEl.getElementsByTagName("div");
		for(var i=0; i<galleryDivs.length; ++i)
		{
			if(galleryDivs[i].className)
			{
				switch(galleryDivs[i].className)
				{
					case "GalleryLargePicture":
					{
						if(url.substring(url.length-4, url.length).toLowerCase() == ".swf")
						{
							ActivateSWFInto(url, "100%", "100%", galleryDivs[i], "showAll");
						}
						else if(url.substring(url.length-4, url.length).toLowerCase() == ".flv")
						{
							ActivateSWFInto("/content/watchvideo.php?VF=" + encodeURIComponent(url), "100%", "100%", galleryDivs[i], "showAll");
						}
						else
						{
							galleryDivs[i].innerHTML = "<img src=\"" + url + "\" alt=\"Large picture\" />";
						}
					} break;
					case "GalleryDescription":
					{
						galleryDivs[i].innerHTML = "<p>" + description + "</p>";
					} break;
				}
			}
		}
	}
}

function GalleryChangeCartButton(selectNum)
{
	var selectEl = document.getElementById("Gallery_" + selectNum + "_Selector");
	var divEls = selectEl.parentNode.getElementsByTagName("div");
	
	for(var i=0; i<divEls.length; ++i)
	{
		if(divEls[i].className == "GalleryCartButton")
		{
			divEls[i].innerHTML = selectEl.options[selectEl.selectedIndex].value;
		}
	}
}

function LoginDisplay(loginPage, siteName, forgotPasswordKey, demoSite, resellerName)
{
	var loginBox = document.getElementById("LoginBoxBackground");
	if(loginBox)
	{
		loginBox.parentNode.removeChild(loginBox);
	}
	var loginDiv = document.createElement("div");
	var newHTML = "";
	
	newHTML = "<form id=\"LoginBoxForm\" action=\"" + loginPage + "\" method=\"post\">";
	newHTML += "<div class=\"LoginBox\">";
	newHTML += "<h1>Log in</h1>";
	newHTML += "<a class=\"LoginClose\" href=\"javascript: void(0);\" onclick=\"LoginClose(); return false;\"><img src=\"" + publicURL + "images/login/20x20dkx.gif\" alt=\"Close\" /></a>";
	if(demoSite)
	{
		newHTML += "<div class=\"LoginDemoExplaination\">As a demo user, your username and password are both &quot;sample&quot;</div>";
	}
	newHTML += "<div class=\"LoginInfo\">";
	newHTML += "<input type=\"hidden\" name=\"Site\" value=\"" + siteName + "\" />";
	if(resellerName != "")
	{
		newHTML += "<input type=\"hidden\" name=\"SiteType\" value=\"" + resellerName + "\" />";
	}
	newHTML += "<label id=\"LoginUsernameLabel\" for=\"LoginUsername\">username:</label>";
	newHTML += "<input type=\"text\" name=\"Username\" id=\"LoginUsername\" size=\"30\" onkeypress=\"LoginSubmitIfEnter(event); return true;\"" + (demoSite ? " value=\"sample\"" : "") + " /><br />";
	newHTML += "<label id=\"LoginPasswordLabel\" for=\"LoginPassword\">password:</label>";
	newHTML += "<input type=\"password\" name=\"Password\" id=\"LoginPassword\" size=\"30\" onkeypress=\"LoginSubmitIfEnter(event); return true;\"" + (demoSite ? " value=\"sample\"" : "") + " /><br />";
	//newHTML += "<div id=\"LoginRememberMeContainer\"><label id=\"LoginRememberMeLabel\" for=\"LoginRememberMe\">remember me <a href=\"" + publicURL + "help/rememberme.php\" onclick=\"window.open('" + publicURL + "help/rememberme.php', '_blank', 'width=320, height=320, scrollbars=yes'); return false;\">(?)</a></label><input type=\"checkbox\" name=\"LoginRememberMe\" id=\"LoginRememberMe\" /></div><br /><br />";
	newHTML += "<div id=\"LoginForgotPassword\"><a href=\"" + secureURL + "forgotpassword.php?U=" + forgotPasswordKey + "\" onclick=\"window.open('" + secureURL + "forgotpassword.php?U=" + forgotPasswordKey + "'); return false;\">I forgot my password</a></div>";
	newHTML += "<a id=\"LoginCancel\" href=\"javascript: void(0);\" onclick=\"LoginClose(); return false;\"><img src=\"" + publicURL + "images/login/dkcancel.jpg\" alt=\"Cancel\" /></a>";
	newHTML += "<input type=\"image\" id=\"LoginSubmit\" src=\"" + publicURL + "images/login/dklogin.jpg\" alt=\"Log in\" onmouseover=\"this.src='" + publicURL + "images/login/ltlogin.jpg';\" onmouseout=\"this.src='" + publicURL + "images/login/dklogin.jpg';\" /></div>";
	newHTML += "</div>";
	newHTML += "</div>";
	newHTML += "</form>";
	
	loginDiv.className = "Overlay LoginBoxBackground";
	loginDiv.id = "LoginBoxBackground";
	loginDiv.innerHTML = newHTML;
	
	DisableFlash();
	loginDiv = document.body.insertBefore(loginDiv, null);
	MoveLoginBox();
	document.getElementById("LoginUsername").focus();
}

function LoginClose(loginPage)
{
	var loginDiv = document.getElementById("LoginBoxBackground");
	
	document.body.removeChild(loginDiv);
	EnableFlash();
}

function LoginSubmitIfEnter(e)
{
	if(!e) e = window.event;
	
	if(e && e.keyCode == 13)
	{
		document.getElementById("LoginBoxForm").submit();
	}
}

function ViewLockAspect(elID, aspect, lockToWidth)
{
	var el = document.getElementById(elID);
	
	if(el)
	{
		if(lockToWidth)
		{
			el.style.height = ((el.clientWidth ? el.clientWidth : el.offsetWidth) / aspect) + "px";
		}
		else
		{
			el.style.width = ((el.clientHeight ? el.clientHeight : el.offsetHeight) * aspect) + "px";
		}
		setTimeout("ViewLockAspect('" + elID + "', " + aspect + ", " + (lockToWidth ? "true" : "false") + ");", 100);
	}
}

function ProductCheckForm(formID)
{
	var formEl = document.getElementById("AddToCartForm" + formID);
	var allChildEls = formEl.getElementsByTagName("*");
	
	//check a product form to make sure all required fields have a value
	for(var i=0; i<allChildEls.length; ++i)
	{
		if(allChildEls[i].className && allChildEls[i].className == "FormRequired")
		{
			switch(allChildEls[i].tagName.toLowerCase())
			{
				case "select":
				{
					if(allChildEls[i].options[allChildEls[i].selectedIndex].value == "")
					{
						alert("Please pick an option for the " + allChildEls[i].options[0].textContent.substring(3, allChildEls[i].options[0].textContent.length-3) + " field");
						return false;
					}
				} break;
			}
		}
	}
	return true;
}

var pageFullscreenGraphicResizeInterval = -1;

function PageFullscreenGraphicExpand()
{
	var el = document.getElementById("PageFullscreenGraphic");
	el.style.width = el.clientWidth + "px";
	el.style.height = el.clientHeight + "px";
	var overlayEl = document.body.appendChild(document.createElement("div"));
	overlayEl.setAttribute("id", "PageFullscreenGraphicExpanded");
	overlayEl.className = "Overlay";
	var xEl = overlayEl.appendChild(document.createElement("a"));
	xEl.href = "javascript:void(0);";
	xEl.onclick = PageFullscreenGraphicCollapse;
	xEl.style.display = "block";
	xEl.style.position = "absolute";
	xEl.style.top = "2%";
	xEl.style.right = "2%";
	xEl.style.fontSize = (overlayEl.clientHeight * 0.03) + "px";
	xEl.style.color = "#ccc";
	xEl.appendChild(document.createTextNode("close"));
	
	//record the aspect ratio for later resizing
	var containerEl = document.getElementById("PageFullscreenGraphicContainer");
	if(!containerEl.aspectRatio)
	{
		containerEl.aspectRatio = containerEl.clientWidth / containerEl.clientHeight;
	}
	
	//hide the click target
	var clickTargetEl = document.getElementById("PageFullscreenGraphicClickTarget");
	clickTargetEl.style.display = "none";
	
	var graphicEl = overlayEl.appendChild(el.removeChild(el.firstChild));
	graphicEl.style.position = "absolute";
	graphicEl.style.left = "2%";
	graphicEl.style.top = "7%";
	graphicEl.style.width = "96%";
	graphicEl.style.height = "91%";
	
	PageFullscreenGraphicResize();
	resizeInterval = setInterval(PageFullscreenGraphicResize, 40);
}

function PageFullscreenGraphicCollapse()
{
	clearInterval(resizeInterval);
	var resizeContainer = document.getElementById("PageFullscreenGraphicContainer");
	resizeContainer.style.width = "100%";
	resizeContainer.style.height = "auto";
	
	var overlayEl = document.getElementById("PageFullscreenGraphicExpanded");
	var el = document.getElementById("PageFullscreenGraphic");
	el.style.width = "auto";
	el.style.height = "auto";
	var graphicEl = el.insertBefore(overlayEl.removeChild(overlayEl.firstChild.nextSibling), el.firstChild);
	overlayEl.parentNode.removeChild(overlayEl);
	graphicEl.style.position = "relative";
	graphicEl.style.left = "0";
	graphicEl.style.top = "0";
	graphicEl.style.width = "480px";
	graphicEl.style.height = "auto";
	
	//show the click target
	var clickTargetEl = document.getElementById("PageFullscreenGraphicClickTarget");
	clickTargetEl.style.display = "block";
}

function PageFullscreenGraphicResize()
{
	//try to make the graphic fit within the parent
	var containerEl = document.getElementById("PageFullscreenGraphicContainer");
	var parentAspect = containerEl.parentNode.clientWidth / containerEl.parentNode.clientHeight;
	var idealWidth = (containerEl.aspectRatio > parentAspect ? containerEl.parentNode.clientWidth : containerEl.aspectRatio * containerEl.parentNode.clientHeight);
	var idealHeight = (containerEl.aspectRatio > parentAspect ? containerEl.parentNode.clientWidth / containerEl.aspectRatio : containerEl.parentNode.clientHeight);
	if(Math.abs(containerEl.clientWidth - idealWidth) >= 1 || Math.abs(containerEl.clientHeight - idealHeight) >= 1)
	{
		containerEl.style.width = idealWidth + "px";
		containerEl.style.height = idealHeight + "px";
	}
	var overlayEl = document.getElementById("PageFullscreenGraphicExpanded");
	overlayEl.firstChild.style.fontSize = (overlayEl.clientHeight * 0.03) + "px";
}

function GlobalBlogValidateComment(fn)
{
	var prefix = "Blog" + (fn ? "_" + fn + "_" : "");
	var nameEl = document.getElementById(prefix + "CommentName");
	var emailEl = document.getElementById(prefix + "CommentEmail");
	var commentEl = document.getElementById(prefix + "CommentComment");
	var captchaEl = document.getElementById(prefix + "CommentCaptchaAnswer");
	if(nameEl.value == "")
	{
		alert("Please enter your name.");
		nameEl.focus();
		return false;
	}
	if(emailEl.value == "" || emailEl.value.indexOf("@") < 1 || emailEl.value.indexOf("@") >= emailEl.value.length - 1)
	{
		alert("The e-mail address entered appears to be invalid.");
		emailEl.focus();
		return false;
	}
	if(commentEl.value == "")
	{
		alert("Please enter a comment to post.");
		commentEl.focus();
		return false;
	}
	if(captchaEl && captchaEl.value == "")
	{
		alert("Please re-enter the letters and numbers at the bottom");
		captchaEl.focus();
		return false;
	}
	return true;
}

