
function closeAndRefreshParent() {
  window.opener.location.href = window.opener.location.href;
  window.opener.location.reload();
  
  if (window.opener.progressWindow) {
		window.opener.progressWindow.close()
  }
//	alert('closing in 2 seconds');
//	setTimeout("window.close()",2000); //close after 2 seconds
  window.close();
}


//this function is to workaround a TOTAL BUG in IE
//where the http_referer does NOT get passed to the processing page, which fucks everything really
function goto(url) {

	var isOpera, isIE = false;
	if(typeof(window.opera) != 'undefined'){isOpera = true;}
	if(!isOpera && navigator.userAgent.indexOf('Internet Explorer')){isIE = true;}	
	
	//do this for ordinary browsers...
	location.href = url;
	
	//then do it again if IE .. 
	if (isIE) {
		var referLink = document.createElement('a');
	  referLink.href = url;
	  document.body.appendChild(referLink);
	  referLink.click();
	} else {
		location.href = url;
	}
}

		    function resize(isIE) {
					var container = document.getElementById("container"); 
					var wrapper = document.getElementById("Wrapper");
					var info = document.getElementById("infoContainer");
					var msg = document.getElementById("msg");
					
					//nav may not exist in the POPUP window
					var nav_height = 0;
					var nav = null;
					if (document.getElementById("nav") != null) {
						nav = document.getElementById("nav");
						nav_height = nav.offsetHeight;
					}
					//menu may not exist in the POPUP window
					var menu_height = 0;
					var menu = document.getElementById("menu");
					if (menu != null) {
						menu_height = menu.offsetHeight;
					}					
					//buttons may not exist
					var buttons_height = 0;
					var buttons = document.getElementById("buttons");
					if (buttons != null) {
						buttons_height = buttons.offsetHeight;
					}
					//tabs may not exist
					var tabs_height = 0;
					var tabs = document.getElementById("tabs");
					if (tabs != null) {
						tabs_height = tabs.offsetHeight;
					}
					
					//the extra info section may also not exist
					var status_height = 0;
					var status = document.getElementById("status");
					if (status != null) {
						status_height = status.offsetHeight;
					}
					
					
					// WE NEED TO RESET OR SOMETHING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
					
					
					//ok - in IE8 and firefox we need 20px padding, in IE7 we need more like 60px...., but 100px for the create_snippet page .. 
					//IE7 is HOPELESS.
					var padding = 0;
					if (isIE) {
						//alert($.browser.version);
						if ($.browser.version <= 7.0) {
							
							//browser is IE7 or less, so we need to check the page to apply fixes for this bROKeN browser							
				      $.urlParam = function(name){
								var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
								return results[1] || 0;
				      }
				      // example.com?someparam=name&otherparam=8&id=6
				      //alert($.urlParam('page')); // name
							if ($.urlParam('page') == 'create_snippet') {
								//IE7 has a bug for this page, but ONLY WHEN IT'S A POPUP ..
								//padding = 100;
								padding = 30; //maybe we don't need this, it's just that the buttons height was wrong. maybe we need NAMESPACE - does tinyMCE have a 'buttons' class already?
								//this is good for POPUP IE 8
								//good for POPUP IE 7 ( where the form is squashed up anyway, so this is needed to avoid the white GAP underneath tinyMCE .. unless we want that? it DOES MAKE SENSE !!!
							} else {
								padding = 20; //was 60 .. but this is good in EMAIL CREATE PAGE in IE 7, 8 and FF
							}							
						}
					}
					//if it's IE and it's the create snippet page in a NEW WINDOW then we need to add 60px padding instead of 20 !!!!
					var xs = padding + menu_height + nav_height + info.offsetHeight + msg.offsetHeight + buttons_height + status_height + tabs_height;

					
					//first we do this in case they are RESIZING the WINDOW - cos we need to RESET the height
					//var containerHeight = wrapper.offsetHeight;
					
					//container.style.height = (container.offsetHeight - xs) + "px";
					container.style.height = (wrapper.offsetHeight - xs) + "px";
					var str = 'resizing container, height is ' + container.offsetHeight + 
					' and wrapper height is ' + wrapper.offsetHeight +
					' and infoContainer height is ' + info.offsetHeight +
					' and msg height is ' + msg.offsetHeight +
					' and menu height is ' + menu_height +
					' and nav height is ' + nav_height + 
					' and buttons is ' + buttons_height + 
					' and status height is ' + status_height + 
					' and tabs height is ' + tabs_height + 
					' and padding is ' + padding + 
					' therefore excess is ' + xs
					;
					//alert(str);
					
				}

 function info(infoText) {
    document.getElementById('info').innerHTML=infoText;
    document.getElementById('infoContainer').style.display = 'block';
   var element = document.getElementById('infoContainer');
   
   if(element.up == null || element.down)
   {
      animate('infoContainer', 0, 20, 150, 0, 250, null);
      element.up = true;
      element.down = false;
      //headerElement.innerHTML = 'vvv';
   }
   else
   {
      animate('infoContainer', 0, 20, 150, 130, 250, null);
      element.down = true;
      element.up = false;
      //headerElement.innerHTML = '^^^';
   }
		//and then we RESIZE !!!
		resize();
 }
 
 function hideInfo() {  
    document.getElementById('infoContainer').style.display = 'none';
 }



	function popUp(URL) {
		day = new Date();
		id = day.getTime();
		eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=900,height=800,left = 50,top = 50');");
	}		

	
	function testJS() {
		alert('hello');
	}
	
	function previewSnippet(url) {
		//var newwindow=window.open(url,toolbar=0,scrollbars=0,statusbar=0,menubar=0,resizable=1,width=850,height=1000,left=200,top=100);
		//if (window.focus) {newwindow.focus();}			
		eval("snippetWindow = window.open('" + url + "', 'preview_snippet', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=950,height=600,left = 50,top = 50');");
		//var newwindow=window.open(url,'contactsWindow',toolbar=0,scrollbars=0,statusbar=0,menubar=0,resizable=1,width=200,height=1000,left=200,top=100);
//		var contactsWindow=window.open(url,toolbar=0,scrollbars=0,statusbar=0,menubar=0,resizable=1,width=650,height=700,left=200,top=100);		
//		if (window.focus) {contactsWindow.focus();}		
	}
	
	
	function openContactsWindow(url) {
		eval("contactsWindow = window.open('" + url + "', 'contactsWindow', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=950,height=800,left = 50,top = 50');");
		//var newwindow=window.open(url,'contactsWindow',toolbar=0,scrollbars=0,statusbar=0,menubar=0,resizable=1,width=200,height=1000,left=200,top=100);
//		var contactsWindow=window.open(url,toolbar=0,scrollbars=0,statusbar=0,menubar=0,resizable=1,width=650,height=700,left=200,top=100);		
//		if (window.focus) {contactsWindow.focus();}		
	}
	
	
	
	function previewEmail(url) {
		eval("previewWindow = window.open('" + url + "', 'preview_email', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=950,height=800,left = 50,top = 50');");	
		//var newWindow=window.open(url,toolbar=0,scrollbars=0,statusbar=0,menubar=0,resizable=1,width=700,height=1000,left=200,top=100);
		//if (window.focus) {previewWindow.focus();}		
	}

