﻿/********************************************************************************
 * ** Global **
 *******************************************************************************/
var Page = {}
Page.url = '';
Page.isxhtml = false;
Page.loader = '<p><img src="/lib/img/icons/16/loader.gif" alt="" style="vertical-align:middle;" /> Loading... </p>';
/********************************************************************************/
Page.BaseObject = {
	isInited: false,
	init: Prototype.emptyFunction	
}
/********************************************************************************/
Page.Common = {
  
  togglelist: function(a, id) {
    if(Element.visible(id)){ 
      a.innerHTML = '+';
      Element.hide(id);
    }
    else{
      a.innerHTML = '-';
      Element.show(id);
    }
  },
  
  toggle: function(id){
    (Element.visible(id)) ? Element.hide(id) : Element.show(id);
  }
  
}
/********************************************************************************/
Page.MessageBox = {

  create: function(id){
    var div = document.createElement('div');
    div.setAttribute('id', id);
    document.getElementsByTagName('body')[0].appendChild(div);  
    this.div = div;
  },
    
  setContent: function(content){
    Element.update(this.div, content);
  },
  
  setMessage: function(message, load){
    if(load)
      Element.update(this.div, '<h3><img src="/lib/img/icons/20/loader.gif" alt=""/>' + message + '...</h3>');
    else
      Element.update(this.div, '<h3>' + message + '</h3>');
    this.div.style.height = '60px';      
  },
  
  getForm: function(){
		return this.div.getElementsByTagName('form')[0];
	},

  close: function(){
	  Element.remove(this.div);
  } 
}
/********************************************************************************/
Page.Dialog = {

	load: function(params, title){
	  Element.update('dialogmessage', '');
		Element.show('dialogcontainer');
		Page.Dialog.resetStatus();
		new Ajax.Request(Page.url, {
			parameters: params,
			onSuccess: function(response){
					Page.Dialog.open(title);
					Page.Dialog.setContent(response.responseText);
					$('dialog').focus();
			},
			onFailure: function(response){ 
					//Page.Debug.text(response.responseText) 
				}
		});				
	},

	save: function(options){
		new Ajax.Request(Page.url, {
			parameters: Form.serialize(Page.Dialog.getForm()),
			onSuccess: function(response){
			  var r = new Page.Response(response);
				if(r.isSuccess()){
					Page.Dialog.close();
					Page.Dialog.setStatus(r.getMessage());
					if(options) (options.onSuccess || Prototype.emptyFunction)(response);
				}
				else{
	 				Page.Dialog.setMessage(r.getErrorList());
					if(options) (options.onFailure || Prototype.emptyFunction)(response);
				}
		  },
			onFailure: function(response){
				Page.Dialog.setMessage('Error occured while communicating with server.')
				//Page.Debug.text(response.responseText);
			}
		});
		return false;	
	},

	open: function(title){
		Page.Dialog.setTitle(title);
		var bg = $('dialogcontainer');
		var dialog = $('dialog');
		var client = window.innerHeight || document.documentElement.clientHeight;
		var full = document.documentElement.scrollHeight;
		var top = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
		var height = (client > full) ? client : full;
		
		bg.style.height = height + 'px';
		dialog.style.top = (top + 50) + 'px';
		Element.show(bg);
		Element.show(dialog);
		setTimeout(function(){ window.scroll(0, top); }, 10);
		
	},
	
	close: function(){
		Page.Dialog.reset();
		Element.hide('dialogcontainer');
		Element.hide('dialog');
	},
	
	reset: function(){
		Page.Dialog.setTitle('');
		Page.Dialog.setContent('');
		Element.hide('dialogmessage');
	},
	
	setMessage: function(message){
		var el = $('dialogmessage');
		Element.update(el, message)
		Element.show(el);
	},

	setTitle: function(title){
		Element.update('dialogtitle', title);
	},
	
	setContent: function(content){
		Element.update('dialogcontent', content)
		form = Page.Dialog.getForm();
		if(form){
			var onsubmit = form.onsubmit;
			if(typeof(onsubmit) == 'function')
				form.onsubmit = function(){ onsubmit(); return false; }
		}
	},
	
	getForm: function(){
		return $('dialogcontent').getElementsByTagName('form')[0];
	},
	
	setStatus: function(message){
		var el = $('status');
		Element.update(el, message);
		Element.show(el);
		Page.Dialog.changeOn = new Date();
		setTimeout('Page.Dialog.hideStatus()', 3000);
	},
	
	hideStatus: function(){
		if((new Date() - Page.Dialog.changeOn) > 2799){
			Page.Dialog.resetStatus();
		}
	},
	
	resetStatus: function(){
		var el = $('status');
		Element.update(el, '');
		Element.hide(el);
	}
		
}
/********************************************************************************/
Page.Response = Class.create();
Page.Response.prototype = {

	initialize: function(response){
		this.response = response;
		this.xml = response.responseXML;
		if(Page.isxhtml){
		  this.xhtml = response.responseXML.lastChild;
		  this.ie = (this.xhtml.childNodes.length == 2);
		  if(this.ie)
		    this.div = this.xhtml.lastChild.firstChild; //*IE6 Fix*
		  else
  		  this.div = this.xhtml.childNodes[3].childNodes[1]; //FireFox 1.x		
		}
	},
	
	isSuccess: function(){
		return this.getStatus() == 1;
	},

	getStatus: function(){
		if(Page.isxhtml){
		  if(this.ie)
  		  return Number(this.div.getElementsByTagName('input')[0].attributes[2].value);
  		else
  		  return Number(this.div.getElementsByTagName('input')[0].value);
		}else 
		  return Number(this.xml.lastChild.getAttribute('status'));
	},
	
	getHTML: function(){
	  if(this.xml){
	    var nodes = this.xml.lastChild.childNodes;
	    for(var i=0; i<nodes.length; i++){
	      if(nodes[i].nodeName == 'html') return nodes[i].firstChild.nodeValue;
	    }
	  }
	},
	
	getMessage: function(){
		if(Page.isxhtml){
		  if(this.ie)
		    return this.div.getElementsByTagName('input')[1].attributes[2].value;
		  else
  		  return this.div.getElementsByTagName('input')[1].value; 
		}else
		  return this.xml.lastChild.getAttribute('message');
	},
	
	getErrors: function(){
		if(Page.isxhtml)
  		return $A(this.div.getElementsByTagName('ul')[0].getElementsByTagName('li'));
		else{
  		var errors = new Array();
			var nodes = this.xml.lastChild.childNodes;
			for(var i = 0; i < nodes.length; i++){
				if(nodes[i].nodeName == 'errors'){
					for(var k = 0; k < nodes[i].childNodes.length; k++){
						if(nodes[i].childNodes[k].nodeName == 'error') errors.push(nodes[i].childNodes[k].getAttribute('message'));
					}
				}
			}
		  return errors;
		}
	},
	
	getErrorsText: function(){
		var list = new Array();
		this.getErrors().each(function(error){
		  list.push(' - ' + error.innerHTML);
		});
		return list.join('\n');
	},
	
	getErrorList: function(){
		var errors = this.getErrors();
		var list = new Array();
		var ie = this.ie;
		this.getErrors().each(function(error){
		  if(Page.isxhtml){		  
		    if(ie)
  			  list.push('<li>' + error.firstChild.nodeValue+ '</li>');		 
  			else		      
  			  list.push('<li>' + error.innerHTML + '</li>');		 
  		}else
  			list.push('<li>' + error + '</li>');		   		 
		});
		if(list.length == 0)
			list.push('<li style="color:red; font-weight:bold;">' + this.getMessage() + '</li>');
		return '<ul>' + list.join('') + '</ul>';
	}  
	  
}
/********************************************************************************/
Page.Cookies = {

	getCookie: function(name) {
		var start = document.cookie.indexOf(name + "=");
		var len = start + name.length + 1;
		if ((!start) && (name != document.cookie.substring(0, name.length))) { return null; }
		if (start == -1){ return null; }
		var end = document.cookie.indexOf(";", len);
		if (end == -1) end = document.cookie.length;
		return unescape(document.cookie.substring(len, end));
	},

	setCookie: function(name, value, expires, path, domain, secure) {
		var today = new Date();
		today.setTime(today.getTime());
		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" : "");
	},

	deleteCookie: function(name, path, domain) {
		if (Page.Cookies.getCookie(name)){ 
			document.cookie = name + "=" +
				((path) ? ";path=" + path : "") +
				((domain) ? ";domain=" + domain : "") +
				";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		}
	}

} 
/********************************************************************************
 * ** E-mail Page **
 *******************************************************************************/
var Send = {

  display: function(){
    if($('sendtoafriend')) return;
    var div = document.createElement('div');
    div.setAttribute('id', 'sendtoafriend');
    div.style.height = '0';
    div.style.overflow = 'hidden';
    var html = [];
    html.push('<h3>Share this page with a friend </h3>');
    html.push('<form id="sendtoafriendform" action="/shared/pages/discoverboating/sendtofriend.aspx" method="post" onsubmit="return Send.send();">');
    html.push(' <div><label>Your Name:</label> <input type="text" name="name" value="" class="textbox" /></div>');
    html.push(' <div><label>Your Email:</label> <input type="text" name="email" value="" class="textbox" /></div>');
    html.push(' <div><p>&nbsp;</p></div>');
    html.push(' <div><label>Friend\'s Email 1:</label> <input type="text" name="email1" value="" class="textbox" /></div>');
    html.push(' <div><label>Friend\'s Email 2:</label> <input type="text" name="email2" class="textbox" /></div>');
    html.push(' <div><label>Friend\'s Email 3:</label> <input type="text" name="email3" class="textbox" /></div>');
    html.push(' <div style="text-align:center; padding:20px;"><input type="submit" value="Send" class="button" /> <input type="button" value="Cancel" onclick="Send.close();" class="button" /></div>');
    html.push(' <div><input type="hidden" name="page" value="' + document.location + '" /><input type="hidden" name="actiontype" value="page_send" /></div>');
    html.push('</form>');
    div.innerHTML = html.join('');
    document.getElementsByTagName('body')[0].appendChild(div);
    if(Animator){
      new Animator({interval:30}).addSubject(new CSSStyleSubject($('sendtoafriend'), 
          'height:0em;', 
          'height:20em;'
        )).play();
    }
		
  },
  
  close: function(){
		el = $('sendtoafriend');
		if(el){ 
      if(Animator){
        new Animator({interval:30, onComplete: function(){ Element.remove(el) } }).addSubject(new CSSStyleSubject($('sendtoafriend'), 
            'height:20em;', 
            'height:0em;'
          )).play();
      }else{
        Element.remove(el);
      }
    }
  },
	
	send: function(){
    var f = $('sendtoafriendform');
		var errors = [];
		
		if(f.name.value == ''){ errors.push(' - Please enter your name '); }
		if(!Send.isValidEmail(f.email.value)){ errors.push(' - Your email address contains errors '); }
		if(f.email1.value != '' && !Send.isValidEmail(f.email1.value)){ errors.push(' - Your friend\'s email 1 address contains errors '); }
		if(f.email2.value != '' && !Send.isValidEmail(f.email2.value)){ errors.push(' - Your friend\'s email 2 address contains errors '); }
		if(f.email3.value != '' && !Send.isValidEmail(f.email3.value)){ errors.push(' - Your friend\'s email 3 address contains errors '); }
		if(f.email1.value == '' && f.email2.value == '' && f.email3.value == ''){ errors.push(' - At least one friend\'s  email is required '); }
		
		if(errors.length > 0){
			alert(errors.join('\n\n'));
		}else{
			new Ajax.Request(f.action, {
				parameters: Form.serialize(f),
				onSuccess: Send.confirm
			});
			var el = $('sendtoafriend');
			if(el){
				Element.update(el, '<h3>Sending...</h3>');
				el.style.height = '120px';
			}
		}
		return false;			
	},
	
	confirm: function(response){
		var el = $('sendtoafriend');
		if(el){
		  var r = new Page.Response(response);
			Element.update(el, '<h3>' + r.getMessage() + '</h3><div style="text-align:center; padding:20px;"><input type="button" value="OK" onclick="Send.close();" class="button" /></div>');
			el.style.height = '120px';
			setTimeout('Send.close()', 3000);
		}
	},
	
	isValidEmail: function(email){
		return (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/).test(email);
	}  
}
/********************************************************************************
 * ** E-Newsletter **
 *******************************************************************************/
var Enewsletter = {

	signup: function(){
	  var email = $('enewsemail').value;
	  var lastname = $('enewslastname').value;
	  var firstname = $('enewsfirstname').value;
	  var postalcode = $('enewspostalcode').value;
		var errors = [];
		
		if(email == '') { errors.push(' - Enter your email address'); }
		if(email != '' && !Send.isValidEmail(email)){ errors.push(' - Your email address contains errors '); }
		
		if(errors.length > 0){
			alert(errors.join('\n\n'));
		}else{
			new Ajax.Request('/shared/pages/discoverboating/sendtofriend.aspx', {
				parameters: 'actiontype=enewsletter_signup&email=' + email + '&lastname=' + lastname + '&firstname=' + firstname + '&postalcode=' + postalcode,
				onSuccess: Enewsletter.confirm
			});
			var el = $('newsletter-subscription');
			if(el){
				Element.update(el, '<h3><img src="/lib/img/icons/20/loader.gif" alt=""/> Sending Information...</h3>');
			}
		}
		return false;			
	},  

	confirm: function(response){
		var el = $('newsletter-subscription');
		if(el){
		  var r = new Page.Response(response);
			Element.update(el, '<div style="padding: 10px 10px 10px 10px; border-bottom: 1px dotted #CCC;"><p><strong>' + r.getMessage() + '</strong></p></div>');
		}
	}	
  
}
/********************************************************************************
 * ** Search **
 *******************************************************************************/
var SiteSearch = {

  display: function(){
    if($('sitesearch')){ Element.remove('sitesearch'); return; }
    var div = document.createElement('div');
    div.setAttribute('id', 'sitesearch');
    div.style.height = '0';
    var c = [];
    c.push('<form id="sitesearchform" action="" method="post" onsubmit="SiteSearch.search(); return false;">');
    c.push('<div class="find-content"><div class="container">');
    c.push('<h3>Search Content</h3>');
    c.push('<div><input type="text" name="content" id="sitesearch-content" value="" class="textbox" /><input type="submit" value="GO" class="button" /></div>');
    c.push('<p>Enter any term and the search will scan the content on the site to find what you looking for</p>');
    c.push('</div></div>');
    
    c.push('<div class="find-exhibitor"><div class="container">');
    c.push('<h3>Find an Exhibitor</h3>');
    c.push('<div><input type="text" name="exhibitor" id="sitesearch-exhibitor" value="" class="textbox" /><input type="submit" value="GO" class="button" /></div>');
    c.push('<p>Enter the name or part of the name of a company or brand</p>');
    c.push('</div></div>');
    c.push('</form>');

    div.innerHTML = c.join('');
    document.getElementsByTagName('body')[0].appendChild(div);
    if(Animator){
      new Animator({interval:30}).addSubject(new CSSStyleSubject($('sitesearch'), 
          'height:0em;', 
          'height:11em;'
        )).play();
    }
  },
  
  search:function(){
    var q = $('sitesearch-content').value;
    (q != '') ? this.searchSite(q) : this.searchExhibitors($('sitesearch-exhibitor').value);
  },
  
  searchSite: function(q){
    if(q == ''){ alert('Please specify search parameters'); return; }
    document.location = '/attendees/search.aspx?q='+q;
  },
  
  searchExhibitors: function(q){
    if(q == ''){ alert('Please specify search parameters'); return; }
    document.location = '/attendees/directory/exhibitorslist.aspx?searchtype=name&name='+q;
  }

}
