// --- changes measurement values 
function kw2le() {
	document.getElementById('le').value = Math.round(parseFloat(document.getElementById('kw').value) * 13.605)/10;
}

function le2kw() {
	document.getElementById('kw').value = Math.round(parseFloat(document.getElementById('le').value) * 7.35)/10;
}

function m2foot(v1,v2) {
	document.getElementById(v2).value = Math.round(parseFloat(document.getElementById(v1).value) * 32.808399)/10;
}

function foot2m(v1,v2) {
	document.getElementById(v2).value = Math.round(parseFloat(document.getElementById(v1).value) * 3.048)/10;
}
// --- Math
// --- show the floating point 

function thousands(field){
	var input = new String(field.value);
	kimeno = '';
	j = 0;
	input = replaceSubstring(input,' ','');
	hossz = input.length;
	for( i = hossz-1;i >= 0; i--) {
		if ( (( j % 3) == 2) && ( j<(hossz-1) ) ) {
			kimeno = ' '+ input.substr(i,1) + kimeno;
		} else {
			kimeno = input.substr(i,1) + kimeno;
		}
		j++;
	}
	field.value = kimeno;
}

// --- Character transformation

function bin2hex(txt) {
	tl=txt.length;
	if (tl==0) return;
	nch=hexchar=code='';
	nxtch=0;
	for (i=0;i<tl;i++) {
		nch="\\"+txt.charAt(i);
		nxtch=nch.charCodeAt(1);
		code=dec2hex(nxtch);
		hexchar +=code;
	}
	return hexchar;
}

function dec2hex(decimal) {
	rv='';
	if (decimal>255) return 0;
	tnum="0123456789ABCDEF";
	hexup=parseInt(decimal/16);
	hxup=tnum.charAt(hexup);
	hexlow=decimal-16*hexup;
	hxlw=tnum.charAt(hexlow);
	rv=hxup+hxlw;
	return rv;
}

//

