// Soap Functions
function AddToWishlist(ID, CC, IPA) {
	if (IPA != "") {
		var url = 'http://www.irelandupclose.com/soap/wls.php';
		var params = {
			imageDesc : ID,
			countryCode : CC,
			ip : IPA
		};
		SOAP.q_async(url, 'urn:WishList', 'ATWL', params, awl_ok, awl_nok);
		alert("This image has been added to your Wishlist. \n\nTo view your wishlist click on the Wishlist button.\n\nNote: do not delete your browser cookies if you would like to keep your wishlist.");
	} else
		alert("We cannot add this image to your wishlist because you do not have a valid ID.  Please clear your browser history (cookies and temporary internet files), restart your browser and try again.\n\nIf you still cannot add images to your wishlist please send an email to info@SceneShifts.com");
}

function RemoveFromWishlist(ID, IPA) {
	// alert("Removing from Wishlist");
	var url = 'http://www.irelandupclose.com/soap/wls.php';
	var params = {
		imageDesc : ID,
		ip : IPA
	};
	SOAP.q_async(url, 'urn:WishList', 'RFWL', params, rwl_ok, rwl_nok);
	alert("This image has been removed from your Wishlist. \n\nYour Wishlist will be re-displayed after a few seconds. Or click on 'My Wishlist' to refresh now.");
}

function awl_ok(responseobject) {
	var xml = responseobject.responseXML;
	// var ret = dom_rtext(dom_ftag(xml,'retval')); // Get return value as
	// string
	var retval = parseInt(dom_rtext(dom_ftag(xml, 'retval'))); // Get return
																// value as
																// integer
	if (retval != 1)
		alert("Add to Wishlist Failed");// else alert("Added to Wishlist");
}

function awl_nok(responseobject) {
	alert("Add to Wishlist Failed");
}

function rwl_ok(responseobject) {
	var xml = responseobject.responseXML;
	// var ret = dom_rtext(dom_ftag(xml,'retval1')); // Get return value as
	// string
	var retval = parseInt(dom_rtext(dom_ftag(xml, 'retval1'))); // Get return
																// value as
																// integer
	if (retval != 1)
		alert("Remove from Wishlist Failed");
	window
			.open(
					"http://www.irelandupclose.com/index.php?pf=98&pfName=SearchResult",
					"_self");
}

function rwl_nok(responseobject) {
	alert("Remove from Wishlist Failed");
}

// This way we call a php page in a separate window/browser
function callphp(iref, redisp) {
	mywindow = window.open(iref, "mywindow",
			"menubar=0,resizable=0,width=1,height=1"); // Call php page to
														// update the database
														// and remove the item
														// from the wishlist
	if (redisp == 1)
		location.reload(true); // Refresh this page
	return;
}

// xml-http functions
var Try = {
	these : function(funclist) {
		for ( var a = 0, b = arguments.length; a < b; ++a) {
			try {
				return (arguments[a])()
			} catch (e) {
			}
		}
	}
}
function create_xmlhttp() {
	return Try.these(function() {
		return new XMLHttpRequest()
	}, function() {
		return new ActiveXObject('Msxml2.XMLHTTP')
	}, function() {
		return new ActiveXObject('Microsoft.XMLHTTP')
	}, function() {
		return window.createRequest()
	}) || false
}

