/* ****************************************************
* File: functions.js
* Project: JProjecta1WK
* Author:         Wayne K.
* Date created:   10/10/05
* Purpose:        to demonstrate that I can create the js file, link it to a web page and have something from that external page actually perform on the linked page
* Functions listed: 1. popupGreeting()
*				                   
* Additional Info: none
***************************************************** */


/*Pop-up an alert message
function popupGreeting()
{

alert('Welcome to MissBehave Productions');

}
*/

//INSERT COMPANY NAME
function companyName()
{
//company name
var companyName="Miss Behave's Dark Carnival";

//display the result in the resultLabel
	//document.getElementById("companyName").value = companyName;
	document.write (companyName);
	
}


//CUSTOMIZED FUNCTIONS
function cookieWelcome()
{
	//Capture the user's input
	var wordValue = document.getElementById('userName').value;
	//Call our cookie writing function
	//The name of our cookie will be 'welcome'
	//The value of our cookie will come from the textbox
	//Type=1 for persisting the cookie, according to how this
	//		writeCookie() function was defined.
	writeCookie('welcome',wordValue,1)
}


function readUser()
{
	//Call the cookie named 'welcome'
	var returnValue = ReadCookie('welcome');
	//Display the cookie value in an alert
	alert('Welcome Back: ' + returnValue);
	
}

function readLogon()
{
	//Call the cookie named 'welcome'
	var returnValue = ReadCookie('welcome');
	//Display the cookie value in an alert
	
	
}

function remove()
{
	//Remove our cookie
	killCookie('welcome');
	
}
//END OF CUSTOMIZED FUNCTION

//READ COOKIE FUNCTION

function ReadCookie(cookieName)
{
	cookieName = cookieName + "=";
	var cookieString = window.document.cookie;
	var startPos = cookieName.length;
	var begin = cookieString.indexOf(cookieName);
	var end = cookieString.indexOf(";",begin + startPos);

	if(begin==-1)//No such cookie
	{
		//alert('no cookie');// Use Alert For debug purposes
		return "";
	}
	else
	{
		if (end == -1)//last cooke in cookie string
		{
			end = cookieString.length;
		}	
	}
	return cookieString.substring(begin + startPos,end);
}
/*To call the above function, an example would be like: returnVal=readCookie("user") 
What you can do to 'test this code' is maybe call a page level function from perhaps a button's click event; such as onclick="demo()" 
Then from within that page level demo() function, call your ReadCookie() function, including as a parameter the 'name' of your cookie. NOTE that this ReadCookie() method will return a value. Either the value of the cookie, or an empty (blank) string if there was no such cookie. You need to declare a variable to capture this return value. (The variable example I showed above was called returnVal. Then, with this returnVal, do something with it. Maybe display the value in a label on your page.																																																																													*/


//WRITE COOKIE FUNCTION
/*The example below has a third attribute called type. This attribute allows the function to be adaptable for those cookies you want to persist or choose those with no expiry date and let it die with the browser instance. An example would be:
*/
function writeCookie(cookieName,cookieValue,type)
{
	//create an instance of the Date object if type 1
	var cookieString;
	switch(type)
	{
		case 1:	//we want an expiry date
			var expDate = new Date();
			//set the date value to one year in the future
			expDate.setTime(expDate.getTime() + 1000 * 60 * 60 * 24 * 365);
			//convert date to a GMT string
			expDate = expDate.toGMTString();	
			cookieString = cookieName + "=" + cookieValue + ";expires=" + expDate;
			break;
		case 0://we do not want to persist the cookie
			cookieString = cookieName + "=" + cookieValue;
			break;
		default:
			cookieString = cookieName + "=" + cookieValue;
	}
	window.document.cookie = cookieString;
}
/* To call the above function, do so with an example like: writeCookie("user",uName,1); 
where "user" is the name we chose to call this cookie, 'uName' is a variable, declared and assigned perhaps with a value from a textbox, and finally '1' is the indicator value to tell the function we want this cookie to persist.																																																																													*/


//REMOVE/KILL COOKIE FUNCTION
function killCookie(cookieName)
{
	//create an instance of the Date object
	var expDate = new Date();
	//set the date value to the past
	expDate.setTime(expDate.getTime() - 1000 * 60 * 60 * 24 * 365);
	//convert date to a GMT string
	expDate = expDate.toGMTString();
	var cookieString = cookieName + "=" + ";expires=" + expDate;
	window.document.cookie = cookieString;
}	
/* And, to call the above cookie killer, one can do something like:killCookie("userName"), where userName was the name of the cookie you want to remove.																																																																			*/

//READ COOKIE FUNCTION

function ReadCookie(cookieName)
{
	cookieName = cookieName + "=";
	var cookieString = window.document.cookie;
	var startPos = cookieName.length;
	var begin = cookieString.indexOf(cookieName);
	var end = cookieString.indexOf(";",begin + startPos);

	if(begin==-1)//No such cookie
	{
		//alert('no cookie');// Use Alert For debug purposes
		return "";
	}
	else
	{
		if (end == -1)//last cooke in cookie string
		{
			end = cookieString.length;
		}	
	}
	return cookieString.substring(begin + startPos,end);
}


//Load large image in new window and replace FUNCTION
function Init() //call this from the page body tag's onload event.
	{		
		//Read the url for pic
		var nameValString = document.location.search;
		//alert('nameValString= ' + nameValString);
		
		//Extract the value for variable name 'pic'
		var theValue = nameValString.substring(nameValString.indexOf('=')+1);
		document.getElementById('ImgTitle').innerHTML = theValue.substring(0,theValue.length-4);
		//alert('theValue= ' + theValue);
		
		//Display the larger version of the image
		document.getElementById('Display').innerHTML='<img src="../Images/' + theValue + '" />';
		
		//better way
		//theValue="../Images/Borg.jpg";
		//document.getElementById('myImg').setAttribute('src',theValue);
	}



