var _base_url = location.protocol+'//fyndes.com/index.php/';

var recipients_list = '';
var portfolios_list = '';

function add_option(){
	var option_el=$('product_options_div');
	var option_count = $('option_cnt').value;
	dtag=document.createElement('div');
	dtag.innerHTML="<div style='float:left;padding-top:8px;'><input onblur=\"$('option_desc').style.display='none';\" onfocus=\"$('option_desc').style.display='block';\" type='text' name='vop"+option_count+"'></div><div style='float:left;padding-left:5px;padding-top:8px;'><input  onblur=\"$('option_help_"+option_count+"').style.display='none';\" onfocus=\"$('option_help_"+option_count+"').style.display='block';\" type='text' name='pop"+option_count+"'><input type='hidden' name='hop"+option_count+"' value=''></div>"+
			"<div style='clear:both'>" +
			"<div style='position:relative;'><div id='option_help_"+option_count+"' style='left:300px;display:none;z-index:10;position:absolute;font-size:11px;background-color:#FFFEAB;padding:2 2 2 2;'>The value you enter here will be added to the base product price.</div></div>"
	option_el.appendChild(dtag);
	$('option_cnt').value=option_count+1;		
}

function get_cookie ( cookie_name ) {
	var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
	
	return results?unescape(results[2]):undefined;
} 

function ShowDetailsBox(i,t,d,p,l,link) {
 	
 	if (p == 'null')
 	{
 		p = '<a class=\'popPrice\' href=\'javascript:ShowLoginBox();\'>Log in</a>'
 	}
 	else
 	{
 		//p = '$' + p;
 	}

        window.location = _base_url + 'p/' + link + '/' + t;
 	return;

 	var myTemplate = new Template('<div style="width:249px;font-size:14px;text-align:left;">'
 		+ '<div style="background: #fff;height:216px;width:249px;" class="button" title="p/#{details}/#{title}"></div>'
 		+ '<div style="margin-top:6px;color:black;font-family:Arial-BoldMT;font-weight:bold;font-size:14px;line-height:14px;'
 		+ 'text-transform:uppercase">#{title}</div><div style="margin-top:3px;font-size:12px;line-height:15px;margin-bottom:10px;'
 		+ 'text-align:justify;color:#848283;">#{description}</div><div style="border-right:1px solid #e7e5e6;width:165px;'
 		+ 'float:left;font-size:12px;line-height:16px;color:#838383;"><strong style="color:black;">Price: #{price}</strong>'
 		+ '<br/>Location: #{location}</div><div style="border-left:1px solid #d2d2d2;padding-left:10px;float:left;font-size:12px;'
	 		+ 'padding-top:17px;padding-bottom:2px"><a style="font-size:12px;color:#E8D576;line-height:13px;font-family:Arial;" href="'
 		+ _base_url + 'p/#{details}/#{title}"'
 		+ '>More Details</a></div></div>');
 	
 	var html = myTemplate.evaluate({img:i,title:t,description:d,price:p,location:l,details:link});
 	
 	LightBox.show(html); 
 	
	$$('div.button[title]').each(function (e) {
		$(e).observe('click', navTo.bind(window, _base_url + $(e).title));
	}); 	
 	
 	new ImagesLoader($$('#lightbox div div div')[0], _base_url+'home/img/249/216/'+i);
}

function ShowDialog(title, text, buttons) {
//	var btn = new Template('<div class="detBtn" style="margin-left:7px;float:right;width:75px" onclick="#{action}">'
//		+ '<div class="round"><div class="round1"></div><div class="round2"></div></div><div class="detBtnCaption">'
//		+ '#{title}</div><div class="round"><div class="round2"></div><div class="round1"></div></div></div>');
	
	var btn = new Template('<div onclick="#{action}" style="margin-left:7px;float:right" class="button button-80-25">#{title}</div>');
	
	var dlg = new Template('<div style="width:336px;text-align:center;margin-left:7px;margin-right:7px;">'
		+ '<div style="font-size:14px;text-align:left;border-bottom:1px solid grey;margin-bottom:15px;">#{title}</div>'
		+ '<div style="float:left;width:48px;height:48px;background: url(release/1_0/images/boxes/close_48x48.gif) 50% 50%;"></div>'
		+ '<div style="float:left;width:275px;margin-left:10px;height:48px;vertical-align:center;text-align:left;color:grey;font-size:14px;line-height:16px;">'
		+ '#{text}</div><div><div>#{buttons}</div></div></div>');
		
	var b = '';
	for(var i=buttons.length;i>0;) {
		--i;
		
		if (buttons[i].action == undefined || buttons[i].action == '') {
			buttons[i].action = 'LightBox.hide();';
		}
		
		b += btn.evaluate(buttons[i]);
	}		
	
	var html = dlg.evaluate({title:title,text:text,buttons:b}); 
	
	LightBox.show(html);
}

