// our main js file

var statesFull = {
	"AL" : "Alabama",
	"AK" : "Alaska",
	"AZ" : "Arizona",
	"AR" : "Arkansas",
	"CA" : "California",
	"CO" : "Colorado",
	"CT" : "Connecticut",
	"DE" : "Delaware",
	"DC" : "Washington DC",
	"FL" : "Florida",
	"GA" : "Georgia",
	"HI" : "Hawaii",
	"ID" : "Idaho",
	"IL" : "Illinois",
	"IN" : "Indiana",
	"IA" : "Iowa",
	"KS" : "Kansas",
	"KY" : "Kentucky",
	"LA" : "Louisiana",
	"ME" : "Maine",
	"MD" : "Maryland",
	"MA" : "Massachusetts",
	"MI" : "Michigan",
	"MN" : "Minnesota",
	"MS" : "Mississippi",
	"MO" : "Missouri",
	"MT" : "Montana",
	"NE" : "Nebraska",
	"NV" : "Nevada",
	"NH" : "New Hampshire",
	"NJ" : "New Jersey",
	"NM" : "New Mexico",
	"NY" : "New York",
	"NC" : "North Carolina",
	"ND" : "North Dakota",
	"OH" : "Ohio",
	"OK" : "Oklahoma",
	"OR" : "Oregon",
	"PA" : "Pennsylvania",
	"RI" : "Rhode Island",
	"SC" : "South Carolina",
	"SD" : "South Dakota",
	"TN" : "Tennessee",
	"TX" : "Texas",
	"UT" : "Utah",
	"VT" : "Vermont",
	"VA" : "Virginia",
	"WA" : "Washington",
	"WV" : "West Virginia",
	"WI" : "Wisconsin",
	"WY" : "Wyoming"};

var telephoneTypes = {
	'0' : 'Home',
	'1' : 'Work',
	'2' : 'Mobile',
	'3' : 'Fax'};

function getOnlyNumbers(v){
	var x = "";
	// regex would be easier but i think this is probably faster, and scriptaculous doesn't
	// screw around with it
	for (var i=0; i<v.length; i++){
		if (parseInt(v[i])==0 || v[i]==1 || v[i]==2 || v[i]==3 || v[i]==4 || v[i]==5 || v[i]==6 || v[i]==7 || v[i]==8 || v[i]==9){
			x += v[i]+'';
		}
	}
	return x;
}

function getNumbersAndDot(v){
	var x = "";
	// regex would be easier but i think this is probably faster, and scriptaculous doesn't
	// screw around with it
	for (var i=0; i<v.length; i++){
		if (parseInt(v[i])==0 || v[i]==1 || v[i]==2 || v[i]==3 || v[i]==4 || v[i]==5 || v[i]==6 || v[i]==7 || v[i]==8 || v[i]==9 || v[i]=="."){
			x += v[i]+'';
		}
	}
	return x;
}

function numericFieldFilter(fld, fmt){
	var nums = getOnlyNumbers(fld.value);
	fld.value = '';

	var numCount = 0;
	var output = "";
	var breakit = 0;
	for (var i=0; i<fmt.length; i++){
		if (nums[numCount]){
			if (fmt[i]=="#"){
				output += nums[numCount];
				numCount++;
			} else {
				output += fmt[i];
			}
		}
	}
	fld.value = output;
}

function formatnum(num) {
	num = getNumbersAndDot(num);
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'(') + num + '.' + cents + ((sign)?'':')'));
}

function updateProductPriceQtyDisplay(field, num){
	var out = $('priceMaxQty'+num);
	var lastMin = $('priceMinQty'+num);
	var newVal = parseInt(field.value)-1;
	var but = $('pricesSaveButton');
	var outAf = $('priceMaxQty'+(parseInt(num)+1));
	if (out && lastMin){
		if (newVal>=parseInt(lastMin.value)){
			out.innerHTML = newVal;
			field.style.backgroundColor = '';
			field.style.color = 'black';
			but.disabled = false;
			if (outAf.innerHTML=="--"){
				outAf.innerHTML = "+";
			}
		} else {
			field.style.backgroundColor = 'red';
			field.style.color = 'white';
			but.disabled = true;
			alert("Sorry, that is an invalid minimum quantity.");
		}
	}
}

function restoreAjaxEditField(container, val){
	var c = $(container);
	if (!c){
		alert("unable to find the '"+container+"' html element.  make sure you have the id right");
		return;
	}
	c.innerHTML = val;
	c.onclick = c.xonclick;
}

