// Form Validation JS
//
// Purpose: Checks form entries (newsletter, contact us pages etc.)
//
// Author: TheEWord (www.theeword.co.uk)
// Developer: Alan Mackin (alan at theeword.co.uk)

function check_linkout_all(limit,id,id2,id3,id4,swid) {

  ida=0;
  fail=0;
  for (ida=0;ida<limit;ida++) {
    j=ida;
    ret=check_linkout(j);
    if (ret==1) {fail=1;}
  }
  if (fail==0) {
  
if (swid) {
      switchPopup = window.open('/php/switch-popup.php?name='+swid,'mywin','width=500,height=500,toolbar=0,resizable=0');
    }    
    
    buyclik2(id,id2,id3,id4);
    outclick(id2,id3,id4);
    //now launch popup
  
    document.linkout.submit();
    }
  else {alert("Please check your details");}

}

function check_linkout(id){

ok=0;
errorcount=0;
var error = new Array (15);
var errormsg= new Array (15);

if (id==1) {tf=0; //name
    if (document.getElementById("fname").value=='') { error[errorcount]=id;tf=1;errorcount++;}
       
    a=document.getElementById("fname").value;
    a=a.toLowerCase();
    sworn=0;
    if (a.indexOf('fuck')>-1) {error[errorcount]=id;tf=1;errorcount++;sworn=1;}
    if (a.indexOf('shit')>-1) {error[errorcount]=id;tf=1;errorcount++;sworn=1;}
    if (a.indexOf('twat')>-1) {error[errorcount]=id;tf=1;errorcount++;sworn=1;}
    if (a.indexOf('bastard')>-1) {error[errorcount]=id;tf=1;errorcount++;sworn=1;}
    if (a.indexOf('cunt')>-1) {error[errorcount]=id;tf=1;errorcount++;sworn=1;}
    if (a.indexOf('piss')>-1) {error[errorcount]=id;tf=1;errorcount++;sworn=1;}
    if (a.indexOf('balls')>-1) {error[errorcount]=id;tf=1;errorcount++;sworn=1;}
    if (tf==0) {ok=id;}
}

if (id==2) { //email
if (document.getElementById("femail").value!='' && checkMail(document.getElementById("femail").value)) {ok=id;} else {
 error[errorcount]=id;errormsg[errorcount]="Please enter your email address";errorcount++;}
}


if (id==3) {tf=0; //telephone
  
  if (document.getElementById("ftelephone").value=='') {tf=1;error[errorcount]=id;errorcount++;}
  if (document.getElementById("ftelephone").value.substring(0,2)!='07' && document.getElementById("ftelephone").value.substring(0,2)!='01' && document.getElementById("ftelephone").value.substring(0,2)!='02') {tf=1;error[errorcount]=id;errorcount++;}
  if (document.getElementById("ftelephone").value.length<10 && document.getElementById("ftelephone").value.length>14) {tf=1;error[errorcount]=id;errorcount++;}
  
  if (tf==0) {ok=3;}
}

if (ok>0) {document.getElementById("ti"+ok).src="/images/tick.png"; }

doneerror=0;
//now show errors on page
for (i=0;i<errorcount;i++) {
if (error[i]){

if (error[i]>0) {document.getElementById("ti"+error[i]).src="/images/cross.png";
doneerror=1;}

}

}

if (id==1 && sworn==1) {
document.getElementById("lomessage").innerHTML="<div style=\"background-color:#FF0000;color:#FFF;padding:8px;font-weight:bold;\">Hey, don't swear! Enter your real name and you could win a free laptop.</div>";
}

return doneerror;

}


function check_reviews_all(limit) {
  ida=0;
  fail=0;
  for (ida=0;ida<limit;ida++) {
    j=ida;
    ret=check_reviews(j);
    if (ret==1) {fail=1;}
  }
  if (fail==0) { document.review.submit();}
  else {alert("Please check your information");}


}

