var rb_message_arr=new Array();
rb_message_arr['PROCESSING']='প্রক্রিয়াধীন....';
rb_message_arr['empty']='<b style="color:#FFFFFF">দয়াকরে {$f} ঘর পুরন করুন</b>';
rb_message_arr['incorrect']='<b style="color:#FFFFFF">১১.৯ -এর অধিক ইঞ্চি দেয়া যাবে না</b>';
rb_message_arr['invalid']='অস্বাভাবিক {$f}';
rb_message_arr['group']=new Array();
rb_message_arr['group'][0]='কাম্য ওজনের চাইতে কম';					//<18.5
rb_message_arr['group'][1]='ওজন স্বাভাবিক ও সুস্থ অবস্থা';				//18.50-24.9
rb_message_arr['group'][2]='কাম্য ওজনের চাইতে বেশি; ওজনাধিক্য';			//25-29.9
rb_message_arr['group'][3]='প্রথমিক স্থুলতা এবং ঝুকিপুর্ণ অবস্থা';			//30-34.9
rb_message_arr['group'][4]='মধ্যবর্তী স্থুলতা এবং নানা স্বাস্থ্য জটিলতা';		//35-39.9
rb_message_arr['group'][5]='রোগাগ্রস্থ চূড়ান্ত পর্যায়ের স্থুলতা';				//>40

var bmi_next_slide=0;
var bmi_time_out=''
var last_location='';
var isProcessingURL=false;
var loadedobjects='';
var SITE_TITLE="::. Ranna Banna .::";
var rootdomain="http://"+window.location.hostname+location.pathname;
var add_poss_timeout="";

function $i(id){return document.getElementById(id);}
function $n(n){return document.getElementsByName(n);}
function $t(tn){return document.getElementsByTagName(tn);}
function $ptr(ref){return (ref.nodeName.toLowerCase()=='tr')?ref:(ref.parentNode)?$ptr(ref.parentNode):null;}
function $pT(ref,t,c){if(typeof t=='undefined'){return t.parentNode;}if(!ref.parentNode){return null;}if(typeof c=='undefined'){c=0;}if(ref.parentNode.nodeName.toLowerCase()==t.toLowerCase() && c==0){return ref.parentNode;}else if(ref.parentNode.nodeName.toLowerCase()==t.toLowerCase()){c--;return $pT(ref.parentNode,t.toLowerCase(),c);}else{return $pT(ref.parentNode,t.toLowerCase(),c);}}

function loadobjs(){
	if (!document.getElementById)
		return;
	for (i=0; i<arguments.length-1; i++){
		var file=arguments[i];
		var fileref="";
		if(loadedobjects.indexOf(file) !=-1){
//			alert("already loaded");
			if(arguments[arguments.length-1]!='')
				eval(arguments[arguments.length-1])();
		}
		else if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
			if (file.indexOf(".js")!=-1){ //If object is a js file
				fileref=document.createElement('script');
				fileref.setAttribute("type","text/javascript");
				fileref.setAttribute("src", file);
			}
			else if (file.indexOf(".css")!=-1){ //If object is a css file
				fileref=document.createElement("link");
				fileref.setAttribute("rel", "stylesheet");
				fileref.setAttribute("type", "text/css");
				fileref.setAttribute("href", file);
			}
			
		}
		if (fileref!=""){
			document.getElementsByTagName("head").item(0).appendChild(fileref);
			loadedobjects+=file+" "; //Remember this object as being already added to page
		}
	}
}


function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function show_status(msg){
	if(typeof msg=='undefined'){
		$('#status_msg_div').hide();
		return;
	}
	$('#status_msg_div').html(msg);
	$('#status_msg_div').show();	
	var status_div=document.getElementById('status_msg_div');
	//status_div.style.top=iecompattest().scrollTop+'px';	
	//status_div.style.left = '0px';
}

$.ajaxSetup({
  cache	: false
});

function GetUP(name,url)		//Ger Url Parameter
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var u=url||window.location.href;
  var results = regex.exec(u);
  if( results == null )
    return "";
  else
    return results[1];
}

function convertUrl(ur){
	if(typeof ur !='undefined'){
		var h=ur;
		
		h=h.replace(rootdomain,"");
		
		var file_name=GetUP('r',h);
		var function_name=GetUP('f',h);
		var left_url="";
		
		var u="";
		if(file_name!=""){
			u=rootdomain+"#"+file_name;
			h=h.replace("r="+file_name,"")
		}
		if(function_name!=""){
			u=u+"#"+function_name;
			h=h.replace("f="+function_name,"")
		}
			h=h.replace("?","");
			h=h.replace("&&","&");
			h=h.replace("&","/");
			h=h.replace(new RegExp("[/]+$", "g"), "");
		return (u+h);	
	}
	else{
		return "";
	}
							  
}

