/*
 * 11/12/2010 (D) - Fixed non-escaped quote which made the upcharge in roster.
 */
 
 // Rewrite candidate; merge to shareandsell.js by 07/01/2012

function send_to_cart(){
	$('buynowbutton').onclick = function(){
		return false;
	};
	var checkqty = update_prices();
	
	if(checkqty[0] == 0){
		alert('Please enter a valid set of purchase quantities.');
		$('buynowbutton').onclick = function(){
			send_to_cart();
		};
	}else{	
		document.sizeqtyform.submit();
	}
	return false;
}
	
function toggle(el_id,w){
	if(w && w.className=="sas_tab_active"){
		return false;
	}
	var place = el_id.split('_');
	var i = 0;
	var a = $('sas_tabs_container').getElementsByTagName('div'),ai=a.length;
	while(ai--){
		if(a[ai]!=w){
			a[ai].className="sas_tab";
		}else{
			a[ai].className="sas_tab_active";
		}
	}
	while($(i + '_' + place[1])){
		if(place[0] == i){
			$(el_id).style.display = 'block';	
		}else{
			$($(i + '_' + place[1])).style.display = 'none';
		}
		i++;
	}
	return false;
}

function add_roster() {
	create_box(jQuery('#team_name_add_popup'));
	build_chart(1, 1);
}

function $(id) {
	return document.getElementById(id);
}
	
function build_chart(name, num) {
	if(jQuery("#names_numbers").val() == "") {	
		var i = 0;
		var output = "";
		var size_array = new Array();
		
		jQuery('#NamesNumbers').empty();
		
		jQuery('.add_to_cart_price_grid_size_input input').each(function(i, e) {
			var temporary_quantity = e.value.replace(/[^0-9]/gi,"");
			
			if(!temporary_quantity){
				temporary_quantity = 0;
			} else {
				temporary_quantity = parseInt(temporary_quantity);
			}
			
			size_array[e.name.replace("size[", "").replace("]", "") ] = temporary_quantity;
			i++;
		});
		
		for(var size in size_array) {
			if(size_array[size] == 0 || typeof size_array[size] == "function") {
				continue;
			}
			
			for(j = 1;j <= size_array[size]; j++) {
				output += "<div class='chart' id=" + size + j + "><select>";
				
				for(var s in size_array){
					if(typeof size_array[s] != "function") {
						selected = "";
						if(s == size){
							selected = "selected";
						}
						output += "<option value=" + s + " " + selected + " >" + s + "</option>";
					}
				}
				
				if(name == 1 && num == 1) {
					output += "</select>&nbsp;&nbsp;&nbsp;Number:&nbsp;&nbsp;<input maxlength='2' value='' onKeyUp='this.value = reg_ex_check(this.value, 1);' onBlur='this.value = reg_ex_check(this.value, 1);' type='text' style='width:50px;padding;0px 20px;' name='numbers[]' />&nbsp;&nbsp;&nbsp;Name:&nbsp;&nbsp;<input name='names[]' type='text' />";
				}
				
				if(name == 1 && num == 0) {
					output += "</select>&nbsp;&nbsp;&nbsp;Number:&nbsp;&nbsp;<input maxlength='2' value='' onKeyUp='this.value = reg_ex_check(this.value, 1);' onBlur='this.value = reg_ex_check(this.value, 1);' type='text' style='width:50px;padding;0px 20px; background: #c0c0c0;' name='numbers[]' disabled />&nbsp;&nbsp;&nbsp;Name:&nbsp;&nbsp;<input name='names[]' type='text' />";
				}
				
				if(num == 1 && name == 0) {
					output += "</select>&nbsp;&nbsp;&nbsp;Number:&nbsp;&nbsp;<input maxlength='2' value='' onKeyUp='this.value = reg_ex_check(this.value, 1);' onBlur='this.value = reg_ex_check(this.value, 1);' type='text' style='width:50px;padding;0px 20px;' name='numbers[]' />&nbsp;&nbsp;&nbsp;Name:&nbsp;&nbsp;<input name='names[]' type='text' disabled style='background: #c0c0c0' />";
				}
				
				output += "&nbsp;&nbsp;&nbsp;<img src='/personalize/images_new/cross.png' border='0' class='cross_image' onclick='remove_nn(this.parentNode)' alt='remove' onMouseOver=\"this.style.cursor='pointer';\" onMouseOut=\"this.style.cursor='auto';\" title='remove' width='16' height='16' /><br /></div>";
			}
		}
		
		if(!output) {
			output += '<p style="margin-left: 0px; text-align: center; font-weight: bold;" id="validqty">You must enter a valid quantity to add names and numbers to your product.</p>';
		}
	}
	
	if(output){
		jQuery('#get_started').empty();
	}
	
	jQuery('#NamesNumbers').append(output);
}
	
