function submitform(thisForm,url)
{
  thisForm.action=url;
  thisForm.submit();
}

function checkEmail(fieldname)
{ 
  var str = fieldname.value; 
//  var reg=/^\w+[\w\.]@[\w\.]+\.\w+$/;
//if (!reg.test(str)) 
if (!str.match(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@([0-9a-z](-?[0-9a-z])*\.)+[a-z]{2,6}$/)) 
{ 
//fieldname.focus(); 
alert("Email is not right");
return false;
  }
 return true;
}

function checkPassword(fieldname)
{
if(fieldname.value.length < 4)
{
fieldname.focus(); 
alert("Password should be longer than 4");
return false;
}
return true;
}

function comparePassword(fieldname1,fieldname2)
{
if(fieldname1.value!= fieldname2.value)
{
fieldname2.focus(); 
alert("Confirm Password should be same as password");
return false;
}
return true;
}

function checkfilled(fieldname,label)
{
if(fieldname.value=="")
{
fieldname.focus(); 
alert(label+" field is empty!");
return false;
}
return true;
}

function checkMenuAlert(menu,label){
var myindex=menu.selectedIndex;
if (myindex==0) {
menu.focus();
alert("You must make a selection from the "+label+" menu.");
return false;
}
return true;
}

function checkMenu(menu){
var myindex=menu.selectedIndex;
if (myindex==0) {
return false;
}
return true;
}


/*

function radio_button_checker(thisform) {
myOption = -1;
for (i=0; i<thisform.postOp.length; i++) {
if (thisform.postOp[i].checked) {
myOption = i;
}
}
if (myOption == -1) {
alert("You must select a radio button");
return false;
}
return true;
}

*/

function validateForm(theForm)
{
if(!checkfilled(theForm.username,"Username"))
{
return false;
}
if(!checkPassword(theForm.password1))
{
return false;
}
if(!comparePassword(theForm.password1,theForm.password2)) 
{
return false;
} 

if(!checkfilled(theForm.firstname,"Firstname"))
{
return false;
}

if(!checkfilled(theForm.surname,"Surname"))
{
return false;
}

/*** if user pick on of birthdate,mon or year, they have to slelect all three ***/ 
if(checkMenu(theForm.birth_day)||checkMenu(theForm.birth_month)||checkMenu(theForm.birth_year))
{
if(!checkMenuAlert(theForm.birth_year,"\'birth year\'"))
return false;
if(!checkMenuAlert(theForm.birth_month,"\'birth month\'"))
return false;
if(!checkMenuAlert(theForm.birth_day,"\'birth day\'"))
return false;


} 

if(!checkfilled(theForm.country_id,"Country"))
{
return false;
} 
if(!checkfilled(theForm.city,"City"))
{
return false;
} 
if(!checkfilled(theForm.word,"Code"))
{
return false;
} 


if(!checkEmail(theForm.email1))
{
return false;
}
if(!radio_button_checker(theForm))
{
return false;
}
return true;

}
function refresh_captcha()
{
if(document.getElementById)
{
crsc = document.getElementById("captcha").src;
crscn = crsc.substring(crsc.lastIndexOf("/"), crsc.lastIndexOf("/")+5);
crscl = crsc.substring(crsc.length-4,crsc.length);
document.getElementById("captcha").src = crscn +Math.round(Math.random()*100000)+crscl; 
} else {
alert("Sorry, cannot autoreload captcha image\nSubmit the form and a new captcha will be loaded");
}
}
function wordcounter(field, notifydiv) {
var maxwd = 15;
var f = field.value;
var notify = document.getElementById(notifydiv);

var t = f.replace('\r\n', ' ');
t = t.replace('\n', ' ');
t = t.split(' ');
var aj = new Array();
var av = new Array();

for(tagcnt=0, x=0; tagcnt<t.length; tagcnt++) {
if(t[tagcnt].length>0) {
aj[x] = t[tagcnt];
x++;
}
if(x==maxwd) {
field.value=aj.join(" ");
break;
}
}
var disp = "(" + x + " of " + maxwd + " tag limit)";
notify.innerHTML=disp;
}
