// JavaScript
// Basic functionality for the client end.

	
function ShowHide(strElement)
{
	obj = document.getElementById(strElement);
	imgObj = document.getElementById('img'+strElement);
	
	ClassPresent = obj.className.indexOf("Hide");	
	ClassName = obj.className;
	
	
	if(ClassPresent > -1)
	{
		ClassName = ClassName.replace(/Hide/i, "Show");
		imgObj.src="Images/e.gif";
	}
	else
	{
		ClassName = ClassName.replace(/Show/i, "Hide");
		imgObj.src="Images/c.gif";
	}

	obj.className = ClassName;
}


//This function displays a popUp window
function popUp(URL) 
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=1,statusbar=0,menubar=0,resizable=1,width=800,height=600,-20,20');");
}
		
	
	
//Functionality makes editing easy
function wrapText(el, openTag, closeTag) 
{
	if (document.selection && document.selection.createRange) 
	{
		var range = document.selection.createRange();
		
		if (trimText(range.text) != "" && trimText(range.text).length > 0) 
		{
			range.text = openTag + srange.text + closeTag;
			range.moveEnd('character', el.selectionEnd);
			range.moveStart('character', el.selectionStart);
			range.text.select();
		}
	}
}

//This function selects ranges of text
function setSelectionRange(textElem, selectionStart, selectionEnd) 
{
	if (textElem.setSelectionRange)
	{ // FF
		window.setTimeout(function(x,posL,posR){ // bug 265159
		return function(){x.setSelectionRange(posL,posR);};}
		(textElem,selectionStart,selectionEnd),100);
	} 
	else if (textElem.createTextRange) 
	{ // IE
		var range = textElem.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	}
}


function trimText(str)
{
	if (str != null && str != "")
		return str.replace(/^\s*|\s*$/g,"");
}


//Return MSIE Version number or 30 if not MSIE
function msieversion()
{
	var ua = window.navigator.userAgent
	var msie = ua.indexOf ( "MSIE " )
 
	if ( msie > 0 ) // If Internet Explorer, return version number
		return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )));
	
	// If another browser, return an outrageous verison number
	else 
		return 30
}
   
function IsiPad()
{
	var ua = window.navigator.userAgent
	var uaTest = ua.indexOf ( "iPad" )
 
	if ( uaTest > 0 )
		return true;
	else
		return false;
}

	
	
function invokeDemo()
{
	window.location = "#site";
	w = 600;
	h = 625;
	displayInputWindow('dimmerForm', w, h);

}

function errorCheckDemo() 
{


}

	
function followUs(strType)
{
	window.location = "#site";
//	AjaxBasic("Scripts/FollowUs.asp?Action=Follow&Type=" + strType, "followUsDiv");
	AjaxGET("Scripts/FollowUs.asp?Action=Follow&Type=" + strType, "", "followUsDiv", false, false)
	w = 500;
	h = 500;
	displayInputWindow("followUsDimmerDiv", w, h);
}

var originalDivHTML;	

//This needs to be made to be more consistent with dimmer functions.
function displayInputWindow(windowName, w, h)
{
	var l, t;

	l = (getWindowWidth() - w) / 2;
	t = (getWindowHeight() - h) / 4;
	displayFloatingDiv(windowName, '', w, h, l, t);			
}


//Actual function that places the dimmer and dimming message on the page.
function displayFloatingDiv(divId, title, width, height, left, top) 
{
	var topPadd;
	var DivID = divId;
	var IEver = msieversion();
	var dimmit = false;
	
/* All gray overlay stuff */
	if(null != document.getElementById('dimmer') || undefined != document.getElementById('dimmer'))
	{
		theDimmer = document.getElementById('dimmer');
		dimmit = true;
	}	
	else if(null != document.getElementById('followUsDimmer') || undefined != document.getElementById('followUsDimmer'))
	{
		theDimmer = document.getElementById('followUsDimmer');
		dimmit = true;
	}
		
	/* make sure we are actually changing a element and not in error */
	if(dimmit != false)
	{
		theDimmer.className = 'dimmerGo';
		if (IEver < 7)
		{
			theDimmer.style.position = 'absolute';
			theDimmer.style.height = '15000px';
			theDimmer.style.width = '1600px';
		}
		else
		{
			if(IsiPad() == true)
			{

			}
			else
			{ 
				theDimmer.style.position = 'fixed';
				theDimmer.style.height = (getWindowHeight() + 20 );
				theDimmer.style.width = (getWindowWidth());
			}
		}

	}

	/* All white window stuff */
	if (null != document.getElementById(divId) && undefined != document.getElementById(divId) && divId != "None")
	{
		theDivContent = document.getElementById(divId);
	
		//Height Width
		theDivContent.style.width  = width + 'px';
		theDivContent.style.height = height + 'px';
		
		//Where at on the Page
		theDivContent.style.left = left + 'px';
									
		if (top <= 600)
		{
			topPadd = ((getWindowHeight() - height) / 2 ) - 2;									
			theDivContent.style.top = Math.round(topPadd) + 'px';
		}
		else
		{
			OtherToppage = 65;
			if(getWindowHeight() <= 600)
				OtherToppage = 0;	
			
		   theDivContent.style.top = (top + OtherToppage) + 'px';	
		}

		originalDivHTML = theDivContent.innerHTML;
		theDivContent.innerHTML = originalDivHTML;
		theDivContent.className = 'dimming';
	}
}



