function imgtoggle(x)
{
	// hide all of the images
	$('div.full-image').hide();
	
	// show the selected one
	$('#productdetails_large_image'+x).show();
}

// Browse/Search Pages
function reset_filters(formObj)
{
	var selects = formObj.elements;

	for(var i = 0; i < selects.length; i++)
	{
		var field_type = selects[i].type.toLowerCase();
		if(field_type == "select-one")
			selects[i].selectedIndex = 0;
	}
	
	formObj.submit();
}

// Product Pages
function select_item_option(item_id, allow_trade, logged_in)
{
	// get the value of the selected option
	var item_option_value = $("#item_option_value").val();
	
	// split the selected value to get the item_option_value_id & the item_availability_id
	var option_pieces = item_option_value.split('|');
	var item_option_value_id = option_pieces[0];
	var item_availability_id = option_pieces[1];
	
	// set up the wishlist link, if user is logged in
	if(logged_in == 1)
	{
		if(item_option_value_id > 0)
			var wishlist_link = '<a href="/wishlist/save/product/'+item_id+'/'+item_option_value_id+'">Add Item to Wish List</a>';
		else
			var wishlist_link = '';
		
		$("#wishlist-link").html(wishlist_link);
	}
	
	// set up the 'add to cart' button
	if(item_option_value_id > 0)
		$("#btn_addtocart").html('<button class="styled" onclick="add_to_cart('+item_id+', '+item_availability_id+', '+allow_trade+', '+item_option_value_id+'); return false;">Add to Cart</button>');
	else
		$("#btn_addtocart").html('&lt;-- Select an Option');
	
	// update the product pricing
	$.ajax({
        type: "POST",
        url: "/products/load_product_pricing/"+item_id+"/"+item_option_value_id,
        error: function(){ $("#product-pricing").html('Oops. An error has occurred trying to update the product pricing.'); },
        success: function(response){ 
        	$("#product-pricing").html(response);
        	tb_init('#product-pricing a.thickbox'); // need to re-initialize the thickbox
    	}
     });
	
	// update the item availability
	$.ajax({
        type: "POST",
        url: "/products/load_product_availability/"+item_id+"/"+item_availability_id,
        error: function(){ $("#product-item-availability").html('Oops. An error has occurred trying to update the product availability.'); },
        success: function(response){ 
        	$("#product-item-availability").html(response);
        	tb_init('#product-item-availability a.thickbox'); // need to re-initialize the thickbox
    	}
     });
	
	// update the image, if the selected option value has one set
	$.ajax({
        type: "POST",
        url: "/products/load_product_image_for_option/"+item_id+"/"+item_option_value_id,
        success: function(response){ 
        	if(response != "")
        	{
        		$("#productdetails_large_image1").html(response);
        		imgtoggle(1);
        		tb_init('#productdetails_large_image1 a.thickbox'); // need to re-initialize the thickbox
        	}
    	}
     });
}

function select_subitem_option(item_id, allow_trade)
{
	// get the value of the selected option
	var item_option_value = $("#subitem"+item_id+"_option_value").val();
	
	// split the selected value to get the item_option_value_id & the item_availability_id
	var option_pieces 			= item_option_value.split('|');
	var item_option_value_id 	= option_pieces[0];
	var item_availability_id 	= option_pieces[1];
	
	// set up the 'add to cart' column
	if(item_option_value_id == -1)
		$("#subitem"+item_id+"-addtocart").html('&lt;-- Select an Option');
	else
		$("#subitem"+item_id+"-addtocart").html('<button class="styled-small" onclick="add_to_cart('+item_id+', '+item_availability_id+', '+allow_trade+', '+item_option_value_id+'); return false;">Add to Cart</button>');
	
	// refresh the pricing (with trade & no trade)
	$.ajax({
        type: "POST",
        url: "/products/load_subitem_pricing/"+item_id+"/"+item_option_value_id,
        error: function(){ $("#subitem"+item_id+"-pricing").html('Oops. An error has occurred trying to update the product pricing.'); },
        success: function(response){
        	var prices = response.split("|");
        	$("#subitem"+item_id+"-trade-pricing").html(prices[0]);
        	$("#subitem"+item_id+"-pricing").html(prices[1]);
        	tb_init('#subitem'+item_id+'-pricing a.thickbox'); // need to re-initialize the thickbox
    	}
     });
	
	// update the image, if the selected option value has one set
	$.ajax({
        type: "POST",
        url: "/products/load_subitem_image_for_option/"+item_id+"/"+item_option_value_id,
        success: function(response){ 
        	if(response != "")
        	{
        		$("#main-image-"+item_id).html(response);
        	}
    	}
     });
}

