
var popupNotReg = "../Inserts/notreg.html";
var popupUpdate = "../Inserts/update.html";
var popupNoCookies = "../Inserts/nocookies.html";

var popupWidth= 200;
var popupHeight= 200;
var stature 	=	popupWindowSize();

var howLong=		1;//This is used to arrange the time out function waiting() in howLong seconds
var base = new Date(0);
dateAdjustment = base.getTime();

var cookieString=	document.cookie;
var cookieData	=	unescape(cookieString);
var cookieName	=	"RestCookie"
var cookieView;



var Registered;
var Name;		// title, first and last name of person 
var Title;
var firstName;
var lastName;
var PrefName;		// preferred Name chosen by person
var emailAddress; 	// email address of person
var Company;		// Company of person
var address1;
var address2;
var City;		// used in combination of Country to select supplies and adds
var Zipcode;		// used in combination of City to select suppliers and adds
var Country;		// used in combination of City to select supplies and adds
var State;		// used in combination of Country to select supplies and adds
var Province;		// used in combination of Country to select supplies and adds

var RegSince;		// should always stay the same from day one.
var MemSince;		// should always stay the same form the first day of membership
var MemType; 		// Can be "guest, mem1, mem3, prof1, prof3 "
var MemTill;		// should remain the same until the date is < than today.

var Count=0;		// should go up one at each page viewed
var AppCount=0; 	//Keeps track of the free appraisals given
var expires;		// getExpParam(typeOf) but cannot be called until after the cookie is read.





// This function times a set period in seconds
function waiting(howLong)
{
var thatLong=howLong*1000
setTimeout("first();",thatLong);
}

// This function is the first to be read after waiting() at each page 
// and checks first if there is a cookie

function first()
{
	if (cookieString != "")
	{		 // if there is something in the cookieString then goto readCookie;
	
			readCookie();
				if ((Count % 10==1) && (MemType=="guest"))
					{
					window.open(popupUpdate,"",stature)
					}
			resetCookie();
	}
	else
	{
		checkCookie()
	}

}


// This function reads the cookie and allocates its values to variables.

function readCookie(form)
{

 var valuePairs = 	new Array();
 var thisPair;
 
 
	valuePairs = 	cookieData.split("/");  
	
		
	for(var loop=0;loop < valuePairs.length;loop++)
	{
		thisPair = valuePairs[loop];
	
		varvalPair(thisPair);
	}
}


// This function splits the valuePairs(thisPair) in to variables with values;

function varvalPair(pairName)
{ 

var pairArray = new Array();

	pairArray =  pairName.split("=");
	
		(pairArray[0] =="RestCookie") 	? 	( RestCookie 	= pairArray[1]):
		(pairArray[0] =="Title") 	? 	( Title		= pairArray[1]):
		(pairArray[0] =="firstName") 	? 	( firstName 	= pairArray[1]):
		(pairArray[0] =="lastName") 	? 	( lastName 	= pairArray[1]):
		(pairArray[0] =="Company") 	? 	( Company 	= pairArray[1]):
		(pairArray[0] =="address1") 	? 	( address1 	= pairArray[1]):
		(pairArray[0] =="address2") 	? 	( address2 	= pairArray[1]):
		(pairArray[0] =="emailAddress")	? 	( emailAddress 	= pairArray[1]):
		(pairArray[0] =="City") 	? 	( City		= pairArray[1]):
		(pairArray[0] =="Zipcode") 	? 	( Zipcode 	= pairArray[1]):
		(pairArray[0] =="State") 	? 	( State		= pairArray[1]):
		(pairArray[0] =="Country") 	? 	( Country	= pairArray[1]):
		(pairArray[0] =="Province") 	? 	( Province 	= pairArray[1]):
		(pairArray[0] =="PrefName")	? 	( PrefName	= pairArray[1]):
		(pairArray[0] =="RegSince") 	? 	( RegSince	= pairArray[1]):
		(pairArray[0] =="MemSince") 	? 	( MemSince	= pairArray[1]):
		(pairArray[0] =="MemType") 	? 	( MemType	= pairArray[1]):
		(pairArray[0] =="MemTill") 	? 	( MemTill	= pairArray[1]):
		(pairArray[0] =="Count") 	? 	( Count		= pairArray[1]):
		(pairArray[0] =="AppCount") 	? 	( AppCount	= pairArray[1]):
		(pairArray[0] ==pairArray[0]);
		
		return;
}


//
//
//
//	Here comes the else part for when there are no cookies
//
// 	This tests whether the user accepts cookies.