function contactDealer(id,un,dn,it,d,p,iu,iid) {
	var tpl = new Template('<div style="width:436px;text-align:left;margin-left:16px;margin-right:16px;margin-bottom:16px;margin-top:28px;padding-top:10px;border-top:1px solid #c3c3c3;">'
		+ '	<form action="' + _base_url + 'dealer/contact" id="contact-dealer" method="post">'
		+ '<input type="hidden" name="dealer_id" value="#{dealer_id}" />'
		+ '<input type="hidden" name="item_id" value="#{item_id}" />'
		+ '<div style="width:267px;padding-right:5px;float:left">'
		+ '<div class="boxLabel" style="margin-top:0px;color:#333;"><strong>From:</strong> '
		+ '<a href="javascript:;" style="color:#E8D576;font-size:14px;">#{user_name}</a></div>'
		+ '<div class="boxLabel" style="margin-top:6px;color:#333;"><strong>To:</strong> '
		+ '<a href="javascript:;" style="color:#E8D576;font-size:14px;">#{dealer_name}</a></div>'
		+ '<div class="boxLabel" style="margin-top:9px;color:#333;font-weight:bold;">Subject:</div>'
		+ '<div class="boxLabel" style="margin-top:3px;color:black;font-weight:bold;">#{item_title}</div>'
		+ '<div class="boxHint" style="margin-top:3px;color:#999;">#{description}</div>'
		+ '<div class="boxLabel" style="margin-top:15px;color:#333;font-weight:bold;">Price:</div>'
		+ '<div class="boxLabel" style="margin-top:3px;color:#333;font-weight:bold;">$#{price}</div></div>'
		+ '<div id="contact-dealer-box-image" style="width:168px;height:146px;float:left;background:no-repeat '
		+ 'url();margin-top:5px;"></div>'
	    + '<div style="width:436px;float:left;">'
	    + '<div class="boxLabel" style="margin-top:9px;color:#333;"><input type="hidden" name="save_msg" value="1" /><input id="send-form-message" type="checkbox" name="save_msg" value="0" checked="checked"/>Save message</div>'
	    + '<div class="boxLabel" style="margin-top:15px;color:#333;font-weight:bold;">Message</div>'
	    + '<textarea name="message" class="boxInput" style="width:436px;" rows="10" cols=""></textarea></div>'
	    + '<div style="margin-top:5px;width:436px;float:left;text-align:right;padding-left:295px;margin-top:24px;">'
//	    + '<div class="detBtn" onclick="document.forms[\'contact-dealer\'].submit();"><div class="round">'
//	    + '<div class="round1"></div><div class="round2"></div></div>'
//	    + '<div class="detBtnCaption">Send Message</div><div class="round"><div class="round2"></div>'
//	    + '<div class="round1"></div></div></div>'
		+ '<div onclick="$(\'contact-dealer\').submit();" class="button button-110-28">Send Message</div>'
	    + '</div></form></div>');

	var items = {user_name:un, dealer_name:dn, dealer_id:id,
		item_title:it, description:d, price:p, image_url:iu, item_id:iid}
		
	LightBox.show(tpl.evaluate(items));
	
	new ImagesLoader('contact-dealer-box-image', _base_url + 'home/img/168/146/' + iu); 
}

