function mySubmit(buttonID) {
document.getElementById('doaction').value=buttonID;

var forms=document.getElementsByTagName('form');
for (i=0;i<forms.length;i++)
{
	if (!forms[i].action) continue;
//	if (document.forms[i].action.search('action=')==-1)
	{
		var addchar='&';
		var a=forms[i].action.split("#");
		if (forms[i].action.indexOf('?')==-1) addchar='?';
//		document.forms[i].action=document.forms[i].action+addchar+'action='+buttonID;

		forms[i].action=a[0]+addchar+'action='+buttonID;
		if (a[1]) forms[i].action=forms[i].action+"#"+a[1];
	}
	
//	alert('mySubmit form.action='+document.forms[i].action);
}

//alert('mySubmit form.action='+document.getElementById('action').value);
}

var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var ie4 = document.all;

function getNumcheckedprefix(prefix) {
	return getNumcheckedprefixForm(prefix,0);
}

function getNumcheckedprefixForm(prefix, formn) {
var i=0;
var numchecked=0;
var form=document.forms[formn];
	for (i=0; (i<form.elements.length); i++) {
//alert(document.forms[0].elements[i].name.substr(0,prefix.length));
//return 0;
		if ( (form.elements[i].type=='checkbox') && 
			 (form.elements[i].name.substr(0,prefix.length) == prefix)) 
		{
//			alert("l");
			if (form.elements[i].checked) { numchecked++; }
		}
	}

	return numchecked;
}

function getNumcheckedForm(forms) {
	return getNumcheckedprefixForm('packids',formn);
}

function getNumchecked() {
	return getNumcheckedprefix('packids');
}

function getRadioChecked(radiobuttons)
{
}

function getCheckedPackIDs(formname)
{
	var i=0;
	var a=new Array();
	var n=0;

	for (i=0; (i<document.forms[formname].elements.length); i++) {
		if ( (document.forms[formname].elements[i].type=='checkbox') && 
			 (document.forms[formname].elements[i].name.substr(0,7) == 'packids') ) {
			if (document.forms[formname].elements[i].checked) 
			{ 
				r=document.forms[formname].elements[i].name.substring(8); 
				r=r.substring(0,r.length-1);
				a[n]=r;
				n++;
			}
		}
	}

	return a;
}


function setModify(name, desc, langid1, langid2, category, quickdownload, privatepack) {
    // code for EDIT tab
	document.forms['mypackform'].packname.value=name;
	document.forms['mypackform'].packdescrip.value=desc;

	document.forms['mypackform'].langid3.value=langid1;
	document.forms['mypackform'].langid4.value=langid2;
	
	document.forms['mypackform'].categ2.value=category;
	
	document.forms['mypackform'].quickdownload2.checked=quickdownload;
	
	document.forms['mypackform'].makeprivate3.checked=privatepack;

	for (i=0; i<document.forms['mypackform'].langid3.options.length; i++) {
		if (document.forms['mypackform'].langid3.options[i].value==langid1) {
			document.forms['mypackform'].langid3.selectedIndex=i;
			break;
		}
	}

	for (i=0; i<document.forms['mypackform'].langid4.options.length; i++) {
		if (document.forms['mypackform'].langid4.options[i].value==langid2) {
			document.forms['mypackform'].langid4.selectedIndex=i;
			break;
		}
	}	

	for (i=0; i<document.forms['mypackform'].categ2.options.length; i++) {
		if (document.forms['mypackform'].categ2.options[i].value==category) {
			document.forms['mypackform'].categ2.selectedIndex=i;
			break;
		}
	}
	//Balazs: for NULL, select the first one
	if (i==document.forms['mypackform'].categ2.options.length) {
		document.forms['mypackform'].categ2.selectedIndex=0;
	}

// code for COPY tab
	document.forms['mypackform'].copypackname.value=name;
	document.forms['mypackform'].copypackdescrip.value=desc;

	document.forms['mypackform'].copylangid3.value=langid1;
	document.forms['mypackform'].copylangid4.value=langid2;
	
	document.forms['mypackform'].copycateg2.value=category;
	
	document.forms['mypackform'].copyquickdownload2.checked=quickdownload;
	
	document.forms['mypackform'].copymakeprivate3.checked=privatepack;

	for (i=0; i<document.forms['mypackform'].copylangid3.options.length; i++) {
		if (document.forms['mypackform'].copylangid3.options[i].value==langid1) {
			document.forms['mypackform'].copylangid3.selectedIndex=i;
			break;
		}
	}

	for (i=0; i<document.forms['mypackform'].copylangid4.options.length; i++) {
		if (document.forms['mypackform'].copylangid4.options[i].value==langid2) {
			document.forms['mypackform'].copylangid4.selectedIndex=i;
			break;
		}
	}	

	for (i=0; i<document.forms['mypackform'].copycateg2.options.length; i++) {
		if (document.forms['mypackform'].copycateg2.options[i].value==category) {
			document.forms['mypackform'].copycateg2.selectedIndex=i;
			break;
		}
	}
	//Balazs: for NULL, select the first one
	if (i==document.forms['mypackform'].copycateg2.options.length) {
		document.forms['mypackform'].copycateg2.selectedIndex=0;
	}

}

