// EXTERNAL JAVASCRIPT LIBRARY FILE (c) Bob Gilmour (2009) :: http://robertgilmourartforms.com

// RESIZE THE INDEX PAGE CONTENT CONTAINER
function resizeIndex(){
	document.getElementById('container').style.width='810px';
	document.getElementById('container').style.height='300px';
	document.getElementById('container').style.marginLeft='-405px';
	document.getElementById('container').style.marginTop='-150px';
} // end function resizeIndex

// RESIZE TO FULL PAGE
function resizeFull(){
	self.moveTo(0,0);
	window.resizeTo(screen.availWidth,screen.availHeight);
	self.focus();	
} // end function resizeFull

// OPEN THE SITE IN A FULL SCREEN WINDOW WITH NO BROWSER TOOLBARS OR SCROLLS ETC
function openGallery(target){
	var wHeight=screen.availHeight-50;
	var wWidth=screen.availWidth-15;	
	window.open(target,'artforms_content','top=0,left=0,scrollbars=no,menubar=no,location=no,status=yes,winX=0,winY=0');
} // end function openGallery


// SHOW OR HIDE THE MAIN DROP MENU
function showDropMenu(){
	document.getElementById('dropMenu').style.display="block";
} // end function showDropMenu

// GET COOKIE
// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}


// SET COOKIE
function setCookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