function contactDesigner(uid,un,it,dt,e,d,i) {
	
	var tpl = new Template('<div style="width:436px;text-align:left;margin-left:16px;margin-right:16px;margin-bottom:16px;margin-top:28px;padding-top:10px;border-top:1px solid #c3c3c3;">'
		+ '	<form action="' + _base_url + 'dealer/contact" id="contact-dealer" method="post">'
		+ '<div style="width:267px;padding-right:5px;float:left">'
		+ '<div class="boxLabel" style="margin-top:10px;color:#333;"><strong>From:</strong> '
		+ '<a href="javascript:;" style="color:#E8D576;font-size:14px;">#{user_name}</a></div>'
		+ '<div class="boxLabel" style="margin-top:15px;color:#333;"><strong>Subject:</strong> #{item_title}</div>'
		+ '<div class="boxLabel" style="margin-top:15px;color:#333;"><strong>Date:</strong> #{date}</div>'
		+ '<div class="boxLabel" style="margin-top:15px;color:#333;"><strong>Expiry Date:</strong> #{expiry}</div></div>'
		+ '<div id="contact-designer-box-image" class="adsImage" style="width:168px;height:146px;float:left;margin-top:5px;"></div>'
		+ '<div class="boxHint" style="margin-top:3px;color:#333;padding-top:15px;font-size:14px;line-height:16px;">#{description}</div>'
	    + '<div style="width:436px;padding-left:355px;float:left;padding-top:15px;">'
		+ '<div onclick="LightBox.hide();composeMessage(#{user_id}, \'Re: #{item_title}\', 1);" class="button button-80-25">Reply</div>'
	    + '</div></form></div>');

	var items = {user_name:un,item_title:it, date:dt, expiry:e, description:d, user_id:uid}
		
	LightBox.show(tpl.evaluate(items));
	
	if (i != undefined && i != '') {
		new ImagesLoader('contact-designer-box-image', _base_url + 'home/adsi/168/146/' + i);
	} 
}

function TblRowShow(aObj) {
	aObj = $(aObj.parentNode.nextSibling);
	if (aObj.visible()) {
		aObj.hide();
	} else {
		aObj.show();
	}
}
function submit_freight_ship(){
	ansi_date=$('pod').value;
	sel_d = new Date(ansi_date.split('-')[0], Number(ansi_date.split('-')[1]) - 1, ansi_date.split('-')[2])
	today = new Date();
	if(today > sel_d){
		$('message').innerHTML='Date must be in the future.';
		return;
	}
	$('mas-box-form').submit();
}
function ShowFreightShipBox(id,addy,city,state,country,zip,df1,df2){
	var html = '<div class="generalBox"><form action="'+_base_url+'/ordermanager/ship/'+id+'" id="mas-box-form" onsubmit="return false;">'
		+ '<em style="font:8.5pt Arial;color:#996633;">Please select a pickup date</em>'
		+ '<div id="message" class="message"></div><div style="padding-bottom:10px;padding-top:10px;">'
		+ '<input type="hidden" name="id" value="'+id+'"> '
		+ 'Pickup Date:&nbsp;&nbsp;<input type="hidden" id="pod" name="pod" value="'+df1+'">' +
		  '<a class="date_link" id="_pod_link" ' +
		  'onclick="DatePicker.toggleDatePicker(\'pod\')">' +
		  df2+'</a>' +
		  '<div id="_pod_calendar" class="date_picker" style="display:none;z-index:1000;"></div>'
		
	html += '</div><div style="margin-bottom:14px">'
		+ '<div class="buttons">'
//		+ '<div class="detBtn" onclick=""><div class="round"><div class="round1"></div'
//		+ '><div class="round2"></div></div><div class="detBtnCaption">Ship</div><div class="round">'
//		+ '<div class="round2"></div><div class="round1"></div></div></div>'
		+ '<div onclick="submit_freight_ship();" style="margin-left:100px;" class="button button-110-28">Ship</div>'
		+ '</div></form>' +
				'<div>' +
				'<h2>Pickup Location:' +
				'<div style="padding-left:20px">' + addy+
				'<div>' + city + ", "+state +" "+ zip
				'</div></div>' +
				'</div></div>';
		
	LightBox.show(html);		
}

function ShowMarkAsShippedBox(id){
	
	var html = '<div class="generalBox"><form action="'+_base_url+'/ordermanager/mark_as_shipped/'+id+'" id="mas-box-form" onsubmit="return false;">'
		+ '<em style="font:8.5pt Arial;color:#996633;">If you have a tracking number please enter it.</em>'
		+ '<div id="message" class="message"></div><div style="padding-bottom:10px;">'
		+ '<input type="hidden" name="id" value="'+id+'"> '
		+ 'Tracking Number: <input type="text" name="tracking_number" value="">'
		
	html += '</div><div style="margin-bottom:14px">'
		+ '<div class="buttons">'
//		+ '<div class="detBtn" onclick=""><div class="round"><div class="round1"></div'
//		+ '><div class="round2"></div></div><div class="detBtnCaption">Mark As Shipped</div><div class="round">'
//		+ '<div class="round2"></div><div class="round1"></div></div></div>'
		+ '<div onclick="$(\'mas-box-form\').submit();" style="margin-left:100px;" class="button button-110-28">Mark As Shipped</div>'
		+ '</div></form></div>';
		
	LightBox.show(html);	
}