function update_names_numbers(){
	jQuery(".add_to_cart_tab").removeClass("add_to_cart_tab_active");
	jQuery(".add_to_cart_tab_content_container").hide();
	jQuery("#roster_tab").show().addClass("add_to_cart_tab_active");
	jQuery("#roster_container").show();

	jQuery("names_numbers").val("");
	var fields = jQuery(".chart");
	var store_string = "";
	var size_update = {};
	
	//flag whether or not there is a blank name and number
	var flag = 0;
	for(i = 0;i < fields.length; i++){
		if(!size_update[fields[i].childNodes[0].value]) {
			size_update[fields[i].childNodes[0].value] = {};
		}
		
		if(!size_update[fields[i].childNodes[0].value].count) {
			size_update[fields[i].childNodes[0].value].count = 0;
		}
		
		if(!size_update[fields[i].childNodes[0].value].size_string) {
			size_update[fields[i].childNodes[0].value].size_string = "";
		}
		
		if(fields[i].childNodes[2].value == ""){
			fields[i].childNodes[2].value = " ";
			flag = 1;
		}
		
		if(fields[i].childNodes[3].value == ""){
			fields[i].childNodes[3].value = " ";
			flag = 1;
		}
		
		var num_tmp = (fields[i].childNodes[2].value == undefined || fields[i].childNodes[2].value == "") ? " " : fields[i].childNodes[2].value;
		var name_tmp = (fields[i].childNodes[4].value == undefined || fields[i].childNodes[4].value == "") ? " " : fields[i].childNodes[4].value;
		
		size_update[fields[i].childNodes[0].value].count++;
		size_update[fields[i].childNodes[0].value].size_string += num_tmp + "|" + name_tmp + "||";
	}
	
	var tmp_size_output = "";
	
	for(s in size_update){
		tmp_size_output += s + ": " + size_update[s].size_string + "\n";
		jQuery("input[name='size[" + s + "]']").val(size_update[s].count);
	}
	
	jQuery("#names_numbers").val(tmp_size_output);
	
	if(flag) {
		alert("Some of your items are missing a custom name and/or number and will be printed using the default name and/or number provided on the design");
	}
	
	update_prices();
	display_only_chart();
}
	
