function confirm_msg(msg,url) {
	
	input_box=confirm(msg);
	
	if (input_box==true){
		document.location=url;
	}
}




function checkIt(object) {
	object.checked = (object.checked) ? false : true;
}


function goToBasket(requestURL) {
	document.details_form.action = requestURL + '/basket';
	document.details_form.submit();
}

function goToBasketDetails(requestURL) {
	document.confirm_form.action = '/basket/?sub=checkout';
	document.confirm_form.submit();
}

function submitRedirect() {
	document.payment_form.submit();
}


function addBookmark(title,url) {
	if (window.sidebar) { 
		alert("Close this message and press CTRL + D to bookmark this page.");
	} else if( document.all ) {
		window.external.AddFavorite( url, title );
	} else if( window.opera && window.print ) {
		return true;
	}
}



function trim(strText) { 
	// this will get rid of leading spaces 
	while (strText.substring(0,1) == ' ') 
		strText = strText.substring(1, strText.length);

	// this will get rid of trailing spaces 
	while (strText.substring(strText.length-1,strText.length) == ' ')
		strText = strText.substring(0, strText.length-1);

	return strText;
} 



function validate_email(str){
	
	var testresults
	var filter=/^.+@.+\..{2,3}$/

	if (filter.test(str))
		testresults = true;
	else {
		
		testresults = false;
	}
	return (testresults);
}



// check checkout data before submitting
function checkBasketDetails() {

	document.details_form.next_but.disabled = true;

	var all_ok = true;

	var name = trim(document.details_form.CustomerName.value);
	if (name.length == 0) { all_ok = false; }


	var email = trim(document.details_form.CustomerEmail.value);
	if (email.length == 0 || validate_email(email) == false) { all_ok = false; }

	
	var phone = trim(document.details_form.ContactNumber.value);
	if (phone.length == 0) { all_ok = false; }


	var delivery_address = trim(document.details_form.DeliveryAddress_1.value);
	if (delivery_address.length == 0) { all_ok = false; }
	
	
	var delivery_postcode = trim(document.details_form.DeliveryPostCode.value);
	if (delivery_postcode.length == 0) { all_ok = false; }



	if (all_ok == false) {
		alert('Please fill all fields as they are mandatory. Also ensure that the e-mail field is filled in correct format.');
		document.details_form.next_but.disabled = false;
	} else {
		document.details_form.next_but.value = "Please wait..";
		document.details_form.submit();
	}
}

function checkTermsAgreement() {
	
	var button = document.getElementById('submit');
	var terms_checkbox = document.getElementById('terms');

	
	var all_ok = true;
	
	button.disabled = true;

	if (!terms_checkbox.checked) { 
		all_ok = false; 
	}

	
	if (all_ok == true) {
		button.value = "Please wait..";
		return true;
	} else {
		alert('You must agree fully to the terms and conditions of sale before you can proceed.');
		button.disabled = false;
		return false;
	}
}


function checkRequiredFields() {
	
	var button = document.getElementById('submit');
//	var user = document.getElementById('user');
//	var first_name = document.getElementById('first_name');
//	var last_name = document.getElementById('last_name');
	var email = document.getElementById('email');
//	var shop_name = document.getElementById('shop_name');
//	var company_name = document.getElementById('company_name');
//	var company_address = document.getElementById('company_address');
	var billing_address = document.getElementById('billing_address');
//	var post_code = document.getElementById('post_code');
//	var tel = document.getElementById('tel');
	
	var all_ok = true;
	
	button.disabled = true;

//	if (user.value.length == 0) { 
//		all_ok = false; 
//	}

//	if (first_name.value.length == 0) { 
//		all_ok = false; 
//	}

//	if (last_name.value.length == 0) { 
//		all_ok = false; 
//	}

	if (email.value.length == 0 || validate_email(email.value) == false) { 
		all_ok = false; 
	}

	if (billing_address.value.length == 0) { 
		all_ok = false; 
	}

	if (all_ok == true) {
		button.value = "Please wait..";
		return true;
	} else {
		alert('Please fill all fields as they are mandatory. Also ensure that the e-mail field is filled in correct format.');
		button.disabled = false;
		return false;
	}
}


// moves option section to list of product options
function addOptionToProduct() {
	
	var option_div = document.getElementById('option_id_div');
	var destination_div = document.getElementById('destination_div');
	
	destination_div.innerHTML += option_div.innerHTML;
	option_div.innerHTML = "";
}

// We just need to remove "option_id_div" part of HTML code
// since its confuses our php script
function submitButton(){

	var option_div = document.getElementById('option_id_div');
	option_div.innerHTML = "";
}


// select all products for deletion
function markForDelete() {

	product_arr = document.getElementsByName('product_delete[]');

	if ($F('del_all') == 1) {

		for (i = 0; i < product_arr.length ; i++ ) {
			product_arr[i].checked = true;
		}

	} else {

		for (i = 0; i < product_arr.length ; i++ ) {
			product_arr[i].checked = false;
		}

	}
}









