// JavaScript Document
String.prototype.tokenize = tokenize;

function tokenize()
  {
     var input             = "";
     var separator         = " ";
     var trim              = "";
     var ignoreEmptyTokens = true;

     try { String(this.toLowerCase()); }
     catch(e) { window.alert("Tokenizer Usage: string myTokens[] = myString.tokenize(string separator, string trim, boolean ignoreEmptyTokens);"); return; }

     if(typeof(this) != "undefined")
       {
          input = String(this);
       }

     if(typeof(tokenize.arguments[0]) != "undefined")
       {
          separator = String(tokenize.arguments[0]);
       }

     if(typeof(tokenize.arguments[1]) != "undefined")
       {
          trim = String(tokenize.arguments[1]);
       }

     if(typeof(tokenize.arguments[2]) != "undefined")
       {
          if(!tokenize.arguments[2])
            ignoreEmptyTokens = false;
       }

     var array = input.split(separator);

     if(trim)
       for(var i=0; i<array.length; i++)
         {
           while(array[i].slice(0, trim.length) == trim)
             array[i] = array[i].slice(trim.length);
           while(array[i].slice(array[i].length-trim.length) == trim)
             array[i] = array[i].slice(0, array[i].length-trim.length);
         }

     var token = new Array();
     if(ignoreEmptyTokens)
       {
          for(var i=0; i<array.length; i++)
            if(array[i] != "")
              token.push(array[i]);
       }
     else
       {
          token = array;
       }

     return token;
  }

function SetPrice(xform) {
	
	var OptId=xform.product_options.selectedIndex;
	var OptName=xform.product_options.options[OptId].text;
	var OptValue=xform.product_options.options[OptId].value;
	var base_price = OptValue.tokenize("-", "", true);
	//MM_setTextOfTextfield(to_price,'',textColorValue);
	xform.choice.value=OptName;
	
	xform.prod_price.value=round_decimals(base_price[1], 2);
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    var value_string = rounded_value.toString()
    var decimal_location = value_string.indexOf(".")
   if (decimal_location == -1) {
        decimal_part_length = 0
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
        decimal_part_length = value_string.length - decimal_location - 1
    }
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

function fncValidateBilling (f) {

		var i;

		var form_validation = new Array();

		form_validation['billing_firstname'] = new Array('First Name or Initial', 1);
		form_validation['billing_lastname'] = new Array('Last Name', 1);
		form_validation['billing_address1'] = new Array('First line of your Address', 1);
		form_validation['billing_city'] = new Array('City', 1);
		form_validation['billing_county'] = new Array('County', 1);
		form_validation['billing_country'] = new Array('Country', 1);		
		form_validation['billing_postcode'] = new Array('Post Code', 1);
		form_validation['billing_telephone'] = new Array('Telephone', 1);		
		form_validation['billing_email'] = new Array('Email', 1,'email');		

		var form_valid = F.Validate(f, form_validation);

		if (!form_valid) {
			return false;
		} else {
			return true;
		}
	}
	
function fncValidateDelivery (f) {

		var i;

		var form_validation = new Array();

		form_validation['delivery_firstname'] = new Array('First Name or Initial', 1);
		form_validation['delivery_lastname'] = new Array('Last Name', 1);
		form_validation['delivery_address1'] = new Array('First line of your Address', 1);
		form_validation['delivery_city'] = new Array('City', 1);
		form_validation['delivery_county'] = new Array('County', 1);
		form_validation['delivery_postcode'] = new Array('Post Code', 1);
		form_validation['delivery_country'] = new Array('Country', 1);		

		var form_valid = F.Validate(f, form_validation);

		if (!form_valid) {
			return false;
		} else {
			return true;
		}
	}	
		

$(document).ready(function(){
 
//CHANGES THE BUTTON FROM OPEN TO CLOSED AND VIS-VERSA
	$("a.toggle").toggle(function(){
		$("a.toggle").removeClass("active");
		$(this).addClass("active");
		$("#fm_product").val( $(this).attr( "name" ) );
		update_picker();
	}, function () {
//		$("a.toggle").removeClass("active");
//		$("#fm_product").val( "" );
	});
	
//CHANGES THE BUTTON FROM OPEN TO CLOSED AND VIS-VERSA
	$("a.fg_colour").toggle(function(){
		$("a.fg_colour").removeClass("picked");
		$("a.fg_colour").animate( { height:"15px"}, 500 )
		$(this).animate( { height:"30px"}, 500 )
		$(this).addClass("picked");
	}, function () {
//		$("a.fg_colour").removeClass("picked");
	});

//CHANGES THE BUTTON FROM OPEN TO CLOSED AND VIS-VERSA
	$("a.bg_colour").toggle(function(){
		$("a.bg_colour").removeClass("picked");
		$("a.bg_colour").animate( { height:"15px"}, 500 )
		$(this).animate( { height:"30px"}, 500 )
		$(this).addClass("picked");
	}, function () {
//		$("a.bg_colour").removeClass("picked");
	});

//CHANGES THE BUTTON FROM OPEN TO CLOSED AND VIS-VERSA
	$("a.fx_colour").toggle(function(){
		$("a.fx_colour").removeClass("picked");
		$("a.fx_colour").animate( { height:"15px"}, 500 )
		$(this).animate( { height:"30px"}, 500 )
		$(this).addClass("picked");
	}, function () {
//		$("a.fx_colour").removeClass("picked");
	});
	
	//CHANGES THE BUTTON FROM OPEN TO CLOSED AND VIS-VERSA
	$("a.ol_colour").toggle(function(){
		$("a.ol_colour").removeClass("picked");
		$("a.ol_colour").animate( { height:"15px"}, 500 )
		$(this).animate( { height:"30px"}, 500 )
		$(this).addClass("picked");
	}, function () {
//		$("a.ol_colour").removeClass("picked");
	});

//CHANGES THE BUTTON FROM OPEN TO CLOSED AND VIS-VERSA
	$("a.lightdir").toggle(function(){
		$("a.lightdir").removeClass("shadow");
		$(this).addClass("shadow");
		dir_lightx = $(this).attr( "lx" );
		dir_lighty = $(this).attr( "ly" );
		light_direction=$(this).attr( "name" );
		update_render();
	}, function () {
//		$("a.lightdir").removeClass("shadow");
	});

	
});