function verifyForm() {
	verifyFormForm(0);
}

function verifyFormForm(formname) {

var action=document.getElementById('doaction').value;
var form=document.forms[formname];

/*alert(formname);
alert(action);*/

	if (action=='trans') {
		if (form.elements['langid2'].value=="NULL" && 
			form.elements['trans2'].value!='')
		{
			alert("You must choose the language of the translation!");
			return false;
		}
	}
	else if (action=='download' || action=='downloada' || action=='downloadq') {
		if (getNumcheckedprefixForm('packids',formname)==0) {
			alert('Please select at least one pack to share!');
			return false;
		}
		else
		{
			var pida=getCheckedPackIDs(formname);
			var wnum=Number(worddown_number);

			// not count, when download quiestions
			var cwn=0;
			if (action!='downloadq')
			{
				for (i=0;i<pida.length;i++)
				{
					var pid=pida[i];
	
					cwn=cwn+getAvailableWordNumInPack(pid);
	//				alert(getAvailableWordNumInPack(pid));
	/*				if (form.elements['packwordnum['+pid+']'])	{
						wnum=wnum+Number(form.elements['packwordnum['+pid+']'].value);
					}*/
				}

				wnum=wnum+cwn;
			}
			if (wnum>1000)
			{
//				alert(wnum);
				alert('Sorry! You can only download up to 1000 words per month. You have downloaded '+worddown_number+' words.');
				return false;
			}
			else
			{
				for (i=0;i<pida.length;i++)
				{
					var pid=pida[i];
	
					if (!isFullPack(pid))
					{
						if (confirm('One or more words in the selected packs do not have a definition, example sentence or translation. Are you sure you want to continue?'))
							break;
						else return false;
					}
				}

				if (cwn!=0)
				{
					if (!confirm('This download will reduce your allowance for this month by '+cwn+' words. Continue?')) {
						return false;
					}
				}
//				alert("klhb");
//				alert(wnum);
				form.elements['worddown_num'].value=wnum;
				worddown_number=wnum;
			}
		}
	}
	else if (action=='share') {
		if (getNumcheckedprefixForm('packids',formname)==0) {
			alert('Please select at least one pack to share!');
			return false;
		}
		var pida=getCheckedPackIDs(formname);
		for (i=0;i<pida.length;i++)
		{
			var pid=pida[i];
			if (form.elements['packshareid['+pid+']'])	{
				alert("You cannot share packs which you do not own. To take ownership, firstly create a copy of the pack.");
				return false;
			}
		}
	}
	else if (action=='shareemail') {
		if (form.shareuserlist.value.length == 0) {
			form.shareuserlist.focus();
			alert('Please list in the box below the email addresses of the people who you want to share your word pack(s) with.');
			return false;
		}
		else 
		{
			var arr=form.shareuserlist.value.split("\n");
			var strs=new Array(); var j=0;
			for (i=0;i<arr.length;i++)
			{
				var arr2=arr[i].split(",");
				for (k=0;k<arr2.length;k++)
				{
					strs[j]=arr2[k];
					j++;
				}
			}
			for (i=0;i<strs.length;i++)
			{
				var emptyline=true;
				for (i2=0;i2<strs[i].length;i2++) if (strs[i][i2]!=' ') {emptyline=false; break;}
				
				if (emptyline) continue;
				
				var lb=strs[i].indexOf("(");
				var rb=strs[i].indexOf(")");
				
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				var emailok=false;
				var email;
				
				if (rb<lb || lb==-1 || rb==-1) 
				{
//					if (form.saveunknown.checked)
					{
//						alert("If you wish to save the contact details to My Students and Groups, all the emails must be followed by a name (in brackets)");
						alert("All the names must be followed by an email in brackets, like this: Bob (bob@address.com)");
						return false;
					}

					email=strs[i];
				}
				else
				{
/*					lb--;
					while (strs[i].charAt(lb)==' ') lb--;
					lb++;*/
			
					//email=strs[i].substr(0,lb);
					email=strs[i].substr(lb+1,rb-lb-1);
				}
				
				emailok=filter.test(email);
				
				if (!emailok)
				{
					alert('The following email address appears to be invalid: '+email); 
					return false;
				}
 			}

		}
		
	} else if (action=='create') {
		if (form.newpackname.value.length == 0) {
			form.newpackname.focus();
			alert('Please specify the name of the pack to create!');
			return false;
		} else if (form.newpackdescrip.value.length == 0) {
			form.newpackdescrip.focus();
			alert('Please specify the description of the pack to create!');
			return false;
		} else if (form.langid1.value == "NULL") {
			form.langid1.focus();
			alert('Please specify the primary language of the pack!');
			return false;
		} else if (form.categ1.value == "NULL") {
			form.langid1.focus();
			alert('Please specify the category of the pack!');
			return false;
		}
	} else if (action=='delete') {
		if (getNumcheckedprefixForm('packids',formname)==0) {
			alert('Please select at least one pack to delete!');
			return false;
		}
		
		return confirm('Are you sure you want to delete the selected pack(s) and all the words contained?');
	} else if (action=='edit') {
		var pida=getCheckedPackIDs(formname);
		var pid=pida[0];
		if (getNumcheckedprefixForm('packids',formname)!=1) {
			alert('Please select exactly one pack!');
			return false;
		}else if (form.elements['packshareid['+pid+']'])	{
			if (confirm("You cannot EDIT packs which you do not own. Would you like to create a copy of the pack?"))
			{
				mySubmit('editcopy');
//				form.elements['createacopy'].checked=true;
			}
			else
			{
				return false;
			}
		}else if (form.langid3.value == "NULL") {
			form.langid3.focus();
			alert('Please specify the primary language of the pack!');
			return false;
		} else if (form.categ2.value == "NULL") {
			form.langid1.focus();
			alert('Please specify the category of the pack!');
			return false;
		}
	} else if (action=='copy') {
		var pida=getCheckedPackIDs(formname);
		var pid=pida[0];
		if (getNumcheckedprefixForm('packids',formname)!=1) {
			alert('Please select exactly one pack!');
			return false;
		}else if (form.copylangid3.value == "NULL") {
			form.copylangid3.focus();
			alert('Please specify the language of the words (being learnt)!');
			return false;
		} else if (form.copycateg2.value == "NULL") {
			form.copylangid1.focus();
			alert('Please specify a category for the pack!');
			return false;
		}
	} else if (action=='addtogroup2') {
		if (form.mygroups.selectedIndex==-1) {
			alert('Please select at least one group'); return false;
		}
	} else if (action=='addtogroup') {
		if (form.allstudents.selectedIndex==-1) {
			alert('Please select at least one student'); 
			return false;
		}
	} else if (action=='addwords') {
		if (form.elements['NewWords[1][word]'].value=="")
		{
			alert('Please add at least one word');
			return false;
		}
		if (form.elements['NewWords[1][package]'].value=="null")
		{
			alert('Please choose a pack!');
			return false;
		}
	} else if (action=='addtopack') {
		var check=false;
		if (!form.radiosel) check=true;
		else if (form.radiosel[1].checked) check=true;
		
		if (check)
		{
			if (form.newpackname.value.length == 0) {
				form.newpackname.focus();
				alert('Please specify the name of the pack to create!');
				return false;
			} else if (form.newpackdescrip.value.length == 0) {
				form.newpackdescrip.focus();
				alert('Please specify the description of the pack to create!');
				return false;
			} else if (form.langid1.value == "NULL") {
				form.langid1.focus();
				alert('Please specify the primary language of the pack!');
				return false;
			} else if (form.category.value == "NULL") {
				form.langid1.focus();
				alert('Please specify the category of the pack!');
				return false;
			}
		}
	} else if (action=='update') {
//		alert(document.forms['addform1'].elements['words']);
		for (x in document.forms['addform1'])
		{
			if (x.indexOf("[example]")==-1) continue;
			var z=document.forms['addform1'].elements[x];
			var y=z.value;

			if (y!='')
			{
				var pn=0;
				for (i=0;i<y.length;i++)
				{
					if (y.charAt(i)=='%') pn=pn+1;
				}
				if (pn<2) 
				{
					alert("Check! In one or more example sentences, the word(s) being learned are not marked up with percentage signs %like% %this%!");
					return false;
				}
			}
		}
		return true;
	} else if (action=='showwspacks')
	{
		if (getNumcheckedprefixForm('packids',formname)==0 ) {
			alert('Please select at least one pack to share!');
			return false;
		}
	} else if (action=='buypack2') {

		if (parseInt(credit)<parseInt(buycredit))
			{
				if (confirm("You do not have enough WordShop Credits on your account. Would you like to get some now?"))
				{
					document.location='/wordshop.php?cmd=buycredits&action=ws_membership';
					return false;
				}
				else return false;
			}
			else
			{
				if (confirm('If you are sure that you want to add the selected packs to your account, press OK. '+buycredit+' WordShop Credits will be deducted from your account.'))
					return true;
				else return false;
			}
	} else if (action=='ws_membership' || action=='ws_checkout1')
	{
		var radio_choice = false;
		var radiobuttons=document.forms['ws_creditsform'].radiobutton;

		for (counter = 0; counter < radiobuttons.length; counter++)
		{
			if (radiobuttons[counter].checked)
			{
				radio_choice = true; 
				var now = new Date();
				fixDate(now);
				now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
				setCookie('ws_credit',radiobuttons[counter].value,now);
			}
		}

		if (action=='ws_checkout1')
		{
			if (getCookie('ws_product')==null && getCookie('ws_credit')==null)
			{
				alert('Please choose your membership level!');
				return false;
			}
		}


/*		if (radio_choice==false)
		{
			alert('You have to select how many creadits want do you buy');
			return false;
		}*/
	} else if (action=='ws_buycredit' || action=='ws_buycredit2' || action=='ws_checkout2')
	{
		var radio_choice = false;
		
		/*var radiobuttons=document.forms['ws_membershipform'].product_id;

		for (counter = 0; counter < radiobuttons.length; counter++)
		{
			if (radiobuttons[counter].checked)
			{
				radio_choice = true; 
				var now = new Date();
				fixDate(now);
				now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
				setCookie('ws_product',radiobuttons[counter].value,now);
			}
		}*/

		if (document.forms['ws_membershipform'])
		{
			var prodid=document.forms['ws_membershipform'].product_id;
			if (prodid)
			{
				var now = new Date();
				fixDate(now);
				now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
				setCookie('ws_product',prodid.value,now);
			}
		}
		
		if (action=='ws_checkout2' || action=='ws_buycredit2')
		{
			if (getCookie('ws_product')==null && getCookie('ws_credit')==null)
			{
				alert('Please choose your membership level.');
				return false;
			}
		}
		
		

/*		if (radio_choice==false)
		{
			alert('Please select a Premium Membership level before proceeding.');
			return false;
		}*/
	} else if (action=='ws_checkout3')
	{
		var menu_choice = false;
//		var selected_credits=document.getElementById('selected_credits');
		var selected_credits=document.forms['ws_creditsform'].selected_credits;
		
//		alert(selected_credits.value);

		if (selected_credits.value != '')
		{
			menu_choice = true; 
			var now = new Date();
			fixDate(now);
			now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
			setCookie('ws_credit',selected_credits.value,now);
		}

		
		if (action=='ws_checkout3')
		{
			if (getCookie('ws_product')==null && getCookie('ws_credit')==null)
			{
				alert('Please make your selection before proceeding.');
				return false;
			}
		}
		
/*		if (menu_choice==false)
		{
			alert('Please make your selection before proceeding.');
			return false;
		}*/
		
	} else if (action=='ws_alterpay2')
	{
		if (form.name.value.length == 0) {
				form.name.focus();
				alert('Please specify your name!');
				return false;
			} else if (form.address.value.length == 0) {
				form.address.focus();
				alert('Please specify your address!');
				return false;
			} else if (form.zipcode.value.length == 0) {
				form.zipcode.focus();
				alert('Please specify yout ZIP code / Postcode!');
				return false;
			}
	}
	else if (action=='upload')
	{
		var gamename=null;

		if (document.getElementById('gamename'))
			gamename=document.getElementById('gamename').value;

		var colsn=new Array();
		var colnums=new Array();
		var error=false;
		for (i=1;i<=4;i++)
		{
			el=document.getElementById("column"+i);
			if (parseInt(el.value) != el.value) continue;

			if (!colnums[el.value]) colnums[el.value]=i;
			else
			{
				alert('Column '+colnums[el.value]+' and Column '+i+
					' cannot have the same value. Please change one of the columns and try again.');
				return false;
			}

			if (!colsn[el.value]) colsn[el.value]=0;
			colsn[el.value]++;
			if (colsn[el.value]>1) 
			{
				error=el.value; 
				alert('Sorry. You can only upload one '+el.options[el.selectedIndex].text+' per line.');
				return false;
			}
		}
		
		if (gamename=="matchmaker")
		{
			if (!colsn[1] && !colsn[2] && !colsn[3])
			{
				alert("To create a Matchmaker puzzle, your chosen words must include example sentences, "+
					  "definitions or translations. Please correct this and try again.");
				return false;
			}
		}
	}
	else if (action=='printcreatepack')
	{
		if (document.getElementById('package_name').value.length == 0) {
				alert('Please specify the name of the pack to create!');
				return false;
			} else if (document.getElementById('package_description').value.length == 0) {
//				form.newpackdescrip.focus();
				alert('Please specify the description of the pack to create!');
				return false;
			} else if (document.getElementById('package_langid1').value == "NULL") {
//				form.langid1.focus();
				alert('Please specify the primary language of the pack!');
				return false;
			} else if (document.getElementById('package_category').value == "NULL") {
//				form.langid1.focus();
				alert('Please specify the category of the pack!');
				return false;
			}
	}

	return true;
}
