//08/31/04 Common javascript for opening windows:
var myForm="";
var size="";
var ar=0;
//alert("daint.js");
function fnGetWindowSize(width, height, location)
{
	//width, height of window, location: C (center), TL: top-left
	var scrWidth=screen.availWidth;
	var scrHeight= screen.AvailHeight;
	ar=scrWidth/800;
	//alert("ar = " + ar);
	var vFudge=20;
	var myWidth=(scrWidth*width/100)/ar;
	var myHeight=(scrHeight*height/100)/ar;
	var myWidthMove=0;
	var myHeightMove=0;
	if (location=="TL")
	{
		//Left top (0,0):
	}
		
	if (location=="C")
	{
		//Center of screen;
		var myWidthMove=(scrWidth-myWidth)/2;
		var myHeightMove=(scrHeight-myHeight)/2;
	}
	//alert("myWidth = " + myWidth + ", myHeight = " + myHeight + "\nmyWidthMove = " + myWidthMove + ", myHeightMove = " + myHeightMove);
	return {left:myWidthMove, top:myHeightMove, width:myWidth, height:myHeight};
}


function fnOpenWindow(val)
{

	//val: emailsignup, store, showlarge - for now:
	//alert("fnOpenWindow val = " + val);
	size="";
	var attribs="";
	var pageName="";
	switch (val)
	{
		case "emailsignup":
		{
			var vFudge=20;
			size=fnGetWindowSize(50,82,"C");
			size.top-=(vFudge*ar)
			pageName="emailsignup.htm";
			break;
		}
		
		/* Not called from here
		case "emailsignupconfirm":
		{
			size=fnGetWindowSize(70,70,"C");
			pageName="emailsignup.htm";
			break;
		}
		*/

		case "store":
		{
			//alert("The Web Store is Under Construction");
			//return;
			size=fnGetWindowSize(80,87.5,"C");
			pageName="store.htm";
			break;
		}
		
		case "showlarge":
		{
			document.getElementById("ImageName").value=val;
			size=fnGetWindowSize(60,70,"C");
			pageName="showlarge.htm";
			break;
		}

	}
	
	attribs="titlebar=no, resize=no, status=no, scrollbars=no, left = " + size.left + ", top= " + size.top + ", width= " + size.width + ", height= " + size.height;
	attribs="'" + attribs + "'";
	//alert(attribs + "\n" + pageName);
	window.open(pageName,"",attribs);
	//alert("End fnOpenWindow");
}


function fnOpenFirstStepsWeb()
{
	//Special: opens pdf:
	//Not used, using target=_blank 
	var attribs="left=0px, top=0px, width=" + screen.availWidth + "px, height= " + screen.availHeight + "px titlebar=yes, resize=yes, status=yes, scrollbars=yes";
	window.open("http://www.daint.org/Library/WatchOutForBurnOut.pdf","",attribs);
}