function add_to_cart(item_id, item_availability_id, allow_trade, item_option_value_id)
{
	if(item_option_value_id == undefined)
		item_option_value_id = "";
	
	if(allow_trade == 1)
		var url = "/products/view_trade_options/"+item_id+"/"+item_availability_id+"/"+item_option_value_id;
	else
		var url = "/cart/add/"+item_id+"/"+item_availability_id+"/"+item_option_value_id;
	
	// redirect the user
	location.href = url;
}

// Trade Options page
function toggle_available_trades(haveTrade)
{
	if(haveTrade == 1)
	{
		$("#available-trades").show();
	}
	else
	{
		$("#available-trades").hide();
		remove_trade();
	}
}

function select_trade(tradeAllowanceID)
{
	if(tradeAllowanceID > 0)
	{
		// show the quantity option
		$("#qty-for-trade").show();
		
		// get the list price
		var list_price = $("#list_price").val();
		
		// if list price is -1, it means it cannot be displayed on product page
		// so don't display the price with trade
		if(list_price > 0)
		{
			// get the discount [is set as the class for the option]
			var $option = $("#trade"+tradeAllowanceID);
			var discount = Number($option.attr("class"));
			$("#trade_discount").val(discount);
			// determine the price with trade
			var price_with_trade = list_price - discount;
			// display the price with trade
			$("#price-after-discount").html(' - <span style="color: #0000ff;"><strong>With Trade:</strong> $'+price_with_trade.toFixed(2)+'</span>');
		}
	}
	else
	{
		remove_trade();
	}
}

function remove_trade()
{
	// remove the price with trade
	$("#price-after-discount").html("");
	// reset the trade dropdown
	$("#trade_allowance_id").val("-1");
	// reset the trade quantity to 1
	$("#trade_quantity").val("1");
	// hide the quantity option
	$("#qty-for-trade").hide();
}

function validate_trade_options()
{
	var trade_allowance_id 	= $("#trade_allowance_id").val();
	var trade_quantity 		= $("#trade_quantity").val();
	
	if(trade_allowance_id > 0)
	{
		if(trade_quantity == "")
		{
			alert("Please enter a numeric quantity for the Quantity available for trade.");
			return false;
		}
		else if(isNaN(trade_quantity) === true)
		{
			alert("Please enter a numeric quantity for the Quantity available for trade.");
			return false;
		}
		else if(trade_quantity <= 0)
		{
			alert("Please enter a numeric quantity greater than 0 for the Quantity available for trade.");
			return false;
		}
	}
	
	$("#trade-in").submit();
}

// Users Section :: Sign up page, Edit Information page :: copy Billing Address to Shipping Address
function billtoship(cbox)
{
	if(cbox.checked == true)
	{
		document.getElementById("ship_address1").value	= document.getElementById("bill_address1").value;
		document.getElementById("ship_address2").value	= document.getElementById("bill_address2").value;
		document.getElementById("ship_city").value		= document.getElementById("bill_city").value;
		document.getElementById("ship_zip").value		= document.getElementById("bill_zip").value;
		
		var ship_state_select = document.getElementById("ship_state_id");
		for(var i=0;i<ship_state_select.length;i++) 
		{
		    if(ship_state_select.options[i].value == document.getElementById("bill_state_id").value)
		    	ship_state_select.options[i].selected = true;
		}
	}
}

// Users Section :: Order Details page
function print_order(order_id)
{
	var url = "/orders/printorder/"+order_id;
	window.open(url);
}

//Users Section :: Order Tracking page
function print_tracking(order_id)
{
	var url = "/order_tracking/print_tracking/"+order_id;
	window.open(url);
}

// Shopping Cart
function onchange_ship_state()
{
	// reset the shipping type
	var shiptypeOption = document.getElementById('shipping_type_id');
	if(shiptypeOption != undefined)
		shiptypeOption.selectedIndex = 0; 
	
	// reset the destination zipcode
	document.getElementById("dest_zip").value = "";
	
	// hide the shipping option dropdown (in case some options need to be filtered out)
	document.getElementById("ship_options").style.display = "none";
	
	// hide the 'continue checkout' button
	var continue_btn = document.getElementById("ccheckout");
	if(continue_btn != undefined)
		continue_btn.style.display = "none";
}

