
function addEvent(evt, elem, newFunction) {
    var oldFunction = elem[evt];
    
    
    if( typeof elem[evt]=='function' ) {

        elem[evt] = function() {
            oldFunction();
            newFunction(); 
        };
        
    } else {
        elem[evt] = newFunction;
    }
}


function getResponseElement() {
	var p = document.getElementById('ajax-response-p');
	if (!p) {
		p = document.createElement('p');
		p.id = 'ajax-response-p';
		document.getElementById('ajax-response').appendChild(p);
		return p;
	}
}

function getProducts(parent,orgid,uid) {
	var xmldoc;
	childs = new sack('/wp-content/plugins/products/products_ajax.php');
	if ( childs.failed ) return true;
	childs.myResponseElement = getResponseElement();
	childs.method = 'GET';
	//childs.onLoading = function() { childs.myResponseElement.innerHTML = 'Sending Data...'; };
	//childs.onLoaded = function() { childs.myResponseElement.innerHTML = 'Data Sent...'; };
	//childs.onInteractive = function() { childs.myResponseElement.innerHTML = 'Processing Data...'; };
	childs.onCompletion = function() {	xmldoc=childs.responseXML;
	
										UpdateList(xmldoc,'PRODUCT','product_id','Choose product',0); 
										childs.myResponseElement.parentNode.removeChild(childs.myResponseElement);
									 };
	childs.runAJAX('action=get_products&parent='+parent+'&uid='+uid+'&orgid='+orgid);
	return false;
}

function UpdateList(xmldoc, tagName, selectBoxName, default_option,sel_value)
{
		var node_list = xmldoc.getElementsByTagName(tagName);
		var select = document.getElementById(selectBoxName);
		var counter = 1;
		
		// Store Past Value (only retain the value if the item is in the stack)
		var stored_value;
		
		if(document.getElementById("stack").value.match(tagName.toLowerCase()) != null)
		{
			//alert(tagName + " in stack, reselecting " + document.getElementById("stack").value.match(tagName.toLowerCase()));
			if(select.selectedIndex != -1)
			{
				stored_value = select.options[select.selectedIndex].value;
			}
		}
		
		// Clear List
		while (select.length > 0) {
			select.remove(0);
		}
		
		// Repopulate
		select.options[0] = new Option(default_option, "0");
		//alert(node_list.length)
		for (var i = 0; i < node_list.length; i++)
		{

			name=node_list.item(i).firstChild.firstChild.nodeValue;
			val=node_list.item(i).lastChild.lastChild.nodeValue;
			if(val!= null)
			{
				select.options[counter] = new Option(name, val);
				counter++;
			}
		}
		
		// Reselect
	//	alert(sel_value);
		for (var i = 0; i < select.options.length; i ++)
		{
			option_value = select.options[i].value;
		//	alert(option_value);
			if(option_value == sel_value || option_value == stored_value)
			{
				select.selectedIndex = i;
			}
		}
} //end UpdateList

/*function CountWords (this_field, show_word_count, show_char_count) {
	if (show_word_count == null) {
		show_word_count = true;
	}
	if (show_char_count == null) {
		show_char_count = false;
	}
var char_count = this_field.value.length;
var fullStr = this_field.value + " ";
var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
var splitString = cleanedStr.split(" ");
var word_count = splitString.length -1;
if (fullStr.length <2) {
word_count = 0;
}
if (word_count == 1) {
wordOrWords = " word";
}
else {
wordOrWords = " words";
}
if (char_count == 1) {
charOrChars = " character";
} else {
charOrChars = " characters";
}
if (show_word_count & show_char_count) {
alert ("Word Count:\n" + "    " + word_count + wordOrWords + "\n" + "    " + char_count + charOrChars);
}
else {
if (show_word_count) {
alert ("Word Count:  " + word_count + wordOrWords);
}
else {
if (show_char_count) {
alert ("Character Count:  " + char_count + charOrChars);
      }
   }
}
return word_count;
}*/

function productFormValidate(form) {
		
	var maxwords=500;		
	var field=form.content;
	words=CountWords(field,false,false);
	if(words>maxwords) 
	{
		alert("Maximum 500 words description");
		return false;
	}
	return checkPrices(form);
	
	//return true;
}

function testNum(value) {
var regenum=/(^\d+$)|(^\d+\.\d+$)|(^\d+,\d+$)/

	if(value!='' && !regenum.test(value))
		return false;
	return true;
}

function checkPrices(form) {



	if((form.one_time_price.value=='' || form.one_time_price.value==0) && (form.monthly_price.value=='' || form.monthly_price.value==0) && (form.yearly_price.value=='' || form.yearly_price.value==0) && (form.user_price.value=='' || form.user_price.value==0))
	{
		alert("You need to fill at least one of the 4 price fields!");
		form.one_time_price.focus();
		return false;
	}
	
	if (!testNum(form.one_time_price.value) || !testNum(form.monthly_price.value) || !testNum(form.yearly_price.value) || !testNum(form.user_price.value) )
	{	
		alert("Only numeric values in each one of the four price fields!");
		form.one_time_price.focus();
		return false;
	}
	return true;
}