function addOne(howMany, name, num){
	jQuery('#validqty').hide();
	howMany = parseInt(howMany);
	
	if(howMany > 200 || howMany <= 0){
		alert("Maximum/Minimum Quantities Exceeded");
		return false;
	}
	
	var i = 0;
	var output ="";
	var size_array = new Array();
	
	jQuery("#size_" + i + "").each(function(i, e) {
		var temporary_quantity = jQuery("#size_" + i + "").val().replace(/[^0-9]/gi, "");
		
		if(!temporary_quantity) {
			temporary_quantity = 0;
		} else {
			temporary_quantity = parseInt(temporary_quantity);
		}
		
		size_array[jQuery('#size_' + i + '').attr('name').replace("size[", "").replace("]", "")] = temporary_quantity;
		i++
	});
	
	for(q = 0;q < howMany; q++) {
		if(size_array[q] == 0 || typeof size_array[q] == "function") {
			continue;
		}
	
		output += "<div class='chart' id='added_" + q + "' ><select>";
		for(var s in size_array){
			/* non-escaped quote was here */
			output += "<option value='" + s + "'>" + s + "</option>";
		}
		if(name == 1 && num == 1) {
			output += "</select>&nbsp;&nbsp;&nbsp;NUMBER:&nbsp;&nbsp;<input maxlength='2' value='' onKeyUp='this.value = reg_ex_check(this.value, 1);' onBlur='this.value = reg_ex_check(this.value, 1);' type='text' style='width:50px;padding;0px 20px;' name='numbers[]' />&nbsp;&nbsp;&nbsp;NAME:&nbsp;&nbsp;<input name='names[]' type='text' />";
		} 
		if(name == 1 && num == 0) {
			output += "</select>&nbsp;&nbsp;&nbsp;NUMBER:&nbsp;&nbsp;<input maxlength='2' value='' onKeyUp='this.value = reg_ex_check(this.value, 1);' onBlur='this.value = reg_ex_check(this.value, 1);' type='text' style='width:50px;padding;0px 20px;' name='numbers[]' disabled />&nbsp;&nbsp;&nbsp;NAME:&nbsp;&nbsp;<input name='names[]' type='text' />";
		}
		if(num == 1 && name == 0) {
			output += "</select>&nbsp;&nbsp;&nbsp;NUMBER:&nbsp;&nbsp;<input maxlength='2' value='' onKeyUp='this.value = reg_ex_check(this.value, 1);' onBlur='this.value = reg_ex_check(this.value, 1);' type='text' style='width:50px;padding;0px 20px;' name='numbers[]' />&nbsp;&nbsp;&nbsp;NAME:&nbsp;&nbsp;<input name='names[]' type='text' disabled />";
		}
		output += "&nbsp;&nbsp;&nbsp;<img src='/personalize/images_new/cross.png' border='0' class='cross_image' onclick='remove_nn(this.parentNode)' alt='remove' onMouseOver=\"this.style.cursor='pointer';\" onMouseOut=\"this.style.cursor='auto';\" title='remove' width='16' height='16' /><br /></div>";
	}
	
	jQuery('#NamesNumbers').append(output);
	
	if(output){
		jQuery('#get_started').empty();
	}
}
	
function remove_nn(el){
	jQuery(el).removeClass();
	jQuery(el).remove();
	var temp = jQuery("input[name='size[" + el.childNodes[0].value + "]']");
	if(temp[0].value > 0){
		temp[0].value--;
	}
	
	if(temp[0].value == 0) {
		jQuery('#validqty').show();
	}
}
	
function show_design(el_id){
	$(el_id).style.display = 'block';
	return false;
}

function hide_design(el_id){
	$(el_id).style.display = 'none';
	return false;
}

function display_only_chart(){
	var display = jQuery('#add_to_cart_names_numbers_container');
	display.empty();
	var data = jQuery('#names_numbers').val();
	var output = '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
	var countem = 0;
	output += '<tr class="chart_head"><td width="20%" align="center">Size</td><td width="20%" align="center">Number</td><td width="60%">Name</td></tr>';
	
	var size_break = data.split("\n");
	
	for(i = 0; i < size_break.length; i++) {
		temp_each_product = size_break[i].split(": ");
		product_size = temp_each_product[0];
		each_name_number = temp_each_product[1].split("||");
		
		for(j=0;j<each_name_number.length-1;j++){
			temp = each_name_number[j].split("|");
			number = temp[0];
			name = temp[1];
			if(!name && !number){
				continue;
			}
			if(name == "undefined") {
				name = "";
			}
			if(number == "undefined") {
				number = "";
			}
			countem++;
			output += '<tr class="item_row"><td>' + product_size + '</td><td>' + number + '</td><td>' + name + '</td></tr>';
			if(countem==3){
				break;
			}
		}
	}
	output += '<tr><td colspan="3"><a href="#">Show All...</a></td></tr></table>';
	if(countem == 0){
		return false;
	}
	jQuery('#add_to_cart_names_numbers_container').append(output);
	jQuery('#add_to_cart_names_numbers_container').show();
}

function reg_ex_check(string, filter){
	var mystring = string;
	//match non-numbers
	if(filter==1){
		mystring = string.replace(/[^0-9]/gi,'');
	}
	//match pipe
	if(filter==2){
		mystring = string.replace(/\u007c/gi,'');
	}
	return mystring;
}

function create_box(content){
	new Boxy(content, {modal:true});
}

function check_existing(content){
	if($('names_numbers').value){
		create_box(content);
	}else{
		return false;
	}
}