function check_ship_destination(theform)
{
	var msg = "";
	
	if(document.getElementById("dest_state_id").value == "-1")
		msg = "Please select your state.\n";
	
	if(document.getElementById("dest_zip").value == "")
		msg += "Please enter your zipcode.\n";
	
	// if there is an error message, alert it and return without submitting
	if(msg != "")
	{
		alert(msg);
		return false;
	}
	
	// otherwise submit the form
	theform.submit();
}

function check_ship_type(theform)
{
	var msg = "";
	
	if(document.getElementById("shipping_type_id").value == "-1")
		msg = "Please select a shipping option.\n";
	
	// if there is an error message, alert it and return without submitting
	if(msg != "")
	{
		alert(msg);
		return false;
	}
	
	// otherwise submit the form
	theform.submit();
}

function save_cart()
{
	location.href = '/cart/save';
}

function clear_cart()
{
	location.href = '/cart/clearall';
}

function update_cart()
{
	// get the form
	var theform = document.getElementById("cart");
	// submit the form
	theform.submit();
}

function goto_checkout()
{
	var msg = "";
	
	if(document.getElementById("dest_state_id").value == "-1")
		msg = "Please select your state.\n";
	
	if(document.getElementById("dest_zip").value == "")
		msg += "Please enter your zipcode.\n";
	
	if(document.getElementById("shipping_type_id").value == "-1")
		msg = "Please select a shipping option.\n";
	
	// if there is an error message, alert it and return without submitting
	if(msg != "")
	{
		alert(msg);
		return false;
	}
	
	// redirect the user to the checkout
	location.href = '/cart/checkout';
}

// Checkout :: Enter Information page
function sameasbilling(sameasbillopt)
{
	if(sameasbillopt.checked == true)
	{
		document.getElementById("ship_first_name").value 	= document.getElementById("bill_first_name").value;
		document.getElementById("ship_last_name").value 	= document.getElementById("bill_last_name").value;
		document.getElementById("ship_address1").value 		= document.getElementById("bill_address1").value;
		document.getElementById("ship_address2").value 		= document.getElementById("bill_address2").value;
		document.getElementById("ship_city").value 			= document.getElementById("bill_city").value;
		document.getElementById("ship_email").value			= document.getElementById("bill_email").value;
		document.getElementById("ship_phone").value 		= document.getElementById("bill_phone").value;
	}
}

function check_financing_payment_type(radio_opt, item_id)
{
	if(radio_opt.value == "credit_card")
		document.getElementById("cc_"+item_id).style.display = "block";
	else
		document.getElementById("cc_"+item_id).style.display = "none";
}

