/**
* general funcitons
*/
// -- breaks out of frames
try { if (top.frames.length!=0) { top.location=self.document.location; } }catch(e){}

function lightboxify() {
	var lblinks = $$('a'); // gets all links
	
	var lgwobj = {
		fade:true, 
		iframe:true,
		height:524, 
		width:700,
		className:"lgwiframe",
		fadeDuration:0.50
//loading:"/img/loading.dots.gif"
	};
	
	for (var i=0; i<lblinks.length; i++) {
		lbrel = lblinks[i].getAttribute('rel');
		if (lbrel=='lightbox') {
			new Control.Modal(lblinks[i],{
				fade:true
			});
		} else if (lbrel=='lightboxi') {
			new Control.Modal(lblinks[i],{
				fade:true, 
				iframe:true
			});
		} else if (lbrel=='lightboxlgw') {
			// var window_close = new Element('div',{ className: 'window_close' })
			new Control.Modal(lblinks[i], lgwobj);
		}
			
	}
}
Event.observe(window,'load',function() { lightboxify(); });
/**
* AJAX
*/
var myGlobalHandlers = {
	onCreate: function(){
		Element.addClassName('body','loading');
	},

	onComplete: function() {
		if(Ajax.activeRequestCount == 0){
			Element.removeClassName('body','loading');
		}
	}
};
Ajax.Responders.register(myGlobalHandlers);

function ajaxsubmit(formid,action,vars,re_func) {
	if ($(formid)) {
		var xform = $(formid);
	} else if ($(formid)) {
		var xform = $('editform');
	}
	if (!action && xform.action) {
		action = xform.action;
	}
	if (!vars && xform) {
		vars = xform.serialize();
	}
	if (!re_func) {
		re_func = ajaxsubmit_re;
	}
	if (action && vars) {
		var myAjax = new Ajax.Request(
			action, 
			{
				method: 'post', 
				parameters: vars,
				onComplete: re_func
			});
	}
	//void(0);
	return false;
}
function ajaxsubmit_re(Obj) {
	re = Obj.responseText;
	alert(re);
}
function submit_subscribe(formid,inputid) {
	var url = '/pommo/user/process.php';
	var vars = 'Email='+$F(inputid)+'&formSubmitted=1&pommo_signup=Subscribe';
	// submit
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: vars,
			onComplete: submit_subscribe_re
		});
	return false; // so form doesn't submit
}
function submit_subscribe_re(Obj) {
	re = Obj.responseText;
	if (re.indexOf('Duplicates are not allowed')>-1) {
		alert('Sorry, that email address is already subscribed.');
	} else {
		alert('Thanks for signing up!');
	}
	return false;
}
function cif(xid,xtest,xdefault) {
	if ($(xid)) {
		if ($F(xid)==xtest) {
			$(xid).value = xdefault;
		}
	}
}
function vacontact(updateid,vfld,obj) {
	ignoreid = 0;
	if ($('ContactId')) {
		ignoreid = $F('ContactId');
	}
	if (ignoreid > 0) {} else {
		ignoreid = 0;
	}
	vvalue = $F(obj);
	if (typeof(vvalue)=='string' && vvalue.length > 0) {
		url = '/contacts/isunique/'+vfld+'/'+vvalue+'/'+ignoreid+'/img';
		var myAjax = new Ajax.Updater(
			updateid,
			url,
			{
				method: 'get'
			});
	} else {
		$(updateid).innerHTML = '<img src="/img/cancel.12px.png" border="0" height="10" width="10" alt="INVALID Input"/>';
	}
}
/**
* == browse : rep ==
*/
function checkall_byclass(source,targetclass,rootelement) {
	try {
		var is = new Array;
		if ($(source)) {
			var changeto = $(source).checked;
			if (rootelement!=undefined && rootelement!='') {
				if ($(rootelement)) {
					is = $(rootelement).getElementsByClassName(targetclass);
				}
			}
			if (is.length==0) {
				is = document.getElementsByClassName(targetclass);
			}
			for (var i=0; i<is.length; i++) {
				is[i].checked = changeto;
			}
		} else {
			alert('Error: checkall_byclass: cannot find source');
		}
	} catch(e) {
		alert(e);
	}
}
function rep_delchecked(targetclass) {
	delchecked(targetclass,'ContactIds');
}
function c_delchecked(targetclass) {
	delchecked(targetclass,'ContentIds');
}
function delchecked(targetclass,hiddenfieldid) {
	var delform = $('delform');
	var XIds = $(hiddenfieldid);
	try {
		if (delform) {
			var is = document.getElementsByClassName(targetclass);
			XIds.value = '';
			var c=0;
			for (var i=0; i<is.length; i++) {
				if (is[i].checked) {
					XIds.value+= ','+is[i].value;
					c++;
				}
			}
			
			if (c>0) {
				if (confirm('Are you sure you want to delete the '+c+' checked entries?')) {
					return Try.these(
							function() {delform.submit()},
							function() {delform.Submit()}
							);
				}
			}
		} else {
			alert('Error: delchecked: cannot find form');
		}
	} catch(e) {
		alert(e);
	}
}
/**
* == edit : rep ==
*/
function isrep_toggle_repid() {
	if ($('ContactIsRep') && $('ContactRepId')) {
		if ($('ContactIsRep').checked) {
			$('ContactRepId').disabled = false;
		} else {
			$('ContactRepId').disabled = true;
			//$('ContactRepId').value='';
		}
	}
}
function rep_copy2ship() {
	var flds=['ContactAddress','ContactAddress2','ContactCity','ContactState','ContactZip'];
	for (var i=0; i<flds.length; i++) {
		var sourceid = flds[i];
		var destid = sourceid.replace(/Contact/gi,'ContactShip');
		if ($(sourceid) && $(destid)) {
			$(destid).value = $F(sourceid);
		}
	}
	if ($('ContactCountry') && $('ContactShipCountry')) {
		$('ContactShipCountry').selectedIndex = $('ContactCountry').selectedIndex;
	}
}
/**
* editor funcitons
*/
function dbtoggle(obj,xid,model,fieldname,controller) {
	try {
		// should be the <a> in the following:
		// <unknown><a><img/></a></unknown>
		var make = null;
		if (typeof(obj)=='string') {
			obj = $(obj);
		}
		if(typeof(obj)=='object' && xid > 0) {
			// -- parent --
			var parent = obj.parentNode;
			// get non <a> parent
			while (parent.tagName == 'a') {
				 parent = parent.parentNode;
			}
			// make sure we know the ID
			if (!parent.id || parent.id==undefined || parent.id=='' || parent.id==null) {
				var now = new Date();
				parent.id = 'dbtoggle_'+now.getTime();
			}
			// -- child / img --
			var imgs = obj.getElementsByTagName('img');
			for (var i=0; i<imgs.length; i++) {
				if (imgs[i].src) {
					if (imgs[i].src.indexOf('ok')>-1) {
						// is currently true
						make = false;
					} else if (imgs[i].src.indexOf('cancel')>-1) {
						// is currently false
						make = true;
					}
				}
			}
			// do something
			if (make===true || make===false) {
				if (make===true) {
					make = 1;
				} else {
					make = 0;
				}
				if (controller=='' || controller==undefined) {
					var controller = model.toLowerCase()+'s';
				}
				var url = '/'+controller+'/change/'+xid+'/'+fieldname+'/'+make+'/'+parent.id;
				var pars = '';
				var myAjax = new Ajax.Request(
					url, 
					{
						method: 'get', 
						parameters: pars,
						onComplete: dbtoggle_re
					});
			}
		}
	} catch(e) {
		alert(e);
	}
}
function dbtoggle_re(originalRequest,aobj) {
	try {
		var src_true = '/img/ok.12px.png';
		var src_false = '/img/cancel.12px.png';
		
		var parts = originalRequest.responseText.split('|');
		if (parts.length>3) {
			id = 		parseInt(parts[0]);
			xfield = 	parts[1];
			xvalue = 	parts[2];
			div_id = 	parts[3];
			
			if ($(div_id)) {
				if (xvalue == 'error') {
					$(div_id).innerHTML = '<small>'+xvalue+'</small>';
				} else {
					var imgs = $(div_id).getElementsByTagName('img');
					for (var i=0; i<imgs.length; i++) {
						if (imgs[i].src) {
							if (xvalue==1) {
								imgs[i].src = src_true;
							} else {
								imgs[i].src = src_false;
							}
						}
					}
					// handle "dbable_expires" updating
					if (parts.length>5) {
						if ($(parts[4])) {
							$(parts[4]).innerHTML = parts[5];
						}
					}
				}
			} else {
				if (xvalue == 'error') {
					alert('Error : dbtoggle : '+originalRequest.responseText);
				}
			}
		}
	} catch(e) {}
}


