




function getObjectDetails(obj, param, subparam) {
	
	var str = "";
	for(sProperty in obj) {
		if(sProperty == param) {			
			var subobj = obj[sProperty];			
			for(subProperty in subobj) {
				if(subProperty == subparam) {
					subsubobj = subobj[subProperty];
					for(subSubProperty in subsubobj) {
						str += "\nsubparam: " + subSubProperty + " = " + subsubobj[subSubProperty] + "\n";
					}
				} else {
					str += "\nparam: " + subProperty + " = " + subobj[subProperty] + "\n";	
				}
			}
			
		} else {
			str += sProperty + " = "  + obj[sProperty] + "\n";
		}		
	}
	//alert(str);
	return str;
}
// JavaScript Document
function openWindow(url, title, w, h) { 
	var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    winprops = 'height=' + h + ',width=' + w + ',top='+wint+',left='+winl+'resizable=0';
	window.open(url, title, winprops);
} 

function openWindow2(title, message, w, h) { 
	var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    winprops = 'height=' + h + ',width=' + w + ',top='+wint+',left='+winl+'resizable=0';
	win = window.open("", title, winprops);
	win.document.write(message);
	win.focus();	
} 


Event.observe(window, 'load', init, false);


function init() {
	//alert(Event.observers);
	if($('typesel')) {
		Event.observe('typesel', 'change', function() {
			var item = typearr.get($('img').itemid);
			item.setActiveType($('typesel').value);
			$$('form')[0].item_number.value = item.getItemCode();
			$$('form')[0].item_name.value = item.getItemName() + " - " + item.getTypeName();
			$$('form')[0].amount.value = item.getPrice();
			var str = "";
			//$$('form')[0].each(function(node) {
			//	str += node.value + "\n";
			//});
			for(var i=0; i<$$('form')[0].length; i++) {
				str += $$('form')[0][i].name +  " = " + $$('form')[0][i].value + "\n";
			}
			alert(str);
		}, false);
	} 
	//if($$('form')[0]) {
		//Event.observe($$('form')[0], 'submit', askLocation, false);
	//}
}


function changeItem(item) {	
	if(typeof(item) == 'undefined') {
		typearr.moveFirst();
		if(typearr.next()) {
			item = typearr.getValue();
		}
	}
	
	// this is because the main page of the gallery will complain about the item.setAttribute() line
	// we should probably include this function or page only in the galleries it self and not the mainpage
	if(document.location.href == "http://localhost/sites/Nomad%20Gifts/index.php?page=7" || 
		document.location.href == "http://www.nomadgifts.com/index.php?page=7") {
		return;
	}
	img = document.createElement("img");
	img.setAttribute("src", item.getPicturePath());
	img.setAttribute("id", "img");
	
	var width = item.getPictureWidth();
	var height = item.getPictureHeight();
	
	img.setAttribute("width", width);
	img.setAttribute("height", height);
	img.setAttribute("itemid", item.getItemID());	

	if(!$('img')) {
		if($('mainpic')) {
			$('mainpic').appendChild(img);		
		}
	} else {
		$('mainpic').replaceChild(img, $('img'));	
	}

	
	
	typeselold = $('typesel');
	typeselnew = document.createElement("select");
	typeselnew.setAttribute("id", "typesel");
	
	types = item.getTypes();
	prices = item.getPrices();
	prices2 = item.getPrices2();
	price_arr = item.price_arr;
	types.moveFirst();
	// the price_arr stuff is that we need the price of the type, but we
	// haven't got access to the typeid, maybe there is a way, until then...
	var i=-1;
	while(types.next()) {
		i++;
		var opt = new Option();
		opt.setAttribute("value", types.getKey());
		var text = document.createTextNode(item.getItemName() + " - " + types.getValue() + " - $" + price_arr[i][0]);
		opt.appendChild(text);
		typeselnew.appendChild(opt);
	}
	
	updatePaypal(item.getItemCode(), item.getItemName() + " - " + item.getTypeName(), prices2.get(item.getTypeID()));
			
	
	
	try {// 'paypalrow'
		$('typeseldiv').replaceChild(typeselnew, typeselold);	
	} catch(e) {
		try {
			$('typeseldiv').appendChild(typeselnew);		
		} catch(ee) {
			
		}
	}
	
	//typeselnew.setAttribute("onchange", "updatePaypal('" + item.getItemCode() + '", "' + 
	//	item.getItemName() + " - " + item.getTypeName() + "', '" + prices2 + "')");
	
	// adding atrtibute onchange to select works on firefox but not in ie, that's why I add it to event object
	Event.observe(typeselnew, 'change', 
		function() { 
			item.setActiveType($('typesel').value);	
			updatePaypal(item.getItemCode(), item.getItemName() + " - " + item.getTypeName(), prices2.get(item.getTypeID())); 
			
			//alert(item.getTypeID());
		}, 
	false);
	
	
	
	
	
}
/*
function askLocation(event) {
	// first check from session that if the user already ansered this question
	shipping_location = prompt("Are you shipping outside Israel?", "no");
	shipping_location = 
	Event.stopObserving($$('form')[0], 'submit', askLocation, false);
	$$('form')[0].submit();
	
}*/

