if( document.getElementById ) {
getElemById = function( id ) {
return document.getElementById( id );
}
} else if( document.all ) {
getElemById = function( id ) {
return document.all[ id ];
}
} else if( document.layers ) {
getElemById = function( id ) {
return document.layers[ id ];
}
}
 
function showhide( el, id) {
if ( el && el.style ) {
getElemById( id ).style.display = (el.checked)? '' : 'none';
}
}

function hideshow( el, id) {
if ( el && el.style ) {
getElemById( id ).style.display = (el.checked)? 'none' : '';
}
}

function show( el, id ) {
if ( el && el.style ) {
getElemById( id ).style.display = '';
}
}

function hide( el, id ) {
if ( el && el.style ) {
getElemById( id ).style.display = 'none';
}
}

function remNewGroup( id ) {
	var d = document.getElementById('more_groups_container');
  	var olddiv = document.getElementById(id);
  	d.removeChild(olddiv);
}

var agroup_divs = 0;

function addAgentGroup() {
	++agroup_divs;

	var moreGroupsDiv = document.getElementById("more_groups");
	
	var newDiv = document.createElement("div");
	newDiv.setAttribute('id','newgroupdiv_' + agroup_divs);
	
	var div_html = '<table width="100%" border="0" cellpadding="5" cellspacing="0" background="images/greypixel.gif">';
    div_html += '<tr>';
    div_html += '<td width="100" align="left" valign="middle"><span class="fieldLabels">New Group Name </span></td>';
    div_html += '<td width="200" align="left" valign="middle"><input name="new_group_' + agroup_divs + '" type="text" id="new_group_' + agroup_divs + '" class="prodStandardTextbox" /></td>';
    div_html += '<td width="200" align="left" valign="middle"><span class="fieldLabels">Permissions can be set after adding </span></td>';
	div_html += '<td align="left" valign="middle"><span class="fieldLabels">';
    div_html += '<input name="del_new_group_' + agroup_divs + '" type="checkbox" id="del_new_group_' + agroup_divs + '" value="checkbox" onclick="remNewGroup(\'newgroupdiv_' + agroup_divs + '\')" />';
    div_html += 'Delete</span></td>';
    div_html += '</tr>';
    div_html += '</table>';
	
	newDiv.innerHTML = div_html;
	  
	moreGroupsDiv.parentNode.insertBefore(newDiv, moreGroupsDiv);
	
	document.getElementById("another_group").checked = false;
}

// Dinamic stuff for adding zips section
function remNewAgent( id ) {
	var d = document.getElementById('more_agents_container');
  	var olddiv = document.getElementById(id);
  	d.removeChild(olddiv);
}

var agent_divs = 0;

function addAgent() {
	++agent_divs;

	var MoreAgentsDiv = document.getElementById("more_agents");
	
	var newDiv = document.createElement("div");
	newDiv.setAttribute('id','newagentdiv_' + agent_divs);
	
	var div_html = '<table width="100%" border="0" cellpadding="5" cellspacing="0" background="images/greypixel.gif">';
    div_html += '<tr>';
    div_html += '<td width="100" align="left" valign="middle"><span class="fieldLabels">Username </span></td>';
    div_html += '<td width="200" align="left" valign="middle"><input name="new_username_' + agent_divs + '" type="text" id="new_username_' + agent_divs + '" class="prodStandardTextbox" value="" /></td>';
    div_html += '<td width="150" align="left" valign="middle"><span class="fieldLabels">';
    div_html += 'In ';
    div_html += '<select name="new_ingroup_' + agent_divs + '">';
    div_html += agroup_options;
	div_html += '</select>';
    div_html += '</span></td>';
	div_html += '<td align="left" valign="middle"><span class="fieldLabels">';
    div_html += '<input name="del_new_agent_' + agent_divs + '" type="checkbox" id="del_new_agent_' + agent_divs + '" onclick="remNewAgent(\'newagentdiv_' + agent_divs + '\')" value="checkbox" />';
    div_html += 'Delete </span></td>';
    div_html += '</tr>';
    div_html += '</table>';
	
	div_html += '<table width="100%" border="0" cellpadding="5" cellspacing="0" background="images/greypixel.gif">';
    div_html += '<tr>';
    div_html += '<td width="100" align="left" valign="middle" class="agents_low_border"><span class="fieldLabels">Password</span></td>';
    div_html += '<td width="200" align="left" valign="middle" class="agents_low_border"><input name="new_pword_' + agent_divs + '" type="password" id="new_pword_' + agent_divs + '" class="prodStandardTextbox" value="" /></td>';
    div_html += '<td width="75" align="left" valign="middle" class="agents_low_border"><span class="fieldLabels">Confirm </span></td>';
    div_html += '<td align="left" valign="middle" class="agents_low_border"><input name="new_pword_confirm_' + agent_divs + '" type="password" id="new_pword_confirm_' + agent_divs + '" class="prodStandardTextbox" value="" /></td>';
    div_html += '</tr>';
    div_html += '</table>';
	
	newDiv.innerHTML = div_html;
	  
	MoreAgentsDiv.parentNode.insertBefore(newDiv, MoreAgentsDiv);
	
	document.getElementById("another_agent").checked = false;
}