function checkCookie()
{
	document.cookie = 'Cookies_on=yes'; // Try to set a cookie.
	    	if (document.cookie.indexOf('Cookies_on=yes')==!-1)	// If it succeeds, delete the cookie
	    			{
					delCookie("Cookies_on");      
	    			}
	    		else
	    			{
	    				(window.open(popupNoCookies,"","width=250, height=250, left=160, resizable=0")); // tells the user to set cookies to accept!
	    		    	}
		

	if (cookieString == "") // This looks for a cookie and if non brings up the notreg.html popupwindow.
		{
		(window.open(popupNotReg,"",stature))
		}	
}


function showCookies(form)
{


if (document.cookie =='') 	// then there is no cookie so return false
	{
	cookieData ="[No cookie data stored.]" 
	form.viewCookies.value = cookieData
	}
else				// there is a cookie
	{
	cookieData = unescape(this.document.cookie)
        form.viewCookies.value = cookieData;
        }
}

//
// This function deletes a cookie (cookieName)

function delCookie(cookieName)
{
        var expParam = getExpired();
        document.cookie = cookieName + "=gone" + expParam;
        
}


function getExpired(cookieName)
{
	var expDate = new Date()
	var dateInSecs = expDate.getTime()
	var expired = (-(24*60 * 60 * 1000)) + dateInSecs - (2 * dateAdjustment)
		
	if (cookieString != "")
	{	
		expDate.setTime(expired);
	}
		
    return ("; expires=" + expDate.toGMTString())

}

// 
//
//This function sets the dates
//

function getExpParam(HowLong)
{
        var expDate = new Date();
        var dateInSecs = expDate.getTime();
        var threeYrFromNow 	= (3* 365.25 *24 * 60 * 60 * 1000) + dateInSecs - (2 * dateAdjustment);
	var oneYrFromNow 	= (365.25 * 24 * 60 * 60 * 1000) + dateInSecs - (2 * dateAdjustment);
	var toDay		= (24*60*60*1000) + dateInSecs - (2 * dateAdjustment);
	var justNow 		= dateInSecs - (2 * dateAdjustment);
	var expired		= (-(24*60*60*1000)) + dateInSecs - (2 * dateAdjustment);
	
	
	if(HowLong!="")
		{
		(HowLong == "Since") ? (expDate.setTime(justNow)):
		(HowLong == "gone")  ? (expDate.setTime(expired)):
		(HowLong == "guest") ? (expDate.setTime(toDay)):
		(HowLong == "mem1")  ? (expDate.setTime(oneYrFromNow)):
		(HowLong == "mem3")  ? (expDate.setTime(threeYrFromNow))  :
		(HowLong == "prof1") ? (expDate.setTime(oneYrFromNow))  :
		(HowLong == "prof3") ? (expDate.setTime(trheeYrFromNow))  :
			(expDate.setTime(oneYrFromNow));
			
		return(expDate.toUTCString())
		}
	return""

		
}



//Next function Saves the preselected cookie for the first time from the ?_Registration_form.html