function position_floting_add(t,b,a){
	try{
	var ie=document.all && !window.opera
	var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
	var docheight=(ie)? standardbody.clientHeight: window.innerHeight
	var fotter_top=$(b).offset({ scroll: false }).top
	var banner_height=a.offsetHeight;
	var bottom_position=$(t).offset({ scroll: false }).top+banner_height;
	
	if(fotter_top-5<=bottom_position){
		$(a).css("position", "");
		return;
	}
	var top_possition=document.body.scrollTop+docheight-banner_height
	var top_offset=(docheight+$(window).scrollTop());	
	//show_status($(a).offset().top+">>"+top_possition+">>"+bottom_position);
	
	if  (top_offset > bottom_position && top_offset<fotter_top){
		  $(a).stop()
		  $(a).css("position", "absolute");
		   //$(a).css("top",top_possition);
		   if($(a).offset().top!=top_possition)
		   		$(a).animate({top: top_possition}, 500);
	}
	
	if(top_offset>=fotter_top ){
		   $(a).stop()//Stop Previous animation
		   $(a).css("position", "absolute");
		   if($(a).offset().top!=(fotter_top-banner_height))
		   $(a).animate({top: fotter_top-banner_height}, 500);
		  // $(a).css("top",fotter_top-banner_height);
	}
	
	//show_status(top_offset+">>"+top_possition+">>"+bottom_position);
	
	if(top_offset <= bottom_position){
	   $(a).css("position", "");
	  // $(a).css("top", "0px");
	}	
	}
	catch(exception){
	  if(add_poss_timeout!=""){
			clearTimeout(add_poss_timeout);
			add_poss_timeout="";
		}
	  add_poss_timeout=setTimeout(fixup_add_poss,600);
	}
}

function fixup_add_poss(){
	var ie=document.all && !window.opera
		position_floting_add(".smartBannerLeft","#tbl_fotter",document.getElementById("bannerLeft"));
		position_floting_add(".smartBannerRight","#tbl_fotter",document.getElementById("bannerRight"));
		if(add_poss_timeout!=""){
			clearTimeout(add_poss_timeout);
			add_poss_timeout="";
		}	
		add_poss_timeout=setTimeout(fixup_add_poss,600);
}

function initAjaxLink(){
	$(".ajaxlink").unbind('click', parseUrl);
	$('.ajaxlink').click(parseUrl);	
	initInpageLink();
}

function initInpageLink(){
$('.inpageLink').click(function() {
          if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')&& location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target|| $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
              var targetOffset = $target.offset().top-15;
              $('html,body').animate({scrollTop: targetOffset}, 3000,'easeOutBounce');
             return false;
         	}
  		} 
	});  	
}
function parseUrl(){
	if(isProcessingURL){		//If previously processing any url
		return false;
	}
	
	var el=$(this);
	var link_url=el.attr('href');	
	
	document.title=SITE_TITLE;		

	if(location.hash=='' && link_url==null){
		return false;
	}
	
	var arr=location.hash.split('#');
	
	if(arr.length<1){
		return false;
	}
	var hashurl=arr[1];
	if(hashurl==''){
		return false;	
	}
	//alert(hashurl)	;
	
	if(link_url==null){
		//return false;
	}else
	{
		var h=convertUrl(link_url);
		window.location=h;
		if(location.hash==''){
			return false;
		}
		var lh=location.hash.replace('%23','#');
		var arr=lh.split('#');
		if(arr.length<1){
			return false;
		}
		var hashurl=arr[1];
		if(hashurl==''){
			return false;	
		}
	}
	
	
	isProcessingURL=true;
	var par_arr=hashurl.split('/');
	
	window.location=location.hash.replace('.php','');
	var ex_p=(par_arr.length>1)?("&"+par_arr[1]):'&prob=r';
	
	var requesturl='parseurl.php?p='+par_arr[0]+ex_p;
	show_status(rb_message_arr['PROCESSING']);
	//alert(requesturl);
	requesturl=requesturl.replace('%23','#');	//Fix for safari
	$.ajax({
		   type		: "GET",
		   url		: requesturl,
		   success	: function(x){
			  document.getElementById('main_body_content').innerHTML=x;
			  if(arr.length>2)
			  	if(typeof arr[2]=='function')
			  		arr[2]();
				else
					eval(arr[2]+'()');
			
			
			last_location=location;	
			//Re initialize the link for lodaded page
			  initAjaxLink();
			  //Fix Png
			  correctPNG();
			  show_status();
			  
			 //Position the page
			 $target = $('[name=topanchor]');
			 if ($target.length) {
				  var targetOffset = $target.offset().top-10;
				  $('html,body').animate({scrollTop: targetOffset}, 1000);
			 }
			 //position the add
			 add_poss_timeout=setTimeout(fixup_add_poss,1000);
			 isProcessingURL=false;
		   },
		   	error:function(xmlHttp, textStatus, errorThrown){
			   show_status();
			   alert('error in communication, error no = '+ xmlHttp.status);
			   isProcessingURL=false;
		   }
		 });		
	return false;
}