function ShowLoginBox() {
	
	var email = get_cookie('uemail');

	var html = '<div class="loginBox"><form id="login-box-form" onsubmit="return false;"><div class="links">'
		+ '<a href="index.php/home/register" class="button" tabindex="0">register</a> | '
		+ '<a href="index.php/home/recover" class="button" tabindex="0">recover</a></div>'
		+ '<em style="font:8.5pt Arial;color:#996633;">Please sign in. Registered members can make use of the Portfolio and Favorites functions, and also contact dealers directly.</em>'
		+ '<div id="message" class="message"></div><div class="boxInput">'
		+ '<input type="text" name="email" class="boxInput '
		
	if (email == undefined) {
		html += 'boxInput-loginHint';
	} 
	
	html += '" onfocus="$(this).removeClassName(\'boxInput-loginHint\')"'
		+ '	onblur="if ($(this).getValue() == \'\') $(this).addClassName(\'boxInput-loginHint\');"'
	
	if (email != undefined) {
		html += 'value="'+email+'"';
	}
		
	html += '/></div><div class="boxInput" style="margin-bottom:14px">'
		+ '<input type="password" name="passwd" class="boxInput boxInput-passwdHint" onfocus="$(this).removeClassName(\'boxInput-passwdHint\')"'
		+ '	onblur="if ($(this).getValue() == \'\') $(this).addClassName(\'boxInput-passwdHint\');" onkeypress="checkKey(event, DoLogin);"/></div>'
		+ '<div class="buttons">'
//		+ '<div class="detBtn" onclick="DoLogin();"><div class="round"><div class="round1"></div'
//		+ '><div class="round2"></div></div><div class="detBtnCaption">SIGN IN</div><div class="round">'
//		+ '<div class="round2"></div><div class="round1"></div></div></div>'
		+ '<div onclick="DoLogin();" style="margin-left:50px;" class="button button-80-25">Sign In</div>'
		+ '</div></form></div>';
		
	LightBox.show(html);
}

function DoLogin(){
	
	new Ajax.Updater('message', _base_url+'home/login',{parameters:$('login-box-form').serialize(true), 
		method:'post',evalScripts:true,
		onFailure:function(){$('message').update('Connection failed').className='error';},
    	onSuccess:function(){$('message').className='error';},
    	onLoading:function(){$('message').update('Signing in...').className='message';}});
}

function showAddToPortfolioBox(id,portfolios_list) {
	if (portfolios_list == undefined) {
		return ;
	}

	var box = new Template('<div style="width:215px;padding-left:12px;padding-right:20px;padding-bottom:8px;text-align:left;">'
			 + '<div style="color:#E3D077;font-size:14px;margin-top:5px">Add to portfolio</div>'
			 + '<div class="infoCnt" style="margin-top:10px;margin-bottom:13px;font-size:12px;">Please select '
			 + 'portfolio to which you wish to add this item</div><div style="text-align:center">'
			 + '#{pl}</div><div style="float:left;width:100px;margin-top:45px;text-align:left;">'
			 + '<a href="index.php/portfolio/my_portfolios/#{id}" style="color:#E3D077;font-size:14px;">Create new</a></div>'
			 + '<div style="float:right;width:60px;margin-top:45px;text-align:right;cursor:pointer;_cursor:hand;" onclick="'
			 + 'window.location = \'index.php/portfolio/add_portfolio_item/\' + $F(\'portfolios-list\')'
			 + ' + \'/#{id}\'">DONE</div></div>');
	
	var html = box.evaluate({pl:portfolios_list,id:id});
	
	LightBox.show(html);
}

function showEnlarge(base_url, img_list) {
  var mtpl = new Template('<div style=";width:600px;text-align:left;margin-left:33px;margin-right:33px;margin-bottom:24px;margin-top:40px;'
           + 'padding-top:491px;background: no-repeat;">'
  		   + '<div>#{images}</div></div>');
  		   
  var itpl = new Template('<div style="float:left;width:57px;height:49px;background:no-repeat url('
  		   + _base_url + 'home/img/57/49/#{base}/#{img});margin-left:9px;margin-bottom:9px;cursor:pointer;_cursor:hand;"'
  		   + ' onclick="new ImagesLoader($$(\'#lightbox div div\')[0], _base_url + \'home/img/600/473/#{base}/#{img}\');"></div>');
  
  var il = '';
  for(var i=0;i<img_list.length;i++) {
  	il += itpl.evaluate({base:base_url, img: img_list[i]});
  }
  
  var html = mtpl.evaluate({base: base_url, img: img_list[0], images: il});
  
  LightBox.show(html);                                                
  
  new ImagesLoader($$('#lightbox div div')[0], _base_url + 'home/img/600/473/' + base_url + '/' + img_list[0]);
}