function flv_make_insert() {
	var id = 45;
	h = $F('mp_height');
	w = $F('mp_width');
	f = $F('mp_path');
	i = $F('mp_imgpath');
	c = $F('mp_caption');
	
	var mpt = ''+
	'<!--\/\/FLV\/'+id+'|'+h+'|'+w+'|'+f+'|'+i+'|'+c+'\/-->'+
	'<div id="flv'+id+'" class="flv" style="width:'+w+';">'+
	/*
	'<embed id="flv'+id+'_mp" src="/js/jw_media_player/mediaplayer.swf" '+
	'width="'+w+'" height="'+h+'" allowfullscreen="true" '+
	'style="height:'+h+';width:'+w+';" '+
	'flashvars="&displayheight='+h+'&file='+f+'&image='+i+'&height='+h+'&width='+w+'&autostart=false" />'+
	*/
	'<!--\/\/FLVMP||'+f+'||'+i+'||'+h+'||'+w+'\/-->'+
	'<img class="flashspacer" width="'+w+'" height="'+h+'" alt="Media Player Placeholder" title="Media Player Placeholder" src="/js/tiny_mce/themes/advanced/images/spacer.gif"/>'+
	'<!--\/\/FLVMPEND||\/-->'+
	'<p>'+c+'</p>'+
	'</div><p class="clear">&nbsp;</p>'+
	'<!--\/\/\\FLV\/\/-->'+
	'';
	/*
	<img width="'+w+'" height="'+h+'" border="0" class="mceItemFlash" title="/js/jw_media_player/mediaplayer.swf" alt="/js/jw_media_player/mediaplayer.swf" mce_src="http://bs.alan.fuzz/js/tiny_mce/themes/advanced/images/spacer.gif" src="http://bs.alan.fuzz/js/tiny_mce/themes/advanced/images/spacer.gif" _moz_resizing="true"/>
	*/
	tinyMCE.execCommand('mceInsertContent', false, mpt);
	tinyMCE.execCommand('mceCleanup', false);
	
	try {myLightbox.end
		flv_edit_modal.close();
	} catch(e) {}
}