function process_info(theform)
{
	var error_msg 			= "";
	var missing_requireds 	= new Array();
	
	// set up the required fields
	var required_fields = new Object();
	// billing details
	required_fields.bill_first_name = "Billing First Name";
	required_fields.bill_last_name 	= "Billing Last Name";
	required_fields.bill_address1 	= "Billing Address (First Line)";
	required_fields.bill_city 		= "Billing City";
	required_fields.bill_state_id 	= "Billing State";
	required_fields.bill_zip 		= "Billing Zipcode";
	required_fields.bill_email 		= "Billing Email";
	required_fields.bill_phone 		= "Billing Phone";
	// shipping details
	required_fields.ship_first_name = "Shipping First Name";
	required_fields.ship_last_name 	= "Shipping Last Name";
	required_fields.ship_address1 	= "Shipping Address (First Line)";
	required_fields.ship_city 		= "Shipping City";
	required_fields.ship_state_id 	= "Shipping State";
	required_fields.ship_zip 		= "Shipping Zipcode";
	required_fields.ship_email 		= "Shipping Email";
	required_fields.ship_phone 		= "Shipping Phone";
	
	// if a payment is required, add the credit card fields to the required_fields array
	if(theform.payment_required.value == "1")
	{
		required_fields.card_type 		= "Credit Card Type";
		required_fields.card_name 		= "Name on Card";
		required_fields.card_number 	= "Credit Card Number";
		required_fields.card_cvv 		= "Credit Card CCV";
		required_fields.card_exp_month 	= "Credit Card Expiration Month";
		required_fields.card_exp_year 	= "Credit Card Expiration Year";
	}
	// if there are financed items, add the general fields to the required_fields array [those not item specific]
	if(theform.have_financed_items.value == "1")
	{
		required_fields.ssn = "Social Security Number (SSN)";
	}
	
	// validate the required fields
	var field_name;
	for(field_name in required_fields)
	{
		if(field_name == 'bill_state_id' || field_name == 'ship_state_id' || field_name == 'card_type')
		{
			if(document.getElementById(field_name).value == "-1")
				missing_requireds.push(required_fields[field_name]);
		}
		else
		{
			if(document.getElementById(field_name).value == "")
				missing_requireds.push(required_fields[field_name]);
		}
	}
	
	// set up the error if there are any missing required fields
	if(missing_requireds.length > 0)
		error_msg += "The following fields are required:\n\t"+missing_requireds.join("\n\t")+"\n\n";
	
	// validate the cc details
	if(theform.payment_required.value == "1")
	{
		// cc number
		if(document.getElementById("card_number").value != "" && validate_cc_number(theform.card_type.value, theform.card_number.value) !== true)
			error_msg += "Please enter a valid credit card number.\n\n";
		
		// cc expiration
		if(validate_cc_expiration(theform.card_exp_month.value, theform.card_exp_year.value) !== true)
			error_msg += "Your credit card you entered for the main payment has expired.\n\n";
	}
	
	// validate the financed items, if there are any
	if(theform.have_financed_items.value == "1")
	{
		var financed_items = theform.financed_detail_ids.value;
		financed_items = financed_items.split(",");
		var payment_type_field;
		var missing;
		var temp_error;
		var item_name;
		var detail_id;
		for(x=0;x<financed_items.length;x++)
		{
			detail_id = financed_items[x];
			item_name = document.getElementById("item_name_"+detail_id).value;
			
			// if paying via credit card for this item, validate the credit card fields
			if(document.getElementById("payment_type_"+detail_id).value === "credit_card")
			{
				temp_error 	= "";
				missing 	= new Array();
				
				// they must check to authorize /* 20100818 Client does not want this field required. */
				//if(document.getElementById("cc_authorize_"+detail_id).checked != true)
				//	temp_error += "You must authorize future payments to be charged to your credit card monthly.\n\t"
				
				// validate the cc number
				if(document.getElementById("card_number_"+detail_id).value != "" && validate_cc_number(document.getElementById("card_type_"+detail_id).value, document.getElementById("card_number_"+detail_id).value) !== true)
					temp_error += "Please enter a valid credit card number.\n\t";
				
				// validate the cc expiration date
				if(validate_cc_expiration(document.getElementById("card_exp_month_"+detail_id).value, document.getElementById("card_exp_year_"+detail_id).value) !== true)
					temp_error += "The credit card you entered for this product has expired.\n\t";
				
				// required fields
				if(document.getElementById("card_type_"+detail_id).value == "")
					missing.push("Credit Card Type");
				if(document.getElementById("card_name_"+detail_id).value == "")
					missing.push("Name on Card");
				if(document.getElementById("card_number_"+detail_id).value == "")
					missing.push("Credit Card Number");
				if(document.getElementById("card_cvv_"+detail_id).value == "")
					missing.push("Credit Card CVV");
				if(document.getElementById("card_exp_month_"+detail_id).value == "")
					missing.push("Credit Card Expiration Month");
				if(document.getElementById("card_exp_year_"+detail_id).value == "")
					missing.push("Credit Card Expiration Year");
				if(missing.length > 0)
					temp_error += "The following fields are required:\n\t\t"+missing.join("\n\t\t")+"";
				
				if(temp_error != "")
					error_msg += "Validation Issue for "+item_name+":\n\t"+temp_error+"\n\n";
			}
			
			// if this is a 30PP, require the free book option
			var free_book = document.getElementsByName("free_book_"+detail_id);
			if(free_book.length > 0)
			{
				var book_selected = false;
				
				for(var i=0; i<free_book.length; i++)
				{
					if(free_book[i].checked)
					{
						book_selected = true;
						break;
					}
				}
				
				if(book_selected !== true)
					error_msg += "The Free Book selection for "+item_name+" is required.\n\n";
			}
		}
		
		if(theform.read_financial_tos.checked !== true)
			error_msg += "Please read the Contract Terms and check the box to proceed.\n\n";
	}
	
	// if there is an error message, display it and don't submit the form
	if(error_msg != "")
	{
		alert(error_msg);
		return false;
	}
	else
		return true;
}