function get_content(u,callback)
{
	if(u==''||!u)
		return '';
	$.ajax({
		   type		: "GET",
		   url		: u,
		   success	: function(x){
			  eval(callback)(x);
		   },
		   	error:function(xmlHttp, textStatus, errorThrown){
			   show_status();
			   alert('error in communication, error no = '+ xmlHttp.status);
		   }
		 });
}


function Round(num,n){	
	if(isNaN(num))
		return '0.00';
	return Math.round(num * Math.pow(10,n))/Math.pow(10,n);
}

function resetBmiChart(ref){
	if((ref.bmi_feet.value=='' && ref.bmi_inche.value=='') || ref.bmi_weight.value==''){
		$('.bmiSlide').cycle(0);
	}
}

function BmicheckEntry(){
	if((ref.bmi_feet.value=='' && ref.bmi_inche.value=='') || ref.bmi_weight.value==''){
		document.getElementById('bmi_result_span').innerHTML='';
	}
	document.getElementById('bmi_result_span').innerHTML='';	
}

function displayMessage(msg){
	document.getElementById('bmi_message_div').innerHTML=msg;
	if(document.getElementById('result_main_contain').style.display=='none'){
			mycycle.cycle(1);
			bmi_time_out=setTimeout(function(){mycycle.cycle(0);},5000);
			return false;
	}
	else{
		try{
			clearInterval(bmi_time_out);
		}
		catch(e){
			//nothing to do	
		}
		return false;
	}
}

function calculateBMI(ref){
	if(ref.bmi_feet.value=='' && ref.bmi_inche.value==''){
		ref.bmi_feet.focus();
		document.getElementById('bmi_result_span').innerHTML='n/a'
		var msg=rb_message_arr['empty'].replace('{$f}','উচ্চতার');
		displayMessage(msg);
		return false;
	}

	if(bn2en_num(ref.bmi_inche.value)>=11.9){
		ref.bmi_inche.focus();
		ref.bmi_inche.select();
		document.getElementById('bmi_result_span').innerHTML='n/a'
		var msg=rb_message_arr['incorrect'];
		displayMessage(msg);
		return false;
	}
	
	if(ref.bmi_weight.value==''){
		ref.bmi_weight.focus();
		document.getElementById('bmi_result_span').innerHTML='n/a'
		var msg=rb_message_arr['empty'].replace('{$f}','ওজনের');
		displayMessage(msg);
		return false;
	}	

	var height=((bn2en_num(ref.bmi_feet.value)*12)+bn2en_num(ref.bmi_inche.value))*2.54/100;
	var weight=bn2en_num(ref.bmi_weight.value);
	var bmi=weight/(height*height);
	document.getElementById('bmi_result_span').innerHTML=en2bn_num(Round(bmi,2));
	var id=0;
	var sw;
	
	bmi=Round(bmi,1);
	if(bmi<18.5)
	{
		id=0;
		sw=(18.5*height*height)-weight;				//Need to be increase
		sw='পরামর্শঃ ওজন '+en2bn_num(Round(sw,2))+' কেজি বাড়ানো উচিত';
	}
	else if(bmi>=18.5 && bmi<=24.9){
		id=1;
		sw='';
	}
	else{	
		sw=weight-(24.9*height*height);				//Need to be reduce
		sw='পরামর্শঃ ওজন '+en2bn_num(Round(sw,2))+' কেজি কমানো উচিত';
		if(bmi>=25 && bmi<=29.9){
			id=2;	
		}
		else if(bmi>=30 && bmi<=34.9){
			id=3;	
		}
		else if(bmi>=35 && bmi<=39.9){
			id=4;	
		}
		else
		{
			id=5;	
		}
	}
	
	document.getElementById('bmi_message_div').innerHTML='মন্তব্যঃ '+rb_message_arr['group'][id]+'<br />'+sw;
	$('.bmiSlide').cycle(1);	
	$.post("bmicount.php");
}