// delete selected products, in the grid control
function deleteSelected() {
	input_box = confirm('Are you sure that you want to delete these items?');
	if (input_box == true){
		$('act').value = 'delete_selected';
		$('grid_control_form').submit();
	}
}





function selectShop( intVar ){
	var f = document.getElementById('shopsForm');
	f.select_shop_id.value = intVar;
	f.submit();
	
}



function selectClient( intVar ){
	var f = document.getElementById('clientsForm');
	f.select_client_id.value = intVar;
	f.submit();
	
}



function changePrice( intProduct )
{
 	var display = document.getElementById("display_" + intProduct );
	var original = document.getElementById("price_" + intProduct );
	var result_price = document.getElementById("result_price_" + intProduct );
	var prices = eval("prices_" + intProduct);
	var result = original.value;
	
	for (var i = 0; i < prices.length; i++) 
	{
		var option = document.getElementById("option_" + intProduct);
		
		if(prices[i][1] == option.options[option.selectedIndex].value) 
		{
			if(prices[i][3] == "+") {
				result = parseFloat(result) + parseFloat(prices[i][2]);	
				result = Math.round(result*100)/100;					
			}
			if(prices[i][3] == "-") {
				result = parseFloat(result) - parseFloat(prices[i][2]);
				result = Math.round(result*100)/100; 
			}
		}
	}
	
	var decimals = new Array();
	decimals = result.toString().split('.');
	
	var length = 0;
	var part = "";
	
	if(decimals.length == 2) {
		length = decimals[1].length;
		
		for(i = 0; i < 2 - length; i++) {
			decimals[1] = decimals[1] + '0'; 
		}
		
		part = decimals[1];
		
	} else {
		part = "00";
	}
	
	part = part.substring(0,2);
	
	result = decimals[0] + "." + part;
	 
	display.innerHTML = result;
	result_price.value = result;
}

////////////////////////////////////////////////
//////////////// Flash corner  /////////////////

function flashCornerExpand() {
 	var flash_object = document.getElementById("banner_finance_preview");
 	flash_object.style.width = '400px';
 	flash_object.style.height = '400px';
}

function flashCornerCollapse() {
 	var flash_object = document.getElementById("banner_finance_preview");
 	flash_object.style.width = '100px';
 	flash_object.style.height = '100px';
}

////////////////////////////////////////////////
/////////////// Validation Part ////////////////

var error_OK = 0;
var error_ERROR = -1;
var validation_fields = new Array();

// field - field ID on a form
// func - a function name you defined to validate that field
function validator_AddRule(field, fieldName, func) {

	// First step: check if data types are correct
	// do nothing... its a validation functions job...

	// Second step: create a rule object, we will save that in the validation_fields array later
	var ruleObject = new Object();
	ruleObject.field = field;
	ruleObject.fieldName = fieldName;
	ruleObject.func = func;


	// Third step: each function can have a diffenet number of arguments - define that
	var allArgs = validator_AddRule.arguments;
	if(allArgs.length > 3) {

		// Create an arguments array in the current object
		ruleObject.args = new Array();
		for(var i=3; i<allArgs.length; i++) {
			ruleObject.args.push(allArgs[i]);
		}
	}

	// Final step: push everything to the validation fields array
	validation_fields.push(ruleObject);
}


function validator_ValidateAll() {

	// First step: validate all the fields in the validation_fields array
	for(var i=0; i<validation_fields.length; i++) {

		// Calculate arguments
		var strArguments = '';
		if((typeof validation_fields[i].args) != 'undefined') {
			for(var j=0; j<validation_fields[i].args.length; j++) {
				strArguments = strArguments + ', ' + 'validation_fields[i].args[' + j + ']';
			}
		}

		strArguments = '"' + validation_fields[i].field + '", true' + strArguments;
		if (eval(validation_fields[i].func + '(' + strArguments + ')') < error_OK)
			return validation_fields[i].fieldName;
	}

	// Final step: return result
	return true;
}

function showHide(strObject)
{
    var elem = document.getElementById(strObject);
    if(!elem) {
    	return true;
    }

    if(elem.className !="visible"){
        elem.className="visible";
    } else {
        elem.className="hidden";
    }
    return;
}

////////////////////////////////////////////////
///////////// Validation Functions /////////////

// strElementID - element ID
// 
function validator_isNonEmpty(strElementID, boolSystemCall) {

	// Try to take an element by field ID
	var element = document.getElementById('validation_field_' + strElementID);
	if((typeof element)=='undefined') return error_ERROR;

	// Define a result value
	var Result = element.value.length == 0 ? error_ERROR : error_OK;

	// Return result
	return Result;
}

// strElementID - element ID
// 
function validator_isNonEmptyEMail(strElementID, boolSystemCall){
	
	// Try to take an element by field ID
	var element = document.getElementById('validation_field_' + strElementID);
	if((typeof element)=='undefined') return error_ERROR;
	
	// Define regular expression
	var eMailFilter=/^.+@.+\..{2,3}$/;

	// Process a string
	var Result = eMailFilter.test(element.value) ? error_OK : error_ERROR;

	// Return result;
	return(Result);
}