$(document).ready( function() {
	setInterval(handlePaste, 500);
});

function handlePaste() {
	if (document.getElementById('company:quantity') != null) {
		var value = document.getElementById('company:quantity').value;
		calculatorChangeHandler(value, validateStoreQuantity, validationFail,
				validationPassed, processEmptyField);
	}
}

function calculatorCallback() {
//	document.getElementById('company:quantity').focus();
}

var previousValue = null;
function updateCalculator() {
	if (!documentReady) {
		return false;
	}
	q_reloadComponents( [ 'company:fullprice',
			'company:price' ], {
		onajaxend :calculatorCallback,
		immediate :true,
		submittedComponentIds : [ 'company:quantity', 'company:selectedItem' ]
	});
}
function calculatorChangeHandler(value, validate, onValidationFail,
		onValidationPass, onEmptyField) {
	if (validate(value)) {
		if (value != previousValue) {
			updateCalculator();
		}
		onValidationPass();
	} else {
		if (value == '') {
			onEmptyField();
		} else {
			onValidationFail();
		}
	}
	previousValue = value;
	return true;
}

function isNumeric(strString) {
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;

	if (strString.length == 0)
		return false;

	// test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

function validationFail() {
	document.getElementById('company:proceedSale').disabled = true;
	document.getElementById('company:quantity').className = 'errorBorder';
}

function validationPassed() {
	document.getElementById('company:proceedSale').disabled = false;
	document.getElementById('company:quantity').className = 'customBorder';
}

function validateStoreQuantity(value) {
	return isNumeric(value) && value > 0;
}

function processEmptyField() {
	document.getElementById('company:proceedSale').disabled = true;
	document.getElementById('company:quantity').className = 'customBorder';
}

function changeSelectedItem(productid, formId, validate) {
	if (!documentReady) {
		return false;
	}
	var selectedElementId = formId + ':selectedItem';
	document.getElementById(selectedElementId).value = productid;
	var additionalAttributes = null;
	var reloadComponents = null;
	if (formId == 'company') {
		if (validate) {
			var value = document.getElementById(formId + ':quantity').value;
			if (validate(value)) {
				reloadComponents = [ formId + ':productSelection',
						 formId + ':calculator' ]
			} else {
				reloadComponents = [ formId + ':productSelection',
						 formId + ':fullprice',
						formId + ':price', formId + ":platofrmname",
						formId + ":architecture" ];
			}
			additionalAttributes = {
				immediate :true
			};
		} else {
			reloadComponents = [ formId + ':productSelection',
					 formId + ':calculator' ]
			additionalAttributes = {
				immediate :true
			};
		}
	}
	if (formId == 'unlimited') {
		reloadComponents = [ formId + ':productSelection',
				formId + ':requestTitle', formId + ':selectedProduct' ]
		additionalAttributes = {
			immediate :true
		};
	}
	if (formId == 'individual') {
		reloadComponents = [ formId + ':productSelection',
				formId + ':calculator' ]
		additionalAttributes = {
			immediate :true
		};
	}
	if (formId == 'source') {
		reloadComponents = [ formId + ':productSelection',
				formId + ':requestTitle', formId + ':selectedProduct' ]
		additionalAttributes = {
			immediate :true
		};
	}
	q_reloadComponents(reloadComponents, additionalAttributes);
	if (formId == 'unlimited' || formId == 'source') {
		refreshRequest(formId);
	}
}

function showPriceList() {
	hideElement(document.getElementById('showPriceListCommandHolder'));
	$('#selection').addClass('openedSeeFullPriceList');
}

function hidePriceList() {
	show('showPriceListCommandHolder');
	$('#selection').removeClass('openedSeeFullPriceList');
}

function validateLicenseRequestForm(formid, element) {
	if (element.value == '') {
		element.className = 'customBorder';
	}
	if (!validate(element.value)) {
		document.getElementById(formid + ":sendRequestProxy").disabled = true;
		element.className = 'errorBorder';
	}
}
function validate(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return reg.test(email);
}

function validateLicenseRequestFormOnKeyDown(formid, element) {
	if (validate(element.value)) {
		document.getElementById(formid + ":sendRequestProxy").disabled = false;
		element.className = 'customBorder';
	}
}

function complexSourceCodeReload() {
	q_reloadComponents( [ 'source:selectedProduct', 'source:requestTitle' ], {
		submittedComponentIds : [ 'source:including' ],
		immediate :true
	});
}

function refreshRequest(formid) {
//	document.getElementById(formid + ":successfullySent").value = 'false';
	q_reloadComponents( [ formid + ':selectedProduct',
			formid + ':licenserequest' ], {
		immediate :true
	});
}

function changeLicenseSelection() {
	
}

function setSourceCodeLicense(productName) {
	if (!documentReady) {
		return false;
	}
	q_reloadComponents( [ 'source:sourceLicenseLink'], {
		onajaxend : changeSelectionToSourceCode,
		immediate :true,
		action : productName + 'PurchaseTypeSelection.changeToSources'
	});
	return false;
}
function changeSelectionToSourceCode() {
	changeSelection('licenseMenu', 'sourceLicense');
}