function  saveCookie()
{
 Title		= window.document.Regis_form.Title.value;
 firstName	= window.document.Regis_form.Firstname.value;
 lastName	= window.document.Regis_form.Lastname.value;
 (window.document.Regis_form.preferred.value=="")?(preferred="none"):(PrefName= window.document.Regis_form.preferred.value);
 
 emailAddress	= window.document.Regis_form.Email.value;
 
 (window.document.Regis_form.company.value=="")?(Company="none"):(Company= window.document.Regis_form.company.value);
 (window.document.Regis_form.address1.value=="")?(address1="none"):(address1= window.document.Regis_form.address1.value);
 (window.document.Regis_form.address2.value=="")?(address2="none"):(address2= window.document.Regis_form.address2.value);
 
 City		= window.document.Regis_form.Cityname.value;
 Zipcode	= window.document.Regis_form.Zipcode.value;
 Country	= window.document.Regis_form.Country.value;
 State		= window.document.Regis_form.State.value;
 Province	= window.document.Regis_form.province.value;
 
 (window.document.Regis_form.Member[0].checked == true ) ? ( MemType = "guest"):
 (window.document.Regis_form.Member[1].checked == true ) ? ( MemType = "mem1"):
 (window.document.Regis_form.Member[2].checked == true ) ? ( MemType = "mem3"):
 (window.document.Regis_form.Member[3].checked == true ) ? ( MemType = "prof1"):
 (window.document.Regis_form.Member[4].checked == true ) ? ( MemType = "prof3"):
 (alert ('MemType= not selected'));
 
 RegSince	= getExpParam("Since") ;	// should always stay the same from day one.
 MemSince	= ((MemType=="mem1")||(MemType=="mem3")||
 		   (MemType=="prof1")||(MemType=="prof3")) ?
 		   ( MemSince = RegSince ):(MemSince = "Not a Member") ;	// should always stay the same from the first day of membership.
 MemTill 	= getExpParam(MemType);		// should remain the same until the date is < than today.


Name		= Title +" "+ firstName +" "+ lastName;

Count		= 2;

AppCount	= 0;


cookieString=	"RestCookie=registered"	+ 
        	"/Title=" 		+ escape(Title) 	+
        	"/firstName=" 		+ escape(firstName)	+ 
        	"/lastName=" 		+ escape(lastName)	+ 
        	"/Company="		+ escape(Company) 	+
        	"/address1=" 		+ escape(address1)	+ 
        	"/address2=" 		+ escape(address2)	+
        	"/emailAddress="	+ escape(emailAddress)	+ 
        	"/City=" 		+ escape(City) 		+ 
        	"/Zipcode=" 		+ escape(Zipcode) 	+
        	"/State=" 		+ escape(State) 	+ 
        	"/Country=" 		+ escape(Country) 	+ 
        	"/Province=" 		+ escape(Province) 	+ 
        	"/PrefName=" 		+ escape(PrefName) 	+ 
        	"/RegSince=" 		+ escape(RegSince) 	+  
        	"/MemSince=" 		+ escape(MemSince)	+
        	"/MemType="		+ escape(MemType)	+
        	"/MemTill="		+ escape(MemTill)	+ 
        	"/Count="		+ Count 		+
        	"/AppCount="		+ AppCount	+
        	"/;expires=" 		+ getExpParam("mem3");
        				
 
 document.cookie = cookieString; 
      
}

//
//Next function Saves the RestCookie each time a page is opened

function  resetCookie()
{
 if (document.cookie !='') 
 {
	Count		= parseInt(Count)+1;

	AppCount	= parseInt(AppCount);
	
	
	cookieString=	"RestCookie=registered"	+ 
	        	"/Title=" 		+ escape(Title) 	+
	        	"/firstName=" 		+ escape(firstName)	+ 
	        	"/lastName=" 		+ escape(lastName)	+ 
	        	"/Company="		+ escape(Company) 	+
	        	"/address1=" 		+ escape(address1)	+ 
	        	"/address2=" 		+ escape(address2)	+
	        	"/emailAddress="	+ escape(emailAddress)	+
	        	"/City=" 		+ escape(City) 		+ 
	        	"/Zipcode=" 		+ escape(Zipcode) 	+
	        	"/State=" 		+ escape(State) 	+ 
	        	"/Country=" 		+ escape(Country) 	+ 
	        	"/Province=" 		+ escape(Province) 	+ 
	        	"/PrefName=" 		+ escape(PrefName) 	+ 
	        	"/RegSince=" 		+ escape(RegSince) 	+  
	        	"/MemSince=" 		+ escape(MemSince)	+
	        	"/MemType="		+ escape(MemType)	+
	        	"/MemTill="		+ escape(MemTill)	+ 
	        	"/Count="		+ Count 		+
	        	"/AppCount="		+ AppCount	+
	        	"/;expires=" 		+ getExpParam("mem3");
	        				
 document.cookie = cookieString; 
	

 }
      
}



//
// This function looks for the screensize and returns the popupwindowsize accordingly ;
  
function popupWindowSize(Size)
{
	popupWidth=popupWindowWidth();			//returns the maximum width of the popupwindow for a given screen.
	popupHeight=popupWindowHeight(popupWidth);	//returns the nessecary height for a given screen.
	Size ="width="+popupWidth +", height="+ popupHeight +", left=160, resizable, scrollbars ";//gives the window attributes in one variable.
	return Size;
//
//	This Function measures the screenwidth and then calculates the width of the popup window
 function popupWindowWidth()
 {
 	var  screen_width = screen.availWidth;
 	var	the_Width = (screen_width - 180);	//180 is 160 on the left for the loge and 20 onthe right so that the close button is definitely on the screen.
 		return the_Width ;
 		
}

//	This function takes the width of the popupwindow and calculates the height
function popupWindowHeight(the_Size)
{
	var the_Height = parseInt(200000/the_Size);	// the_Size is equall to the with thus min. 640-180=460
	if(the_Height< 320)				// This makes sure ther is enough height for all the sentences to fit in the proper manner.
	 	{
	 	the_Height=320
	 	}
 	
 		
 		return the_Height;
}
}