function updatePaypal(item_number, item_name, prices) {	
	//alert(prices);
	amount = prices[0];
	shipping = prices[1];
	shipping2 = prices[2];
	$$('form')[0].item_number.value = item_number;
	$$('form')[0].item_name.value = item_name;	
	$$('form')[0].amount.value = amount;
	//$$('form')[0].shipping.value = shipping;
	//$$('form')[0].shipping2.value = shipping2;
	//form = $$('form')[0];
	//var str = $F('item_number') +  " " + $F('item_name') +  " " + F('amount') + " " + $F('shipping') + " " + $F('shipping2');
	/*str = "";
	if($F('item_number')) {
		str = $F('item_number') +  " || " + $F('item_name') +  " || " + $F('amount') + " || " + $F('shipping') + " || " + $F('shipping2');
	}
	*/
	//alert(str);
	
}

//chk if an object is an array or not.
function isArray(obj) {
	//returns true is it is an array
	if (obj.constructor.toString().indexOf("Array") == -1) {
		return false;
	} else {
		return true;
	}
}

function inArray(needle, haystack) {
	var n = haystack.length;
	for (var i=0; i<n; i++) {
		if (haystack[i] == needle) {
			return true;
		}
	}
	return false;
}




Item.prototype.itemid = null;
Item.prototype.itemname = null;
Item.prototype.types = null;
Item.prototype.prices = null;
Item.prototype.prices2 = null;
Item.prototype.price_arr = null;
Item.prototype.activeType = null;
Item.prototype.picturePath = null;
Item.prototype.pictureWidth = null;
Item.prototype.pictureHeight = null;

function Item(itemid, itemname){
	this.itemid = itemid;
	this.itemname = itemname;
	this.types = new HashTable();
	this.prices = new HashTable();
	this.prices2 = new HashTable();
	this.price_arr = new Array();
}

Item.prototype.addType = function(typeid, typename, price, shipping, shipping2, setActive) {
	
	this.types.put(typeid, typename);
	if(price) {
		this.prices.put(typeid, price);
		//alert("addming price: " + price);
		this.price_arr.push(new Array(price, shipping, shipping2));
		this.prices2.put(typeid, new Array(price, shipping, shipping2));
	}	
	// set the active type if there is none active yet
	// also one can set *this* type active with the setActive booleanm value
	if(!this.activeType || setActive) {
		this.setActiveType(typeid);
	}
}


Item.prototype.getTypes = function() {
	return this.types;
}


Item.prototype.getPrices = function() {
	return this.prices;
}

Item.prototype.getItemID = function() {
	return this.itemid;
}

Item.prototype.setItemID = function(itemid) {
	this.itemid = itemid;
}

Item.prototype.getItemName = function() {
	return this.itemname;
}

Item.prototype.setItemName = function(itemname) {
	this.itemname = itemname;
}

Item.prototype.getTypeID = function() {
	return this.activeType;
}

Item.prototype.getTypeName = function() {
	return this.types.get(this.activeType);
}

Item.prototype.setActiveType = function(typeid) {
	this.activeType = typeid;
}

Item.prototype.getPrice = function() {
	return this.prices.get(this.activeType);
}

Item.prototype.getPrices2 = function() {
	return this.prices2; //.get(this.activeType);
}



Item.prototype.setPrice = function(typeid, price) {
	// what if the typeid exeist already?
	this.prices.put(typeid, price);
}

Item.prototype.setPicture = function(path, width, height) {
	this.picturePath = path;
	this.pictureWidth = width;
	this.pictureHeight = height;
}
Item.prototype.getPicturePath = function() {
	return this.picturePath;
}
Item.prototype.getPictureWidth = function() {
	return this.pictureWidth;
}
Item.prototype.getPictureHeight = function() {
	return this.pictureHeight;
}

Item.prototype.getItemCode = function() {
	return this.getItemID() + "-" + this.getTypeID();
};


/*******************************************************************************************
 * Object: HashTable
 * Description: Implementation of HashTable
 * Author: Uzi Refaeli
 *******************************************************************************************/

//======================================= Properties ========================================
HashTable.prototype.hash	 	= null;
HashTable.prototype.keys		= null;
HashTable.prototype.location	= null;

/**
 * HashTable - Constructor
 * Create a new HashTable object.
 */
function HashTable(){
	this.hash = new Array();
	this.keys = new Array();

	this.location = 0;
}

/**
 * put
 * Add new key
 * param: key - String, key name
 * param: value - Object, the object to insert
 */
HashTable.prototype.put = function (key, value){
	if (value == null)
		return;

	if (this.hash[key] == null)
		this.keys[this.keys.length] = key;

	this.hash[key] = value;
}

/**
 * get
 * Return an element
 * param: key - String, key name
 * Return: object - The requested object
 */
HashTable.prototype.get = function (key){
		return this.hash[key];
}

/**
 * remove
 * Remove an element
 * param: key - String, key name
 */
