//  Copyright 2005 Eugene Kardash. All rights reserved.

var agt=navigator.userAgent.toLowerCase();

var gbNN = (navigator.appName.indexOf("Netscape") != -1);
var gbNN4 = (null!=document.layers);

var gbOpera = (agt.indexOf("opera")!=-1); // Opera (mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; en) opera 8.02)
var gbSafari = (agt.indexOf("safari") != -1); // Safari (mozilla/5.0 (macintosh;u; ppc mac os x; en) applewebkit/124 (khtml, like gecko) safari/125)
var gbNN6 = (agt.indexOf("mozilla/5")!=-1 && !gbSafari); // use as NN6/Mozilla/Firefox
var gbIE = (agt.indexOf("msie") != -1 && !gbOpera);
var gbIE5 = ((agt.indexOf("msie 5")!=-1 || agt.indexOf("msie 6")!=-1) && !gbOpera);
var gbWebTV = (agt.indexOf("webtv")!=-1);

gbDom = (gbNN6 || gbIE5 || gbOpera);

var WIN = (agt.indexOf("win")!=-1);
var MAC = (agt.indexOf("mac")!=-1);

//.........................................................................................
// is Acrobat Reader installed check
function acrobatInstalled() {
	result = false; 
	if (!gbIE || !WIN || gbOpera) {
		if (navigator.mimeTypes["application/pdf"]) {
			result = (navigator.mimeTypes["application/pdf"].enabledPlugin != null);
		}
	} else {
		document.write('<script language="VBScript">on error resume next\nresult = IsObject(CreateObject("PDF.PdfCtrl.5"))</script>');
	}
	return result;
}
//.........................................................................................
// is Macromedia Flash Player installed check
function flashInstalled(version) {
	result = false; 
	if (!gbIE || gbOpera || (MAC && gbIE5)) {
		if (navigator.plugins && navigator.plugins.length > 0) {
			x = navigator.plugins["Shockwave Flash"];
			if (x) {
				if (x.description) {
					if (x.description.charAt(x.description.indexOf('.')-1) >= version) {
						result = true;
					}
				}
			}
		}
	} else {
		document.write('<script language="VBScript">on error resume next\nresult = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.' + version + '"))</script>');
	}
	return result;
}
//.........................................................................................
// Generate email string
function supportEmail() {
	return "CustomerService" + '\x40' + "lanzy" + '\x2E' + "c" + '\x6F' + "m";
}
//.........................................................................................
// confirm functions

function myconfirm(_url,_action,_object) { 
	if (confirm('Do you really want to '+_action+' this '+_object+' ?')) 
			location.href = _url 
}

function doWarn(_url,_warn) { 
	if (confirm(_warn)) location.href = _url 
}

//.........................................................................................
// shorten the textareas

function textarealimit(fieldname, limit) {
	str = fieldname.value.trim();
	if (str.length >= limit-1) {
		alert('You have reached the space limit (' + limit + ' characters) for this field.');
		fieldname.value = str.substring(0,limit-2);
	}
}

// These functions will trim leading, trailing or all spaces from a string
// arg = the value you wish to have trimmed..

//===================================
function trimString(arg) {
//===================================
	str = this != window? this : str;
    str.replace(/^\s*/, '').replace(/\s*$/, ''); 
	return str;
}

//===================================
function ltrimString(arg) {
//===================================
	str = this != window? this : str;
    str.replace(/^\s*/, ''); 
	return str;
}

//===================================
function rtrimString(arg) {
//===================================
	str = this != window? this : str;
    str.replace(/\s*$/, ''); 
	return str;
}

//The above function is set up so that it can be added as a method to the 
//String.prototype:

String.prototype.trim = trimString;
String.prototype.ltrim = ltrimString;
String.prototype.rtrim = rtrimString;

//-----------------------------------------------------------------------------
// auto center popup window script

var win = null;
function NewWindow(mypage,myname,w,h,scroll){
	LeftPosition = (screen.width) ? (screen.width-w)/2.5 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2.5 : 0;
	settings =
	'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable,offscreenBuffering=false,status'
	win = window.open(mypage,myname,settings);
	win.offscreenBuffering = false;
	win.focus()
}

win = null;
function NewWindowWithBars(mypage,myname,w,h){
	LeftPosition = (screen.width) ? (screen.width-w)/2.5 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2.5 : 0;
	settings =
	'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars,resizable,toolbar,menubar,offscreenBuffering=false,status'
	win = window.open(mypage,myname,settings);
	win.offscreenBuffering = false;
	win.focus()
}

// in html page:
//<a href="http://www.google.com" onclick="NewWindow(this.href,'name','500','500','yes');return false">Google</a>