// Dinamic stuff for adding zips section
function remNewZip( id ) {
	var d = document.getElementById('morezips_container');
  	var olddiv = document.getElementById(id);
  	d.removeChild(olddiv);
}

var zipdivs = 0; // used to keep track of how many new zip fields there are and name new ones accordingly

function addZipField() {
	++zipdivs;

	var zip_name = 'new_zip_' + zipdivs;
	var zip_fee_name = 'new_zip_fee_' + zipdivs;
	var moreZipsDiv = document.getElementById("morezips");
	
	var newDiv = document.createElement("div");
	newDiv.setAttribute('id','newzipdiv_' + zipdivs);
	
	var div_html = '<table width="100%" border="0" cellpadding="5" cellspacing="0" background="images/greypixel.gif">';
    div_html += '<tr>';
    div_html += '<td width="100" align="left" valign="middle"><span class="fieldLabels">New Zip Code<br />';
    div_html += '<input name="rem_new_zip_' + zipdivs + '" type="checkbox" id="rem_new_zip_' + zipdivs + '" value="checkbox" onclick="remNewZip(\'newzipdiv_' + zipdivs + '\')" />';
    div_html += 'Remove</span></td>';
    div_html += '<td width="200" align="left" valign="middle"><input name="' + zip_name + '" type="text" id="' + zip_name + '" class="prodStandardTextbox" value="" /></td>';
    div_html += '<td width="75" align="left" valign="middle"><span class="fieldLabels">Delivery Fee</span></td>';
    div_html += '<td align="left" valign="middle"><span class="fieldLabels">$</span>';
    div_html += '<input name="' + zip_fee_name + '" type="text" class="prodStandardTextbox" id="' + zip_fee_name + '" value="" /></td>';
    div_html += '</tr>';
    div_html += '</table>';
	
	newDiv.innerHTML = div_html;
	  
	moreZipsDiv.parentNode.insertBefore(newDiv, moreZipsDiv);
	
	document.getElementById("another_zip").checked = false;

}

// Dinamic stuff for adding referrals section
function remNewRef( id ) {
	var d = document.getElementById('morerefs_container');
  	var olddiv = document.getElementById(id);
  	d.removeChild(olddiv);
}

var refdivs = 0; // used to keep track of how many new referral fields there are and name new ones accordingly

function addRefField() {
	++refdivs;

	var ref_name = 'new_ref_' + refdivs;
	var moreRefsDiv = document.getElementById("morerefs");
	
	var newDiv = document.createElement("div");
	newDiv.setAttribute('id','newrefdiv_' + refdivs);
	
	var div_html = '<table width="100%" border="0" cellpadding="5" cellspacing="0" background="images/greypixel.gif">';
    div_html += '<tr>';
    div_html += '<td width="100" align="left" valign="middle"><span class="fieldLabels">New Referrer</span></td>';
    div_html += '<td width="200" align="left" valign="middle"><input name="' + ref_name + '" type="text" id="' + ref_name + '" class="prodStandardTextbox" value="" /></td>';
    div_html += '<td width="75" align="left" valign="middle"><span class="fieldLabels">Remove</span></td>';
    div_html += '<td align="left" valign="middle"><input type="checkbox" name="rem_new_ref_' + refdivs + '" value="checkbox" onclick="remNewRef(\'newrefdiv_' + refdivs + '\')" /></td>';
    div_html += '</tr>';
    div_html += '</table>';
	
	newDiv.innerHTML = div_html;
	  
	moreRefsDiv.parentNode.insertBefore(newDiv, moreRefsDiv);
	
	document.getElementById("another_ref").checked = false;

}

// Dinamic stuff for adding surfaces section
function remNewSurface( id ) {
	var d = document.getElementById('moresurfaces_container');
  	var olddiv = document.getElementById(id);
  	d.removeChild(olddiv);
}

var surfacedivs = 0; // used to keep track of how many new surface fields there are and name new ones accordingly