function watermark(ref,img){
	 if (ref.value == '') { ref.style.background = '#FFFFFF url('+img+') left no-repeat'; }
}

 var enbnarr=new Array();

	enbnarr['0']='\u09e6';//'০'; 
	enbnarr['1']='\u09e7';//'১';
	enbnarr['2']='\u09e8';//'২';
	enbnarr['3']='\u09e9';//'৩';
	enbnarr['4']='\u09ea';//'৪';
	enbnarr['5']='\u09eb';//'৫';
	enbnarr['6']='\u09ec';//'৬';
	enbnarr['7']='\u09ed';//'৭';
	enbnarr['8']='\u09ee';//'৮';
	enbnarr['9']='\u09ef';//'৯';
	enbnarr['.']='.';//'৯';
	
	//Decode
	enbnarr['\u09e6']='0';//'০'; 
	enbnarr['\u09e7']='1';//'১';
	enbnarr['\u09e8']='2';//'২';
	enbnarr['\u09e9']='3';//'৩';
	enbnarr['\u09ea']='4';//'৪';
	enbnarr['\u09eb']='5';//'৫';
	enbnarr['\u09ec']='6';//'৬';
	enbnarr['\u09ed']='7';//'৭';
	enbnarr['\u09ee']='8';//'৮';
	enbnarr['\u09ef']='9';//'৯';
	enbnarr['.']='.';//'৯';

function insertBAtCursor(myValue,myField) {
	
	if (document.selection) {
		//alert("hello2");
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		sel.collapse(true);
		sel.select();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == 0) {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;
		startPos = (startPos == -1 ? myField.value.length : startPos );
		myField.value = myField.value.substring(0, startPos)
		+ myValue
		+ myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
		myField.scrollTop = scrollTop;
	} else {
		var scrollTop = myField.scrollTop;
		myField.value += myValue;
		myField.focus();
		myField.scrollTop = scrollTop;
	}
}


function pastBangla(ref){
	var prev_value=ref.value;
	setTimeout(function(){ ref.value=en2bn_num(ref.value)},0);
	return false;
}

function bn2en_num(num){
	var str='';
	var cur='';
	for(var i=0,j=num.length; i<j; i++)
		{
			cur=num.substr(i,1);
			str+=(cur=='.')?'.':(cur.charCodeAt(0)<127)?((cur.charCodeAt(0)>47 && cur.charCodeAt(0)<58)?cur:''):(enbnarr[cur]||'');
		}
	var val=parseFloat(str);	
	return (isNaN(val))?0:val;
}

function en2bn_num(n){
	var str='';
	var cur='';
	var num=""+n;
	for(var i=0,j=num.length; i<j; i++)
		{
			cur=num.substr(i,1);
			str+=(cur.charCodeAt(0)>127)?cur:(enbnarr[cur]||'');
		}
	return str;
}

function aceptBangla(e,ref){
   var keynum;
   var keychar;
   var numcheck;
   
   if(e.which) 	// Netscape / Firefox / Opera
   {
      keynum = e.which;
   }
   else if(window.event) // IE
   {
      keynum = e.keyCode;
   }
   if(keynum==8 || keynum==9 || keynum==13){
   	return keynum;
   }
   keychar = String.fromCharCode(keynum); 
   numcheck = /[\d\.]/;
   var c=numcheck.test(keychar);
	
	if (!c)  //Oh my god :-( no bangla equivalent for this keystroke
	{
		return false; //return a null value
	}
	else
	{
		insertBAtCursor(enbnarr[""+keychar],ref);		//Hooray  - we've found bangla equivalent
		return false;  
	}
}


function initsubscription(){
	//Load all required javascript first
	$.getScript("scripts/bk.js", function(){
		loadobjs('scripts/validate.js','');
		loadobjs('scripts/messages.js','');
		loadobjs('scripts/rajax.js','');
		loadobjs("scripts/array.js",'');
		loadobjs("scripts/subscription_form.js",'subscription_form_setup');});
}

function initfoodvaluetbl(){
	//Load all required javascript first
	loadobjs("scripts/food_value.js",'');
}