function imageUrl(d,i,n,w,h) {
	return (_base_url + 'home/img/' + w + '/' + h + '/' + d + '/' + i + '/' + n);
}

var portfolios_list = new Array();
var old_active_p = '-1';

function changeTab(index) {

	if (index == old_active_p) return;
	var action_url=_base_url+'cart/add_to';
	
	var tpl = new Template('<div style="background-image: url(#{image_url});">' 
		+"<form id='add_to_cart_form_#{iid}' method='post' action='#{action_url}' style='display:inline'>"
		+"<input type='hidden' name='id' value='#{iid}' >"
		+"</form>"
		+ '<a style="cursor:hand;cursor:pointer;width:100px;" onclick="$(\'add_to_cart_form_#{iid}\').submit();">Buy Now</a><br>'
		+ '<a href="'+_base_url+'portfolio/remove_portfolio_item/#{pid}" tabindex="0">Remove</a><br/> '
		+ '<a href="'+_base_url+'p/#{iid}/#{item_title}" tabindex="0">Details</a></div>');
	
	var s = '';
	for(var i=0;i<portfolios[index].length;i++) {
      	portfolios[index][i].action_url=action_url;
        s += tpl.evaluate(portfolios[index][i]);
    }
	$('portfolio' + old_active_p).className = 'portfolioTab0';
	$('portfolio' + index).className = 'portfolioTab1';
	
	old_active_p = index;
	$('portfolio-items').update(s);
}

function PortfolioShowHide(aObj) {
	var obj = aObj.lastChild,
		i = obj.className == "portfolioShowCaption";
	aObj.parentNode.nextSibling.style.display = i ? "block" : "none";
	obj.className = "portfolio" + (obj.innerHTML = i ? "Hide" : "Show") + "Caption"
	new Ajax.Request('index.php/home/tab_status/'+i);
}

function checkKey(aEvent, f) {
	var keyCode = aEvent.keyCode || aEvent.charCode;
	if (keyCode==13) {
		f();
	}
}
function DoSearch() {
	window.location = _base_url + 'home/search/' + $F('simple-search');
}

function savePasswd() {
	$('save-passwd-results').show().update('Saving...').highlight();
	new Ajax.Updater('save-passwd-results', _base_url + 'user/save_passwd/', 
		{method:'post',parameters:$('save-passwd-form').serialize(true)});
}

function saveTabState(id,s) {
	new Ajax.Request(_base_url + 'portfolio/portfolio_tabs/'+id+'/'+s);
}

