var w;
function add_enquiry(){
	if(check_cookies()){
		if(w!=undefined && !w.closed){
			w.close();
		}
		var width = 355;
		var height = 215;
		var left = ((screen.width)-width)/4;
		var top = ((screen.height)-height)/2;
		w = window.open("/add_enquiry.htm","myenquiry","status=no,menubar=no,resizable=yes,location=no,toolbar=no,scrollbars=yes,width="+width+", height="+height+",screenX="+left+",screenY="+top+",top="+top+",left="+left);
		if(w!=undefined){
			w.focus();
		}
	} else {
		window.location.href='cookies.php';
	}
}
function my_enquiry(){
	if(w!=undefined && !w.closed){
		w.close();
	}
	
	if(get_item_count()==false){ // no items - form will be general enquiry which is taller
		var height = 365;
		var width = 355;
		var left = ((screen.width)-width)/4;
		var top = ((screen.height)-height)/2;
		w = window.open("/mailer.php","myenquiry","status=no,menubar=no,resizable=yes,location=no,toolbar=no,scrollbars=yes,width="+width+", height="+height+",screenX="+left+",screenY="+top+",top="+top+",left="+left);
	} else {
		var height = 215;
		var width = 355;
		var left = ((screen.width)-width)/4;
		var top = ((screen.height)-height)/2;
		w = window.open("/show_enquiry.htm","myenquiry","status=no,menubar=no,resizable=yes,location=no,toolbar=no,scrollbars=yes,width="+width+", height="+height+",screenX="+left+",screenY="+top+",top="+top+",left="+left);
	}
	if(w!=undefined){
		w.focus();
	}
}

function PortItem(code, name, notes){
	this.code = code;
	this.name = name;
	this.notes = notes;
}
function PortItem_serialize(){
	// serialize to cookie
	//var nextmonth = new Date();
	//nextmonth.setMonth(nextmonth.getMonth()+1);
	var cookieStr = "pl_item"+escape(this.code)+"="+(escape(this.name).length)+"-"+escape(this.name)+"-"+(escape(this.notes).length)+"-"+escape(this.notes);
	document.cookie = cookieStr;
}
PortItem.prototype.serialize = PortItem_serialize;

function PortItem_unserialize(){
	// unserialize from cookie
	var allcookies = document.cookie;
	var pos = allcookies.indexOf("pl_item"+escape(this.code)+"=");
	
	if(pos!=-1){ // if we find the cookie, extract its data into our object
		var start = pos+(escape(this.code).length+8);
		var end = allcookies.indexOf("-", start);
		var name_len = parseInt(allcookies.substring(start,end));
		start = end+1;
		end=start+name_len;
		this.name = unescape(allcookies.substring(start,end));
		start=end+1;
		end = allcookies.indexOf("-", start);
		var notes_len = parseInt(allcookies.substring(start,end));
		start = end+1;
		end = start+notes_len;
		this.notes = unescape(allcookies.substring(start,end));
	} else { // else return false - no such cookie existed
		return false;
	}
}
PortItem.prototype.unserialize = PortItem_unserialize;

function PortItem_destroy(){
	// destroy the cookie
	var kill_time = new Date("January 1, 1970");
	var kill_string = "pl_item"+escape(this.code)+"=null; expires="+kill_time.toGMTString();
	document.cookie = kill_string;
}
PortItem.prototype.destroy = PortItem_destroy;

function getPortItems(){
	var allcookies = document.cookie;
	var pos = allcookies.indexOf("pl_item");
	if(pos!=-1){
		var port_list = Array();
		while(pos!=-1){
			var start = pos+7;
			var end = allcookies.indexOf("=", start);
			var pitem = new PortItem(unescape(allcookies.substring(start,end)),'','');
			pitem.unserialize();
			port_list.push(pitem);
			var pos = allcookies.indexOf("pl_item",start);
		}
		return port_list;
	} else { // no cookies
		return false;
	}
}

// function to return get params
function parseQuery ( query ) { 
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

// function to check opener exists
function openerAlive(){
	if(window.opener!=null && window.opener.closed){ // ie
		return false;
	} else { // ns
		if(window.opener==null){
			return false;
		}
	}
	return true;
}

// function to update items count
function update_item_count(){
	var count_element = findObj('itemcount');
	if(count_element!==false){
		var port_item_count = get_item_count();
		if(port_item_count == false){
			count_element.innerHTML='My&nbsp;enquiry';
		} else {
			count_element.innerHTML='<b>My&nbsp;enquiry</b>&nbsp;[<span class="small">'+port_item_count +'</span>]';
		}
	}
}

function get_item_count(){
	var port_items = getPortItems();
	if(port_items!=undefined && port_items!=false){
		return port_items.length
	}
	return false;
}

function product_in_basket(pcode){
	if(port_items!=undefined){
		for(var i=0; i<port_items.length; i++){
			if(port_items[i].code==pcode){
				return true;
			}
		}
		return false;
	}
}

function check_cookies(){
	var MyStringLength;
	document.cookie = 'PortlantisCookieTest';
	MyStringLength = document.cookie.length;
	if(document.cookie.substring(MyStringLength-20,MyStringLength)=='PortlantisCookieTest' || document.cookie.indexOf('PortlantisCookieTest')!=-1){ 
		return true;
	} else { 
		return false; 
	}
}
function resizeOuterTo(w,h) {
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			top.outerWidth=w;
			top.outerHeight=h;
		} else top.resizeTo(w,h);
	}
}
