// various configuration parameters

var conf_date_format = 'y-m-d';

var _whitespace = ' \n\r\t';
var _digits = '0123456789';
var _hexDigits = '0123456789ABCDEF';
var _smallChars = 'abcdefghijklmnopqrstuvwxyz';
var _capChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var _chars = _smallChars + _capChars;
var _protocols = new Array ('http://', 'file://', 'https://', 'ftp://');
var _image_extensions = new Array ('.gif', '.jpg', '.jpeg');


/**
 *	Common Functions
 */
 
function isEmpty(s) {
	if (s.length == 0) return true;
	if (trim(s).length == 0) return true;
	return false;
}

 

/**
 *	Returns the length of the chars that are in chars.
 */
function getValidCharsLength (t, chars, startAt) {
	for(var i = startAt; i < t.length; i++)
		if (chars.indexOf(t.charAt(i)) == -1)
			break;
	return (i - startAt);
}

function isEmail (t) {
	t = trim(t);
	i = getValidCharsLength (t, _smallChars + _capChars + _digits + '._-', 0);
	if (i < 1) return false;
	if (getValidCharsLength (t, '@', i) != 1) return false;
	dots = -1;
	do {
		len = getValidCharsLength (t, _smallChars + _capChars + _digits + '_-', ++i);
		if ((len < 2) || (len > 64)) return false;
		i += len;
		dots++;
	} while (getValidCharsLength (t, '.', i) == 1)
	
	return ((len < 6) && (len > 1) && (i >= t.length) && (dots > 0));
}

function isPassword(s) {
	return true;
}
	
function trim(s) {
	var i = 0;
	while(isspace(s.charAt(i))) i++;
	if (i == s.length) return new String('');
		
	var j = s.length - 1;
	while(isspace(s.charAt(j))) j--;
		
	return s.substring(i, j + 1);
}
	
function isspace(c) {
	if (c == ' ') return true;
	if (c == '\n') return true;
	return false;
}
	
function isFloat(string) {
	return isNaN(parseFloat(string));
}

/**
 * Checks whether parameter is a number.
 */
function isNumber(s) {
	var v = trim(s);
	var i, c, we_had_dot = false;
	
	for(i = 0; i < v.length; i++) {
		c = v.charAt(i);
		if (i == 0) if ((c == '-')||(c == '+')) continue;
		if ((c == '.')&&(we_had_dot == false)) {
			we_had_dot = true;
			continue;
		}
		if ((c < '0') || (c > '9')) return false;
	}
	
	return true;
}

/**
 *
 */
function verifyDate(day, month, year) {
	if ( isEmpty(day.value) || isEmpty(month.value) || isEmpty(year.value) )
		return false;
	
	var _day = parseInt(day.value, 10);
	var _month = parseInt(month.value, 10);
	var _year = parseInt(year.value, 10);
	
	// alert('[verifyDate checkpoint 1] ' + _day + '-' + _month + '-' + _year);
	
	if (isNaN(_day) || isNaN(_month) || isNaN(_year))
		return false;
	
	if ((_month < 1) || (_month > 12)) return false;
	
	var max_day_count;
	switch(_month) {
		case  1 :
		case  3 :
		case  5 :
		case  7 :
		case  8 :
		case 10 :
		case 12 :
			max_day_count = 31;
			break;
			
		case  2 :
			if ((_year % 4) == 0) max_day_count = 29;
			else max_day_count = 28;
			break;
			
		default :
			max_day_count = 30;
	}
	
	if ((_day < 1) || (_day > max_day_count)) return false;
	return true;
}

/**
 *
 */
