
// Detect if the operating system is Windows or Macintosh            
if ((navigator.appVersion.indexOf("Win")!=-1) || 
(navigator.appVersion.indexOf("Mac")!=-1)){
   document.write("<link href='/carol.css' rel='styleSheet' type='text/css'>");
}
// Platform is not PC or Mac
else {
 document.write("<link href='/carolW.css' rel='styleSheet'  type='text/css'>");
}

//################################################################
//#         Functions used by the html summaries start here      #
//################################################################

function licenceAlert(request, catid) {
/* Open a new window to display a Licence Alert. */

/* In the following the JavaScript tag needs to be after */
/* the body tag, because of a problem with X11 Netscape. */
var text= '<html><head><title>CPC Licence Alert</title></head>\n';
    text += '<body bgcolor="#feffff" link="#000099" vlink="#0033ff" text="#000099" alink="red">\n';
    text += '<font size="2" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">\n';
    text += '<center><b>CPC LICENCE ALERT</b>\n';
    text += '<p><font color="red"><b>This software is provided on an "as is" basis.</b></font>\n';
    text += '<p> Before proceeding<br> please read the\n';
    text += '<A href=\"/cpc/cgi-bin/licenceCheck.pl?catid='+catid+'\" target=\"main\">'+catid+' Licence.</A>\n';
    text += '<p> Do you accept its terms and conditions ?\n';
    text += '<form name="licencecheck" onSubmit="go(this)">';
    text += '<input type="radio" value="Yes" name="choice" checked >Yes<br>\n';
    text += '<input type="radio" value="No" name="choice" >No<br>\n';    
    text += '<p> <font size="1"> Authors are often interested to know who has downloaded their programs so they can contact users about improvements or corrections and receive feedback on the usefulness of their code. In order to be able to provide this information to the authors we should be grateful if you would enter your email address. This is not compulsory and if you choose not to do so the code will still be provided.</font> <br/><br/>\n';    
    text += 'Email:<input type= "text"  name="email" size= "35"/><br/><br/>\n';     
    text += '<input type="submit" value="Continue " onClick="go(this.form);">\n';
    text += '</form>\n';
    text += '</center></font></body>\n';
    text += '<script language="JavaScript">\n';
    text += 'function go(form)\n';
    text += '{var request;\n';
    text += 'var emailpara= "&email=";\n';
    text += 'var formEmail= form.email.value;\n';     
    text += 'if (form.choice[1].checked){\n';
    text += 'window.close();}\n';
    text += 'else if (form.choice[0].checked){\n';
    text += 'request= "'+request+'";\n';
    text += 'request = request+emailpara+formEmail;\n';    
    text += 'opener.location.href=request; window.close();}\n';
    text += '}\n';
    text += '</script>\n'; 
    text += '</html>';
cpcLicence=window.open("","cpcLicence","width=350,height=400,resizable=yes");
if (cpcLicence!=null) {
	if (cpcLicence.opener==null) cpcLicence.opener=window;
	cpcLicence.document.open();
	cpcLicence.document.write(text);
	cpcLicence.document.close(); 
	cpcLicence.focus();
    }
}
function getFileType(request, catid) {
/* Open a new window so user can select summary file type. */

/* In the following the JavaScript tag needs to be after */
/* the body tag, because of a problem with X11 Netscape. */
summarytype=window.open("","summarytype","width=200,height=250,resizable=yes");
var text = summarytype.document;
    text.write('<html><head><title>New Version Summary</title></head>\n');
    text.write('<body bgcolor="#feffff" link="#000099" vlink="#0033ff" text="#000099" alink="red">\n');
    text.write('<font size="2" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">\n');
    text.write('<center><b>Select Summary Type</b>\n');
    text.write('<p><font color="red"><b>Please select the file format you require.'); 
    text.write('<form name="fileTypeCheck">');
    text.write('<input type="radio" value="LaTeX" name="choice" checked >LaTeX<br>\n');
    text.write('<input type="radio" value="Text" name="choice" >Text <br>\n');
    text.write('<input type="button" value="Continue" onClick="go(this.form);">\n');
    text.write('</form>\n');
    text.write('</center></font></body>\n');
    text.write('<script language="JavaScript">\n');
    text.write('function go(form)\n');    
    text.write('{var request;');       
    text.write('var latex = \'latex\';');    
    text.write('var text = \'text\';');
    text.write('if (form.choice[1].checked){\n');    
    text.write('request = "'+request+'";');
    text.write('request = request+text;\n');
    text.write('opener.location.href=request; window.close();}\n');    
    text.write('else if (form.choice[0].checked){\n');       
    text.write('request = "'+request+'";');
    text.write('request = request+latex;\n');       
    text.write('opener.location.href=request; window.close();}');
    text.write('}\n');
    text.write('</script>\n'); 
    text.write('</html>');
    text.close();
}

//################################################################
//#            Functions used by upload.pl start here            #
//################################################################