// soapfun functions
var SOAP = {
	sens : 'http://schemas.xmlsoap.org/soap/encoding/', /* SOAP-ENC */
	xsins : 'http://www.w3.org/2001/XMLSchema-instance', /* xsi */
	svns : 'http://schemas.xmlsoap.org/soap/envelope/', /* SOAP-ENV */

	q_async : function(api_url, /* the endpoint address indicated by the WSDL */
	wsdl_namespace, /* the name found in targetNamespace in the WSDL */
	function_name, /* the function to call */
	params, /* the parameters to pass */
	ok_callback, /* callback function for receiving the response */
	error_callback /*
					 * callback function for error situation (not supported on
					 * IE)
					 */
	) {
		var req = create_xmlhttp();
		req.open('POST', api_url, true);
		/*
		 * change true to false here^ if you want to use synchronous calling
		 * instead
		 */
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				ok_callback(req);
			}
		}
		if (req.onerror)
			req.onerror = function() {
				error_callback(req);
			}
		req.send('<?xml version="1.0" encoding="utf-8"?>'
				+ '<s:Envelope xmlns:s="' + SOAP.svns + '"' + ' xmlns:n="'
				+ wsdl_namespace + '"' + '>' + '<s:Body><n:' + function_name
				+ '>' + SOAP.esc_params(params) + '</n:' + function_name
				+ '></s:Body>' + '</s:Envelope>');
	},

	esc_params : function(a) {
		if (typeof a != 'object')
			return a.toString().replace(/&/g, '&amp;').replace(/</g, '&lt;')
					.replace(/>/g, '&gt;');
		var p, res = '';
		for (p in a)
			res += '<' + p + '>' + this.esc_params(a[p]) + '</' + p + '>';
		// alert(res);
		return res
	},

	q_async_trans : function(url, ns, q, params, func_ok, func_nok) {
		SOAP.q_async(url, ns, q, params, function(req) {
			return func_ok(SOAP.trans(req, q))
		}, func_nok)
	},

	// Translates a XML response object into a function return value.
	trans : function(req, q) {
		if (!req)
			throw "no xmlhttp?";
		var x = req.responseXML, rr = new RegExp('^' + q + 'Response$');
		if (!x)
			throw req.responseText;
		return SOAP.respt.find(x, rr)
	},

	respt : { // response translator
		itp : /^xsd:(int|long|short|byte|((non)?(nega|posi)tive)?integer)$/,
		ftp : /^xsd:(float|double)$/,
		find : function(xml, rr) {
			var x = xml.childNodes, a, b = x.length, n;
			for (a = 0; a < b; ++a) {
				n = x[a];
				var nn = n.localName;
				if (nn != 'undefined' && rr.test(nn))
					return this.trans(n.firstChild)
					// ^Note: multipart return values not supported
				var subq = this.find(n, rr)
				if (typeof subq != 'null')
					return subq
			}
			return null
		},
		trans : function(n) {
			if (n.nodeType != 1)
				throw 'unexpected nodetype: ' + n.nodeType;
			if (n.hasAttributeNS(SOAP.sens, 'arrayType')) {
				// Array of something
				var res = [], x = n.childNodes, a, b = x.length;
				for (a = 0; a < b; ++a)
					res.push(this.trans(x[a]))
				return res
			}
			if (n.hasAttributeNS(SOAP.xsins, 'nil'))
				return ''; // null;
			var text = dom_rtext(n), type = n
					.getAttributeNS(SOAP.xsins, 'type');
			if (type == 'xsd:boolean')
				return text == 'true' || text == '1';
			if (this.itp.test(type))
				return parseInt(text, 10);
			if (this.ftp.test(type))
				return parseFloat(text);
			if (!type || /^xsd:/.test(type))
				return text;

			// It was not an XSD type, so handle it as a struct/object then.
			var res = {}, x = n.childNodes, a, b = x.length;
			for (a = 0; a < b; ++a) {
				var nn = x[a].localName;
				res[nn] = this.trans(x[a])
			}
			return res
		}
	}
}

// domfun functions
/* Copyright (C) 1992,2007 Joel Yliluoma ( http://iki.fi/bisqwit/ ) */
/* Various javascript utility functions - http://bisqwit.iki.fi/jsgames/ */

// A very handy shorthand.
function rgel(root, id) {
	return root.getElementById(id)
}
function gel(id) {
	return rgel(document, id)
}
// Utilities for common form input properties
// selectboxes
function id(id) {
	return gel(id).selectedIndex
}
function ids(id, v) {
	gel(id).selectedIndex = v
}
// checkboxes
function ch(id) {
	return gel(id).checked ? 1 : 0
}
function chs(id, v) {
	gel(id).checked = v > 0
}
// textlines
function tx(id) {
	return gel(id).value
}
function txs(id, v) {
	gel(id).value = v
}

/* Now the actual DOM functions */

function dom_wipe(block) {
	var a, k = block.childNodes, b = k.length;
	for (a = b; a-- > 0;)
		block.removeChild(k[a])
	return block
}
var clearBlock = dom_wipe;

/* Would use 'const' here, but IE does not support it. */
var dom_is_event_activator_name = {
	onkeydown : 1,
	onmouseover : 1,
	onclick : 1,
	onblur : 1,
	onfocus : 1,
	onmouseout : 1,
	ondblclick : 1,
	onmouseup : 1,
	onmousedown : 1,
	onkeypress : 1,
	onkeyup : 1,
	onchange : 1,
	onload : 1,
	onmousemove : 1,
	onselect : 1,
	onsubmit : 1,
	onunload : 1,
	onerror : 1,
	className : 1
}
function dom_tag_finish_with(t, params) {
	for ( var i in params)
		if (dom_is_event_activator_name[i])
			t[i] = params[i];
		else
			t.setAttribute(i, params[i]);
	return t
}
function dom_add_children(t, children) {
	var a, b = children.length;
	for (a = 0; a < b; ++a)
		dom_append(t, children[a])
	return t
}