function viewMessage(rt,st,mt,me,mc,mid,r,sid,reply) {
	var tpl = '<div style="width:500px;text-align:left;margin-left:16px;margin-right:16px;margin-bottom:16px;margin-top:28px;padding-top:10px;border-top:1px solid #c3c3c3;">' 
	        + '<div style="width:480px;padding-right:5px;float:left">'
	        + '<div class="boxLabel" style="margin-top:0px;color:#333;">To: '
	        + '<a href="javascript:;" style="color:#E8D576;font-size:14px;">#{recipient_title}</a></div>'
	        + '<div class="boxLabel" style="margin-top:0px;color:#333;">From: '
	        + '<a href="javascript:;" style="color:#E8D576;font-size:14px;">#{sender_title}</a></div>'
	        + '<div class="boxLabel" style="margin-top:9px;color:#333;">Subject: #{title}</div>'
	        //+ '<div class="boxLabel" style="margin-top:15px;color:#333;">Expiry Date: #{expiry}</div></div>'
	        + '<div style="width:480px;float:left;color:black">'
	        + '<div class="boxLabel" style="margin-top:15px;color:#333;font-weight:bold;padding-bottom:5px">Message:</div>'
	        + '#{message}</div><div style="margin-top:5px;width:480px;float:left;text-align:right;margin-top:24px;">';

//	    tpl += '<div class="detBtn" style="margin-left:10px;float:right;" onclick="LightBox.hide();"><div class="round"><div class="round1"></div><div class="round2"></div>'
//	        + '</div><div class="detBtnCaption">Close</div><div class="round"><div class="round2"></div><div class="round1">'
//	        + '</div></div></div>';
		tpl += '<div onclick="LightBox.hide();" style="margin-left:10px;float:right;" class="button button-80-25">Close</div>';
	        
	if (reply) {
//		tpl += '<div class="detBtn" style="float:right;" onclick="replyMessage(#{sender_id},\'#{title}\');"><div class="round"><div class="round1"></div><div class="round2"></div>'
//	        + '</div><div class="detBtnCaption">Reply</div><div class="round"><div class="round2"></div><div class="round1">'
//	        + '</div></div></div>';

		tpl += '<div onclick="replyMessage(#{sender_id},\'#{title}\');" style="margin-left:10px;float:right;" class="button button-80-25">Reply</div>';
	} 
       
	
	tpl = new Template(tpl + '</div></div>');
	        	        
	var html = tpl.evaluate({sender_title:st,title:mt,expiry:me,message:mc.unescapeHTML(),msg_id:mid,sender_id:sid, recipient_title:rt});
	        
	LightBox.show(html);
	
	if (!r) {
		new Ajax.Request(_base_url+'user/mark_as_read/' + mid);
		
		$('msg-' + mid + '-1').addClassName('mailRead').removeClassName('mailUnRead');
		$('msg-' + mid + '-2').addClassName('mailRead').removeClassName('mailUnRead');
		$('msg-' + mid + '-3').addClassName('mailRead').removeClassName('mailUnRead');
	}
}

function composeMessage(rid,title, r) {
	var tpl = new Template('<div style="width:500px;text-align:left;margin-left:16px;margin-right:16px;margin-bottom:16px;margin-top:28px;padding-top:10px;border-top:1px solid #c3c3c3;">' 
	        + '<form id="send-form" action="index.php/user/send_message" method="post"><div style="width:480px;padding-right:5px;float:left">'
	        + (r?'<input type="hidden" id="recipients-list" name="recipient" />':'<div class="boxLabel" style="margin-top:0px;color:#333;">To: '+recipients_list+'</div>')
	        + '<div class="boxLabel" style="margin-top:9px;color:#333;">Subject: <input id="send-form-subject" name="subject" value="#{title}"/> <span id="send-form-subject-hint"></span></div>'
	        //+ '<div class="boxLabel" style="margin-top:15px;color:#333;">Expiry Date: <select name="expire">'
	        //+ '<option value="7">7 day</option><option value="14">14 day</option><option value="30">30 day</option>'
	        //+ '<option value="0">Until I find it!</option></select></div></div>'
	        + '<div class="boxLabel" style="margin-top:9px;color:#333;"><input type="hidden" name="save_msg" value="1" /><input id="send-form-message" type="checkbox" name="save_msg" value="0" checked="checked"/>Save message</div>'
	        + '<div style="width:480px;float:left;color:black">'
	        + '<div class="boxLabel" style="margin-top:15px;color:#333;font-weight:bold;padding-bottom:5px">Message:</div>'
	        + '<textarea name="message" style="width:500px" rows="8"></textarea></div><div style="margin-top:5px;width:480px;float:left;text-align:right;padding-left:375px;margin-top:24px;">'
//	        + '<div class="detBtn" onclick="validateMsgForm();"><div class="round"><div class="round1"></div><div class="round2"></div>'
//	        + '</div><div class="detBtnCaption">SEND</div><div class="round"><div class="round2"></div><div class="round1">'
//	        + '</div></div></div>'
			+ '<div onclick="validateMsgForm();" style="margin-left:45px" class="button button-80-25">Send</div>'
	        + '</div></form></div>');
	        
	LightBox.show(tpl.evaluate({title:title}));
	
	$('recipients-list').value = rid;	
}

function validateMsgForm() {

	if (!$('send-form-subject').value) {
		$('send-form-subject').setStyle({border:'1px red solid'});
		$('send-form-subject-hint').update('Subject is required').setStyle({color:'red',fontSize:'12px'});
	} else {
		$('send-form-subject').setStyle({border:'1px green solid'});
		$('send-form-subject-hint').update('');
		$('send-form').submit();
	}
}

function replyMessage(rid,title) {
	LightBox.hide();
	composeMessage(rid, 'Re: ' + title, 1);
}

