// JavaScript Document

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function createCopyright(){
		var curDate=new Date();
		var curYear=curDate.getFullYear()
		{document.write ( "Copyright <sup>©</sup> 2005-" + curYear + " Sanctuary Curio Shoppe<br /> All rights reserved");} 
}
function ctc(pic,w,alttxt){ 
		var picpath='images/prod/' + pic + '_t.jpg';//full path and name of picture
		hcode ='<img class="pt" src="'+ picpath + '" width="'+w+'px"alt="'+alttxt+'"';
		hcode += 'onClick="pp(\''+pic+'\',\''+alttxt+'\')" />';
		//hcode += 'onClick="pp(\'' + pic + '\')" />';
		document.write(hcode);  
}
function updatePicHD(){	// updates picture from somewhere on the hard drive, for viewing before upload.
	var pic=document.getElementById('pic');
	var picsrc=document.getElementById('fileName').value;
	var string=new String(picsrc);
	if (string.length>5){
		pic.src=picsrc;}
	return picsrc;
}
function fixInputBoxes(){
	// called by uploadForm
	// calls replaceContent function to enter default values into textboxes when selecting new item for uploading.
	var picPath=document.getElementById('fileName').value;
	var picName= extractPictureName(picPath);
	replaceContent('imgTitle',picName);
	replaceContent('altText',picName);
	replaceContent('description',picName);
}
function extractPictureName(path){
	if (path != null && path.length>0){
		var endPos=path.lastIndexOf('.');
		var startPos=((path.lastIndexOf("\\"))+1);
		var name=path.substring(startPos,endPos);
		return name;
		}
}
function replaceContent(controlName,picName){// enters picName into each textbox when selecting new picture for upload form.
// called by fixInputBoxes only.
	txttitle=document.getElementById(controlName).value;
	//if (txttitle == null || txttitle.length == 0 ||txttitle.length == 1 ){
	document.getElementById(controlName).value=picName;
	//}
}
function updatePic(destId,picPath){	// updates picture from somewhere on the hard drive, for viewing before upload.
	var pic=document.getElementById(destId);// where to put the picture
	pic.src=picPath;
}
function fillPicInfoFields(controlName,picId){
	//alert("In the Pic Fields function.  Id is:  " + picId);
	document.getElementById(controlName).value=picId;
}
function getElementVal(controlName){ // gets value of whatever element is passed in. 
	var elementVal= document.getElementById(controlName).value;
	return elementVal;
}
function setCookie(cName,cValue){
	var cookieValue=escape(cValue);
	var cExpires=new Date();
	cExpires.setTime(cExpires.getTime()+1000*60*60*24*3); // sets cExpires to time 3 days from when it's set.
	document.cookie = escape(cName) + "=" + escape(cookieValue) + escape(";expires=")+ cExpires.toGMTString();
	//alert(document.cookie);
}
function refreshPage(){
	window.location.reload(true);
}
function insertPic(targetCell,picName,w,h,altText,desc){ // creates contents of cell, no cells per se, though.
	// first remove picture if it's there.
	var picCell=document.getElementById(targetCell);
	try{
		var picImg=document.getElementById('pic');
			picCell.removeChild(picImg);
		var br=document.getElementById('picbr');
			picCell.removeChild(br);
		var picDesc=document.getElementById('picDesc');
			picCell.removeChild(picDesc);
	}
	catch(e){
		//alert("try exception in RemoveChildren ");
	}
	// now add the new bit:
	try{
		var newImg=document.createElement('img');
			newImg.setAttribute('src',picName);
			newImg.setAttribute('class','rt');
			newImg.setAttribute('name','pic');
			newImg.setAttribute('id','pic');
			newImg.setAttribute('width',w);
			newImg.setAttribute('height',h);
			newImg.setAttribute('alt',altText);	
		picCell.appendChild(newImg);	
		var newlb = document.createElement('br');
			newlb.setAttribute('id','picbr');
		picCell.appendChild(newlb);
		var newPar=document.createElement('p');
			newPar.setAttribute('id','picDesc');
			newPar.innerText=desc;
			picCell.appendChild(newPar);
	}
	catch(e){
	}
	
}
function KeepStatic(){
	var crossobj=document.all? document.all.sc : document.getElementById('sc');
	var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
	var dsocleft=document.all? iebody.scrollLeft : pageXOffset; // dsoc stands for document scroll offset coordinates 
	var dsoctop=document.all? iebody.scrollTop : pageYOffset;
		if (document.all||document.getElementById){
			crossobj.style.left=dsocleft+400+'px';
			crossobj.style.top=dsoctop+185+'px';
		}
}
function calcXPos(){// calculates the required distance x so center the picture horizontally.
	//alert('In the calcXPos function');
	//var pic=document.getElementById('picCell').firstChild;
	//alert('pic width is:  '+pic.width);
	//var pCtr=(pic.width/2)+259;
	//alert('pCtr is: '+pCtr + '\ncentre position is: '+pCtr);
	//picWidth=((document.getElementById('picCell').firstChild).width)/2;
	//picPos=300-picWidth;
	picPos=600+((document.getElementById('picCell').firstChild).width)/2;
	//alert('PicPos is: '+picPos);
	return picPos;
}
/*function clearElementOfChildren(elementName){ // this is pseudo-code; won't work but here to make sure I don't forget
	try{
		for (var x = elementName.children.length; x>=0;x--){
			remove Node;
		}
	}
	catch(e){}
//***********************	or try:  ***************************
	while( elementName.hasChildNodes() ) { 
		elementName.removeChild(elementName.getFirstChild() ); } 

//***********************	or try:  ***************************	

	
}*/
function LoadPicturePage(name,winput,hinput,alttext){ // not used in porgram; left in as example.  loads picture from name into window according to name, width and height passed in.
		var newWindow = window.open("","NewWindow","resizeable=yes,menubar=yes,width="+(winput + 40)+", height="+(hinput +60));	// variable to define the new window.
			var cont = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ';
			cont += '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
			cont += '<html xmlns="http://www.w3.org/1999/xhtml">';
			cont += '<head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />';
			cont += '<title> Sanctuary:  Product Catalogue Item  </title>';
			cont += '<link href="styles.css" type="text/css" rel="stylesheet"></head>';
			cont += '<body><img src="' + name + '" width= "'+ winput + '"px height = "'+ hinput +'px" alt="'+ alttext +'">';
			cont += '<br />';
			cont += '<form><input style="margin-top:5px" type="button"name="cl" value="Close" id="cl"onClick="window.close();"/></form>';
			cont += '</body></html>';		
			newWindow.document.write(cont);
	}
function getCookieVal(cName){// not used in the program; script works and left here in case we want to implement it later.
	var cVal=unescape(document.cookie);
	var cStart = cVal.indexOf('=');	 // finds position of cookie name with "=" sign after it; this is the start of the cookie value.
	var cEnd=cVal.indexOf('expires');
	cEnd=cEnd-1;
	if (cStart == -1){
		alert("cookie could not be found so item info can not be filled in.");	
	}
	else{
		cStart = cStart + 1;
		//alert("cStart is:  " + cStart +"\ncEnd is:  " +cEnd);
		//cEnd=cVal.length;
		cVal=unescape(cVal.substring(cStart,cEnd));
	}
	//alert("The cookie value is:  " + cVal);
	return cVal;
}