function dom_tag(t) {
	return document.createElement(t)
}
function dom_text(content) {
	return document.createTextNode(content)
}
function dom_tag_with(t, params) {
	return dom_tag_finish_with(dom_tag(t), params)
}
function dom_tag_class(t, cls) {
	return dom_tag_with(t, {
		className : cls
	})
}
function dom_tag_class_with(t, cls, params) {
	return dom_tag_finish_with(dom_tag_class(t, cls), params)
}
function dom_tag_with_children(t, children) {
	return dom_add_children(dom_tag(t), children)
}
function dom_tag_attr_with_children(t, params, children) {
	return dom_add_children(dom_tag_with(t, params), children)
}
function dom_tag_class_with_children(t, cls, children) {
	return dom_add_children(dom_tag_class(t, cls), children)
}
function dom_tag_text(t, text) {
	return dom_tag_with_children(t, [ dom_text(text) ])
}
function dom_tag_attr_text(t, params, text) {
	return dom_finish_with(dom_tag_text(t, text), params)
}
function dom_append(root, t) {
	return root.appendChild(t)
}
function dom_rtext(t) {
	/* Loads the text content from this tag. Assuming it has text content */
	var c = t.childNodes;
	if (!c)
		throw 'dom_rtext';
	var result = '', a, b = c.length;
	for (a = 0; a < b; ++a)
		result += c[a].nodeValue;
	return result
}
function dom_rtags(root, t) {
	/* Loads an array of matching tags */
	return root.getElementsByTagName(t)
}
function dom_ftag(root, t) {
	/* Loads the matching tag, throws exception if not found */
	var l = dom_rtags(root, t);
	if (l.length == 0)
		throw 'tag ' + t + ' missing';
	if (l.length > 1)
		throw 'tag ' + t + ' is ambiguous';
	return l[0]
}

// soap functions
function AddToWishlist(ID, CC, IPA) {
	if (IPA != "") {
		var url = 'http://www.irelandupclose.com/soap/wls.php';
		var params = {
			imageDesc : ID,
			countryCode : CC,
			ip : IPA
		};
		SOAP.q_async(url, 'urn:WishList', 'ATWL', params, awl_ok, awl_nok);
		alert("This image has been added to your Wishlist. \n\nTo view your wishlist click on the Wishlist button.\n\nNote: do not delete your browser cookies if you would like to keep your wishlist.");
	} else
		alert("We cannot add this image to your wishlist because you do not have a valid ID.  Please clear your browser history (cookies and temporary internet files), restart your browser and try again.\n\nIf you still cannot add images to your wishlist please send an email to info@SceneShifts.com");
}

function RemoveFromWishlist(ID, IPA) {
	// alert("Removing from Wishlist");
	var url = 'http://www.irelandupclose.com/soap/wls.php';
	var params = {
		imageDesc : ID,
		ip : IPA
	};
	SOAP.q_async(url, 'urn:WishList', 'RFWL', params, rwl_ok, rwl_nok);
	// alert("This image has been removed from your Wishlist. \n\nClick OK: your
	// Wishlist will be re-displayed after a few seconds.");
}

function awl_ok(responseobject) {
	var xml = responseobject.responseXML;
	// var ret = dom_rtext(dom_ftag(xml,'retval')); // Get return value as
	// string
	var retval = parseInt(dom_rtext(dom_ftag(xml, 'retval'))); // Get return
																// value as
																// integer
	if (retval != 1)
		alert("Add to Wishlist Failed");// else alert("Added to Wishlist");
}

function awl_nok(responseobject) {
	alert("Add to Wishlist Failed");
}

function rwl_ok(responseobject) {
	var xml = responseobject.responseXML;
	// var ret = dom_rtext(dom_ftag(xml,'retval1')); // Get return value as
	// string
	var retval = parseInt(dom_rtext(dom_ftag(xml, 'retval1'))); // Get return
																// value as
																// integer
	if (retval != 1)
		alert("Remove from Wishlist Failed");
	window
			.open(
					"http://www.irelandupclose.com/index.php?pf=98&pfName=SearchResult",
					"_self");
}

function rwl_nok(responseobject) {
	alert("Remove from Wishlist Failed");
}

// This way we call a php page in a separate window/browser
function callphp(iref, redisp) {
	mywindow = window.open(iref, "mywindow",
			"menubar=0,resizable=0,width=1,height=1"); // Call php page to
														// update the database
														// and remove the item
														// from the wishlist
	if (redisp == 1)
		location.reload(true); // Refresh this page
	return;
}

