function open_popup(url, width, height) {
	popup=window.open(url, 'Dialog_box', 'width='+width+',height='+height+',resizable,scrollbars')
  popup.focus();
}

function open_popup2(url, width, height) {
  popup=window.open(url, 'Dialog_box2', 'width='+width+',height='+height+',resizable,scrollbars')
  popup.focus();
}

function open_popup_full(name,url) {
  popup=window.open(url,name,'fullscreen');
  popup.focus();
}


function toggle_display(id) {
  var display_div = document.getElementById(id);
			
	if (display_div.style.display == 'none') {
		display_div.style.display = 'block';
	} else {
		display_div.style.display = 'none';
	}
}

function update_calling_page(url) {
  if(opener!==null) {	
		if(url=='ORIGINAL') {
			opener.location.reload();
		} else {
			if(url!='') opener.location.href = url;
		}
	}
	self.close();	
}

function update_calling_page_alt(url,alt_url) {
  if(opener!==null) {	
		if(url=='ORIGINAL') {
			opener.location.reload();
		} else {
			if(url!='') opener.location.href = url;
		}
		self.close();
	} else {
	  if(alt_url!='') window.location=alt_url;
	}  
}

function mysql_date_convert(str) {
	var year=str.substr(0,4);
  var month=str.substr(5,2);
  var day=str.substr(8,2);
  
  date = new Date(year,month-1,day);
  return date;
}

function js_date_str(date,format) {
	var date_str='';
	var year=date.getFullYear();
  var month=date.getMonth()+1;
  var month_str;
  var day=date.getDate();
  var day_str;
    
	if(format=='MYSQL') {
		month_str=month.toString();
		if(month_str.length==1) month_str='0'+month_str;
		day_str=day.toString();
		if(day_str.length==1) day_str='0'+day_str;
		
	  date_str=year.toString()+'-'+month_str+'-'+day_str;
  }	
	 
  return date_str;
}

function check_valid_email(email) {
   var email_reg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
   var regex = new RegExp(email_reg);
   return regex.test(email);
}

function is_numeric(str) {
  var ValidChars = "0123456789.";
  for(i=0; i<str.length; i++) {
    if(ValidChars.indexOf(str.charAt(i))==-1) return false;
  }
  return true;
}

function date_diff(date_from,date_to,diff_output) {
	var calc_diff=0;
	var interval=0;
	
	if(date_from<date_to) {	
	  if(diff_output=='DAYS') {
	    interval=1000*60*60*24; //one day in milliseconds
		  calc_diff=Math.ceil((date_to.getTime()-date_from.getTime())/interval);
	  }	
  }
  
  return calc_diff;
}

/*
base ajax function.
url : is php/html file that returns response text
param : is in 'xxx=123&yyy=456' url format
handler : is the function name to call with returned response text
handle_id : is div/span container id="handle_id" which will receive response text (but can also just be a marker)
*/
function xhr(url,param,handler,handle_id){
  var ro=window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
  ro.open('POST',url,true);
  ro.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  ro.onreadystatechange=function(){if(ro.readyState==4) handler(ro.responseText,handle_id)};
  ro.send(param);
}