//-----------------------------------------------------------------------------
/* 	isNumeric function
	returns boolean
	isReal:
		1 - validation of Real number
		other - validation of Integer number
*/
function IsNumeric(sText, isReal) {
	var IsNumber = true, dotCount = 0;
	var Char1 = sText.charAt(0);

	if (isReal == "1") { 
		var ValidChars = "0123456789."; 
	} else { 
		var ValidChars = "0123456789"; 
	}
	if (ValidChars.indexOf(Char1) == -1 && Char1 != "-" ) {
		IsNumber = false;
	} else {
		for (i = 1; i < sText.length && IsNumber == true; i++) {
			Char1 = sText.charAt(i);
			if (ValidChars.indexOf(Char1) == -1) {
				IsNumber = false;
			} else {
				if (Char1 == ".") { dotCount++ }
				if (dotCount > 1) { IsNumber = false }
			}
		}
	}
	return IsNumber;
}

/*	validate value of html field
	dataType: 
		0 - Integer
		1 - real
*/
function inlineValidate(dataType, object) {
	var Char1 = '', newValue = '', isNum = false;
	switch(dataType) {
		case 0:
			var ValidChars = "-0123456789";
			isNum = IsNumeric(object.value,0); break;
		case 1:
			var ValidChars = "-0123456789.";
			isNum = IsNumeric(object.value,1); break;
	}
	if (!isNum) {
		alert('Enter valid value!');
		Char1 =  object.value.charAt(0);
		if (ValidChars.indexOf(Char1) != -1) { newValue += Char1 }
		for (i = 1; i < object.value.length; i++) {
			Char1 = object.value.charAt(i);
			if (ValidChars.indexOf(Char1) != -1 && Char1 != "-") { newValue += Char1 }
		}
		object.value = newValue;
	}
}

//-----------------------------------------------------------------------------
/*	rounding function
	
*/
var precision = 2;
function roundOff(value, precision)
{
	value = "" + value //convert value to string
	precision = parseInt(precision);
	var whole = "" + Math.round(value * Math.pow(10, precision)); //whole value with all the digits multiplied by 10^precision
	var decPoint = whole.length - precision;
	if(!(decPoint < 0)) { 		//simplest situation (whole number of digits >= precision value)
		if (decPoint == 0) { 	//whole number of digits = precision value
			result = "0";
		} else { 				//whole number of digits > precision value
			result = whole.substring(0, decPoint);
		}
		result += ".";
		result += whole.substring(decPoint, whole.length);
	} else { 					//whole number of digits < precision value (ex. value=0.04)
		result = whole / Math.pow(10, precision);
	}
	return result;
}

//.........................................................................................

// example of work:
// input: <p align="right">oldHTML: whatever here, will be cut anyway</p>
// output: <p align="right">newText</p>
function saveHtmlFormatting(oldHTML, newText) {
	var beginHTML = "", endHTML = "";
	oldHTML = oldHTML.replace(/.*(<.+>)(.*)(<\/.+>).*/i, "$1$2$3");
	beginHTML = RegExp.$1;
	endHTML = RegExp.$3;
	return beginHTML + newText + endHTML;
}
//.........................................................................................

function closeWindow() {
	window.close();
}

function historyBack() {
	history.back();
}

function waitCursor() {
	if (document.body != null)
		document.body.style.cursor = 'wait';
}
// set focus on specified field
function setFocus(formName, objName) {
	try {
		document.forms[formName].elements[objName].focus();
	} catch(e) {}
}

//.........................................................................................
// functions for formatting the negative amounts
//

function jsReplaceChar(item, OldChar, NewChar) {
	var r = "";
	for(var i=0; i<item.length; i++) {
		if (item.charAt(i) == OldChar) { 
			r += NewChar; 
		} else {
			r += item.charAt(i);
		}
	}
	return r;
}

function jsRemoveChar(item, needlessChar) {
	var r = "";
	for (var i=0; i<item.length; i++) {
		if (item.charAt(i) != needlessChar) r += item.charAt(i);
	}
	return r;
}

function formatParenthesesToNumber(value) {
	var sStr = value;
	var comma = ',', leftParenthesis = '(', rightParenthesis = ')';
	
	sStr = jsRemoveChar(sStr,comma);
	sStr = jsReplaceChar(sStr, leftParenthesis,'-');
	sStr = jsRemoveChar(sStr, rightParenthesis);
	
	return sStr;
}

function formatNumberToParentheses(value) {
	var sStr = value;
	var leftParenthesis = '(', rightParenthesis = ')';
	
	if (sStr.indexOf('-') == 0) {
		sStr = jsReplaceChar(sStr, '-', leftParenthesis);
		sStr += rightParenthesis;
	}
	
	return sStr;
}

//.........................................................................................
/** Remove all occurrences of a token in a string
*   s  string to be processed
*   t  token to be removed
*  returns new string
*/
function remove(s, t) {
	i = s.indexOf(t);
	r = "";
	if (i == -1) return s;
	r += s.substring(0,i) + remove(s.substring(i + t.length), t);
	return r;
}
//.........................................................................................	