function replaceSubstring(inputString, fromString, toString) {
	var temp = inputString;
	if (fromString == "") {
		return inputString;
	}
	if (toString.indexOf(fromString) == -1) {
		while (temp.indexOf(fromString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	}else{
		var midStrings = new Array("~", "`", "_", "^", "#");
		var midStringLen = 1;
		var midString = "";
		while (midString == "") {
			for (var i=0; i < midStrings.length; i++) {
				var tempMidString = "";
				for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
				if (fromString.indexOf(tempMidString) == -1) {
					midString = tempMidString;
					i = midStrings.length + 1;
				}
			}
		}
		while (temp.indexOf(fromString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			temp = toTheLeft + midString + toTheRight;
		}
		while (temp.indexOf(midString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(midString));
			var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	}
	return temp;
}
//
function inputcasetext(iname, mode) {
	if (mode == "u") {
		document.getElementById(iname).value=document.getElementById(iname).value.toUpperCase();
	}
	if (mode == "l") {
		document.getElementById(iname).value=document.getElementById(iname).value.toLowerCase();
	}
}
// popups
// -- EUB utasbiztositás V3.0 --
function OpenBrWindow(theURL, winName, features, myWidth, myHeight, isCenter) {
	if(window.screen)if(isCenter)if(isCenter == "true" ) {
		var myLeft = (screen.width-myWidth)/2;
		var myTop = (screen.height-myHeight)/2;
		features += (features !='' )?',':'';
		features += ',left='+myLeft+',top='+myTop;
 	}
	window.open(theURL,winName, features+(( features != '' )? ',':'' ) + 'width=' + myWidth + ',height=' + myHeight );
}

// ---

function euszemelybiz() {
	newWindow = window.open("http://www.best-office.hu/info/euhealth_b/euszemelybiz3.html", "EU_biztositas", "left=400,top=335,width=520,height=240, toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no");
}

// -- Stock in trade
function stockintrade() {
	newWindow = window.open("http://www.best-office.hu/info/stockintrade.html", "Repertoar","left=400,top=335,width=400,height=320, toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no");
}

// -- EUB ablak
function eubtrade() {
	newWindow = window.open("http://www.eub.hu/online/index.php?pkod=26513", "", "left=240,top=200, width=1024, height=675, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no");
}

// -- AXA ablak
function axatrade() {
	newWindow = window.open("https://www.cslife.travelonline.hu/bestoffice","", "left=220,top=150,width=580,height=740, toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no");
}

// -- Megtakaritas
function megtakaritas() {
	newWindow = window.open("http://www.best-office.hu/info/megtakaritas.html", "Megtakaritas","left=250,top=100,width=800,height=700, toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no");
}

// -- Garancia gfb ár
function gfbargarancia() {
	newWindow = window.open("http://www.best-office.hu/info/gfb_ar_garancia.html", "Argarancia", "left=320,top=250,width=480,height=480, toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no");
}


// Begin validation

function runValidation()
	{
	var obj_Error= new ErrorMsg();
	obj_Error.AddCategory();
	for ( var i = 1; i < ( numChecks + 1 ); i++ )
		{
		if ( checkFields[i] != "" ) obj_Error.AddMessage( checkFields[i] );
	}

	// Display errors
	if( obj_Error.errors == false )
		{
		document.forms[0].submit();
		} else {
		obj_Error.DisplayMessage();
		return false;
	}
}

function isRequired (fieldName, fieldDescription, errorLabel)
	{
	errorMsg = "";
	fieldValue = document.forms[0][fieldName].value;
	if(( fieldValue == "" ) || ( fieldValue == " " ))
		{
		errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel + "";
	}
	return errorMsg ;
}

function validateMinMax (fieldName, fieldDescription, minLength, maxLength, minValue, maxValue)
	{
	errorMsg = "";
	fieldValue = document.forms[0][fieldName].value;
	fieldLength = document.forms[0][fieldName].value.length;
	if (fieldLength<minLength )
		{
		errorMsg = "\"" + fieldDescription +  "\"  - Adjon meg minimum " + minLength + " karaktert.";
	} else if (fieldLength>maxLength)
		{
		errorMsg = "\"" + fieldDescription +  "\"  - Maximum " + maxLength + " karaktert adjon meg.";
	} else if (fieldValue>maxValue)
		{
		errorMsg = "\"" + fieldDescription +  "\"  - Maximum " + maxValue + " lehet.";
	} else if (fieldValue<minValue)
		{
		errorMsg = "\"" + fieldDescription +  "\"  - Minimum " + minValue + " lehet.";
	}
	return errorMsg ;
}


function checkValidChars( fieldName, fieldDescription, errorLabel )
	{
	errorMsg = "";
	fieldValue = document.forms[0][fieldName].value;
	fieldLength = document.forms[0][fieldName].value.length;
	for( var i=0; i<fieldLength; i++ )
		{
		if ( validChars.indexOf( fieldValue.charAt( i )) == -1 )
			{
			errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
		}
	}
	return errorMsg ;
}

function checkPhoneChars( fieldName, fieldDescription, errorLabel )
	{
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;
	for( var i=0; i<fieldLength; i++ )
		{
		if ( phoneChars.indexOf( fieldValue.charAt( i )) == -1 )
			{
			errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
		}
	}
	return errorMsg ;
}

function checkIdentical( fieldName, fieldDescription, errorLabel )
	{
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;
	if (( fieldLength > 1 ) && ( isComposedOfChars( fieldValue.charAt( 0 ), fieldValue )))
		{
		errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
	}
	return errorMsg ;
}

function isComposedOfChars( curChar, inString )
	{
	return ( indexOfFirstNotIn( curChar, inString ) == -1 );
}

function indexOfFirstNotIn( okayChars, inString )
	{
	for (var i=0; i<inString.length; i++)
		{
		if (okayChars.indexOf( inString.charAt(i)) == -1 )
			{
			return i;
		}
	}
	return -1;
}

function checkSequential( fieldName, fieldDescription, errorLabel )
	{
	errorMsg = "";
	fieldValue = document.forms[0][fieldName].value;
	fieldLength = document.forms[0][fieldName].value.length;
	if ( fieldLength > 1 )
		{
		for (var i=1; i < (numSeqStr+1); i++)
			{
			if (strSeq[i].indexOf(fieldValue) != -1)
				{
				errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
			}
		}
	}
	return errorMsg ;
}

function validateSelectBox( fieldName, fieldDescription, errorLabel, checkWhat )
	{
	errorMsg	= "";
	optSelected	= document.forms[0][fieldName].selectedIndex;
	fieldValue	= document.forms[0][fieldName].options[optSelected].value;
	if( fieldValue == checkWhat )
		{
		errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
	}
	return errorMsg ;
}

function validateBtnsBox( fieldName, fieldDescription, errorLabel )
	{
	errorMsg	= "";
	selection	= null;
	thisButton	= document.forms[0][fieldName];
	for( var i=0; i<thisButton.length; i++ )
		{
		if( thisButton[i].checked )
			{
		   	selection = thisButton[i].value;
		}
	}
	if( selection == null )
		{
		errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
	}
	return errorMsg;
}

function validateNumBox( fieldName, fieldDescription, errorLabel )
	{
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;
	for( var i = 0; i < fieldLength; i++ )
		{
		var ch = fieldValue.substring( i, i + 1 );
		if (ch<'0' || ch>'9')
			{
			errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
		}
	}
	return errorMsg ;
}

function validateFloatBox( fieldName, fieldDescription, errorLabel )
	{
	errorMsg	= '';
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;
	point		= 0;
	ch		= fieldValue.charAt(0);
	if (ch < '0'  ||  ch > '9' ) {errorMsg = "\"" + fieldDescription +  "\" - Elso karakter szám legyen " + errorLabel + "";} 
	for (var i=1; i<fieldLength; i++)
		{
		ch = fieldValue.charAt(i);
		if ((ch<'0' || ch>'9') && ch !="." && ch !=",") 
			{
			errorMsg = "\"" + fieldDescription +  "\" - Nem számjegy karakter " + errorLabel + "";
		}
	}
	for (var i=1; i<fieldLength; i++)
		{
		ch = fieldValue.charAt(i) ;
		if (ch=="." || ch==",") 
			{
			point++ ;
			if (point>1)
				{
				errorMsg = "\"" + fieldDescription +  "\" - Sok tizedesjel " + errorLabel + "";
			}
		}
	}
	return errorMsg ;
}

function validateEmailBox ( fieldName, fieldDescription, errorLabel )
	{
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength = document.forms[0][fieldName].value.length;

	if ( fieldLength > 0 )
		{
		if (( errorLabel == "" ) || ( errorLabel == null ))
			{
			errorLabel2 = "Hiányzik a [ @ ] jel.";		// Missing "@"
			errorLabel3 = "Hiányzik a [ . ]";		// Missing "."
			errorLabel4 = "Nem hiányozhat az országkód.";	// Missing "nation."
			errorLabel6 = "Nem kezdodhet [ @ ] jellel.";	// Starts with "@"
			errorLabel7 = "Nem kezdodhet [ . ].";		// Starts with "."
			errorLabel8 = "Túl rövid az e-mail cím.";	// Too short mail

			if ( fieldValue.indexOf( "@" ) == -1 )
				{
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel2 + "";
			} else if ( fieldValue.indexOf( "." ) == -1 )
				{
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel3 + "";
			} else if ( fieldValue.charAt( fieldLength-1 ) == "." )
				{
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel4 + "";
			} else if ( fieldValue.charAt( 0 ) == "@" )
				{
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel6 + "";
			} else if ( fieldValue.charAt( 0 ) == "." )
				{
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel7 + "";
			} else if ( fieldLength < 9 )
				{
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel8 + "";
			}
		}
	}
	return errorMsg ;
}

// END VALIDATION FUNCTIONS


// BEGIN OBJECT FUNCTIONS

function AddCategory( str_Name )
	{
	// Creates a new category object at the next space in the array
	this.obj_Errors[this.obj_Errors.length] = new NewCategory( str_Name );
	this.NewCategory = NewCategory;
}

function NewCategory( str_Name)
	{
	//  Initializes the category values
	this.str_Name = str_Name;
	this.str_Error = "";
}

function AddMessage( str_Msg )
	{
	for ( var i = 0; i < this.obj_Errors.length; i++ )
		{
		this.obj_Errors[i].str_Error += errorBullet;
		this.obj_Errors[i].str_Error += str_Msg + "\n";
		this.errors = true;
		return true;
	}
}

function ErrorMsg()
	{
	//  This is the object you create to keep track of errors in the document
	this.obj_Errors	= new Array();
	this.errors = false;
	this.AddCategory = AddCategory;
	this.AddMessage	= AddMessage;
	this.DisplayMessage = DisplayMessage;
}
// END OBJECT FUNCTIONS


function DisplayMessage()
	{
	// Displays the error messages.
	// If none, false is returned and no message is displayed.
	if ( this.errors == false )
		{
		return false;
	} else {
		var str_msg = "";
		str_msg += errorLine1 + "\n";
		str_msg += errorLine2 + "\n";
		for ( var i = 0; i < this.obj_Errors.length; i++ )
			{
			// Go through all of the objects
			if ( this.obj_Errors[i].str_Error != '' )
				{
				// If errors, write the errors
				str_msg += "\n" + errorLine3 + "\n";
				str_msg += this.obj_Errors[i].str_Error + "\n";
			}
		}
		alert( str_msg );
		// Display the error message
		return true;
	}
}