// SceneShifts general functions
var arrImages = new Array();
var drawImage = 0;
arrImages[0] = new Image();
arrImages[1] = new Image();
arrImages[2] = new Image();
arrImages[0].src = 'images/draw.jpg';
arrImages[1].src = 'images/draw1.jpg';
arrImages[2].src = 'images/draw2.jpg';

function cycleImages() {
	drawImage++;
	if (drawImage > 2) {
		drawImage = 0;
	}
	if (document.images) {
		if (arrImages[drawImage] != null) {
			if (arrImages[drawImage].complete) {
				document.drawImg.src = arrImages[drawImage].src;
			}
		}
	}
}

var picIndex = 0;
var aPics = new Array("PixIntel Logo.jpg", "SceneShifts1.jpg",
		"SceneShifts2.jpg");
function changepic(idelay) {
	// alert("ChangePic");
	if (picIndex >= aPics.length) {
		picIndex = 0;
	}
	setTimeout("changepic(2000)", 8000);

	document["img"].src = aPics[picIndex];

	picIndex++;
}

function getObj(name) {
	if (document.getElementById) {
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	} else if (document.all) {
		this.obj = document.all[name];
		this.style = document.all[name].style;
	} else if (document.layers) {
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}

function inspect(elm) {
	var str = "";
	alert("inspect");
	for ( var i in elm) {
		elem = elm.getAttribute(i);
		if (elem != null)
			str += i + ": " + elm.getAttribute(i) + "\n";

	}
	alert(str);
}

function inspectStyle(elm) {
	if (elm.style) {
		alert("element style");
		var str = "";
		for ( var i in elm.style) {
			elem = elm.style[i];
			if (elem != null)
				str += i + ": " + elem + "\n";
		}
		alert(str);
	} else
		alert("No style for this element");
}

function getStyle(elem) {
	elem = "mainPic";
	alert(elem);
	var header = document.getElementById(elem);
	inspectStyle(header);
}

var secs = 0;
var timerID = null;
var timerRunning = false;
var delay = 1000;

function InitializeTimer(timeout) {
	// Set the length of the timer, in seconds
	secs = timeout;
	StopTheClock();
	StartTheTimer();
}

function StopTheClock() {
	if (timerRunning)
		clearTimeout(timerID);
	timerRunning = false;
}

function StartTheTimer() {
	if (secs == 0) {
		StopTheClock();
		// Here's where you put something useful that's
		// supposed to happen after the allotted time.
		// For example, you could display a message:
	} else {
		self.status = secs;
		secs = secs - 1;
		timerRunning = true;
		timerID = self.setTimeout("StartTheTimer()", delay);
	}
}

function getSize(pic) {
	alert("Picture Loaded");
	// var img = document.images.pic;

	/*
	 * var DHTML = (document.getElementById || document.all || document.layers);
	 * if (!DHTML) { alert("No DHTML Support"); return; }
	 */
	// alert (document["mainPicture"].id); //works
	// alert(document["mainPicture"].width + ' ' +
	// document["mainPicture"].height); //Works
	/*
	 * myimage = new Image(); myimage = pic;
	 */
	// alert('name = ' + myimage.name); // gives undefined
	// alert('Current dimensions: ' + myimage.width + ' ' + myimage.height);
	// //gives undefined
}
var p;
var once = false;
function loadimage(mimg) {
	if (!once) {
		once = true;
		p = document.getElementById("mainPic");
		p.src = mimg;
	}
}

function showmainpic(p, myimage) {
	self.setTimeout("p.src = myimage", 100);
	// document["mainPicture"].src = myimage; // Works
}

function showpic(pic, artist, description, narrative, year, reference, width,
		height, pf, pfname, thumbnail, searchString, picIndex) {
	g_name = artist;
	g_description = description;
	g_reference = reference;
	g_year = year;
	re = /!c/g;
	narrative = narrative.replace(re, ",");
	re = /!a/g;
	narrative = narrative.replace(re, "'");
	picIndex--;

	var DHTML = (document.getElementById || document.all || document.layers);
	if (!DHTML) {
		alert("No DHTML Support");
		return;
	}
	/*
	 * document.getElementById("artist").firstChild.data = artist;
	 * document.getElementById("description").firstChild.data = description;
	 * //document.getElementById("medium").firstChild.data = medium; //
	 * document.getElementById("year").firstChild.data = year;
	 * document.getElementById("reference").firstChild.data = "Ref: " +
	 * reference; document.getElementById("imageDescription").firstChild.data =
	 * narrative;
	 * 
	 */
	/*
	 * if(secs == 0) { InitializeTimer(5);
	 *  } else alert("Please wait ... image loading");
	 */
	/*
	 * myimage = new Image();
	 * 
	 * myimage = pic;
	 */
	pic = "../" + pic;

	var ww = Number(width) + 100;
	var hh = Number(height) + 300;

	var surl = "portfolio/popup.php?picture=" + pic + "&width=" + width
			+ "&height=" + height + "&description=" + description
			+ "&reference=" + reference + "&narrative=" + narrative + "&pf="
			+ pf + "&pfname=" + pfname + "&thumbnail=" + thumbnail
			+ "&searchString=" + searchString + "&picIndex=" + picIndex;

	myref = window
			.open(
					surl,
					'_self',
					'left = 0, top = 0,resizable=yes,scrollbars=yes,menubar=0,toolbar=0,statusbar=0,location=0,directories=0,width='
							+ ww + ', height=' + hh);
	// document["mainPicture"].src = 'images/blank.jpg';
	p = document.getElementById("mainPic");
	p.onload = onloadp; // Define onload event function
	p.src = 'images/blank.jpg';

}

function onloadp() {
	showmainpic(p, myimage);
}

function wopen(surl, name, ww, hh, resizable) {
	var left = (screen.width / 2) - (ww / 2);
	var top = (screen.height / 2) - (hh / 2);
	var res = 'yes';
	if (resizable == 0)
		res = 'no';

	if (ww == 0)
		ww = screen.width;
	if (hh == 0)
		hh = screen.height;

	myref = window
			.open(
					surl,
					'name',
					'left=0,top=0,menubar=0,toolbar=0,scrollbars=yes,status=0,directories=no,location=0,maximize=res,width='
							+ ww
							+ ',resizable='
							+ res
							+ ',height='
							+ hh
							+ ',top=' + top + ',left=' + left);

}

function popUp(url) {
	window
			.open(
					url,
					'',
					'resizable=0,menubar=0,toolbar=0,statusbar=0,location=1,directories=1,width=1600,height=1200');
}

// var dis = document.getElementById('description');
// dis.style.visibility="visible":
// inspect(dis.obj);
// dis.innerText="LJOFDJFL";//document["mainPicture"].src = myimage;

// alert(myimage.style.width);
// document["mainPicture"].width=50; THIS WORKS
// document["mainPicture"].height=50;

// w=pic.style.width;
// alert(w);
// document["description"].text = description;
// For IE this works
// if (document.all)
// {
// document.all['edition'].innerText = edition;
// document.all['description'].innerText = description;
// document.all['medium'].innerText = medium;
// document.all['size'].innerText = size;

// }

// For Netscape, something like this
// p = document.getElementById["picdesc"];
// alert (p.innerHTML);

// ----------------------------------------------------------------------------------------------------------------------------------
//
// These functions are used for the cart
//
// ----------------------------------------------------------------------------------------------------------------------------------
// This function loads frame2 - can be called by any frame including frame2 if
// it wants to reload itself or load another page
function loadframe2(url) {
	// parent.myvar = 0; // Just as example
	parent.document.all("TopFrame").rows = "67%,*";
	parent.frame2.location.href = url;
}

// This function is called by frame2 - for example if it wants to get the
// calling frame to reload itself.
function OnFrame2Load(p) {
	// alert("Parent OnFrame2Load " + p); // This would just display the
	// parameter which was passed to frame2 in the url (see loadframe2 line
	// below).
	// parent.frame1.location.href = 'Cart.php'; // Reload myself
	// parent.TopFrame.rows = "67%, *";
	// parent.document.all("TopFrame").rows="67%,*";
}

function imageObject() {

	this.name = document.getElementById("edition").firstChild.data;
	this.description = document.getElementById("description").firstChild.data;
	this.reference = document.getElementById("size").firstChild.data;
}

var arr = new Array();
var index = 0;
function addToCart(nm, ds, rf) {
	if (g_name == "")
		g_name = nm;
	if (g_description == "")
		g_description = ds;
	if (g_reference == "")
		g_reference = rf;
	// alert(document.getElementById('reference').value);
	newImage = new imageObject();
	arr[0] = newImage;
	// alert(newImage.name);
	// alert(arr[0].name);
	// alert("Top Layer Frameset rows: " +
	// parent.document.all("TopFrame").rows);

	loadframe2('Cart.php?name=' + g_name + '&description=' + g_description
			+ '&reference=' + g_reference);
}