function copy_cc_info(theform, item_id)
{
	document.getElementById("card_name_"+item_id).value 		= theform.card_name.value;
	document.getElementById("card_number_"+item_id).value 		= theform.card_number.value;
	document.getElementById("card_cvv_"+item_id).value 			= theform.card_cvv.value;
	
	var card_type_select = document.getElementById("card_type_"+item_id);
	for(var i=0;i<card_type_select.length;i++) 
	{
	    if(card_type_select.options[i].value == theform.card_type.value)
	    	card_type_select.options[i].selected = true;
	}

	var card_month_select = document.getElementById("card_exp_month_"+item_id);
	for(var i=0;i<card_month_select.length;i++) 
	{
	    if(card_month_select.options[i].value == theform.card_exp_month.value)
	    	card_month_select.options[i].selected = true;
	}
	
	var card_year_select = document.getElementById("card_exp_year_"+item_id);
	for(var i=0;i<card_year_select.length;i++) 
	{
	    if(card_year_select.options[i].value == theform.card_exp_year.value)
	    	card_year_select.options[i].selected = true;
	}
}

function validate_cc_expiration(exp_month, exp_year)
{
	var now     	= new Date();
	var thismonth 	= now.getMonth() + 1; // returns 0-11, so need to add 1 to get the month
	var thisyear  	= now.getFullYear();
	
	if(exp_year < thisyear)
		return false;
	else
	{
	    if(exp_year == thisyear && exp_month < thismonth)
	    	return false;
	}
	
	return true;
}

function validate_cc_number(card_type, card_number)
{
	var valid_number = false;
	
	switch(card_type)
	{
		case "AmericanExpress":
			// prefix: 34 or 37
			// length: 15
			var prefix 		= card_number.substr(0,2);
			var no_length 	= card_number.length;
			
			if(no_length == 15)
			{
				if(prefix == "34" || prefix == "37")
					valid_number = true;
			}
			break;
		case "Discover":
			// prefix: 6011
			// length: 16
			var prefix 		= card_number.substr(0,4);
			var no_length 	= card_number.length;
			
			if(no_length == 16)
			{
				if(prefix == "6011")
					valid_number = true;
			}
			break;
		case "Mastercard":
			// prefix: 51-55
			// length: 16
			var prefix 				= card_number.substr(0,2);
			var no_length 			= card_number.length;
			var allowed_prefixes 	= new Array("51", "52", "53", "54", "55");
			
			if(no_length == 16)
			{
				if(in_array(prefix, allowed_prefixes))
					valid_number = true;
			}
			break;
		case "Visa":
			// prefix: 4
			// length: 13,16
			var prefix 		= card_number.substr(0,1);
			var no_length 	= card_number.length;
			
			if(no_length == 13 || no_length == 16)
			{
				if(prefix == "4")
					valid_number = true;
			}
			break;
		default:
			break;
	}
	
	return valid_number;
}

function in_array(string, array)
{
   for(i = 0; i < array.length; i++)
   {
      if(array[i] == string)
         return true;
   }
   return false;
}



// Misc
function searchBarValidate()
{
	if(document.getElementById("searchkey").value == "")
	{
		alert("Please enter a term to search by.");
		return false;
	}
	else if(document.getElementById("searchkey").value.length < 3)
	{
		alert("Please enter a search term containing 3 or more characters.");
		return false;
	}
	else
		return true;
}

function validate_search(formObj)
{
	if(formObj.search_string.value == "")
	{
		alert("Please enter a term to search by.");
		return false;
	}
	else if(formObj.search_string.value.length < 3)
	{
		alert("Please enter a search term containing 3 or more characters.");
		return false;
	}
	else
		return true;
}

// Admin Specific functions
function edititem_availability()
{
	var availability_id = document.getElementById('availability_id').value
	if(availability_id == "3")
	{
		document.getElementById('qty_unlimited').checked = true;
	}
	else
	{
		document.getElementById('qty_unlimited').checked = false;
	}
}

function validate_cc_details()
{
	if (document.getElementById('card_type').value=="" || document.getElementById('card_name').value=="" || document.getElementById('card_number').value=="" || document.getElementById('card_exp').value=="" || document.getElementById('card_cvv').value=="" || document.getElementById('baddr1').value=="" || document.getElementById('baddr2').value=="" || document.getElementById('city').value=="" || document.getElementById('state').value=="" || document.getElementById('zip').value=="")
	{
		alert("All fields are mandaory");
		return false;
	}else{
		return true;
	}
}

