var isIE = false;

/*
* CreateXMLHttpRequest()
* Multi-browser compatible function to create xmlHttpRequest object
*/
function CreateXMLHttpRequest() {
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_req = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		try {
			http_req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
		isIE = true;
	} 
	return http_req;
}

/* 
* getElementTextNS(string, string, string, int)
* retrieve text of an XML document element including
* elements using namespaces 
* @param	prefix			prefix for namespace
* @param	local			local extension
* @param	parentElem		object collection
* @param	index			index of object collection to fetch from
*/
function getElementText(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else if(result.firstChild) {
			return result.firstChild.nodeValue;    		
        } else {
			return "";
		}
    } else {
        return "";
    }
}

/*
* function getObject(string)
* Retrieve object collection based on provided id
* Allows for several different methods of access
* for different browsers
* @param	obj				ID of object to return
* @return	object			HTML object
*/
function getObject(obj) {
  // step 1
  if ( getObject ) {
    obj = document.getElementById( obj );

  // step 2
  } else if ( document.all ) {
    obj = document.all.item( obj );

  //step 3
  } else {
    obj = null;
  }

  //step 4
  return obj;
}

/*
* function removeChildren(string)
* Remove all child nodes associated with provided ID
* @param	obj		HTML Object with children
*/
function removeChildren(obj)
{
	if(obj) {
		if(obj.hasChildNodes) {
			var objLen = obj.childNodes.length;
			for(var a=0; a < objLen; a++) {
				obj.removeChild(obj.childNodes[0]);
			}
		}
	}
}

/* 
* function removeObject(string)
* Remove an object from the DOM entirely
* @param	id			ID of element to be removed
*/
function removeObject(id)
{
	var x = getObject(id);
	if(x != null)
	{
		removeChildren(x);
		x.parentNode.removeChild(x);
		x.innerHTML = '';
	}
}

/*
fetch_item_data()
-----------------------------------------------------------------------
Retrieve XML data via XMLHttpRequest objecet
@return		null
*/
function fetch_before_after(category, uniq_id)
{
	// Clear product options table + reset counter
	//var product_options = getObject("product_options_area");
	//removeChildren(product_options);
	var url = '/before_after_xml.php?category=' + escape(category) + '&item_num=' + escape(uniq_id);
	if (http_req) { 
		http_req.open('GET', url, true); 
		http_req.onreadystatechange = fetch_before_after_data;	// fxn: Fetch item details
		http_req.send(null); 
	}
	else
		alert("Cannot connect to page. Server might be busy or javascript has been disabled");
}

