// JavaScript Document

function deleterow(table,id) {

if (confirm('Are you sure you want to delete a row form table: ' + table + '?')) {
	
	// Set up the request
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('POST', 'php/admin_update.php', true);
	
	// The callback function
	xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var response_stat = xmlhttp.responseXML.getElementsByTagName('deleted')[0].firstChild.data;
					if (response_stat == 1) {
						window.location.reload();
					} else {
						alert('Failed to delete row: ' + xmlhttp.responseXML.getElementsByTagName('error')[0].firstChild.data + '.');
					}
				}
			}
		}
		
		// Send the POST request
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send('delete_row=' + id +'& table=' + table);
	}
}


function moverowup(table,id) {

// Set up the request
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', 'php/admin_update.php', true);

// The callback function
xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			var response_stat = xmlhttp.responseXML.getElementsByTagName('move')[0].firstChild.data;
				if (response_stat == 1) {
					window.location.reload();
				} else {
					alert('Failed to move row: ' + xmlhttp.responseXML.getElementsByTagName('error')[0].firstChild.data + '.');
				}
			}
		}
	}
	
	// Send the POST request
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('row_up=' + id +'& table=' + table);
}

function moverowdown(table,id) {

// Set up the request
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', 'php/admin_update.php', true);

// The callback function
xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			var response_stat = xmlhttp.responseXML.getElementsByTagName('move')[0].firstChild.data;
				if (response_stat == 1) {
					window.location.reload();
					// perhaps call update to all of table changing each value
				} else {
					alert('Failed to move row: ' + xmlhttp.responseXML.getElementsByTagName('error')[0].firstChild.data + '.');
				}
			}
		}
	}
	
	// Send the POST request
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('row_down=' + id +'& table=' + table);
}

function update_id(table,row,id){
	var up_value=document.getElementById(id).value;
	// Set up the request
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('POST', 'php/admin_update.php', true);
	
	// The callback function
	xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var response_stat = xmlhttp.responseXML.getElementsByTagName('status')[0].firstChild.data;
					if (response_stat == 1) {
						//alert('update success');
						//window.location.reload();
						document.getElementById('echo_update').removeAttribute("class");
						var classToAppend = "updateMessage";
						document.getElementById('echo_update').setAttribute("class", classToAppend);
						new Effect.Highlight(id, { startcolor: '#c2ffc2', endcolor: '#FDFDFD' }); return false;									
					} else {
						alert('Failed to update! This is why: \n\n' + xmlhttp.responseXML.getElementsByTagName('error')[0].firstChild.data + '.');
						new Effect.Highlight(id, { startcolor: '#ff3e00', endcolor: '#FDFDFD' }); return false;
					}
				}
			}
		}
		
	// Send the POST request
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('row_update=' + row +'& ' + 'table=' + table + '& ' + 'id=' + id +'& ' + 'value=' + up_value);
	
	}

function newrow(table,id) { // where id is max id
// Set up the request
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', 'php/admin_update.php', true);

// The callback function
xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			var response_stat = xmlhttp.responseXML.getElementsByTagName('add')[0].firstChild.data;
				if (response_stat == 1) {
					window.location.reload();
				} else {
					alert('Failed to add row: ' + xmlhttp.responseXML.getElementsByTagName('error')[0].firstChild.data + '.');
				}
			}
		}
	}
	
	// Send the POST request
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('row_add=' + id +'& table=' + table);
}



function update_priceset(set,row,id,type){
	var up_value=document.getElementById(id).value;
	// Set up the request
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('POST', 'php/admin_update.php', true);
	
	// The callback function
	xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var response_stat = xmlhttp.responseXML.getElementsByTagName('status')[0].firstChild.data;
					if (response_stat == 1) {
						//alert('update success');
						//window.location.reload();
						//alert('Debug:: \n\n' + xmlhttp.responseXML.getElementsByTagName('debug')[0].firstChild.data + '.');		
						document.getElementById('echo_update').removeAttribute("class");
						var classToAppend = "updateMessage";
						document.getElementById('echo_update').setAttribute("class", classToAppend);
						new Effect.Highlight(id, { startcolor: '#c2ffc2', endcolor: '#FDFDFD' }); return false;
													
					} else {
						alert('Failed to update! This is why: \n\n' + xmlhttp.responseXML.getElementsByTagName('error')[0].firstChild.data + '.');
						new Effect.Highlight(id, { startcolor: '#ff3e00', endcolor: '#FDFDFD' }); return false;
					}
				}
			}
		}
		
	// Send the POST request
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('row_setupdate=' + row +'& ' + 'set=' + set + '& ' + 'id=' + id +'& ' + 'value=' + up_value +'& ' + 'type=' + type);
	
	}

