/* 
 * Teleuco Web Framework
 * /home/js/function
 *
 * @copyright 	Copyright (C) 2010 Teleuco s.r.l. (http://teleuco.com)
 * @license 	Commercial
 * @version	1.0.1
 * @package	Teleuco Web Framework 1.0.1 - js
 *
 */

function isEven(number)
{
	if (isNaN(number) == false) {
		return (number %2 == 1 ?  true : false);
	} else {
		return null;
	}
}

/**
/ THIRD FUNCTION
* getPageSize() by quirksmode.com
*
* @return Array Return an array with page width, height and window width, height
*/
function getPageSize() {
  var xScroll, yScroll;
  if (window.innerHeight && window.scrollMaxY) {
    xScroll = window.innerWidth + window.scrollMaxX;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }
  var windowWidth, windowHeight;
  if (self.innerHeight) {	// all except Explorer
    if(document.documentElement.clientWidth){
      windowWidth = document.documentElement.clientWidth;
    } else {
      windowWidth = self.innerWidth;
    }
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }
  // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight){
    pageHeight = windowHeight;
  } else {
    pageHeight = yScroll;
  }
  // for small pages with total width less then width of the viewport
  if(xScroll < windowWidth){
    pageWidth = xScroll;
  } else {
    pageWidth = windowWidth;
  }
  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
  return arrayPageSize;
}

function validateRequired(field) {
  with (field) {
    if (value==null || value=="") {
      return false;
    } else {
      return true;
    }
    }
}

function validateEmail(field) {
  with (field) {
    atpos = value.indexOf("@");
    dotpos = value.lastIndexOf(".");
    if (atpos<1 || dotpos-atpos<2) {
      return false;
    } else {
      return true;
    }
    }
}

function stringToBoolean(string) {
  string = new String(string);
  switch(string.toLowerCase()){
    case "true": case "yes": case "1":
      return true;
    case "false": case "no": case "0": case null:
      return false;
    default:
      return Boolean(string);
  }
}

function sizeToString(bytes) {
	/*var postfix = 'B', size = new Number(bytes);
	
	if (size > 1024) {
		size = size / 1024;
		//size.toFixed();
		postfix = 'kB';
	}
	
	if (size > 1024) {
		size = size / 1024;
		//size.toFixed();
		postfix = 'MB';
	}
	
	if (size > 1024) {
		size = size / 1024;
		//size.toFixed();
		postfix = 'GB';
	}
	
	
	size = size.toFixed(2);
	
	return size + '&nbsp;' + postfix;*/
    var i = -1;                                    
    do {
        bytes = bytes / 1024;
        i++;  
    } while (bytes > 99);
    
    return Math.max(bytes, 0.1).toFixed(2) + ['kB', 'MB', 'GB', 'TB', 'PB', 'EB'][i];
}

function text2html(text){
    return text.replace(/\n/gi, '<br />')
                .replace(/'/gi, '&#39;')
                .replace(/’/gi, '&#39;')
//                .replace(/"/gi, '&quot;')
                .replace(/à/gi, '&agrave;')
                .replace(/è/gi, '&egrave;')
                .replace(/é/gi, '&eacute;')
                .replace(/ì/gi, '&igrave;')
                .replace(/ò/gi, '&ograve;')
                .replace(/ù/gi, '&ugrave;')
                .replace(/€/gi, '&euro;')
                //.replace(/&/gi, '&amp;')
                .replace(/©/gi, '&copy;');
}

function html2text(html){
	html = new String (html);
    return html.replace(/<br \/>/gi, '\n').replace(/<br>/gi, '\n')
                .replace(/&#39;/gi, '\'')
                .replace(/&#34;/gi, '\"')
                .replace(/&agrave;/gi, 'à')
                .replace(/&egrave;/gi, 'è')
                .replace(/&eacute;/gi, 'é')
                .replace(/&igrave;/gi, 'ì')
                .replace(/&ograve;/gi, 'ò')
                .replace(/&ugrave;/gi, 'ù')
                .replace(/&euro;/gi, '€')
                .replace(/&copy;/gi, '©');
}