/*
fetch_item_data_xml()
-----------------------------------------------------------------------
Fetch XML data and load into designated IDs on page
@return		null
*/
function fetch_before_after_data()
{
	if (http_req.readyState == 4)
	{ 	
		if(http_req.status == 200 || http_req.status == 304)
		{
			var xmlDoc = http_req.responseXML;
			var results = xmlDoc.getElementsByTagName("results").item(0);
			var node_value;
			
			// Fetch info
			node_value = getElementText('', 'title', results, 0);
			if(temp = getObject('disp_title'))
			{
				temp.innerHTML = node_value;
			}			
			node_value = getElementText('', 'client_profile', results, 0);
			if(temp = getObject('disp_client_profile'))
			{
				temp.innerHTML = node_value;
			}
			
			node_value = getElementText('', 'procedure_page', results, 0);
			if(temp = getObject('disp_link'))
			{
				temp.innerHTML = 'For more information, <strong><a href="' + node_value + '" >click here</a></strong>';
			}
			
			/* 
			Clear the buttons before we do it
			Make sure we insert the category as well as the item_num
			*/
			// Do the next/previous buttons
			if(temp = getObject('disp_button_next'))
			{
				node_value = getElementText('', 'next_item', results, 0);
				cat_value = getElementText('', 'category', results, 0);
				temp.className = 'faux_nav_button';
				temp.innerHTML = '<a href="#" onclick="fetch_before_after(\'' + cat_value + '\', \'' + node_value + '\')">Next ></a>';
				
			}
			
			// Do the next/previous buttons
			if(temp = getObject('disp_button_previous'))
			{
				node_value = getElementText('', 'prev_item', results, 0);
				cat_value = getElementText('', 'category', results, 0);
				temp.className = 'faux_nav_button';
				temp.innerHTML = '<a href="#" onclick="fetch_before_after(\'' + cat_value + '\', \'' + node_value + '\')">< Previous</a>';
				
			}
			
			// Do the images
			var xml_images = new Array('before_pic1','before_pic2','after_pic1','after_pic2');
			for(var i = 0; i < xml_images.length; i++)
			{
				var picture = results.getElementsByTagName(xml_images[i]).item(0);
				var filename,img_height,img_width,new_link;
				if(temp = getObject('disp_' + xml_images[i]))
				{
					removeChildren(temp);
					filename = getElementText('', 'filename', picture, 0);
					thumb = getElementText('', 'thumbnail', picture, 0);
					img_height = getElementText('', 'height', picture, 0);
					img_width = getElementText('', 'width', picture, 0);
					var oThumb = createBeforeAfterImage(thumb,img_height,img_width);
					//new_link = '<a href="" onclick="NewWindow(this.href,\'popup\',\'300\',\'225\',\'yes\'); return false;">' + temp + '</a>';
					//temp.innerHTML = new_link;
					new_link  = document.createElement('A');
					new_link.href = '/before_after_pics/' + filename;
					new_link.onclick = function() {NewWindow(this.href, 'popup','300','225','yes'); return false;}	
					new_link.appendChild(oThumb);
					temp.appendChild(new_link);				
					
					
				}
				
			}
			/*
			
			
			
			*/
			// Show shipping options	
			// NOTE: This needs to be added before the checkboxes are loaded
			// otherwise they won't be checked in IE
			/*
			var copy = getObject('prototype_ship_options');
			removeChildren(getObject('ship_options_area'));
			var ship = copy.cloneNode(true);
			ship.id = 'test';
			ship.style.display = 'block';
			getObject('ship_options_area').appendChild(ship);
			
			// Update globals vars to enabled/disable shipping options
			var s_opt_1 = getElementTextNS("", 'shipOption1Enabled', productNode[index], 0);
			var s_opt_2 = getElementTextNS("", 'shipOption2Enabled', productNode[index], 0);
			ship_opt1_enabled = (s_opt_1 == 1)? true : false;
			ship_opt2_enabled = (s_opt_2 == 1)? true : false;
			*/
			//  Per product

			// Product Image for item_edit
			/*
			var product_img_id = getObject('product_img');
			removeChildren(product_img_id);
			image_name = getElementTextNS("", 'image', productNode[index], 0);
			if(image_name.length && document.images)
			{
				var image_path = getElementTextNS("", 'imagePath', productNode[index], 0);
				var image_x = parseInt(getElementTextNS("", 'imageWidth', productNode[index], 0));
				var image_y = parseInt(getElementTextNS("", 'imageHeight', productNode[index], 0));
				var thumb_max = 125;
				var scale = 0;
				var product_img = document.createElement('img');
				product_img.alt = 'Loading';
				product_img.src = image_path;
				product_img.border = 1;
				product_img.borderColor = "#000000";
				if((image_x > image_y) && (image_x > thumb_max))
				{
					scale = thumb_max/image_x;
					image_y = parseInt(scale*image_y);
					image_x = thumb_max;
				}
				else if(image_y > thumb_max)
				{
					scale = thumb_max/image_y;
					image_x = parseInt(scale*image_x);
					image_y = thumb_max;
				}
				product_img.width = image_x;
				product_img.height = image_y;
				
				product_img_id.appendChild(product_img);
			}
			*/	     			
		}
	}
}

function createBeforeAfterImage(thumb, height, width, alt)
{
	
	var img = document.createElement('img');
	img.alt = alt;
	img.src = '/before_after_pics/' + thumb;
	img.border = 0;
	img.height = height;
	img.width = width;
	
	return img;
}


