
// Cookie Functions

function get_cookie(name) {
	var cookie = document.cookie;
	if (cookie == "") return "";
	var start = cookie.indexOf(name + '=');
	if (start == -1) return "";
	start += name.length + 1;
	var end = cookie.indexOf(';', start);
	if (end == -1) end = cookie.length;
	var value = cookie.substring(start,end);
	return value;
}

function set_cookie(name,gap) {
	var expire = new Date();
	expire.setTime(expire.getTime() + gap * 1000);
	document.cookie = name + "=" + parseInt(expire.getTime() / 1000) + "; expires=" + expire.toGMTString() + "; path=/";
}

function get_cookie_nick() {
	var value = get_cookie("Transaction");
	if (value == "") return "";
	var values = value.split('%26');
	if (values.length <1) return "";
	return values[1];
}

function get_cookie_msisdn() {
	var value = get_cookie("Transaction");
	if (value == "") return "";
	var values = value.split('%26');
	if (values.length <2) return "";
	var chrs = values[2].split('g');
	var nr = '';
	for(var i = 0; i <chrs.length; i++) {
		nr = nr + String.fromCharCode(parseInt(chrs[i],16));
	}
	return nr;
}

function get_cookie_plus() {
	var value = get_cookie("Transaction");
	if (value == "") return "";
	var values = value.split('%26');
	if (values.length <3) return "";
	return values[3];
}

// Msisdn

function check_msisdn(msisdn,network_prefixes) {
  var ok = 0;
  if (msisdn.match(/^\d+$/)) {
    for (var i=0;i<network_prefixes.length;i++) {
      if (network_prefixes[i].indexOf(msisdn) == 0 || msisdn.indexOf(network_prefixes[i]) == 0) {
        if (msisdn.match(/^\d{10,20}$/)) {
          return 1;
        } else {
          return -1;
        }
      }
    }
  }
  return 0;
}

// Email

function check_email(email) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email) == false) {
		if (email.match(/(.+)\@(.+)\.(..+)/i) || !email.match(/^[a-zA-Z0-9\-\@\.]+$/i)) {
			return 0;
		}
	} else {
		return 1;
	}
	return -1;
}

// Community Teaser Functions

function resize_image_max_size(element,width,height) {
	ratio = height / width;
	if (element.height> 0) {
		if (element.height / element.width> ratio) {
			element.width=height/element.height*element.width;element.height=height;
		} else {
			element.width=width;
		}
	}
}

function load_image_max_size(src,width,height,no_zoom,return_code) {
	var img = new Image();
	img.src = src;
	ratio = height / width;
	var html_code = '';
	if (no_zoom && img.height > 0 && height >= img.height  && width >= img.width) {
		html_code = '<img src="'+img.src+'" width="'+img.width+'" alt="" />';
	} else if (img.height / img.width> ratio) {
		html_code = '<img src="'+img.src+'" height="'+height+'" alt="" />';
	} else if (img.height / img.width <= ratio && img.width != 0) {
		html_code = '<img src="'+img.src+'" width="'+width+'" alt="" />';
	} else {
		html_code = '<img src="'+img.src+'" width="'+width+'" onload="resize_image_max_size(this,'+width+','+height+');" alt="" />';
	}
	
	if (return_code) {
	  return html_code;
	} else {
	  document.write(html_code);
	}
}

function nickpage(nick, subpage) {
	var win = window.open("http://my.jippii.com/"+nick+"/"+subpage,"nickpage_"+nick);
	win.focus();
}

// Mobile Teaser Functions

function plf_play(type, id) {
  var params = (id || id == '0') ? type + "," + id :  type;
  var url = "/mobile/player/?" + params;
  document.getElementById('plf').src = url;
  return false;
}

function plf_stop() {
  var url = "about:blank";
  document.getElementById('plf').src = url;
  return false;
}

var block_mouse = 0;
var block_mouse_bV  = parseInt(navigator.appVersion);
var block_mouse_bNS = navigator.appName=="Netscape";
var block_mouse_bIE = navigator.appName=="Microsoft Internet Explorer";