function toggle_calc()
{
	if(document.getElementById('override').checked){
		document.getElementById('pcalc').style.display="none";
	}else{
		document.getElementById('pcalc').style.display="block";
	}
}

function price_calc()
{
	var cost 		= document.getElementById('cost').value;
	var msrp 		= document.getElementById('msrp').value;
	var value 		= document.getElementById('calc_num_value').value;
	var calcType 	= document.getElementById('calc_type').value;
	var constain 	= document.getElementById('calc_constraint').value;
	var bvalue 		= document.getElementById('calc_bvalue').value;
	var pprice 		= 0;
	var price 		= 0;
	var error 		= false;

	if (eval(bvalue)==""  && value > 0)
	{
		alert("Please enter the "+ bvalue);
		error=true;
	}
	if(document.getElementById('override').checked=="on" || document.getElementById('override').checked==1){
		error=true;
	}

	if (error==false)
	{
		if (calcType=="%")
		{
			pprice=(eval(bvalue) * (value/100));
		}else if (calcType=="$"){
			pprice=value;
		}

		if (constain=="Above"){
			price=(parseFloat(eval(bvalue)) + parseFloat(pprice));
		}else if (constain=="Below"){
			price=(parseFloat(eval(bvalue)) - pprice);
		}
		
		if (isNaN(price))
		{
			document.getElementById('price').value = 0;
		}else{
			document.getElementById('price').value = Math.round(price);
		}	
	}
}

function toggle_parent_value(container_id)
{
	// get the current state of the div
	var div_display 	= document.getElementById(container_id).style.display;
	var toggle_link_id 	= container_id+"-link";
	
	// toggle the field
	if(div_display == "none")
	{
		document.getElementById(container_id).style.display = "block";
		document.getElementById(toggle_link_id).innerHTML 	= '<a href="javascript:void(0);" onclick="toggle_parent_value(\''+container_id+'\');">Hide Parent Value</a>';
	}
	else
	{
		document.getElementById(container_id).style.display = "none";
		document.getElementById(toggle_link_id).innerHTML 	= '<a href="javascript:void(0);" onclick="toggle_parent_value(\''+container_id+'\');">Show Parent Value</a>';
	}
}

function show_all_parent_values()
{
	var fields 			= new Array("name", "details", "sales-notes", "cost", "msrp", "price", "price-type", "financing", "sale-price", "sale-exp", "quantity", "availability", "category", "manufacturer", "condition", "skill-level", "finish-color", "tags", "weight", "length", "width", "height", "truck-shipped", "free-shipping", "image1", "image2", "image3", "attributes", "accessories", "serial-numbers", "allowances", "emphasis");
	var container 		= "";
	var toggle_link_id 	= "";
	
	for(var i = 0; i < fields.length; i++)
	{
		container 		= document.getElementById("parent-"+fields[i]);
		toggle_link 	= document.getElementById("parent-"+fields[i]+"-link");
		
		if(container != undefined)
		{
			container.style.display = "block";
			toggle_link.innerHTML	= '<a href="javascript:void(0);" onclick="toggle_parent_value(\'parent-'+fields[i]+'\');">Hide Parent Value</a>';
		}
	}
}

function hide_all_parent_values()
{
	var fields 			= new Array("name", "details", "sales-notes", "cost", "msrp", "price", "price-type", "financing", "sale-price", "sale-exp", "quantity", "availability", "category", "manufacturer", "condition", "skill-level", "finish-color", "tags", "weight", "length", "width", "height", "truck-shipped", "free-shipping", "image1", "image2", "image3", "attributes", "accessories", "serial-numbers", "allowances", "emphasis");
	var container 		= "";
	var toggle_link_id 	= "";
	
	for(var i = 0; i < fields.length; i++)
	{
		container 		= document.getElementById("parent-"+fields[i]);
		toggle_link 	= document.getElementById("parent-"+fields[i]+"-link");
		
		if(container != undefined)
		{
			container.style.display = "none";
			toggle_link.innerHTML	= '<a href="javascript:void(0);" onclick="toggle_parent_value(\'parent-'+fields[i]+'\');">Show Parent Value</a>';
		}
	}
}