function deleteaddrow_priceset(set,row,del_add){
	// Set up the request
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('POST', 'php/admin_update.php', true);
	
	// The callback function
	xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var response_stat = xmlhttp.responseXML.getElementsByTagName('status')[0].firstChild.data;
					if (response_stat == 1) {
						//alert('update success');
						//alert('Debug:: \n\n' + xmlhttp.responseXML.getElementsByTagName('debug')[0].firstChild.data + '.');	
			 			window.location.reload();	
					} else {
						alert('Failed to update! This is why: \n\n' + xmlhttp.responseXML.getElementsByTagName('error')[0].firstChild.data + '.');
					}
				}
			}
		}
		
	// Send the POST request
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('row_setoptiondelete=' + row +'& ' + 'set=' + set +'& del_add=' + del_add);
	
	}

function addnew_priceset(){
	// Set up the request
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('POST', 'php/admin_update.php', true);
	// The callback function
	xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var response_stat = xmlhttp.responseXML.getElementsByTagName('status')[0].firstChild.data;
					if (response_stat == 1) {
						//alert('Debug:: \n\n' + xmlhttp.responseXML.getElementsByTagName('debug')[0].firstChild.data + '.');	
			 			window.location.reload();	
					} else {
						alert('Failed to update! This is why: \n\n' + xmlhttp.responseXML.getElementsByTagName('error')[0].firstChild.data + '.');
					}
				}
			}
		}
		
	// Send the POST request
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('new_priceset=1');
	
	}

function edit_priceset(set,id,edit_del){
	var textvalue=document.getElementById(id).value;
	// Set up the request
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('POST', 'php/admin_update.php', true);
	// The callback function
	xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var response_stat = xmlhttp.responseXML.getElementsByTagName('status')[0].firstChild.data;
					if (response_stat == 1) {
						//alert('Debug:: \n\n' + xmlhttp.responseXML.getElementsByTagName('debug')[0].firstChild.data + '.');	
						window.location.reload();	
					} else {
						alert('Failed to update! This is why: \n\n' + xmlhttp.responseXML.getElementsByTagName('error')[0].firstChild.data + '.');
					}
				}
			}
		}
		
	// Send the POST request
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('set=' + set +'& edit_del_set=' + edit_del +'& value=' + textvalue);
	
	}
///////////

function update_secoptionset(set,row,id,type){
	var up_value=document.getElementById(id).value;
	// Set up the request
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('POST', 'php/admin_update.php', true);
	
	// The callback function
	xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var response_stat = xmlhttp.responseXML.getElementsByTagName('status')[0].firstChild.data;
					if (response_stat == 1) {
						//alert('update success');
						//window.location.reload();
						//alert('Debug:: \n\n' + xmlhttp.responseXML.getElementsByTagName('debug')[0].firstChild.data + '.');		
						document.getElementById('echo_update').removeAttribute("class");
						var classToAppend = "updateMessage";
						document.getElementById('echo_update').setAttribute("class", classToAppend);
						new Effect.Highlight(id, { startcolor: '#c2ffc2', endcolor: '#FDFDFD' }); return false;
													
					} else {
						alert('Failed to update! This is why: \n\n' + xmlhttp.responseXML.getElementsByTagName('error')[0].firstChild.data + '.');
						new Effect.Highlight(id, { startcolor: '#ff3e00', endcolor: '#FDFDFD' }); return false;
					}
				}
			}
		}
		
	// Send the POST request
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('row_setupdate_sec=' + row +'& ' + 'set=' + set + '& ' + 'id=' + id +'& ' + 'value=' + up_value +'& ' + 'type=' + type);
	
	}