function addSurfaceField() {
	++surfacedivs;

	var surface_name = 'new_surface_' + surfacedivs;
	var moreSurfacesDiv = document.getElementById("moresurfaces");
	
	var newDiv = document.createElement("div");
	newDiv.setAttribute('id','newsurfacediv_' + surfacedivs);
	
	var div_html = '<table width="100%" border="0" cellpadding="5" cellspacing="0" background="images/greypixel.gif">';
    div_html += '<tr>';
    div_html += '<td width="100" align="left" valign="middle"><span class="fieldLabels">New Surface</span></td>';
    div_html += '<td width="200" align="left" valign="middle"><input name="' + surface_name + '" type="text" id="' + surface_name + '" class="prodStandardTextbox" value="" /></td>';
    div_html += '<td width="75" align="left" valign="middle"><span class="fieldLabels">Remove</span></td>';
    div_html += '<td align="left" valign="middle"><input type="checkbox" name="rem_new_surface_' + surfacedivs + '" value="checkbox" onclick="remNewSurface(\'newsurfacediv_' + surfacedivs + '\')" /></td>';
    div_html += '</tr>';
    div_html += '</table>';
	
	newDiv.innerHTML = div_html;
	  
	moreSurfacesDiv.parentNode.insertBefore(newDiv, moreSurfacesDiv);
	
	document.getElementById("another_surface").checked = false;

}

var holdivs = 0; // used to keep track of how many new document fields there are and name new ones accordingly

var month_options = '<option value="01">January</option>';
month_options += '<option value="02">February</option>';
month_options += '<option value="03">March</option>';
month_options += '<option value="04">April</option>';
month_options += '<option value="05">May</option>';
month_options += '<option value="06">June</option>';
month_options += '<option value="07">July</option>';
month_options += '<option value="08">August</option>';
month_options += '<option value="09">September</option>';
month_options += '<option value="10">October</option>';
month_options += '<option value="11">November</option>';
month_options += '<option value="12">December</option>';

var day_options = '';
for(i = 1; i <= 31; i++)
{
	if(i < 10) {
		day_options += '<option value="0' + i + '">0' + i + '</option>';
	}
	else {
		day_options += '<option value="' + i + '">' + i + '</option>';
	}
}

function addHolidayField() {
	++holdivs;

	var hol_name = 'new_hol_name_' + holdivs;
	var hol_month = 'new_hol_month_' + holdivs;
	var hol_day = 'new_hol_day_' + holdivs;
	var moreHolsDiv = document.getElementById("morehols");
	
	var newDiv = document.createElement("div");
	newDiv.setAttribute('id','newholdiv_' + holdivs);
	
	var div_html = '<table width="100%" border="0" cellpadding="5" cellspacing="0" background="images/greypixel.gif">';
    div_html += '<tr>';
    div_html += '<td width="100" align="left" valign="middle"><span class="fieldLabels">Holiday Date</span></td>';
    div_html += '<td width="200" align="left" valign="middle">';
	div_html += '<select name="' + hol_month + '" id="' + hol_month + '">';
	div_html += month_options;
	div_html += '</select>';
	div_html += ' <select name="' + hol_day + '" id="' + hol_day + '">';
	div_html += day_options;
	div_html += '</select>';
	div_html += '</td>';
    div_html += '<td width="75" align="left" valign="middle"><span class="fieldLabels">Holiday Name </span></td>';
    div_html += '<td align="left" valign="middle"><input name="' + hol_name + '" type="text" id="' + hol_name + '" class="prodStandardTextbox" /></td>';
    div_html += '</tr>';
    div_html += '</table>';
	
	newDiv.innerHTML = div_html;
	  
	moreHolsDiv.parentNode.insertBefore(newDiv, moreHolsDiv);
	
	document.getElementById("another_holiday").checked = false;

}

// Dinamic stuff for adding document section
function remNewDoc( id ) {
	var d = document.getElementById('moresurfaces_container');
  	var olddiv = document.getElementById(id);
  	d.removeChild(olddiv);
}

var docdivs = 0; // used to keep track of how many new document fields there are and name new ones accordingly

function addDocumentField() {
	++docdivs;

	var doc_name = 'new_doc_' + docdivs;
	var moreDocsDiv = document.getElementById("moredocs");
	
	var newDiv = document.createElement("div");
	newDiv.setAttribute('id','newdocdiv_' + docdivs);
	
	var div_html = '<table width="100%" border="0" cellpadding="5" cellspacing="0" background="images/greypixel.gif">';
    div_html += '<tr>';
    div_html += '<td width="100" align="left" valign="middle"><span class="fieldLabels">New File</span></td>';
    div_html += '<td><input type="file" name="' + doc_name + '" />';
    div_html += '<span class="fieldLabels">Upload new Document</span></td>';
    div_html += '</tr>';
    div_html += '</table>';
	
	newDiv.innerHTML = div_html;
	  
	moreDocsDiv.parentNode.insertBefore(newDiv, moreDocsDiv);
	
	document.getElementById("another_document").checked = false;

}