function verifyDate2(_date) {
	if (isEmpty(_date))
		return false;
	
	var _day = parseInt(_date.substring(8, 10), 10);
	var _month = parseInt(_date.substring(5, 7), 10);
	var _year = parseInt(_date.substring(0, 4), 10);
	
	// alert('[verifyDate checkpoint 1] ' + _day + '-' + _month + '-' + _year);
	
	if (isNaN(_day) || isNaN(_month) || isNaN(_year))
		return false;
	
	if ((_month < 1) || (_month > 12)) return false;
	
	var max_day_count;
	switch(_month) {
		case  1 :
		case  3 :
		case  5 :
		case  7 :
		case  8 :
		case 10 :
		case 12 :
			max_day_count = 31;
			break;
			
		case  2 :
			if ((_year % 4) == 0) max_day_count = 29;
			else max_day_count = 28;
			break;
			
		default :
			max_day_count = 30;
	}
	
	if ((_day < 1) || (_day > max_day_count)) return false;
	return true;
}


/**
 * Prepare date form fields printed with print_form_date1()
 */
// todo: in development
function createDate1(outfield, infield, required) {
// todo: for print_form_date1()
	outfield.value = '9999-01-01';
	
	_date = infield.value;
	
	// todo: parse for incomplete date
	var _day = parseInt(_date.substring(0, 2), 10);
	var _month = parseInt(_date.substring(3, 2), 10);
	var _year = parseInt(_date.substring(6, 4), 10);
	
	if ((required != 'no') || (isEmpty(_date) != false))
		if (verifyDate(_day, _month, _year) == false) return false;
	
	return createDate(outfield, _day, _month, _year);
}

/**
 * Prepare date form fields printed with print_form_date2/3()
 */
function createDate(outfield, day, month, year) {
	outfield.value = '9999-01-01 00:00:00';
	
	// accept no date at all
	if (isEmpty(day.value) && isEmpty(month.value) && isEmpty(year.value)) return true;
	
	// alert(day.value + '-' + month.value + '-' + year.value);
	
	if (verifyDate(day, month, year) == false) return false;
	
	var _day = parseInt(day.value, 10);
	var _month = parseInt(month.value, 10);
	var _year = parseInt(year.value, 10);
	
	var mzero = '';
	var dzero = '';
	
	if (_month < 10) mzero = '0';
	if (_day < 10) dzero = '0';
	
	outfield.value = '' + _year + '-' + mzero + _month + '-' + dzero + _day;
	
	return true;
}


/**
 * TODO : VERIFY TIME
 */
 function verifyTime(minutes, hours) {
 	return true;
 }
 
/**
 * Formats both date and time values
 */
function createDateTime(outfield, minutes, hours, day, month, year) {
	// formats date
	if (createDate(outfield, day, month, year) == false) return false;
	
	if (verifyTime(minutes.value, hours.value) == false) return false;
	
	var mzero = '';
	var hzero = '';
		
	var _minutes = parseInt(minutes.value, 10);
	var _hours = parseInt(hours.value, 10);
	
	if (_minutes < 10) mzero = '0';
	if (_hours < 10) hzero = '0';
	
	outfield.value += ' ' + hzero + _hours + ':' + mzero + _minutes + ':00';
	
	return true;
}


function printDateTime() {
  var today = new Date();
  var month = today.getMonth()+1;
  var year = today.getYear();
  var day = today.getDate();
  if(day<10) day = "0" + day;
  if(month<10) month= "0" + month; 
  if(year<1000) year+=1900;

  var curHour = today.getHours()
  var curMin = today.getMinutes()
  var curSec = today.getSeconds()
  var curTime = 
    ((curHour < 10) ? "0" : "") + curHour + ":" 
    + ((curMin < 10) ? "0" : "") + curMin + ":" 
    + ((curSec < 10) ? "0" : "") + curSec 
  document.write(curTime);     
  document.write(" - " + day + ". " + month +". " + year + ".")

}