function deleteaddrow_secoptionset(set,row,del_add){
	// Set up the request
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('POST', 'php/admin_update.php', true);
	
	// The callback function
	xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var response_stat = xmlhttp.responseXML.getElementsByTagName('status')[0].firstChild.data;
					if (response_stat == 1) {
						//alert('update success');
						//alert('Debug:: \n\n' + xmlhttp.responseXML.getElementsByTagName('debug')[0].firstChild.data + '.');	
			 			window.location.reload();	
					} else {
						alert('Failed to update! This is why: \n\n' + xmlhttp.responseXML.getElementsByTagName('error')[0].firstChild.data + '.');
					}
				}
			}
		}
		
	// Send the POST request
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('row_setoptiondelete_sec=' + row +'& ' + 'set=' + set +'& del_add=' + del_add);
	
	}

function addnew_secoptionset(){
	// Set up the request
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('POST', 'php/admin_update.php', true);
	// The callback function
	xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var response_stat = xmlhttp.responseXML.getElementsByTagName('status')[0].firstChild.data;
					if (response_stat == 1) {
						//alert('Debug:: \n\n' + xmlhttp.responseXML.getElementsByTagName('debug')[0].firstChild.data + '.');	
			 			window.location.reload();	
					} else {
						alert('Failed to update! This is why: \n\n' + xmlhttp.responseXML.getElementsByTagName('error')[0].firstChild.data + '.');
					}
				}
			}
		}
		
	// Send the POST request
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('new_secoptionset=1');
	
	}

function edit_secoptionset(set,id,edit_del){
	var secvalue=document.getElementById(id).value;
	// Set up the request
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('POST', 'php/admin_update.php', true);
	// The callback function
	xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var response_stat = xmlhttp.responseXML.getElementsByTagName('status')[0].firstChild.data;
					if (response_stat == 1) {
						//alert('Debug:: \n\n' + xmlhttp.responseXML.getElementsByTagName('debug')[0].firstChild.data + '.');				
						window.location.reload();
						
					} else {
						alert('Failed to update! This is why: \n\n' + xmlhttp.responseXML.getElementsByTagName('error')[0].firstChild.data + '.');
					}
				}
			}
		}
		
	// Send the POST request
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('set=' + set +'& edit_del_secset=' + edit_del +'& value=' + secvalue);
	
	}



function ForceCaps(itemid){
	var y=document.getElementById(itemid).value;
	document.getElementById(itemid).value=y.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );	
}

function ForceCents(itemid){
	var y=document.getElementById(itemid).value;
	if(y.indexOf('.') == -1) {
	document.getElementById(itemid).value=y+'.00';	
	}
}

function clearUpdate(){
	document.getElementById('echo_update').removeAttribute("class");
	var classToAppend = "Hidden";
	document.getElementById('echo_update').setAttribute("class", classToAppend);
	
	}
	
	
function deactivate_option(id_deactivate){
		document.getElementById(id_deactivate).disabled = true;
		document.getElementById(id_deactivate).value = '';
	}

function activate_option(id_activate){
		document.getElementById(id_activate).disabled = false;
		document.getElementById(id_activate).value = '0.00';
	}	
	
	






	
////////// not in use below line	
	

function sendRequest(id_in) {
	new Ajax.Request("php/test.php", 
		{ 
		method: 'post', 
		postBody: 'name='+ $F(id_in) + '& id=' +id_in,
		onComplete: function(req) {$('show').innerHTML= req.responseText;}
		});
	}
	
	
	
	
	
	
// not in use but easy to create lists that update server and return easily...	



function createmovablelist(set_id, tag_type, handle_type,id_echo) {
  	Sortable.create(set_id, {tag:tag_type, handles:handle_type,
		onUpdate: function() {
        new Ajax.Request("php/test.php", {
            method: "post",
            parameters: { data: Sortable.serialize(set_id,{tag:tag_type}) },
        	onComplete: function(req) {$(id_echo).innerHTML= req.responseText;} // passes forward php echo from sever
		});
		}
	});    
  }
  // php code for server side of this ajax function
  // <?
// use to read data from post
//if(isset($_POST['data'])){
//	parse_str($_POST['data']);
//	print_r($List);
//	}
	
// notes on how to update the tables with new values

//for ($i = 0; $i < count($list_to_sort); $i++) {
    // SQL Query:
    // UPDATE `table` SET `order_column` = $i WHERE `id` = $list_to_sort[$i]
//}
//?>