// Dinamic stuff for Recommended Products section
//var fieldDivs2 = 1; // used to keep track of how many upload fields there are and name new ones accordingly
// !!! THIS VARABLE MUST BE SENT BEFORE USING THE FUNCTION BELOW, CURRENTLY BEING IN SET IN prod_otherinfo.php

function addRecProdsField() {
	++fieldDivs2;

	var moreProdsDiv = document.getElementById("morerecprods");
	
	var newDiv = document.createElement("div");
	newDiv.setAttribute('id','recprod');
	
	var recprod_html = '<table width="100%" border="0" cellpadding="5" cellspacing="0" background="images/greypixel.gif">';
    recprod_html += '<tr>';
    recprod_html += '<td width="100" align="left" valign="top"><span class="fieldLabels">Category</span></td>';
    recprod_html += '<td width="125" align="left" valign="top">';
	recprod_html += '<select name="selcat_' + fieldDivs2 + '" class="catMenu" onchange="prodRequest(this.value, ' + fieldDivs2 + ')">';
	recprod_html += cats_options;
    recprod_html += '</select></td>';
	recprod_html += '<td width="25" align="left" valign="top">';
	recprod_html += '<div id="catworking_' + fieldDivs2 + '" style="display:none;"><img src="../images/working.gif" /></div>';
	recprod_html += '<img src="images/trans_px.gif" width="1" height="1" />';
	recprod_html += '</td>';
    recprod_html += '<td width="65" align="left" valign="top"><span class="fieldLabels">Product</span></td>';
    recprod_html += '<td align="left" valign="top"><div id="selprod_' + fieldDivs2 + '">';
	recprod_html += '<select name="prodmenu_' + fieldDivs2 + '" disabled="disabled" id="prodmenu_' + fieldDivs2 + '" class="productMenu">';
    recprod_html += '<option value="0">Select Category First</option>';
    recprod_html += '</select></div></td></tr></table>';
	
	newDiv.innerHTML = recprod_html;
	  
	moreProdsDiv.parentNode.insertBefore(newDiv, moreProdsDiv);
	
	document.getElementById("another_insert2").checked = false;

}

function prodRequest(url, index) {
	
	if(url == 0) return;
	
	var http_request = GetXmlHttpObject();

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() { updateProducts(http_request, index); };
	http_request.open('GET', url, true);
	http_request.send(null);

}

function updateProducts(http_request, index) {

	if(getElemById( 'catworking_' + index ).style.display == 'none') {
		getElemById( 'catworking_' + index ).style.display = '';
		
		var temp_html = '<select name="prodmenu_' + index + '" disabled="disabled" class="productMenu" id="prodmenu_' + index + '">';
		temp_html += '<option value="0">Products Loading...</option></select>';
		
		var tempDiv = document.createElement("div");
		tempDiv.setAttribute('id', 'selprod_' + index);
		tempDiv.innerHTML = temp_html;
		
		var tempOldDiv = document.getElementById("selprod_" + index);
		tempOldDiv.parentNode.replaceChild(tempDiv, tempOldDiv); 
	}
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			getElemById( 'catworking_' + index ).style.display = 'none';
			
			var xmldoc = http_request.responseXML;
			var itemElements = xmldoc.getElementsByTagName('item');

			var selprod_html = '<select name="prodmenu_' + fieldDivs2 + '" id="prodmenu_' + fieldDivs2 + '" class="productMenu">';
			selprod_html += '<option value="0">Select Product</option>';
			for (var i = 0; i < itemElements.length; i++) {
				selprod_html += '<option value="' + itemElements[i].childNodes[0].firstChild.data + '">';
				selprod_html += itemElements[i].childNodes[1].firstChild.data + '</option>';
			}
			
			selprod_html += '</select>';
			
			var newDiv = document.createElement("div");
			newDiv.setAttribute('id','selprod_' + index);
			newDiv.innerHTML = selprod_html;
			
			var oldDiv = document.getElementById("selprod_" + index);
			oldDiv.parentNode.replaceChild(newDiv, oldDiv);
			
		} else {
			getElemById( 'catworking_' + index ).style.display = 'none';
			alert('There was a problem with the request.');
		}
	}

}

// Globally used functions
function GetXmlHttpObject()
{
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	return http_request; // will be false if unable to create
}
