function insertAfter(newChild, refChild) { 
	refChild.parentNode.insertBefore(newChild,refChild.nextSibling); 
} 

function plusCat(catId, max, price)
{
	
	if (document.getElementById('selected_extra')) {
		var select = document.getElementById('catSelect');
		children = document.getElementById('selected_extra').childNodes;
		
		var newSelect = select.cloneNode(true);
		newSelect.id = "";
		var notWhitespace = /\S/;
	  	for (var x = 0; x < newSelect.childNodes.length; x++) {
		    var childNode = newSelect.childNodes[x];
		    if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
				/* that is, if it's a whitespace text node*/
		      	newSelect.removeChild(newSelect.childNodes[x]);
		      	x--;
		    }
		}
		var opt = newSelect.childNodes[0].options;
		for (i = 0; i < opt.length; i++) {
			if (opt[i].value == catId) {
				opt[i].selected = true;
			}
		}
	
		var delLink = document.getElementById('catDelLink');
		var a = delLink.cloneNode(true);
		a.id = "";
		a.style.display = "inline";
		newSelect.appendChild(a);
		
		var listingCombo = document.getElementById("CATEGORY_ID_TOP");
		
		var listingType = listingCombo.options[listingCombo.selectedIndex].text;
		
		
		var max = checkCategorySelection(listingType, 2);
		
		if (children.length < max) {	
			delLink.parentNode.insertBefore(newSelect, delLink);
	    } else {
	    	/*if (document.getElementById("price_feat") != null)
			document.getElementById("price_feat").innerHTML = eval(eval(document.getElementById("price_feat").innerHTML)+eval(price)); 
	    	if (document.getElementById("price_norm") != null)
	    	document.getElementById("price_norm").innerHTML = eval(eval(document.getElementById("price_norm").innerHTML)+eval(price));
	    	*/ 
	    	alert("You have selected the maximum number of categories ("+max+") allowed for your payment plan.");
		}
		
	} else {
		var select = document.getElementById('catSelect');

		var newSelect = select.cloneNode(true);
		
		newSelect.id = "";
		var notWhitespace = /\S/;
	  	for (var x = 0; x < newSelect.childNodes.length; x++) {
		    var childNode = newSelect.childNodes[x];
		    if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
				/* that is, if it's a whitespace text node*/
		      	newSelect.removeChild(newSelect.childNodes[x]);
		      	x--;
		    }
		}
		var opt = newSelect.childNodes[0].options;
		for (i = 0; i < opt.length; i++) {
			if (opt[i].value == catId) {
				opt[i].selected = true;
			}
		}
	
		var delLink = document.getElementById('catDelLink');
		var a = delLink.cloneNode(true);
		a.id = "";
		a.style.display = "inline";
		newSelect.appendChild(a);
		
		delLink.parentNode.insertBefore(newSelect, delLink);
	}
	
}	

function removeCat(element, max, price)
{
	var toRem = element.parentNode;
	toRem.parentNode.removeChild(toRem);
		
	document.getElementById('plusCategory').style.display = "block";
	
	if (document.getElementById('selected_extra')) {
		children = document.getElementById('selected_extra').childNodes;
		if (children.length > max) {	
			/*if (document.getElementById("price_feat") != null)
			document.getElementById("price_feat").innerHTML = eval(eval(document.getElementById("price_feat").innerHTML)-eval(price));
			if (document.getElementById("price_norm") != null)
			document.getElementById("price_norm").innerHTML = eval(eval(document.getElementById("price_norm").innerHTML)-eval(price));
			*/ 
		}
	}
	
}


function checkCategorySelection(cat, pack) {
	
	var xmlHttp;
	try {
  		/* Firefox, Opera 8.0+, Safari*/
  		xmlHttp = new XMLHttpRequest();
  	} catch (e) {
  		/* Internet Explorer*/
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} catch (e) {
    		try {
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		} catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
      		}	
    	}
  	}
  	xmlHttp.onreadystatechange=function() {
    	if(xmlHttp.readyState==4) {	
      	}
    }
   
   	//var url = "http://localhost/hot100/checkhot100settings.php";
	var url = "http://hot100contractorsaz.com/checkhot100settings.php";
	xmlHttp.open("POST", url, false);
	xmlHttp.setRequestHeader( 
	   	'Content-Type', 
	   	'application/x-www-form-urlencoded; charset=UTF-8' 
	); 
	xmlHttp.send('cat='+cat+'&check='+1);
	var rez = xmlHttp.responseText;
	return rez;
	//alert(rez);
	
}


function checkFilterSelection(currentCheckbox) {
	//Get the number of the currently selected checkboxes
	var checkboxes = document.getElementsByName("FILTER_ID[]"); 
	var total = 0;
	for(var i = 0; i < checkboxes.length; i++) {
		if(checkboxes[i].checked == true) {
			total++;
		}
	}
	
	var xmlHttp;
	try {
  		/* Firefox, Opera 8.0+, Safari*/
  		xmlHttp = new XMLHttpRequest();
  	} catch (e) {
  		/* Internet Explorer*/
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} catch (e) {
    		try {
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		} catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
      		}	
    	}
  	}
  	xmlHttp.onreadystatechange=function() {
    	if(xmlHttp.readyState==4) {	
      	}
    }
   
   	//var url = "http://localhost/hot100/checkhot100settings.php";
	var url = "http://hot100contractorsaz.com/checkhot100settings.php";
	xmlHttp.open("POST", url, false);
	xmlHttp.setRequestHeader( 
	   	'Content-Type', 
	   	'application/x-www-form-urlencoded; charset=UTF-8' 
	); 
	
	var comboPackage = document.getElementById("PACKAGE");	
	var listingPackage = comboPackage.options[comboPackage.selectedIndex].text;
	
	var typeCombo = document.getElementById("CATEGORY_ID_TOP");	
	var listingType = typeCombo.options[typeCombo.selectedIndex].text;
	
	xmlHttp.send('cat='+listingType+'&check='+2+'&pack='+listingPackage);
	var rez = xmlHttp.responseText;

	
	if (rez < total) {
		alert("You have selected the maximum number of filters ("+rez+") allowed for your payment plan.");
		currentCheckbox.checked = false;
	}
	return false;
}


function validate_form(thisform)
{
	
	var listingCombo = document.getElementById("CATEGORY_ID_TOP");	
	var listingType = listingCombo.options[listingCombo.selectedIndex].text;
	if (listingType != "Sponsor") {		
		var select = document.getElementById('catSelect');
		children = document.getElementById('selected_extra').childNodes;
		if (children.length == 0) {
			alert("At least 1 category must be selected to save record");
			return false;
		}
		var checkboxes = document.getElementsByName("FILTER_ID[]"); 
		var total = 0;
		for(var i = 0; i < checkboxes.length; i++) {
			if(checkboxes[i].checked == true) {
				total++;
			}
		}
		if (total == 0) {
			alert("At least 1 filter must be selected to save record");
			return false;
		}
	}
}