document.observe('dom:loaded', function(){
	$$('form div.tblRow div.tblDescr').each(function (e) {
		new Ajax.InPlaceEditor(e, 'index.php/designer/save_notes/' + $(e).id, {rows:3,cols:30});
	});
});

function confirmSubmit() {
	return confirm("Are you sure you wish to delete this record?");
}

function DeleteItem(id) {
	window.location = _base_url + 'dealer/delete_item/' + id; 
}
function DeleteAds(id) {
	window.location = _base_url + 'designer/delete_ads/' + id; 
}

function SetSoldItem(id) {
	window.location = _base_url + 'dealer/sold_inventory/' + id;
}

function SetHomePageItem(id) {
	window.location = _base_url + 'dealer/main_page_inventory/' + id;
}

function doSaveSubscribe(opt) {
	new Ajax.Request(_base_url + 'user/subscribe/'+$(opt).checked);
}

var cats = [];
var subcats = [];

function do_load_subs(){
    $('sub-cats').innerHTML = '';
    var a = null;
    
    //if($F('section')=="4") // Temporary fix for 'Shop' category
    	//return;
    
    for(var v in subcats[$F('category')]) {
    
    	if(v==0) // Fix; stop processing if a null value is found
    	{
    		a = new Element('option', {'value':0}).update('n/a');
    		$('sub-cats').appendChild(a);
    		break;
    	}
    	
    	//if(v >= "0" && v <= "9") // Not sure what this was for, but it was breaking certain categories
    	{
    		a = new Element('option', {'value':v}).update(subcats[$F('category')][v]);
    	}
		$('sub-cats').appendChild(a);
    }
}

function do_load_categories(){
    $('category').innerHTML = '';
    for(var v in cats[$F('section')]) {
        var a = new Element('option', {'value':v}).update(cats[$F('section')][v]);
        $('category').appendChild(a);
    }
}

function email2friend(iid,e,t,m,p,did,image,url) {	
	var html = new Template('<div style="width:585px;height:585px;text-align:left;padding-left:50px;">'
		+ '<form action="index.php/home/email_friend" id="email2friend" method="post"><input type="hidden" name="item_id" value="#{iid}" />'
		+ '<div class="boxCaption" style="padding-top:35px;">Email A Friend</div>'
		+ '<div class="boxDescription" style="padding-top:35px;width:520px;" >Please complete the fields below, '
		+ 'and we\'ll forward your friends an email from you with a link to this page. Your friends\' names will '
		+ 'not be added to our email list unless they personally choose to sign up.</div>'
		+ '<div style="margin-top:16px;height:360px;"><div style="width:323px;margin-right:6px;float:left;">'
		+ '<div class="boxLabel">Enter your friends\' email address(es)</div> '
		+ '<div><input name="email" id="e2e-email" class="boxInput" /></div> '
		+ '<div class="boxHint" id="e2e-email-hint">e.g. john@hotmail.com, jane@aol.com</div> '
		+ '<div class="boxLabel">Email title</div> '
		+ '<div><input name="title" class="boxInput" value="Look what I found at www.fyndes.com" /></div> '
		+ '<div class="boxLabel">Email message</div> '
		+ '<div><textarea name="msg" class="boxInput" rows="5">I thought you might like this item at ' 
		+ '#{link}</textarea></div> '
		+ '<div class="boxLabel">Your Email address</div> '
		+ '<div><input name="uemail" class="boxInput" value="#{uemail}" /></div> '
		+ '<div class="boxCheckHint" style="margin-top:40px"><input type="hidden" name="accept" value="1" '
		+ '/><input type="hidden" name="copy2me" value="0" /><input type="checkbox" name="copy2me" value="1" ' 
		+ '/>Send me a copy of this message.</div></div>'
		+ '<div style="margin-left:6px;float:left;width:200px;">'
		+ '<div id="email-friend-box-image" style="width:186px;height:161px;background:no-repeat;margin-top:28px;"></div> '
		+ '<div style="margin-top:6px;color:black;font-family:Arial-BoldMT;font-weight:bold;font-size:14px;line-height:14px;">'
		+ '#{title}</div> '
		+ '<div style="margin-top:6px;color:#666;font-family:ArialMT;font-size:14px;line-height:14px;">'
		+ '#{mes}</div>'
		+ (p != 'null' ?
		  '<div style="margin-top:6px;color:#666;font-weight:bold;font-family:ArialMT;font-size:14px;line-height:14px;">'
		+ '$#{price}</div>'
		+ '<div class="boxCheckHint" style="margin-top:6px;"><input type="hidden" name="hideprice" value="0" />'
		+ '<input type="checkbox" name="hideprice" value="1" />Do not show price.</div></div></div>':
		'<input type="hidden" name="hideprice" value="1" /></div></div>')
		+ '<div style="margin-top:5px;padding-left:430px;">'
//		+ '<div class="detBtn" onclick="validate_email2friend();">'
//		+ '<div class="round"><div class="round1"></div><div class="round2"></div></div><div class="detBtnCaption">'
//		+ 'SUBMIT</div><div class="round"><div class="round2"></div><div class="round1"></div></div></div>'
		+ '<div onclick="validate_email2friend();" class="button button-80-25">Submit</div>'
		+ '</div></form></div>');
		
	LightBox.show(html.evaluate({iid:iid,uemail:e,title:t,mes:m,price:p, link:url}));
	
	new ImagesLoader('email-friend-box-image', 'index.php/home/img/186/161/'+did + '/' + iid + '/' + image);	
}