HashTable.prototype.remove = function (key){
	for (var i = 0; i < this.keys.length; i++){
		//did we found our key?
		if (key == this.keys[i]){
			//remove it from the hash
			this.hash[this.keys[i]] = null;
			//and throw away the key...
			this.keys.splice(i ,1);
			return;
		}
	}
}

/**
 * size
 * Return: Number of elements in the HashTable
 */
HashTable.prototype.size = function (){
    return this.keys.length;
}

/**
 * populateItems
 * Deprecated
 */
HashTable.prototype.populateItems = function (){}

/**
 * next
 * Return: true if theres more items
 */
HashTable.prototype.next = function (){
	if (++this.location < this.keys.length)
		return true;
	else
		return false;
}

/**
 * moveFirst
 * Move to the first item.
 */
HashTable.prototype.moveFirst = function (){
	try {
		this.location = -1;
	} catch(e) {/*//do nothing here :-)*/}
}

/**
 * moveLast
 * Move to the last item.
 */
HashTable.prototype.moveLast = function (){
	try {
		this.location = this.keys.length - 1;
	} catch(e) {/*//do nothing here :-)*/}
}

/**
 * getKey
 * Return: The value of item in the hash
 */
HashTable.prototype.getKey = function (){
	try {
		return this.keys[this.location];
	} catch(e) {
		return null;
	}
}

/**
 * getValue
 * Return: The value of item in the hash
 */
HashTable.prototype.getValue = function (){
	try {
		return this.hash[this.keys[this.location]];
	} catch(e) {
		return null;
	}
}

/**
 * getKey
 * Return: The first key contains the given value, or null if not found
 */
HashTable.prototype.getKeyOfValue = function (value){
	for (var i = 0; i < this.keys.length; i++)
		if (this.hash[this.keys[i]] == value)
			return this.keys[i]
	return null;
}


/**
 * toString
 * Returns a string representation of this HashTable object in the form of a set of entries,
 * enclosed in braces and separated by the ASCII characters ", " (comma and space).
 * Each entry is rendered as the key, an equals sign =, and the associated element,
 * where the toString method is used to convert the key and element to strings.
 * Return: a string representation of this HashTable.
 */
HashTable.prototype.toString = function (){

	try {
		var s = new Array(this.keys.length);
		s[s.length] = "{";

		for (var i = 0; i < this.keys.length; i++){
			s[s.length] = this.keys[i];
			s[s.length] = "=";
			var v = this.hash[this.keys[i]];
			if (v)
				s[s.length] = v.toString();
			else
				s[s.length] = "null";

			if (i != this.keys.length-1)
				s[s.length] = ", ";
		}
	} catch(e) {
		//do nothing here :-)
	}finally{
		s[s.length] = "}";
	}

	return s.join("");
}

/**
 * add
 * Concatanates HashTable to another HashTable.
 */
HashTable.prototype.add = function(ht){
	try {
		ht.moveFirst();
		while(ht.next()){
			var key = ht.getKey();
			//put the new value in both cases (exists or not).
			this.hash[key] = ht.getValue();
			//but if it is a new key also increase the key set
			if (this.get(key) != null){
				this.keys[this.keys.length] = key;
			}
		}
	} catch(e) {
		//do nothing here :-)
	} finally {
		return this;
	}
};



//Disable right mouse click Script
//By Maximus (maximus@nsimail.com) w/ mods by DynamicDrive
//For full source code, visit http://www.dynamicdrive.com

var message="Please contact us if you wish pictures";

///////////////////////////////////
function clickIE4(){
	if (event.button==2){
		alert(message);
		return false;
	}
}

function clickNS4(e){
	if (document.layers||document.getElementById&&!document.all){
		if (e.which==2||e.which==3){
			alert(message);
			return false;
		}
	}
}

if (document.layers){
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown=clickNS4;
} else if (document.all&&!document.getElementById){
	document.onmousedown=clickIE4;
}

document.oncontextmenu=new Function("alert(message);return false")





/**
 * Validate a form.
 * First arequment: the name of the form
 * Second - nth argument: required field.
 */
function validateForm(formid, req_fields) {
	for(i=0; i<req_fields.length; i++) {
		if(!$F(req_fields[i])) {
			alert("Fill all the fields marked with asterisk (*)");
			$(req_fields[i]).focus();
			return false;
		} else {
		
			if(req_fields[i] == "email") {
				if(!echeck($(req_fields[i]).value)) {
					$(req_fields[i]).focus();
					return false;
				}
			}
		}
	}
	return true;
}


/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {

	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   alert("Invalid E-mail ID 0");
	   return false;
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid E-mail ID 1");
	   return false;
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("Invalid E-mail ID 2");
		return false;
	}
	
	 if (str.indexOf(at,(lat+1))!=-1){
		alert("Invalid E-mail ID 3");
		return false;
	 }
	
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("Invalid E-mail ID 4");
		return false;
	 }
	
	 if (str.indexOf(dot,(lat+2))==-1){
		alert("Invalid E-mail ID 5");
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert("Invalid E-mail ID 6");
		return false;
	 }
	
	 return true;					
}

function ValidateForm(){
	var emailID=document.frmSample.txtEmail
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
 