//	function previewEmail(emailID) {
		//so we gotta open a new window with the search term and the ROW and LOC etc...
		//we need the SMARTY TAGS here ..
//		previewEmail('main.php?page=preview_email&emailID={$emailID}&wid=preview_email&popup=1&sid={$sid}');
//		window.open('main.php?page=preview_email&emailID={$emailID}&wid=preview_email&popup=1&sid={$sid}', 'preview_email', 'toolbar=no, status=no, scrollbars=yes, location=no, menubar=no, directories=no, width=850, height=1000');           
		//it is VERY IMPORTANT that we return FALSE else the form will also SUBMIT to the window .. we don't want that !!
//		return false;
//	}
	
	
	
	
	function openWindow(url) {
		var newwindow=window.open(url,toolbar=0,scrollbars=0,statusbar=0,menubar=0,resizable=1,width=400,height=300,left = 440,top = 250);
		if (window.focus) {newwindow.focus()}
	}
	

	function MM_preloadImages() { //v3.0
		var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
		for(i=0; i<a.length; i++)
			if (a[i].indexOf("#")!=0){ 
				d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
			}
		}
	}

	function MM_swapImgRestore() { //v3.0
		var i,x,a=document.MM_sr; 
		for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) {
			x.src=x.oSrc;
		}
	}

	
	function MM_findObj(n, d) { //v4.01
		var p,i,x;  if(!d) d=document; 
		if((p=n.indexOf("?"))>0&&parent.frames.length) {
			d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
		}
		if(!(x=d[n])&&d.all) 
			x=d.all[n];
		for (i=0;!x&&i<d.forms.length;i++) 
			x=d.forms[i][n];
		for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
			x=MM_findObj(n,d.layers[i].document);
		if(!x && d.getElementById)
			x=d.getElementById(n); 
			
		return x;
	}

	function MM_swapImage() { //v3.0
		//alert('MM_swapImage()');
		var i,j=0,x,a=MM_swapImage.arguments; 
		document.MM_sr=new Array; 
		for(i=0;i<(a.length-2);i+=3) {
			if ((x=MM_findObj(a[i]))!=null){
				document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];
			}
		}
	}	
	
	function initSnippetTextEditor(codeView, SERVER_URL) {
		var buttons = "bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,bullist,numlist,outdent,indent,undo,redo";
		if (codeView == 1) {
			buttons += ", code"; 
		}
		//site_url: "http://purple-channel.net/edistributor/new", 
		//we now get this from config
		tinyMCE.init({
			mode : "textareas",
			theme : "advanced",
			relative_urls : 0,
			convert_urls : 0,
			SERVER_URL : SERVER_URL, 
			theme_advanced_buttons1 : "styleselect-font,fontselect,fontsizeselect,forecolorpicker,|,link,unlink",
			theme_advanced_buttons2 : buttons,
			theme_advanced_buttons3 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			extended_valid_elements : "a[name|style|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style],table[style],tr,td"						
		});	
	}
	