function makeWapFriendly(text){
      waptext = text.replace(/<P/g, "<p");
	    waptext = waptext.replace(/<\/\P>/g, "</p>");
	    waptext = waptext.replace(/<BR>/g, "<br/>");
	    waptext = waptext.replace(/<br>/g, "<br/>");
	    waptext = waptext.replace(/<\/\A>/g, "</a>");
	    waptext = waptext.replace(/<A/g, "<a");
      waptext = waptext.replace(/<STRONG>/g, "<strong>");
	    waptext = waptext.replace(/<\/\STRONG>/g, "</strong>");	 
      waptext = waptext.replace(/<FONT/g, "<font");
	    waptext = waptext.replace(/<\/\FONT>/g, "</font>");	
      waptext = waptext.replace(/<H1>/g, "<h1>");
	    waptext = waptext.replace(/<\/\H1>/g, "</h1>");	
      waptext = waptext.replace(/<H2>/g, "<h2>");
	    waptext = waptext.replace(/<\/\h2>/g, "</h2>");	
      waptext = waptext.replace(/<H3>/g, "<h3>");
	    waptext = waptext.replace(/<\/\H3>/g, "</h3>");	
      waptext = waptext.replace(/<B>/g, "<b>");
	    waptext = waptext.replace(/<\/\B>/g, "</b>");	
      waptext = waptext.replace(/<I>/g, "<i>");
	    waptext = waptext.replace(/<\/\I>/g, "</i>");	
      waptext = waptext.replace(/<U>/g, "<u>");
	    waptext = waptext.replace(/<\/\U>/g, "</u>");	  
      waptext = waptext.replace(/<UL>/g, "<ul>");
	    waptext = waptext.replace(/<\/\UL>/g, "</ul>");	
      waptext = waptext.replace(/<OL>/g, "<ol>");
	    waptext = waptext.replace(/<\/\OL>/g, "</ol>");	
      waptext = waptext.replace(/<LI>/g, "<li>");
	    waptext = waptext.replace(/<\/\LI>/g, "</li>");	
      waptext = waptext.replace(/<BLOCKQUOTE/g, "<blockquote");
	    waptext = waptext.replace(/<\/\BLOCKQUOTE>/g, "</blockquote>");	  
      waptext = waptext.replace(/<EM>/g, "<em>");
	    waptext = waptext.replace(/<\/\EM>/g, "</em>");	                            	    
      return waptext;

}

function getFlash(file_name, width, height, url, align) {
	var text = new String();
	text = '';
	text += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" ZINDEX:10 width="'+width+'" height="'+height+'" align="'+align+'">';
  	text += '<param name="movie" value="g/images/'+file_name+'">';
	text += '<param name="quality" value="high">';
	if (window.navigator.mimeTypes && window.navigator.mimeTypes["application/x-shockwave-flash"]){ 
		text = '<embed src="g/images/'+file_name+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'" align="'+align+'"></embed>';
	} else {
		file_name = file_name.replace('.swf','.jpg')
	
		if (url != '') {
			text+= '<a href="'+url+'">';
		}
		text += '<img  src="g/images/'+file_name+'" width="'+width+'"  height="'+height+'" border="0" vspace="0" hspace="0" align="'+align+'">';
		if (url != '') {
			text+= '</a>';
		}
		
	}
	text += '</object>';
	document.write(text);
}

function getFlashWithParam(file_name, width, height, url, align, param) {
	var text = new String();
	text = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" ZINDEX:10 width="'+width+'" height="'+height+'" align="'+align+'">';
  	text += '<param name="movie" value="g/images/'+file_name+'?mycontent='+ param +'">';
	text += '<param name="quality" value="high">';
	if (window.navigator.mimeTypes && window.navigator.mimeTypes["application/x-shockwave-flash"]){ 
		text = '<embed src="g/images/'+file_name+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'" align="'+align+'"></embed>';
	} else {
		file_name = file_name.replace('.swf','.jpg')
	
		if (url != '') {
			text+= '<a href="'+url+'">';
		}
		text += '<img  src="g/images/'+file_name+'" width="'+width+'"  height="'+height+'" border="0" vspace="0" hspace="0" align="'+align+'">';
		if (url != '') {
			text+= '</a>';
		}
		
	}
	text += '</object>';
	document.write(text);
} 

/**
 * Short for document.getElementById
 *
 * @access public
 * @param string id ID of the element
 * @return object DOM element
 **/
function ebid(id) {
	return document.getElementById(id);
}