function sorry(e) {
  if (block_mouse > 0) {
   	if (block_mouse_bNS && e.which > 1){
 	    alert("Right-mouse function is disabled");
 	    return false;
   	} else if (block_mouse_bIE && (event.button >1)) {
  	  alert("Right-mouse function is disabled");
 	    return false;
   	}
  }
}

document.onmousedown = sorry;
if (document.layers) window.captureEvents(Event.MOUSEDOWN);
if (window.sidebar) document.addEventListener( "MOUSEDOWN", sorry, true );
if (block_mouse_bNS && block_mouse_bV < 5) window.onmousedown = sorry;



// Mobile Qualified Devices
function qualified_devices(view_src) {
  var view_width = 470;
  var view_height = 439;
  var view_scroll = 'yes';
  var view_resize = 1;
  var view_status = 0;
  var view_border = 0;
  var view_name = 'jippii_qualified_devices';

  view_win = window.open(view_src, view_name,'WIDTH='+view_width+',HEIGHT='+view_height+',RESIZEABLE='+view_resize+',BORDER='+view_border+',STATUS='+view_status+',SCROLLBARS='+view_scroll);
  view_win.focus();

  return view_win;
}


// Error Displaying Functions

function show_error(error_place_id, error_text, zindex_offset) {
  
  var error = dom.byId(error_place_id);
  error.style.display = '';
  error.style.position = 'relative';
  if (!zindex_offset) zindex_offset = 0;
  var zindex = 500+zindex_offset;
  if (!error_text) error_text = '<img style="cursor:help;" src="/images/error.gif" alt="Error" title="" />';
  var text_html = '';
  
  text_html += '<table style="width:280px;padding: 0px 0px 0px 0px; ">';
  text_html += '   <tr>';
  text_html += '      <td rowspan="3" style="padding: 0px 0px 0px 0px;"><img src="/images/error/error_left.png" alt="" width="26" height="129" /></td>';
  text_html += '      <td colspan="2" style="padding: 0px 0px 0px 0px;"><img src="/images/error/error_header.png" alt="" width="233" height="10"/></td>';
  text_html += '      <td rowspan="3" style="padding: 0px 0px 0px 0px;"><img src="/images/error/error_right.png" alt="" width="21" height="129" /></td>';
  text_html += '   </tr>';
  text_html += '   <tr>';
  text_html += '      <td style="height:70px; background-image:url(/images/error/error_middle.png);padding: 0px 0px 0px 0px; "  colspan="2" >';
  text_html += '         <table style="height:70px; width:100%;padding: 0px 0px 0px 0px; " >';
  text_html += '            <tr>';
  text_html += '               <td style="height:20px;padding: 0px 0px 0px 0px; " class="error">Error:</td>';
  text_html += '               <td style="text-align:right; vertical-align:top;padding: 0px 0px 0px 0px; "><a onclick="hide_error(\''+error_place_id+'\')"><img src="/images/error/error_delete.gif" alt="" width="15" height="14" /></a></td>';
  text_html += '            </tr>';
  text_html += '            <tr>';
  text_html += '               <td colspan="2" style="padding: 0px 0px 0px 0px;">'+error_text+'</td>';
  text_html += '            </tr>';
  text_html += '         </table>';
  text_html += '      </td>';
  text_html += '   </tr>';
  text_html += '   <tr>';
  text_html += '      <td style="height:49px;padding: 0px 0px 0px 0px; " ><img src="/images/error/error_special.png" alt="" width="63" height="49" /></td>';
  text_html += '      <td style="height:49px;padding: 0px 0px 0px 0px; " ><img src="/images/error/error_footer.png" alt="" width="170" height="49" /></td>';
  text_html += '   </tr>';
  text_html += '</table>';
  
  error.innerHTML = '<div style="position:absolute;bottom:-3px;left:5px;z-index:'+zindex+';">'+text_html+'</div>';
  
  return true;
}

function hide_error(error_place_id) {
  
  var error = dom.byId(error_place_id);
  error.style.display = 'none';
  
  return true;
}