function validate_email2friend() {

	if (!$('e2e-email').value) {
		$('e2e-email').setStyle({border:'1px red solid'});
		$('e2e-email-hint').update('Email is required').setStyle({color:'red',fontSize:'12px'});
	} else {
		$('e2e-email').setStyle({border:'1px green solid'});
		$('e2e-email-hint').update('');
		$('email2friend').submit();
	}
}

document.observe('dom:loaded', function (){
	$$('.ajax-form').each(function (e) {
		$(e).observe('submit', ajaxFormSubmit.bind(window, e));
	});
	
	$$('.post-back-form').each(function (e) {
		new Ajax.PostBack($(e).id, _base_url+$(e).title, {});
	});
	
	$$('div.button[title]').each(function (e) {
		$(e).observe('click', navTo.bind(window, _base_url + $(e).title));
	});
	
	new Hover('div.button', 'button-hover');
});

function navTo(url) {
	window.location = url;
}

function ajaxFormSubmit(e) {
	$(e + '-results').update('Processing...').show();
	new Ajax.Updater($(e).id + '-results', _base_url+$(e).title, {parameters:$(e).serialize(true), evalScripts:true});
	
	return false;
}

function convert2edits(btn) {
	$(btn).hide();	$('save-details-btn').show();
	$$('.static-edit-text').each(function (e) {
		$(e).update(new Element('input', {name:$(e).id.camelize().underscore(), value:$(e).innerHTML}).addClassName('clsText').addClassName('static-edit-text-input'));
	});
}

function convert2text() {
	$('save-details-btn').hide(); $('edit-details-btn').show();
	$$('.static-edit-text-input').each(function (e) {
		$(e.parentNode).update($(e).value);
	});
}










Event.observe(window, 'load', function() {
	if($('Radiobox1')){
		$('Radiobox1').observe('change', function(){
			undoAdditionalRequiredFields();
		});
		$('Radiobox2').observe('change', function(){
			undoAdditionalRequiredFields();
		});
		$('Radiobox3').observe('change', function(){
			addAdditionalRequiredFields();
		});
		
		// Apply required fields if we're coming back from a failed form submission
		// with the Seller account type selected
		if($('Radiobox3').checked){
			addAdditionalRequiredFields();
			$('Companyname').addClassName('tblCCaptR');
			$('web-site').show();
		}
		
		if($('Radiobox3').checked || $('Radiobox2').checked){
			$('company_description-div').show();
		}
	}
// Applies 'jump' functionality for the 'Who's Here' section of the homepage
        if($('shop_jump')){
                $('shop_jump').observe('change', function(){
                  if($('shop_jump').value != ''){ console.debug('made it');
                        window.location = 'http://' + window.location.hostname + $('shop_jump').value;
                  }
                });
        }
});


function undoAdditionalRequiredFields(){
	$('label-street-address').removeClassName('tblCCaptR');
	$('label-state').removeClassName('tblCCaptR');
	$('label-post-code').removeClassName('tblCCaptR');
	$('label-telephone').removeClassName('tblCCaptR');
}

function addAdditionalRequiredFields(){
	$('label-street-address').addClassName('tblCCaptR');
	$('label-state').addClassName('tblCCaptR');
	$('label-post-code').addClassName('tblCCaptR');
	$('label-telephone').addClassName('tblCCaptR');

}

