
var showHelp = "";
var siteurl = location.protocol+'\/\/'+location.hostname;


/*
Function: $get
This function provides access to the ñgetî variable scope + the element anchor

Version: 1.3

Arguments:
key - string; optional; the parameter key to search for in the urlÍs query string (can also be ñ#î for the element anchor)
url - url; optional; the url to check for ñkeyî in, location.href is default

Example:
>$get(ÓfooÓ,Óhttp://example.com/?foo=barÓ); //returns ÒbarÓ
>$get(ÓfooÓ); //returns the value of the ÒfooÓ variable if itÕs present in the current url(location.href)
>$get(Ó#Ó,Óhttp://example.com/#mooÓ); //returns ÒmooÓ
>$get(Ó#Ó); //returns the element anchor if any, but from the current url (location.href)
>$get(,Óhttp://example.com/?foo=bar&bar=fooÓ); //returns {foo:ÕbarÕ,bar:ÕfooÕ}
>$get(,Óhttp://example.com/?foo=bar&bar=foo#mooÓ); //returns {foo:ÕbarÕ,bar:ÕfooÕ,hash:ÕmooÕ}
>$get(); //returns same as above, but from the current url (location.href)
>$get(Ó?Ó); //returns the query string (without ? and element anchor) from the current url (location.href)

Returns:
Returns the value of the variable form the provided key, or an object with the current GET variables plus the element anchor (if any)
Returns ÒÓ if the variable is not present in the given query string

Credits:
Regex from [url=http://www.netlobo.com/url_query_string_javascript.html]http://www.netlobo.com/url_query_string_javascript.html[/url]
Function by Jens Anders Bakke, webfreak.no
*/
function $get(key,url){
    if(arguments.length < 2) url = location.href;
    if(arguments.length > 0 && key != ''){
        if(key == '#'){
            var regex = new RegExp('[#]([^$]*)');
        } else if(key == '?'){
            var regex = new RegExp('[?]([^#$]*)');
        } else {
            var regex = new RegExp('[?&]'+key+'=([^&#]*)');
        }

        var results = regex.exec(url);
        return (results == null )? '' : results[1];
    } else {
        url = url.split('?');
        var results = {};

        if(url.length > 1){
            url = url[1].split('#');
            if(url.length > 1) results["hash"] = url[1];
            url[0].split('&').each(function(item,index){
                item = item.split('=');
                results[item[0]] = item[1];
            });    
        }
        return results;
    }
}

function addressbook(code, MID)
{
  url = siteurl+"/legends/?code="+code+"&MID="+MID+"&s="+$get('s');
  window.open(url,'addressbook','width=430,height=300,resizable=no,scrollbars=yes'); 
}

function PopUp(url, name, width,height,center,resize,scroll,posleft,postop) {
    if (posleft != 0) { x = posleft }
    if (postop  != 0) { y = postop  }

    if (!scroll) { scroll = 1 }
    if (!resize) { resize = 1 }

    if ((parseInt (navigator.appVersion) >= 4 ) && (center)) {
      X = (screen.width  - width ) / 2;
      Y = (screen.height - height) / 2;
    }
    if (scroll != 0) { scroll = 1 }

    var Win = window.open( url, name, 'width='+width+',height='+height+',top='+Y+',left='+X+',resizable='+resize+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no');
    return false;
}

function hideBlock (blockId) {
     document.getElementById(blockId).style.display='none';
     showHelp = "";
}

function inlineHelp(divId,lang) {
     if (!showHelp)
     {
          var showlang = lang+'<div align="right" class="compact"><A HREF="javascript:hideBlock(\''+divId+'\');">Close This</A> &nbsp;</div>';
          document.getElementById(divId).innerHTML = showlang;
          document.getElementById(divId).style.display = 'block';
          showlang = null;
          showHelp = 1;
     }
     else
     {
          document.getElementById(divId).style.display='none';
          showHelp = "";
     }
}

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_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_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
 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];}
}

tinyMCE.init(
		{
		    mode : "exact",
		    elements : "htmlpost",
		    theme : "advanced",
		    entity_encoding : "raw",
		    relative_urls : false,
		    convert_urls: false,
		    apply_source_formatting : false,
		    remove_script_host: false,
		    convert_newlines_to_brs : false,
		    media_strict: false,
		    verify_css_classes : true,
		    verify_html : true,
		    plugins : "safari,media,advimage,fullscreen,paste,preview,imagemanager",
		    theme_advanced_toolbar_location : "top",
		    theme_advanced_toolbar_align : "center",
		    theme_advanced_path_location : "bottom",
		    theme_advanced_resizing : true,
		    theme_advanced_blockformats : "p,blockquote,h1,h2,h3,h4,h5,h6",
		    theme_advanced_buttons1 : "formatselect,bold,italic,underline,strikethrough,bullist,numlist,outdent,indent,link,unlink,media,image",
		    theme_advanced_buttons2 : "undo,redo,removeformat,cleanup,cut,copy,paste,pasteword,hr,charmap,fullscreen,preview,code",
		    theme_advanced_buttons3 : "",
		    extended_valid_elements : "img[class|src|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name|style|border=0],div[style|align]",
		    theme_advanced_styles : "Title=sw-ed-title;Subtitle=sw-ed-subtitle;Section Blue=sw-ed-section_blue;Section Red=sw-ed-section_red;Section Green=sw-ed-section_green;Small Text=sw-ed-small;Medium Text=sw-ed-medium;Normal Text=sw-ed-normal;Large Text=sw-ed-large;",
		    content_css: siteurl+"/editor.css",
		    external_image_list_url: siteurl+"/files/showfiles/"
		}
		);
/*
	function resizeEditorBox (editor) {
	   document.getElementById(editor.id + "_tbl").style.width = "100%";
	}
	   
	function toggletinyMCE(id) {
	   if (!tinyMCE.getInstanceById(id)) {
	      tinyMCE.execCommand('mceAddControl', false, id);
	   } else {
	      tinyMCE.execCommand('mceRemoveControl', false, id);
	   }
	}
*/

function initPage() {
    var Ajax =  new Request({
        evalScripts: true,
        async: false,
        useSpinner: true,
        spinnerTarget: $('popcontent'),
        onSuccess: function() {    
        	
        	if ($('popupbox')) {
        		$('popupbox').destroy();
        	}
        	
        	if ($('alertBox')) {
        		$('alertBox').destroy();
        	}
        	
        	var popup = new Element('div', {id:'popupbox', html:this.response.text});
        	popup.inject('body');
//        	$('popupbox').makeDraggable();
            
            // Make tooltips if needed
            var myTips = new Tips($$('.tips'));
                 
            if ($('popform')) {                            
                $('popform').addEvent('submit', function(f) {    
                    f.stop();
                    new Request({
                        useSpinner: true,
                        spinnerTarget: $('popcontent'),
               			url: $('popform').action,
               			onSuccess: function() {
                    		$('popupbox').set('html',this.response.text);
                    	}
              		}).post($('popform'));
                });                     
            }    
        }
    });

    $$('.poplink').addEvent('click',function(e){
        e.stop();
        Ajax.get(this.href);
    });	

    var myTips = new Tips($$('.tips'));
}


function reloadWindow() {
	var redirectURI = new URI(window.location);
	redirectURI.setData({coupon_id:'',alert:''},true);
	redirectURI.go();
}