//Hides called dimmers.
function hideFloatingDiv(divId)
{
	if (null != document.getElementById(divId) && undefined != document.getElementById(divId))
	{
		/* white window part */	
		whiteWindow = document.getElementById(divId);

		if (null != document.getElementById('dimmer') || undefined != document.getElementById('dimmer') && document.getElementById('dimmer').className == "dimmerGo") 
			grayOverlay = document.getElementById('dimmer');
			
		else if (null != document.getElementById('followUsDimmer') || undefined != document.getElementById('followUsDimmer') && document.getElementById('followUsDimmer').className == "dimmerGo") 
			grayOverlay = document.getElementById('followUsDimmer');
				
		whiteWindow.className = 'windowContent';
		grayOverlay.className = "dimmer";				
		
		/*Unhides Select boxes in IE 6*/
		if (msieversion() < 7)
		{
			for(i=0; (selectBox = document.getElementsByTagName("select")[i]); i++) 
				selectBox.style.visibility = "visible";
				
			window.onscroll="";
		}	
	}
}		

//Window Viewable Width
function getWindowWidth()
{
	var winW;

	if (self.innerWidth)
		winW = self.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth)
		winW = document.documentElement.clientWidth;
	else if (document.body)
		winW = document.body.clientWidth;
		
	
	return winW;
}

//Window Viewable Height
function getWindowHeight()
{
	var winH;

	if (self.innerHeight)
		winH = self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		winH = document.documentElement.clientHeight;
	else if (document.body)
		winH = document.body.clientHeight;
	
	return winH;
}

//Window Scrollable Width
function getLeftScroll()
{
	var winL;

	if (self.scrollLeft)
		winL = self.scrollLeft;
	else if (document.documentElement && document.documentElement.scrollLeft)
		winL = document.documentElement.scrollLeft;
	else if (document.body)
		winL = document.body.scrollLeft;
	
	return winL;
}

//Window Scrollable Height
function getTopScroll()
{
	var winT;

	if (self.scrollTop)
		winT = self.scrollTop;
	else if (document.documentElement && document.documentElement.scrollTop)
		winT = document.documentElement.scrollTop;
	else if (document.body)
		winT = document.body.scrollTop;
	return winT;
}



function demoCheckErrors()
{
	var daFrom = document.getElementById("myForm");
	var msg = "";
	
	if(null != daFrom)
	{
		msg += checkFormText("Name", "Name");
		msg += checkFormText("Title", "Title");
		msg += checkFormText("Company", "Organization");
		msg += checkFormText("Address", "Address");
		msg += checkFormText("City", "City");
		msg += checkFormText("State", "State");
		msg += checkFormText("Zip", "Zip Code");
		msg += checkFormText("Phone", "Primary Phone");
		msg += checkFormText("Email", "Email Address");
		msg += checkFormText("Comments", "Comments");
		//msg += checkFormText("strCAPTCHA", "Security Code");
	}
	else
		msg = "An unexpected error occured, please email support@e-pulse.net to notify us of the issue.";	
	
	if(msg != "")
	{
		alert("Errors were detected on the form please correct and resubmit.\n\r\n\r" + msg);
		return false;
	}
	else
		return true;	
	
}
	
function checkFormText(strElementID, strName)
{
	var el = document.getElementById(strElementID);
	if(null != el)
	{
		if(undefined != el.value)
		{
			switch(el.type)
			{
				case "text":
					if(strElementID == "strCAPTCHA")
					{
						
						//	AjaxBasic("../Scripts/GetCap.asp", "hidder");
						AjaxPOST("../Scripts/GetCap.asp", "", "hidder", false, false);
						var capVal =  document.getElementById("txtHide");
						
						if("" != el.value)
						{
							if(el.value == capVal.value)
								return "";
							else
								return " - " + strName + " is incorrect.\n\r";								
						}
						else
							return " - " + strName + " is a required field.\n\r";	
					}
					else
					{				
						if("" != el.value)
							return "";
	
						else
							return " - " + strName + " is a required field.\n\r";
					}
				break;
				case "textarea":
					if("" != el.value)
						return "";

					else
						return " - " + strName + " is a required field.\n\r";
				break;	
				
				case "select-one":
						
					if("" != el.selectedIndex > 0)
						return "";
					
					else
						return " - " + strName + " is a required field.\n\r";
				break;
				
				case "radio":
					if(!el)
						return "";
					var elLength = el.length;
					if(elLength == undefined)
					{
						if(!el.checked)
							return " - " + strName + " is a required field.\n\r";
						else
							return "";
					}
					else
						return "";

				break;
				
			}
		}
		else
			return " - " + strName + " is a required field.\n\r";
	}
	else
		return " - " + strName + " was not present on the form. Please contact us by email at support@e-pulse.net.\n\r	";
}




