// JavaScript Document
function AddPlan() {
	var currCountEl=document.getElementById('totalplans');
	var tbaleEl=document.getElementById('paymentsettings');
	var nextindex=Number(currCountEl.value)+1;
	var nextrow=Number(currCountEl.value)*8+1;
	// name
	var tblrow=tbaleEl.insertRow(nextrow);
	tblrow.setAttribute('align','left');
	tblrow.setAttribute('vAlign','top');
	var cell;
	cell = tblrow.insertCell(0);
	cell.innerHTML='<strong>Subscription Plan '+nextindex+' Name:</strong>';
	cell = tblrow.insertCell(1);
	cell.setAttribute('align','right');
	var theName = document.createElement('input');
	theName.type='text';
	theName.size='40';
	theName.id='subname'+nextindex;
	theName.name='subname'+nextindex;
	theName.validate='yes';
	theName.message='The Subscription plan '+nextindex+' name is empty';
	cell.appendChild(theName);
	// free
	nextrow++;
	tblrow=tbaleEl.insertRow(nextrow);
	tblrow.setAttribute('align','left');
	tblrow.setAttribute('vAlign','top');
	cell = tblrow.insertCell(0);
	cell.innerHTML='<strong>This Plan '+nextindex+' is FREE:</strong>';
	cell = tblrow.insertCell(1);
	cell.setAttribute('align','right');
	var strtext='<input value="Yes" name="freeplan'+nextindex+'" id="freeplan'+nextindex+'" type="radio" /> Yes';
	strtext+='<input checked value="No" name="freeplan'+nextindex+'" id="freeplan'+nextindex+'" type="radio" /> No';
	cell.innerHTML=strtext;
	// amount
	nextrow++;
	tblrow=tbaleEl.insertRow(nextrow);
	tblrow.setAttribute('align','left');
	tblrow.setAttribute('vAlign','top');
	cell;
	cell = tblrow.insertCell(0);
	cell.innerHTML='<strong>Subscription Plan '+nextindex+' Amount:</strong>';
	cell = tblrow.insertCell(1);
	cell.setAttribute('align','right');
	var theAmount = document.createElement('input');
	theAmount.type='text';
	theAmount.size='10';
	theAmount.id='subamount'+nextindex;
	theAmount.name='subamount'+nextindex;
	theAmount.validate='yes';
	theAmount.message='The Subscription plan '+nextindex+' amount is empty';
	theAmount.required='positivrealcheck';
	theAmount.onkeypress=function () { return CheckPositiveReal(); }
	cell.appendChild(theAmount);
	var theText=document.createTextNode(' USD');
	cell.appendChild(theText);
	// period
	nextrow++;
	tblrow=tbaleEl.insertRow(nextrow);
	tblrow.setAttribute('align','left');
	tblrow.setAttribute('vAlign','top');
	cell = tblrow.insertCell(0);
	cell.innerHTML='<strong>Subscription Plan '+nextindex+' Period:</strong>';
	cell = tblrow.insertCell(1);
	cell.setAttribute('align','right');
	var theAmount = document.createElement('input');
	theAmount.type='text';
	theAmount.size='10';
	theAmount.id='subperiod'+nextindex;
	theAmount.name='subperiod'+nextindex;
	theAmount.validate='yes';
	theAmount.message='The Subscription plan '+nextindex+' period is empty';
	theAmount.required='positivnumbercheck';
	theAmount.onkeypress=function () { return CheckPositiveInteger(); }
	cell.appendChild(theAmount);
	var theText=document.createTextNode(' months');
	cell.appendChild(theText);
	// additional
	nextrow++;
	tblrow=tbaleEl.insertRow(nextrow);
	tblrow.setAttribute('align','left');
	tblrow.setAttribute('vAlign','top');
	cell = tblrow.insertCell(0);
	cell.innerHTML='<strong>This Plan '+nextindex+' allow additional info:</strong>';
	cell = tblrow.insertCell(1);
	cell.setAttribute('align','right');
	var strtext='<input value="Yes" name="additionalinfo'+nextindex+'" id="additionalinfo'+nextindex+'" type="radio" /> Yes';
	strtext+='<input checked value="No" name="additionalinfo'+nextindex+'" id="additionalinfo'+nextindex+'" type="radio" /> No';
	cell.innerHTML=strtext;
	// images
	nextrow++;
	tblrow=tbaleEl.insertRow(nextrow);
	tblrow.setAttribute('align','left');
	tblrow.setAttribute('vAlign','top');
	cell = tblrow.insertCell(0);
	cell.innerHTML='<strong>This Plan '+nextindex+' allow Images:</strong>';
	cell = tblrow.insertCell(1);
	cell.setAttribute('align','right');
	var strtext='<input value="Yes" name="pimages'+nextindex+'" id="pimages'+nextindex+'" type="radio" /> Yes';
	strtext+='<input checked value="No" name="pimages'+nextindex+'" id="pimages'+nextindex+'" type="radio" /> No';
	cell.innerHTML=strtext;
	// menus
	nextrow++;
	tblrow=tbaleEl.insertRow(nextrow);
	tblrow.setAttribute('align','left');
	tblrow.setAttribute('vAlign','top');
	cell = tblrow.insertCell(0);
	cell.innerHTML='<strong>This Plan '+nextindex+' allow Menu management:</strong>';
	cell = tblrow.insertCell(1);
	cell.setAttribute('align','right');
	var strtext='<input value="Yes" name="menum'+nextindex+'" id="menum'+nextindex+'" type="radio" /> Yes';
	strtext+='<input checked value="No" name="menum'+nextindex+'" id="menum'+nextindex+'" type="radio" /> No';
	cell.innerHTML=strtext;
	// description
	nextrow++;
	tblrow=tbaleEl.insertRow(nextrow);
	tblrow.setAttribute('align','left');
	tblrow.setAttribute('vAlign','top');
	cell = tblrow.insertCell(0);
	cell.innerHTML='<strong>Subscription Plan '+nextindex+' Description:</strong>';
	cell = tblrow.insertCell(1);
	cell.innerHTML='<textarea name="subdesc'+nextindex+'" id="subdesc'+nextindex+'" cols="40" rows="10"></textarea>';
	currCountEl.value=nextindex;
}