// DELETE COOKIE
// this deletes the cookie when called
function delCookie( name, path, domain ) {
if ( getCookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


// VERTICAL SCROLL SPINNER
defaultStep=20;
step=defaultStep;

	function scrollDivDown(id){
		clearTimeout(timerDown);
		document.getElementById(id).scrollTop+=step;
		timerDown=setTimeout("scrollDivDown('"+id+"')",10);
	}

	function scrollDivUp(id){
		clearTimeout(timerUp);
		document.getElementById(id).scrollTop-=step;
		timerUp=setTimeout("scrollDivUp('"+id+"')",10);
	}

	function scrollDivTop(id){
		clearTimeout(timerUp);
		document.getElementById(id).scrollTop-=document.getElementById(id).innerHeight;
		timerUp=setTimeout("scrollDivTop('"+id+"')",10);
	}

	timerDown="";
	timerUp="";

	function stopMe(){
		clearTimeout(timerDown);
		clearTimeout(timerUp);
	}

	document.onmousemove=function(){stopMe();
	}

// WRITE HTML STRING TO MAIN GALLERY WINDOW	
function writeIt(target,htmlStr){
	document.getElementById(target).innerHTML=htmlStr;
	//document.getElementById('dropMenu').style.display="none";
	document.getElementById(target).style.display='block'
} // end function writeIt

// WRITE THE ITEM INFO PANEL
function writeInfo(infoStr){
	document.getElementById('itemInfo').style.display='block';
	htmlStr="<span style=\"float: right;\"><a href=\"javascript:\" onclick=\"document.getElementById('itemInfo').style.display='none'\">close</a></span>" + infoStr;
	writeIt('itemInfo',htmlStr);
} // end function writeInfo


// SHOW THE INFO PANELS OR PAGES
function showInfo(target){

	//retrieve the info cookie
	infoStr = getCookie('artformsInfo');
		infoStr_split = infoStr.split("||");
		urlPath = infoStr_split[0];
		artistComment = infoStr_split[1];
		artistInfo = infoStr_split[2];
		websiteInfo = infoStr_split[3];
	  
	switch(target){
		
		case 'website':
			websiteStr = "<div id=\"websiteInfo\" style=\"top: 30px; width: 500px;\">" + websiteInfo + "</div>";
			websiteStr += "<img id=\"aboutwebsiteImg1\" style=\"position: absolute; top: 220px; left: 500px;\" src=" + urlPath + "_resources/img_aboutwebsite_1.jpg alt=\"\">"; 
			websiteStr += "<img id=\"aboutwebsiteImg2\" style=\"position: absolute; top: 250px; left: 800px;\" src=" + urlPath + "_resources/img_aboutwebsite_2.jpg alt=\"\">"; 
			websiteStr += "<img id=\"aboutwebsiteImg3\" style=\"position: absolute; top: 350px; left: 100px;\" src=" + urlPath + "_resources/img_aboutwebsite_3.jpg alt=\"\">"; 
			websiteStr += "<img id=\"aboutwebsiteImg4\" style=\"position: absolute; top: 0px; left: 600px;\" src=" + urlPath + "_resources/img_aboutwebsite_4.jpg alt=\"\">";						
			
			writeIt('subHead','ABOUT THIS WEBSITE');
			writeIt('contentWindow',websiteStr);
			break;

		case 'artist':
			artistStr = "<img id=\"profilePic\" src=" + urlPath + "_resources/bob_bw_profile.jpg alt=\"\">"; 
			artistStr += "<div id=\"artistComment\">" + artistComment + "</div>";
			artistStr += "<div id=\"artistInfo\">" + artistInfo + "</div>";
			artistStr += "<div id=\"menu_aboutartist\">";
			artistStr += "<a class=\"item_comment\" id=item_info style=\"border-right: none;\" onClick=\"getElementById('artistComment').style.display='none';getElementById('artistInfo').style.display='block'\">background</a>";
			artistStr += "<a class=\"item_comment\" id=item_comment onClick=\"getElementById('artistComment').style.display='block';getElementById('artistInfo').style.display='none'\">comment</a>";			
			artistStr += "</div>";
  
  			writeIt('subHead','ABOUT THE ARTIST');
			writeIt('contentWindow',artistStr);
  			break;
			
		case 'contact':
			contactStr = "<form id=\"form_email\" class=\"content_sml\" method=\"POST\" name=\"form_email\" action=\"http://biz70.inmotionhosting.com/~bobgil5/sendmail/sendmail.php\"  onSubmit=\"return validateEmail()\">";
			contactStr += "<input type=\"hidden\" name=\"env_report\" value=\"REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT,AUTH_TYPE,REMOTE_USER\" />";
			contactStr += "<input type=\"hidden\" name=\"recipients\" value=\"infoX1WVX7Brobertgilmourartforms.com\" />"; //X1WVX7B
			//contactStr += "<input type=\"hidden\" name=\"thankyou_url\" value=\"" + urlPath + "gallery/thankyou/\"\">";
			contactStr += "<input type=\"hidden\" name=\"good_url\" value=\"http://www.robertgilmourartforms.com/gallery/thankyou/\" />";
			contactStr += "<input type=\"hidden\" name=\"bad_url\" value=\"\" />";
			contactStr += "<input type=\"hidden\" name=\"required\" value=\"email:Your email address,realname:Your name\" />";
			contactStr += "<input type=\"hidden\" name=\"subject\" value=\"ENQUIRY FROM WEBSITE || ROBERT GILMOUR ARTFORMS\" />";
			contactStr += "<table id=\"formDetails\">";
			contactStr += "<tr><td class=\"detailsTag\">To:</td><td class=\"detailsInput\">ROBERT GILMOUR ARTFORMS</td></tr>";
			contactStr += "<tr><td class=\"detailsTag\">Subj:</td><td class=\"detailsInput\">ENQUIRY FROM WEBSITE || ROBERT GILMOUR ARTFORMS</td></tr>";
			contactStr += "<tr style=\"height: 50px; vertical-align: bottom;\"><td class=\"detailsTag\">Your name:</td><td class=\"detailsInput\"><input class=\"detailsInputField\" type=\"text\" id=\"realname\" name=\"realname\"></input></td></tr>";
			contactStr += "<tr><td class=\"detailsTag\">Your email:</td><td class=\"detailsInput\"><input class=\"detailsInputField\" type=\"text\" id=\"email\" name=\"email\"></input></td></tr>";
			contactStr += "<tr><td class=\"detailsTag\">Message:</td><td class=\"detailsInput\"><textarea class=\"detailsMessage\" id=\"message\" name=\"message\"></textarea></td></tr>";
			
			contactStr += "<tr><td>";
			
			  contactStr += "<div id=\"security_catch\">";
			  contactStr += "<div class=\"captcha_msg\" id=\"captchaMsg\">Please enter the security code and click SUBMIT CODE ... and then click the SEND button.</div>";
			  contactStr += "<div class=\"captcha_img\" id=\"captchaImg\"><img src=\"" + urlPath + "_includes/CaptchaSecurityImages.php?width=100&height=40&characters=5\" /></div>";
			  contactStr += "<div class=\"captcha_input\"><input style=\"width: 80px;\" id=\"security_code\" name=\"security_code\" type=\"text\" /></div>";
			  contactStr += "<input class=\"captcha_button\" type=\"button\" value=\"SUBMIT CODE\" id=\"button_capcha\" onclick=\"captchaCheck(urlPath)\">";
 			  contactStr += "</div>";
 			  contactStr += "</td></tr>";
 			  
 			  contactStr += "</table>";
			  contactStr += "<input class=\"detailsButtonSend\" type=\"submit\" value=\"SEND\" id=\"buttonSend\" />";
			  contactStr += "</form>";		
			
  			writeIt('subHead','CONTACT');
			writeIt('contentWindow',contactStr);
  			break;
			
		case 'gilmourlinks':
  			writeIt('subHead','OTHER GILMOUR WEBSITES');
			writeIt('contentWindow','');
  			break;
			
		case 'weblinks':
			linkStr = "<table>";
			linkStr += "<tr>";
			linkStr += "<td><a href=\"http://www.wotartist.com\" target=\"_blank\"><img class=\"link_image\" src=\"_content/links/linkImg_wotartist.jpg\" alt=\"\"></a> </td>";
			linkStr += "<td><span class=\"link_name\">wotartist</span><span>A website listing selected international artists.</span></td>";
			linkStr += "</tr>";
			linkStr += "</table>";
		
  			writeIt('subHead','WEB LINKS');
			writeIt('contentWindow',linkStr);
  			break;
		
		case 'sitehelp':
			linkStr = "<table id=\"info_sitehelp\">";
			linkStr += "<tr>";
			linkStr += "<td class=\"helpitem\">CONTACT</td><td class=\"helpinfo\">To make email contact with the artist, please click the \"i\" icon (above) and select \"CONTACT\" from the drop-down menu. You will be presented with a standard contact form. In most cases, you'll receive a reply within 24 hours (allowing for time zone differences).</td>";
			linkStr += "</tr><tr>";			
			linkStr += "<td class=\"helpitem\">BOOKMARKS & FAVOURITES</td><td class=\"helpinfo\">If you wish to save the location of an item displayed on the site, please use the \"BOOKMARK THIS PAGE\" link at the top of the viewing window ... or press the keys <b>Control D</b> (IE/Mozilla/Netscape) or <b>Control T</b> (Opera).<br><br>You can also use the <b>RSS Feed Icon</b> to subscribe to this site's \'News Feed\' and receive changes to the website news items as they are added to the site. This requires that you have access to a suitable News Feed Reader.</td>";
			linkStr += "</tr>";
			linkStr += "</table>";
		
  			writeIt('subHead','HELP USING THIS WEB SITE');
			writeIt('contentWindow',linkStr);
  			break;			
		} // end switch
} // end function showInfo

// ADD TO FAVOURITES
function bookmark(url,title){
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
  window.external.AddFavorite(url,title);
  } else if (navigator.appName == "Netscape") {
    window.sidebar.addPanel(title,url,"");
  } else {
    alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
  }
}
	
	
// AJAX :: GET THUMBNAILS FROM SERVER
function getThumbs(thmNum,thmTarget,thmFileName,incr,sefPath){

	document.getElementById('contentWindow').innerHTML=""; // clear the gallery window
	document.getElementById('contentWindow').style.paddingTop='50px'; // space the thumbnails down in window
	document.getElementById('contentWindow').style.height='410px'; // resize content Window height so bottom menu is not obscured == height - 50px
	
	var passData = 'thmNum=' + thmNum + '&thmTarget=' + thmTarget + '&thmFileName=' + thmFileName + '&thmSet_incr=' + incr + '&sefPath=' + sefPath;
	//alert(passData);

	var AJAX = null;
	if (window.XMLHttpRequest) {
   		AJAX=new XMLHttpRequest();
		} else {
   			AJAX=new ActiveXObject("Microsoft.XMLHTTP");
			}
		if (AJAX==null) {
   			alert("Your browser doesn't support AJAX.");
   			return false
			} else {
				AJAX.open("POST", sefPath + "includes/send_thumbset.php", true);
   				AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   				AJAX.onreadystatechange = function() {
	   			if (AJAX.readyState == 4 || AJAX.readyState == "complete") {
					
					// AJAX response
		   			var responseStr = AJAX.responseText;
					
				   // parse the response string
		   			var responseParts = responseStr.split("|");
						writeIt('subHead',responseParts[0]); // write the sub heading
						writeIt('contentWindow',responseParts[1]); // write the thumbnails					
						} // end if AJAX
					} // end AJAX
				AJAX.send(passData);
			} // end else
} // end function getThumbs


// AJAX :: GET MAIN IMAGE WHEN THUMBSTRIP ITEM IS CLICKED
function getMainImage(placeholder,m_image,offset_left,offset_top){ //placeholder,m_image,offset_left,offset_top

var main_image = document.createElement('img');
var img_left = offset_left + "px";
var img_top = offset_top + "px";
    main_image.onload = function (evt) {
	    document.getElementById(placeholder).style.left=img_left;
	    document.getElementById(placeholder).style.top=img_top;
        document.getElementById(placeholder).src=this.src;
    	}
	main_image.src=m_image;
	document.getElementById('itemInfo').style.display='none'; // close the info panel if required
} // end function getMainImage


// GET ITEM PAGE
function getItem(urlTarget,itemNum){
	delCookie('currentItem');
	setCookie('currentItem',itemNum,'1','/','','');
	var urlStr = "../../" + urlTarget;
	location.href = urlStr;
} // end function getItem


// AJAX :: CAPCHA CHECK CONTACT
function captchaCheck(urlPath){

  // send the security code up to the server for checking by AJAX
  var codeData = 'secCode=' + document.form_email.security_code.value;
  	
  var AJAX = null;
  if (window.XMLHttpRequest) {
    AJAX=new XMLHttpRequest();
   } else {
     AJAX=new ActiveXObject("Microsoft.XMLHTTP");
     }
     if (AJAX==null) {
     alert("Your browser doesn't support AJAX.");
     return false
     } else {
	   AJAX.open("POST", "" + urlPath + "includes/cap_chk.php", true);
       AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       AJAX.onreadystatechange = function() {
       if (AJAX.readyState==4 || AJAX.readyState=="complete") {
			var responseStr = AJAX.responseText;
			if (responseStr=="success"){
			document.getElementById('security_catch').innerHTML	= "<span style=\"margin-top: 20px; float: right; margin-right: 20px; color: #FF8C00;\">SUCCESS --> Click Send Button</span>";
			document.form_email.buttonSend.style.display='block';
			} else {
				var randomnumber=Math.floor(Math.random()*1000001)
				document.getElementById('captchaMsg').innerHTML = "<span style=\"color: #FF8C00;\">FAILURE - Please re-enter the security code</span>";
				document.getElementById('captchaImg').innerHTML = "<img src=\"" + urlPath + "includes/CaptchaSecurityImages.php?width=100&height=40&characters=5&" + randomnumber + "\" />"; // the random number is to bypass firefox cache
				}
			}
		}
   AJAX.send(codeData);
   			 
   }
}


// VALIDATES EMAIL FORM
function validateEmail() {
	
  // check for empty fields	
	if (document.form_email.realname.value=="") {
    	alert('Please enter your name');
    	return false;
		}
    	
	if (document.form_email.email.value==""){
    	alert('Please enter your email address');
    	return false;
		}
}