function createAjaxEditField(container, controller, editField, keyField, keyValue, defaultValue){
	var c = $(container);
	if (!c){
		alert("unable to find the '"+container+"' html element.  make sure you have the id right");
		return;
	}
	
	c.xonclick = c.onclick;
	c.onclick = null;
	
	var textFieldID = container+"Text";
	
	var originalValue = c.innerHTML;

	if (defaultValue==''){
		defaultValue = c.innerHTML;
	}
	
	if (!controller){
		alert("please pass a valid controller.");
		return;
	}
	
	if (!editField || !keyField || !keyValue){
		alert("you have to pass an edit field, a key field, and a key value.");
		return;
	}
	
	var w = c.getWidth();
	// put this here to make the floating elements around the container not go haywire...
	spacerSpan = "<span style='width: "+w+"px;'>"+defaultValue+"</span>";
		
	w = w * 1.5;
	if (w<100){
		w = 100;
	}
	var widthVal = "width: "+w+"px;";
	
	var fov = originalValue.replace(/'/, "&apos;");
	
	var iHTML = "<form style=\"display: inline; margin: 0;\" onsubmit=\"return handleAjaxEditField(this);\">";
	iHTML += "<input type='hidden' name='controller' value='"+controller+"' />";
	iHTML += "<input type='hidden' name='container' value='"+container+"' />";
	iHTML += "<input type='hidden' name='editField' value='"+editField+"' />";
	iHTML += "<input type='hidden' name='keyField' value='"+keyField+"' />";
	iHTML += "<input type='hidden' name='keyValue' value='"+keyValue+"' />";
	iHTML += "<input type='hidden' name='originalValue' value='"+originalValue.replace(/'/, "&apos;")+"' />";
	iHTML += "<div style='border: 1px solid black; position: absolute; display: inline; background-color: white;'>";
	iHTML += "<input type='text' id='"+textFieldID+"' style='"+widthVal+"border: 0;' name='editValue' value='' />"; //"+defaultValue.replace(/'/, "&apos;").replace(/"/, "&quot;")+"' />";
	iHTML += "<input type='image' align='absmiddle' style='cursor: pointer; border: 0;' src='images/icons/action_check.png' />";
	iHTML += "<img src='images/icons/action_delete.png' align='absmiddle' style='cursor: pointer; border: 0;' onclick='restoreAjaxEditField(\""+container+"\", unescape(\""+escape(originalValue)+"\"));' />";
	iHTML += "</div>"+spacerSpan;
	iHTML += "</form>";
	
	c.innerHTML = iHTML;
	$(textFieldID).value = defaultValue;
	
	$(textFieldID).focus();
	
}

function handleAjaxEditField(f){
	if (!f){
		alert("handle ajax edit field received an invalid form");
		return false;
	}
	
	var controller = f.controller.value;
	var containerName = f.container.value;
	var editField = f.editField.value;
	var editValue = f.editValue.value;
	var keyField = f.keyField.value;
	var keyValue = f.keyValue.value;
	var originalValue = f.originalValue.value;
	
	var c = $(containerName);
	
	new Ajax.Updater(
		c,
		'index.php?'+controller+'&onTheFly=1&action=save&'+keyField+'='+keyValue+'&editField='+editField+'&editValue='+editValue+'&originalValue='+originalValue,
		{
			method:'get',
			evalScripts:true
		}
	);

	c.onclick = c.xonclick;
	
	return false;
	
}

//	var myAjax = new Ajax.Request(
//		'autocomplete.php?recordOffset='+lastOffset+'&action={/literal}{getActionID action="GetAutocompleteDomains"}{literal}&sid={/literal}{$sid}{literal}&searchTerm='+term+'&'+Math.random(),
//		{
//			method: 'get',
//			onComplete: bulkTransferDomainSearchResultHandler
//		}
//	);

function parseProductPrices(p){
	if (!p){
		return new Array();
	}
	var prices = new String(p).split("|");
	for (x in prices){
		prices[x] = new String(prices[x]).split("_");
	}
	return prices;
}


//	gets all elements with this attribute
//	var spans = $$('[productPriceData]');
//	if (spans.length>0){
//		for (x in spans){
//		}
//	}


////////////////////////////////////////////////////

var gcso = null;

function performGracefulContentSwitchNoDelay(objectID, newContent){
	gcso = objectID;
	$(objectID).setAttribute("gracefulSwitchContent", newContent);
	if ($(objectID).innerHTML==""){
		$(objectID).hide();
		pgcwA();
	} else {
		Effect.Fade(objectID);
		Effect.SlideUp(objectID, {afterFinish : pgcwA});
	}
}

function performGracefulContentSwitch(objectID, newContent){
	gcso = objectID;
	$(objectID).setAttribute("gracefulSwitchContent", newContent);
	Effect.Fade(objectID);
	Effect.SlideUp(objectID, {afterFinish : pgcwA});
}

function pgcwA(){
	var obj = $(gcso);
	obj.innerHTML = obj.getAttribute("gracefulSwitchContent");
	Effect.Appear(obj);
	Effect.SlideDown(obj);
}

function numberOfItemsLeft(itemType){
	var blocks = $$('[itemType="'+itemType+'"]');
	if (blocks.length>0){
		var numBlocks = 0;
		for (x in blocks){
			if (typeof blocks[x]=="object" && blocks[x].style && blocks[x].style.display!="none"){
				numBlocks++;
			}
		}
		return numBlocks;
	} else {
		return 0;
	}
}