function ShowSearchRestaurants() {
	var ajaxInst=new DivesAjax();
	ajaxInst.url=currtemplpath+"ajax/restaurantsrch.php";
	ajaxInst.urlparameter="";
	ajaxInst.ShowDivElementName='restaurantsearch';
	ajaxInst.runAJAXInstance();
}

function GetSearchRestaurants(){
	var spanID=document.getElementById('restaurantsearch');
	var ajaxInst=new DivesAjax();
	ajaxInst.url=currtemplpath+"ajax/searchshow.php";
	ajaxInst.urlparameter='keyword='+document.getElementById('keyword').value;
	ajaxInst.urlparameter+='&stateco='+document.getElementById('stateco').value;
	ajaxInst.urlparameter+='&state='+document.getElementById('state').value;
	ajaxInst.urlparameter+='&cityco='+document.getElementById('cityco').value;
	ajaxInst.urlparameter+='&city='+document.getElementById('city').value;
	ajaxInst.method="POST";
	ajaxInst.ShowDivElementName='restaurantsearch';
	//ajaxInst.onCompletion=function() { InsertNewUser(ajaxInst); }
	spanID.innerHTML='<img src="'+lightboximagepath+'ajaxloading.gif">';
	ajaxInst.runAJAXInstance();
}

function CloseSearchRestaurants(){
	document.getElementById('restaurantsearch').innerHTML='';
}

function GetSelectedRestaurants(id){
	var selectedelname='rid'+id;
	var formname=document.getElementById('formmenuupdate');
	for (var i=0;i<formname.elements.length;i++) {
		var currentel=formname.elements[i];
		if (currentel.name.indexOf("rid")==0) {
			if (selectedelname==currentel.name) currentel.checked=true;
			else currentel.checked=false;
		}
	}
	var checkEL=document.getElementById(selectedelname);
	var theBrokenData=checkEL.value.split(":");
	document.getElementById('restaurantname').value=theBrokenData[1];
	document.getElementById('restaurantid').value=theBrokenData[0];
}

