var MAM = {
  validEmail: function (email) {
      var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
      if (! email.match(re)) { return false; }
      else { return true;}
  },
  getCookie: function ( name ) {
		// first we'll split this cookie up into name/value pairs
		// note: document.cookie only returns name=value, not the other components
		var a_all_cookies = document.cookie.split( ';' );
		var a_temp_cookie = '';
		var cookie_name = '';
		var cookie_value = '';
		var b_cookie_found = false; // set boolean t/f default f
			
		for ( i = 0; i < a_all_cookies.length; i++ )
		{
			// now we'll split apart each name=value pair
			a_temp_cookie = a_all_cookies[i].split( '=' );
			
			
			// and trim left/right whitespace while we're at it
			cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		
			// if the extracted name matches passed check_name
			if ( cookie_name == name )
			{
				b_cookie_found = true;
				// we need to handle case where cookie has no value but exists (no = sign, that is):
				if ( a_temp_cookie.length > 1 )
				{
					cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
				}
				// note that in cases where cookie is initialized but no value, null is returned
				return cookie_value;
				break;
			}
			a_temp_cookie = null;
			cookie_name = '';
		}
		
		if ( !b_cookie_found )
		{
			return null;
		}
  },
  setCookie: function (name, value, expires, path, domain, secure) {
		var today = new Date();
		today.setTime( today.getTime() );
		if ( expires ) {
			expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = name+'='+escape( value ) +
				( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
				( ( path ) ? ';path=' + path : '' ) +
				( ( domain ) ? ';domain=' + domain : '' ) +
				( ( secure ) ? ';secure' : '' );    
  },
	deleteCookie: function (name,path,domain) {
		if ( this.getCookie( name ) ) { document.cookie = name + '=' +
								( ( path ) ? ';path=' + path : '') +
								( ( domain ) ? ';domain=' + domain : '' ) +
								';expires=Thu, 01-Jan-1970 00:00:01 GMT'; }
	},
	popUp: function (url,w,h) {
		var s = Object.extend({
			resizable: true, toolbar: false,
			menubar : false, location: false,
			location: false, scrollbars: true,
			directories: false,
			name: "cdepop"
		},arguments[3]||{});
		//build the browser_info  	
		browser_info="width="+w+",height="+h;
		browser_info+=',top='+((screen.width-w)/2);
		browser_info+=',left='+((screen.height-h)/2);  	
		browser_info+= ',toolbar='    +(s.toolbar    ? '1':'0');
		browser_info+= ',location='   +(s.location   ? '1':'0');
		browser_info+= ',directories='+(s.directories? '1':'0');
		browser_info+= ',status='     +(s.status     ? '1':'0');
		browser_info+= ',menubar='    +(s.menubar    ? '1':'0');
		browser_info+= ',scrollbars=' +(s.scrollbars ? '1':'0');
		browser_info+= ',resizable='  +(s.resizable  ? '1':'0');
		var cdepop = window.open(url,s.name,browser_info); 
		if (!cdepop) {
  		alert("Popup blocker is enabled!");
		} else {
			if (window.focus) { cdepop.focus();}
			return cdepop;      
		}
	}
}

function eShow (eid) {
  document.getElementById(eid).style.display='block';
}

function eHide (eid) {
  document.getElementById(eid).style.display='none';
}

var prev = {mnu:null, img: null};
function categShow (mnu, img) {
  
  if ($(prev.mnu) && $(prev.mnu) !== $(mnu)) {
    $(prev.mnu).hide();
    $(prev.img).src= "images/bullet2-category.gif";
  }
  var ison = Element.visible(mnu);
  Element[(ison?'hide':'show')](mnu);
  $(img).src=ison ? "images/bullet2-category.gif" : "images/bullet-category.gif";
  prev.mnu = $(mnu); prev.img = $(img);
}

var FormCheck = {
  required: function (frm, elems) {
    var elemnames= arguments[2]||[];    
    for(i=0,j=elems.length; i<j; i++){
      if (frm[elems[i]].value.replace(/^\s+|\s+$/,'') == '') {
        if (elemnames[i]) alert(elemnames[i]+" is required!");
        return false
      }      
    }
    return true;
  }
};

var langtrans = function (code){
  MAM.setCookie('ln', code, 365, '/');
  top.location.reload();
};

MAM.subscribeGuest = function (form) {
  if (!form) return false;
  var email = form['txtEmail'].value;
  if (!MAM.validEmail( email ) ) {
    alert(email + " is not a valid email address");
    return false;
  }
  var action = form.action;
    new Ajax.Request(action, {
        method:'POST', asynchronous:true,
        parameters: $(form).serialize(),
        onComplete: function (req){
          var resp = req.responseText;
          if (resp == 'ok'){
            $('txtGuestSubscribe').hide();
            $('txtSubscribeSuccess').show();
          } else {
            alert(resp);
          }
        }
      });
}