function fnOpenAdobe()
{
	//Not used, using target=_blank 
	var attribs="left=0px, top=0px, width=" + screen.availWidth + "px, height= " + screen.availHeight + "px titlebar=yes, resize=yes, status=yes, scrollbars=yes";
	window.open("http://www.adobe.com/products/acrobat/readstep.html","",attribs);
}
/////////////////////////////////////////////////////////////////////////////
//09/01/04 Moved ALL javascript into here: 
function fnOnLoad()
	{
		//alert("fnOnLoad)");
		//Common fnOnLoad():

		myForm=document.Form1;
		myResize=false;
		//alert("fnOnLoad location.href = " + location.href);
	
		if ((location.href.indexOf("org/emailsignup.",0)>-1) || (location.href.indexOf("daint/emailsignup.",0)>-1))
		{
			//alert("signup");
			//size=fnGetWindowSize(50,90,"C");
			//alert("fnOnload() 111\n" + size.left + "\n" + size.top + "\n" + size.width + "\n" + size.height);

			myForm.Date.value=fnGetDate("DT");
			document.getElementById("divDate").innerHTML=fnGetDate("L");
			//alert(fnGetDate("DT") + "\n" + fnGetDate("L"));
			//fnWriteMsg("&nbsp;",1);	//To keep vertical spacing constant
		}
		

		if (location.href.indexOf("shtml.dll/emailsignup.",0)>-1)
		{
			//alert("signupconfirm");
			size=fnGetWindowSize(55,75,"C");		//Was height:66
			myResize=true;
		}

		//For test:
		if (location.href.indexOf("emailsignupconfirm.",0)>-1)
		{
			//alert("signupconfirm");
			size=fnGetWindowSize(55,75,"C");	// Was height:65
			myResize=true;
		}
		
		if (location.href.indexOf("store.",0)>-1)
		{
			//size=fnGetWindowSize(80,87.5,"C");
		}
		
		if (location.href.indexOf("showlarge.",0)>-1)
		{
			//size=fnGetWindowSize(60,70,"C");
			/////////////////////////////////////////////////////////////////
			var imageName=window.opener.document.getElementById("ImageName").value;
			document.getElementById("divImage").innerHTML="<img src='http://www.thewellnessinstitute.com/products/images/" + imageName + "' border=0  height=" + (325*1) + "px>";
			/////////////////////////////////////////////////////////////////
		}
		
		if (myResize==true)
		{
			//alert("fnOnload() Resize\n" + size.left + "\n" + size.top + "\n" + size.width + "\n" + size.height);
			try
			{
				window.moveTo(size.left, size.top);
				window.resizeTo(size.width, size.height);
			}
			catch(e) {}
		}

	//alert("End fnOnload()");
	
	}
	//////////////////////////////////////////////////////////////////////
	////////////// Email sign Up Validation ////////////////////////////
	//////////////////////////////////////////////////////////////////////
	
	function fnCheckForCommas(val)
	{
		//Eliminate commas or double quotes in text:
		var temp=document.getElementById(val).value;
		if ((temp.indexOf(",",0)>-1) | (temp.indexOf("\"",0)>-1))
		{
			alert("No commas (,) or double quotes (\") in " + val + " please!");
			//document.getElementById(val).focus(); //These don't work?!
			//document.getElementById(val).select();
			return false;
		} 
		return true;
	}
	
	function fnGetDate(val)
	{
		//Today's date:
		//val: L: January, 10 2004, S: 01/10/04, DT: 01/01/04 03:34:45 - for now:
		var weekDays=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
		var months=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
		
		var today=new Date();
		//alert(today);
		var year=today.getYear();
		var month=today.getMonth();
		var monthName=months[month];
		//alert("month = " + month);
		var date=today.getDate();
		var day=today.getDay();
		var hours=today.getHours();
		var minutes=today.getMinutes();
		var seconds=today.getSeconds();
		//alert("day = " + day);
		var weekDay=weekDays[day];
		if (month<10) {month="0"+month;}
		if (date<10) {date="0"+date;}
		if (hours<10) {hours="0"+hours;}
		if (minutes<10) {minutes="0"+minutes;}
		if (seconds<10) {seconds="0"+seconds;}
		switch (val)
		{
			case "L":
			{
				//Long i.e. January, 10 2004:
				sDate=weekDay + ", " + monthName + " " + date + " " + year;
				break;
			}
			case "S":
			{
				//Short i.e. 01/01/04:
				year=year.toString().substr(2);
				sDate=month + "/" + date + "/" + year;
				break;
			}
			case "DT":
			{
				//Short i.e. 01/01/04:
				year=year.toString().substr(2);
				sDate=month + "/" + date + "/" + year + " " + hours + ":" + minutes + ":" + seconds;
				break;
			}		
		}
		//alert(sDate);
		return sDate;
	}
	
	
	function fnReset()
	{
		fnWriteMsg("&nbsp;",1);
		myForm.Name.value="";
		myForm.Email.value="";
		myForm.Email2.value="";
		//myForm.City.value="";
		//myForm.State.selectedIndex=0;
		myForm.Zip.value="";
	}
	
	/*
	function fnValCityState(val)
	{
		//alert("fnValCityState val = " + val);
		fnWriteMsg("&nbsp;",1);
		if (val=="City")
		{
			if (fnCheckForCommas("City")!=true) {return false;}
		}
		else
		{
			if (fnCheckForCommas("State")!=true) {return false;}

			if (fnValZip()!=true) 
			{
				myForm.Zip.focus();
				return false;
			}
		}
		return true;
	}
	*/
	
	function fnValEmail(val)
	{
		//val: 0 - called from HTML, 1 - Called from fnValForm()
		var email=myForm.Email.value;
		var email2=myForm.Email2.value;
		var posAt=-1;
		var posDot=-1;
		fnWriteMsg("&nbsp;",1);
		if (val<3)
		{
			//////////////////////////////////////////////
			//Decide to always allow it because the email could be valid: 
			if (val==1)
			{
				if (email!="")
				{
					if (fnCheckForCommas("Email") !=true) {return false;}
					
					posAt=email.indexOf("@",0);
					posDot=email.lastIndexOf(".",email.length);
					//alert("posAt = " + posAt);
					//alert("posDot = " + posDot);
					if ((posAt==-1) | (posDot==-1) | (posDot<posAt))
					{
						alert("Email does not appear to be a valid email address");
					}
				}
			}
			
			if (val==2)
			{
				if (email2!="")
				{
					if (fnCheckForCommas("Email2") !=true) {return false;}
					
					posAt=email2.indexOf("@",0);
					posDot=email2.lastIndexOf(".",email2.length);
					//alert("posAt = " + posAt);
					//alert("posDot = " + posDot);
					if ((posAt==-1) | (posDot==-1) | (posDot<posAt))
					{
						alert("Confirm Email does not appear to be a valid email address");
					}
				}
			}
		}
		
		/////////////////////////////////////////////
		if ((email!="") & (email2!=""))
		{
			if (fnCheckForCommas("Email")!=true) {return false;}
			if (fnCheckForCommas("Email2")!=true) {return false;}

			if (email!=email2)
			{
				fnWriteMsg("Email and Confirm Email do not agree!",0);
				if (email=="") {myForm.Email.focus();}
				return false;
			}
			else
			{
				fnWriteMsg("&nbsp;",1);
				return true;
			}
		}
		else
		{
			if (val==3)
			{
				if (email=="")
				{
					fnWriteMsg("Email required please",0);
					myForm.Email.focus();
					return false;
				}
				
				if (email2=="")
				{
					fnWriteMsg("Email confirmation required please",0);
					myForm.Email2.focus();
					return false;
				}
				
			}
		}
	}
	
	function fnValForm()
	{
		//alert("fnValform");
		fnWriteMsg("&nbsp;",1);
		//alert(myForm.Name.value);
		if (myForm.Name.value=="") 
		{
			fnWriteMsg("Name required please",0);
			myForm.Name.focus();
			return false;
		}
		else
		{
			if (fnCheckForCommas("Name")!=true)
			return false;
		}
		
		if (fnValEmail(3)!=true) 	
		{
			return false;
		} 
		
		/*
		if ((myForm.City.value!="") & (myForm.State.selectedIndex==0))
		{
			if (fnCheckForCommas("City")!=true) {return false;}
			fnWriteMsg("State required with City",0);
			return false;
		}
		if ((myForm.City.value=="") & (myForm.State.selectedIndex>0))
		{
			fnWriteMsg("City required with State",0);
			return false;
		}
		*/
		if (fnCheckForCommas("Zip")!=true) {return false;}
		fnWriteMsg("Form completed successfully",1);
		
		/////////////////////////////////////
		//For demo/testing:
		//{
		//	var attribs="titlebar=no, resize=no, status=no, scrollbars=no";
		//	window.open("signupconfirm.htm","",attribs);
		//}
		/////////////////////////////////////////
		return true;

		//return false;	//For Test
	}
	

	function fnValName()
	{
		fnWriteMsg("&nbsp;",1);
		if (fnCheckForCommas("Name")!=true) 
		{
			myForm.Name.focus();
			return false;
		}
	}
	
	
	function fnValZip()
	{
		//alert("fnValZip");
		var temp=myForm.Zip.value;
		var temp2="";
		if (temp=="") {return true;}
		
		if (fnCheckForCommas("Zip")!=true) {return false;}
		
		var index=myForm.State.selectedIndex;
		if (index==0) {return true;}
		
		var Char="";
		var CharCode=-1;
		
		if (myForm.State.options[index].value.length<xx3)
		{
			//US state, numbers and - only:
			//////////////////////////////////////
			
			//temp=temp.replace(/ /g,""), temp2="";   //Remove white spaces: (RegExp)
			
			//////////////////////////////////////
			//Check for digits and remove other than:
			for (i=0;i<temp.length;i++)
			{
				Char=temp.charAt(i);
				CharCode=temp.charCodeAt(i);				
				//alert("Char, Code = " + Char + ", " + CharCode);
				{
					if  ((CharCode<48) || (CharCode>57))
					{
						if ((Char!="-") && (Char!=" "))
						{
							alert("US Zip codes contain digits and - only");
							return false;
						}
					}
					else
					{
						temp2+=Char;
					}
				}
			}
			//////////////////////////////////////
			//alert("length = " + temp2.length);
			if ((temp2.length!=5) & (temp2.length!=9))
			{
				alert("US zip codes have either 5 or 9 digits");
				return false;
			}
			//Normalize:
			if (temp2.length==5){myForm.Zip.value=temp2;}
			if (temp2.length==9) {myForm.Zip.value=temp2.substr(0,5) + "-" + temp2.substr(5);}
			//////////////////////////////////////
		}
		return true;
	}
	
	
	function fnWriteMsg(val,val2)
	{
		//val: text, val2: color:
		//myForm.Msg.value=val;
		//myForm.Msg.style.color=(val2==1)?"black":"red";
		if (val=="") {val="&nbsp;";}
		var sColor=(val2==1)?"black":"red";
		sMsg="<font face='arial' size='2' color = '" + sColor + "'>" + val + "</font>";
		//alert("sMsg = " + sMsg);
		document.getElementById("divMsg").innerHTML=sMsg;
	}