function AddMenuLine() {
	var currCountEl=document.getElementById('countitems');
	var tbaleEl=document.getElementById('menulinestable');
	var nextindex=Number(currCountEl.value)+1;
	var nextrow=Number(currCountEl.value)+1;
	var tblrow=tbaleEl.insertRow(nextrow);
	tblrow.setAttribute('align','center');
	tblrow.setAttribute('vAlign','top');
	var cell;
	cell = tblrow.insertCell(0);
	cell.id='tdunderline';
	cell.innerHTML='<input validate="yes" message="The line '+nextindex+' title is empty!" name="title'+nextindex+'" id="title'+nextindex+'" type="text" size="25" />';
	cell = tblrow.insertCell(1);
	cell.id='tdunderline';
	cell.innerHTML='<textarea name="description'+nextindex+'" id="description'+nextindex+'" cols="45" rows="5"></textarea>';
	cell = tblrow.insertCell(2);
	cell.id='tdunderline';
	cell.innerHTML='<input name="prevfilename'+nextindex+'" id="prevfilename'+nextindex+'" type="file" />';
	cell = tblrow.insertCell(3);
	cell.id='tdunderline';
	cell.innerHTML='<input name="price'+nextindex+'" id="price'+nextindex+'" type="text" size="10" />';
	cell = tblrow.insertCell(4);
	cell.id='tdunderline';
	var strin='<select name="status'+nextindex+'" id="status'+nextindex+'">';
	strin+='<option value="Active" selected="selected">Active</option>';
	strin+='<option value="Suspend">Suspend</option>';
	strin+='</select>';
	cell.innerHTML=strin;
	currCountEl.value=nextindex;
}

function AddUser() {
	var ajaxInst=new DivesAjax();
	ajaxInst.url=currtemplpath+"ajax/adduser.php";
	ajaxInst.urlparameter="";
	ajaxInst.ShowDivElementName='addnewuser';
	ajaxInst.runAJAXInstance();
}

function DoUser(currpath) {
	var spanID=document.getElementById('addnewuser');
	var username=document.getElementById('username');
	var pass=document.getElementById('password');
	var cpass=document.getElementById('cpass');
	var email=document.getElementById('email');
	if (username.value=="") {
		alert("The username is empty");
		username.focus();
		return false;
	}
	if (pass.value=="") {
		alert("The password is empty");
		pass.focus();
		return false;
	} else {
		if (pass.value!=cpass.value) {
			alert ("The password is not confirm!");
			cpass.focus();
			return false;
		}
	}
	if (email.value=="") {
		alert("The email is empty");
		email.focus();
		return false;
	} else {
		var valerror=EmailCheck (email.value);
		if (valerror) {
			alert (valerror);
			email.focus();
			return false;
		}
	}
	var ajaxInst=new DivesAjax();
	ajaxInst.url=currtemplpath+"ajax/adduserdo.php";
	ajaxInst.urlparameter='username='+username.value;
	ajaxInst.urlparameter+='&password='+pass.value;
	ajaxInst.urlparameter+='&email='+email.value;
	ajaxInst.method="POST";
	ajaxInst.ShowDivElementName='addnewuser';
	ajaxInst.onCompletion=function() { InsertNewUser(ajaxInst); }
	spanID.innerHTML='<img src="'+lightboximagepath+'ajaxloading.gif">';
	ajaxInst.runAJAXInstance();
}

function InsertNewUser(p) {
	var obj = document.getElementById('userid');
	var spanID=document.getElementById('addnewuser');
	if (p.http_request.readyState == 4) {
		var results=p.http_request.responseText;
		if (results.indexOf("Error")==0) {
			var errstring=results.replace("Error:","");
			var ajaxInst=new DivesAjax();
			ajaxInst.url=currtemplpath+"ajax/adduser.php";
			ajaxInst.urlparameter="err="+errstring;
			ajaxInst.ShowDivElementName='addnewuser';
			ajaxInst.runAJAXInstance();
		} else {
			for(var i=obj.options.length-1;i>=1;i--){
				obj.remove(i);
			}
			eval(results);
			var spanText='<span style="color:#093;">';
			spanText+='The User successfull add to database';
			spanText+='</span><br>';
			spanID.innerHTML=spanText;
			var theLink = document.createElement('a');
			var theText=document.createTextNode('Add New User');
			theLink.setAttribute('href','#');
			theLink.onclick=function () { AddUser(currtemplpath);}	
			theLink.appendChild(theText);
			spanID.appendChild(theLink);
			//spanText+='<a href="#" onclick="AddUser("'+currtemplpath+'");return false;">Add New User</a>';
		}
	}
}