function validateF95(form)
{ 
 var formFileName = form.upfile.value; 
 var formLines= form.lines.value;
 var formOption= form.option.value;
 var extArray = new Array(".f", ".f90", ".for",".f95", ".ftn", ".F", ".ff",".ff90", ".ff95", ".ff");
 
// alert(formFileName);
// alert(formOption);
 
 allowSubmit = false;
//
// check that a file has been selected

 if (!formFileName) 
  {
   alert("Please select the fortran source code file to be uploaded"); 
   return false;
  } 
//
// check that the file extension is valid
// firstly extract extension from path/filename
 
 while (formFileName.indexOf("\\") != -1)
 formFileName = formFileName.slice(formFileName.indexOf("\\") + 1);
 ext = formFileName.slice(formFileName.indexOf(".")).toLowerCase();
 for (var i = 0; i < extArray.length; i++) 
 {
  if (extArray[i] == ext) { allowSubmit = true; break; }
 }
 if (!allowSubmit) 
  {
    alert("Please only upload files that end with one of the following extensions:  " 
    + (extArray.join("  ")) + "\nPlease select a new "
    + "file to upload and submit again.");
    return false;
  }
//
// Check that, if maxcontin is checked no. of lines > 0 

 if (form.maxcontin.checked) 
    {
      if (formLines < 1) 
       {
         form.lines.focus();
         alert("-MAXCONTIN \n Limit for no. of continuation lines must be entered"); 
          return false;         
       }
    }
//
// Check that, if lines > 0, maxcontin is checked 
 if (formLines > 0)
    {
      if  (!form.maxcontin.checked)
       {       
        alert("-MAXCONTIN \n no. of lines entered, but maxcontin option not checked. \n If option is required please check maxcontin box."); 
        return false;
       }
    }    

//
// Check that, if kind option is checked, value selected is not 'none'. 

 if (form.kind.checked) 
    {
      if (formOption == 'none') 
       {         
         alert("-KIND \n value for kind, (byte or sequential) must be selected."); 
          return false;         
       }
    }
//
// Check that, if kind option has been entered, -kind is checked 
 if (formOption != 'none')
    {
      if  (!form.kind.checked)
       {       
        alert("-KIND \n value entered, but kind option not checked. \n If option is required please check -kind box."); 
        return false;
       }
    }    
}  

//################################################################
//#            Functions used by showversions.pl start here      #
//################################################################

function downloadRequest(id, deliveryType)
{ 
   /* base address of cgi scripts */
  var base="http://cpc.cs.qub.ac.uk/cpc/cgi-bin/";
  /* parameters for cgi download script */
  var dltype="&usertype=toolbar&deliverytype=www";
  var emtype="&usertype=toolbar&deliverytype=email";
  var view="&usertype=toolbar&deliverytype=view";
  var request;  
  if (deliveryType == "www")
   {
    request = base+"preprocessrequest.pl/?catid="+id+dltype; 
    		             licenceAlert(request,id);  
   }
  else
   {
    request = base+"getemailaddress.pl/?catid="+id+emtype;  
                             licenceAlert(request,id);
   }  
} 

//################################################################
//#            Functions used by individual.pl start here        #
//################################################################
function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are 
valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last 
character
        return false;
    }   
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function clearForm(form)
{
 form.title.value=""; 
 form.initials.value="";
 form.surname.value="";
 form.email.value="";  
 form.institution.value="";
}

function validateInvoice(form)
{
 var formTitle= form.title.value; 
 var formInitials= form.initials.value;
 var formSurname= form.surname.value;
 var formEmail= form.email.value;  
 var formInst= form.institution.value;

 if (formInitials ==""){alert("Please enter your initials");
  form.initials.focus(); return false;}
 if (formSurname == ""){alert("Please enter your surname"); 
  form.surname.focus(); return false;}
 if (!isValidEmail(formEmail)){alert("Please enter a valid email address");
      form.email.focus(); return false;}
 return true;
 }

//################################################################
//#            Functions used by nonsub.pl start here            #
//################################################################
function clearFullForm(form)
{
 form.address.value="";
 form.city.value="";
 form.country.value="";
 form.postcode.value="";
 form.vat.value="";
}

function validateFullInvoice(form)
{
 var formTitle= form.title.value; 
 var formInitials= form.initials.value;
 var formSurname= form.surname.value;
 var formEmail= form.email.value;  
 var formInst= form.institution.value;
 var formAddress= form.address.value;
 var formCity= form.city.value;
 var formCountry= form.country.value;
 var formPostcode= form.postcode.value;
 var formVat= form.vat.value;

 if (formInitials ==""){alert("Please enter your initials");
  form.initials.focus(); return false;}
 if (formSurname == ""){alert("Please enter your surname"); 
  form.surname.focus(); return false;}
 if (formEmail == ""){alert("Please enter a valid email address");
      form.email.focus(); return false;} 
 if (formAddress ==""){alert("Please enter your address"); 
     form.address.focus(); return false;}
 if (formCity == ""){alert("Please enter the city name"); 
     form.city.focus(); return false;}
 if (formCountry ==""){alert("Please enter the country name"); 
     form.country.focus(); return false;}
 if (formPostcode == ""){alert("Please enter the your Zip/Postcode"); 
     form.postcode.focus(); return false;} 
 return true;
}