function check_reviews(id){

ok=0;
errorcount=0;
var error = new Array (15);
var errormsg= new Array (15);

if (id==1) {
if (document.getElementById("firstname").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your contact name";errorcount++;}
else {ok=id;}
}

if (id==2) {
if (document.getElementById("surname").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your surname";errorcount++;}
else {ok=id;}
}

if (id==3) {
if (document.getElementById("housenumber").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your house number";errorcount++;}
else {ok=id;}
}

if (id==4) {
if (document.getElementById("address").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your address";errorcount++;}
else {ok=id;}
}

if (id==5) {
if (document.getElementById("city").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your city";errorcount++;}
else {ok=id;}
}

if (id==6) {
if (document.getElementById("county").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your county";errorcount++;}
else {ok=id;}
}

if (id==7) {
if (document.getElementById("postcode").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your postcode";errorcount++;}
else {ok=id;}
}

if (id==8) {
if ( document.getElementById("email").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your postcode";errorcount++;}
else {ok=id;}
}



if (ok>0) {document.getElementById("ti"+ok).src="/images/tick.png"; }


doneerror=0;
//now show errors on page
for (i=0;i<errorcount;i++) {
if (error[i]){
if (error[i]>0) {document.getElementById("ti"+error[i]).src="/images/cross.png";
doneerror=1;}
}
}

return doneerror;
}


function check_news_all(limit){
  ida=0;
  fail=0;
  for (ida=0;ida<limit;ida++) {
    j=ida;
    ret=check_news(j);
    if (ret==1) {fail=1;}
  }
  if (fail==0) { document.newsletter.submit();} 
  else {alert("Please check your information");}
}





function check_news(id){

ok=0;
errorcount=0;
var error = new Array (15);
var errormsg= new Array (15);

if (id==1) { //name
if (document.getElementById("firstname").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your contact name";errorcount++;}
else {ok=id;}
}

if (id==2) { 
if (document.getElementById("surname").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your surname";errorcount++;}
else {ok=id;}
}

if (id==3) { 
if (document.getElementById("housenumber").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your house number";errorcount++;}
else {ok=id;}
}

if (id==4) { 
if (document.getElementById("address").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your address";errorcount++;}
else {ok=id;}
}

if (id==5) { 
if (document.getElementById("city").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your city";errorcount++;}
else {ok=id;}
}

if (id==6) { 
if (document.getElementById("county").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your county";errorcount++;}
else {ok=id;}
}

if (id==7) { 
if (document.getElementById("postcode").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your postcode";errorcount++;}
else {ok=id;}
}

if (id==8) {
if ( !checkMail(document.getElementById("email").value)) { error[errorcount]=id;errormsg[errorcount]="Please enter your postcode";errorcount++;}
else {ok=id;}
}

if (ok>0) {document.getElementById("ti"+ok).src="/images/tick.png"; }


doneerror=0;
//now show errors on page
for (i=0;i<errorcount;i++) {
if (error[i]){

if (error[i]>0) {document.getElementById("ti"+error[i]).src="/images/cross.png";
doneerror=1;}

}

}

return doneerror;

}


function check_cont_all(limit){
  ida=0;
  fail=0;
  for (ida=0;ida<limit;ida++) {
    j=ida;
    ret=check_cont(j);
    if (ret==1) {fail=1;}
  }
  
  var datecheck = false;
  
  
		function checkAge(){
			var min_age = 18;
			var year = Number(document.getElementById('year').value);
			var month = Number(document.getElementById('month').value - 1);
			var day = Number(document.getElementById('dayofyear').value);
			var newyear = year + min_age;
			var theirDate = new Date(newyear, month, day);
    //  alert(theirDate);
      var today = new Date;
			if ( (today.getTime() - theirDate.getTime()) < 0) {
	//			alert('too young');
				return false;
			}	else {
		//		alert('old enough');
				return true;
			}
		}

if ((document.getElementById('age').checked) && ( checkAge() ) ){
//if (document.getElementById('age').checked){
  if((document.getElementById('dayofyear').value != 'DD') && (document.getElementById('month').value != 'MM') && (document.getElementById('year').value != 'YYYY'))
        {      
              if (fail==0){
              document.contact.submit();
              } else {
              alert("Please check your information");
                  }
         }else{
           alert("Please select your date of birth");
         }
 
  }else{
  alert("You must be over 18 to join");
    document.getElementById('bage').style.border="1px solid #ff0000";
  }
  
  
}





function check_cont(id){

ok=0;
errorcount=0;
var error = new Array (15);
var errormsg= new Array (15);

if (id==1) { //name
if (document.getElementById("firstname").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your contact name";errorcount++;}
else {ok=id;}
}

if (id==2) { 
if (document.getElementById("surname").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your surname";errorcount++;}
else {ok=id;}
}

if (id==3) { 
if (document.getElementById("housenumber").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your house number";errorcount++;}
else {ok=id;}
}

if (id==4) { 
if (document.getElementById("address").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your address";errorcount++;}
else {ok=id;}
}

if (id==5) { 
if (document.getElementById("city").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your city";errorcount++;}
else {ok=id;}
}

if (id==6) { 
if (document.getElementById("county").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your county";errorcount++;}
else {ok=id;}
}

if (id==7) { 
if (document.getElementById("postcode").value=='') { error[errorcount]=id;errormsg[errorcount]="Please enter your postcode";errorcount++;}
else {ok=id;}
}

if (id==8) {
if ( !checkMail(document.getElementById("email").value)) { error[errorcount]=id;errormsg[errorcount]="Please enter your email";errorcount++;}
else {ok=id;}
}

if (ok>0) {document.getElementById("ti"+ok).src="/images/tick.png";}


doneerror=0;
//now show errors on page
for (i=0;i<errorcount;i++) {
if (error[i]){

if (error[i]>0) {document.getElementById("ti"+error[i]).src="/images/cross.png";
doneerror=1;}

}

}

return doneerror;

}

function check_extra(extnum,someid){
  if(document.getElementById(someid).value==''){
  //  document.getElementById("ti"+extnum).src="/images/cross.png";
  }else{
    document.getElementById("ti"+extnum).src="/images/tick.png";
  }
}

function checkMail(value)
{	var x = value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return 1;
	else return 0;
  }



