﻿
var DB = {};

DB.isFlashMenuEnabled = true;

DB.Utils = {

	renderMenu: function(movie, width, height, flashvars, requiredversion, movieid){
		if(DB.isFlashMenuEnabled){
			document.write('<div id="flashmenu">' + getFlashHtml(movie, width, height, flashvars, requiredversion, movieid) + '</div>');
		}
	}

}

var Search = {

  _inited: false,

  start: function(){
    var q = $('q').value;
    if(q != ''){
      window.location = '/search.aspx?q=' + q; 
    }
  },

  focus: function(el){
    el.style.color = '#000'; 
    if(!Search._inited){
      el.value = '';
      Search._inited = true;
    }
  },
  
  blur: function(el){
    el.style.color = '#CCC';
  },
  
  keyPressed: function(e){
    if(!e){ var e = window.event; }
    var code = (e.keyCode) ? e.keyCode : e.which;
    if(code == 13){ 
      Search.start();
      return false;
    }
  }
  
}

printPage = function(){
  window.print();
}

var Send = {

  display: function(){
    var div = document.createElement('div');
    div.setAttribute('id', 'sendtoafriend');
    var html = [];
    html.push('<h3>Share this page with a friend </h3>');
    html.push('<form id="sendtoafriendform" action="/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 + '" /></div>');
    html.push('</form>');
    div.innerHTML = html.join('');
    document.getElementsByTagName('body')[0].appendChild(div);
		Form.focusFirstElement('sendtoafriendform');
		
  },
  
  close: function(){
		el = $('sendtoafriend');
		if(el) 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){
			Element.update(el, '<h3>' + response.responseText + '</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);
	}

  
}

var Tracking = {

	onClick: function(typeid, brandid, accountid, url){
		new Ajax.Request('/ajax/brands.aspx', {
				parameters: 'typeid=' + typeid + '&brandid=' + brandid + '&accountid=' + accountid + '&url=' + escape(url)
			})
	},
	
	go: function(url){
		new Ajax.Request('/ajax/referrals.aspx', {	parameters: 'url=' + escape(url) })
	}
	
}

