var mand_off = false;

function is_Comment(str)     {
        if((mand_off)&&(str.length == 0))
            return true;
                
        if(str.length>=1)
            return true;
          
        return false;      
        //regexpr = /^[#A-Za-z0-:<-@_\ \!\.\-\/]{1,128}$/;
        //return regexpr.test(str);
}

function is_Email(str)     {
        regexpr = /^\w(\.?[-\w])*@\w(\.?[-\w])*\.[a-z]{2,4}$/i;
        //regexpr = /(^[a-z]([a-z_\.]*)@([a-z_\.\-]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.\-]*)([.][a-z]{4})$)|(^[a-z]([a-z_\.]*)@([a-z_\.\-]*)(\.[a-z]{2})(\.[a-z]{2})*$)/i;
        return regexpr.test(str);
}

function is_Phone(str)     {
        if((mand_off)&&(str.length == 0))
            return true;

        regexpr = /^(\+\d{1,3} ?)?(\(\d{1,5}\)|\d{1,5}) ?\d{3} ?\d{0,7}( ?(x|xtn|ext|extn|extension)?\.? ?\d{1,5})?$/i;
        return regexpr.test(str);
}

function is_Memo(str)     {
        if((mand_off)&&(str.length == 0))
            return true;

        if(str.length>=1)
            return true;
            
        return false;            
        //regexpr = /^[#(-z!-&_\ \!\u0027\u0060\u000A\u000D\uF0E6\u2022\u006F\uF0A7\uF0FC\uF0D8\uF076\u0009\.\-\/]{1,5000}$/;
        //return regexpr.test(str);
}

function is_LargeMemo(str)     {
        if((mand_off)&&(str.length == 0))
            return true;

        if(str.length>=1)
            return true;
            
        return false;            
        //regexpr = /^[#(-z!-&_\ \!\u0027\u0060\u000A\u000D\uF0E6\u2022\u006F\uF0A7\uF0FC\uF0D8\uF076\u0009\.\-\/]{1,5000}$/;
        //return regexpr.test(str);
}


function is_Date( strValue ) {
  if((mand_off)&&(strValue.length == 0))
    return true;

  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
  
  if(!objRegExp.test(strValue)){
    return false; //doesn't match pattern, bad date
  }else{    
    for(i=0;i<strValue.length;i++){
      if((strValue.charAt(i)>'9') || (strValue.charAt(i)<'0')){      
        break;      
      }    
    }
    var strSeparator=strValue.charAt(i);
    var arrayDate = strValue.split(strSeparator);
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[0]);
    if(arrayDate[0].length == 1){
      arrayDate[0] = '0'+arrayDate[0];
    }
    if(arrayDate[1].length == 1){
      arrayDate[1] = '0'+arrayDate[1];
    }
    if(arrayLookup[arrayDate[1]] != null) {
      if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0){
        return true;
      }
    }

    var intMonth = parseInt(arrayDate[1]);
    if(intMonth == 2){ 
       var intYear = parseInt(arrayDate[2]);  
       if(((intYear%4==0)&&(intDay<=29) || (intYear%4!=0)&&(intDay <=28))&&(intDay!=0)){
          return true;
       }
    }
  }
  return false;
}

function is_Nmr(dde){
   if((mand_off)&&(dde.length == 0))
     return true;

   var day=parseInt(dde,10);
   if(isNaN(day))
    day=-1;
   if(day!=dde)
    day=-1;        
   if((dde.indexOf(",")!=-1)||(dde.indexOf(".")!=-1))
    day=-1;
   if(day==-1)
    return false;
   return true;
}

function is_Flt(dde){
   if((mand_off)&&(dde.length == 0))
     return true;

   var vl=parseFloat(dde);
   if(isNaN(vl))
    return false;
   var dd=new String(dde);
   if(dd.indexOf(",")!=-1){
    dd=null;
    return false;
   }
   dd=null;
   return true;
}

function ValidateForm()  
{
/*
//========================== Remove it ====================================//
document.f.Theme.value = 1;
document.f.Project_title.value = "My Project";
document.f.Host_HEI.value = 2;
document.f.Host_HEI_Dept.value = "Host_HEI_Dept";
document.f.HEI2.value = 4;
document.f.HEI2_Dept.value = "HEI2_Dept";
document.f.Other_HEI.value = 3;
document.f.Industry_Partner.value = 'FAER';
document.f.Partners_Address.value = "Partners address";
document.f.Nature_of_Business.value = "Nature of business";
document.f.Charity.checked = true;
document.f.Public.checked = true;
document.f.SME.checked = true;
document.f.SRE.checked = true;
document.f.LP_Commit[1].checked = true;
document.f.OEP_name_address.value = "OEP_Name_address";
document.f.OEP_Nature_of_Business.value = "Software";
document.f.OEP_Charity.checked = true;
document.f.OEP_Public.checked = true;
document.f.OEP_SME.checked = true;
document.f.OEP_SRE.checked = true;
document.f.Total_cost.value = "4500.00";    
document.f.Total_contr_partners.value = "1240.00";
document.f.Total_contr_suppartners.value = "1240.00";
document.f.Explanation_less_50.value = "Explanation_less_50";
document.f.Add_Contr.value = "120.55";
document.f.Add_Contr_Explanation.value = "Add_Contr_Explanation";
document.f.Total_Required.value = "7500.00";
document.f.Project_Start_date.value = "29/2/2008";
document.f.Project_End_date.value = "29/3/2009";
document.f.Project_Description.value = "My Project description";
document.f.Project_Abstract.value = "My Project abstract";
document.f.Project_fit.value = "My_test_fit";
document.f.Project_col_nature.value = "project Col Nature";
document.f.Proj_Linked[1].checked = true;
document.f.Project_Links_GWR_App.value = "Project links";
document.f.Contact_name.value = 'Petar Sabev';
document.f.Email_address.value = 'sabev@faer.info';
document.f.Postal_address.value = 'Postal address';
document.f.Contact_telephone.value = '+359 (82) 455274';
document.f.EWB[2].checked = true;
document.f.HEI_1_RAE.value = "HEI_1_RAE";
document.f.HEI_2_Supervisor.value = "HEI_2_Supervisor";
document.f.HEI2SEWB[1].checked = true;
document.f.Other_HEI_Supervisor.value = "Other_HEI_Supervisor";
document.f.OtherHEISEWB[1].checked = true;
document.f.arrangement_for_supervision.value = "Arrangement For Supervision";
document.f.ARDER.checked = true;
document.f.ARDER_Details.value = "ARDER_test";
document.f.HODNTP.checked = true;
document.f.HODNTP_Details.value = "HODNTP_test";
document.f.ISTW.checked = true;
document.f.ISTW_Details.value = "ISTW_test";
document.f.EISW.checked = true;
document.f.EISW_details.value = "EISW_test";
document.f.EISW_Other.checked = true;
document.f.EISW_Other_Details.value = "EISW_Other_test";
document.f.Benefits_ext_partner.value = "Benefits_ext_partner";
document.f.EP_HEI_prev[1].checked = true;
document.f.Ext_partner_worked_HEI.value = "Ext_partner_worked_HEI";
document.f.Potential_collabor.value = "Potential_collabor";
//========================== Remove it ====================================//
*/
    var old_mand = mand_off;
    if(mand_off) mand_off = false;      
    if(!is_Comment(document.f.Contact_name.value)){
	   alert("Enter valid Lead HEI Supervisor Name please!");
	   document.f.Contact_name.focus();
	   return false;
	}
    mand_off = old_mand;
    
    
    if(!mand_off && (document.f.Theme.value == -1)){
	   alert("Select Theme please!");
	   document.f.Theme.focus();
	   return false;
	}
    if(!is_Comment(document.f.Project_title.value)){
	   alert("Enter valid Project Title please!");
	   document.f.Project_title.focus();
	   return false;
	}
    if(!mand_off && (document.f.Host_HEI.value == -1)){
	   alert("Select Host Institution please!");
       document.f.Host_HEI.focus();
	   return false;
	}
    if(!is_Comment(document.f.Host_HEI_Dept.value)){
	   alert("Enter valid Department please!");
	   document.f.Host_HEI_Dept.focus();
	   return false;
	}
    if(!mand_off && (document.f.HEI2.value == -1)){
	   alert("Select 2nd Institution please!");
       document.f.HEI2.focus();
	   return false;
	}
    if(!is_Comment(document.f.HEI2_Dept.value)){
	   alert("Enter valid Department please!");
	   document.f.HEI2_Dept.focus();
	   return false;
	}
    if(!is_Comment(document.f.Industry_Partner.value)){
	   alert("Enter valid Industry Partner please!");
	   document.f.Industry_Partner.focus();
	   return false;
	}
    if(!is_Memo(document.f.Partners_Address.value)){
	   alert("Enter valid Address please!");
	   document.f.Partners_Address.focus();
	   return false;
	}
    if(!is_Memo(document.f.Nature_of_Business.value)){
	   alert("Enter valid Nature of Business please!");
	   document.f.Nature_of_Business.focus();
	   return false;
	}    
    if(document.f.Total_cost.value.length == 0){
      document.f.Total_cost.value = "0.00";
    }
    if(!is_Flt(document.f.Total_cost.value)){
	   alert("Enter valid Total cost of studentship please!");
	   document.f.Total_cost.focus();
	   return false;
	}
    vl=parseFloat(document.f.Total_cost.value);
    if(!mand_off && (vl <= 0)){
	   alert("Enter valid Total cost of studentship please!");
	   document.f.Total_cost.focus();
	   return false;    
    }    
    if(document.f.Total_contr_partners.value.length == 0){
      document.f.Total_contr_partners.value = "0.00";
    }    
    if(!is_Flt(document.f.Total_contr_partners.value)){
	   alert("Enter valid Total contribution from Lead External Partner please!");
	   document.f.Total_contr_partners.focus();
	   return false;
	}    
    vl=parseFloat(document.f.Total_contr_partners.value);
    if(!mand_off && (vl <= 0)){
	   alert("Enter valid Total contribution from Lead External Partner please!");
	   document.f.Total_contr_partners.focus();
	   return false;
	}    
    if(document.f.Total_contr_suppartners.value.length == 0){
      document.f.Total_contr_suppartners.value = "0.00";
    }    
    if(!is_Flt(document.f.Total_contr_suppartners.value)){
	   alert("Enter valid Total contribution from Supporting External Partners please!");
	   document.f.Total_contr_suppartners.focus();
	   return false;
	}    
    vl=parseFloat(document.f.Total_contr_partners.value);
    if(!mand_off && (vl < 0)){
	   alert("Enter valid Total contribution from Supporting External Partners please!");
	   document.f.Total_contr_suppartners.focus();
	   return false;
	}    
    if(document.f.Add_Contr.value.length != 0){
        if(!is_Flt(document.f.Add_Contr.value)){
	        alert("Enter valid Supplementary contribution please!");
	        document.f.Add_Contr.focus();
	        return false;
	    }    
        vl=parseFloat(document.f.Add_Contr.value);
        if(!mand_off && (vl < 0)){
	        alert("Enter valid Supplementary contribution please!");
	        document.f.Add_Contr.focus();
	        return false;
	    }
    }
    if(document.f.Total_Required.value.length == 0){
      document.f.Total_Required.value = "0.00";
    }    
    if(!is_Flt(document.f.Total_Required.value)){
	   alert("Enter valid Total required from GWR please!");
	   document.f.Total_Required.focus();
	   return false;
	}    
    vl=parseFloat(document.f.Total_Required.value);
    if(!mand_off && (vl <= 0)){
	   alert("Enter valid Total required from GWR please!");
	   document.f.Total_Required.focus();
	   return false;
	}
    //if(document.f.Project_Start_date.value.length != 0){
      if(!is_Date(document.f.Project_Start_date.value)){
	    alert("Enter valid Project Start Date please!");
	    document.f.Project_Start_date.focus();
	    return false;
	  }
    //}
    //if(document.f.Project_End_date.value.length != 0){
      if(!is_Date(document.f.Project_End_date.value)){
	    alert("Enter valid Project Finish Date please!");
	    document.f.Project_End_date.focus();
	    return false;
	  }
    //}
    /*if(document.f.Project_duration_in_Years.value.length != 0){
      if(!is_Nmr(document.f.Project_duration_in_Years.value)){
	    alert("Enter valid Project Duration please!");
	    document.f.Project_duration_in_Years.focus();
	    return false;
	  }
    }*/
    if(!is_LargeMemo(document.f.Project_Description.value)){
	   alert("Enter valid Project Description please!");
	   document.f.Project_Description.focus();
	   return false;
	}
    if(!is_LargeMemo(document.f.Project_Abstract.value)){
	   alert("Enter valid Project Abstract please!");
	   document.f.Project_Abstract.focus();
	   return false;
	}
    if(!is_Memo(document.f.Project_fit.value)){
	   alert("Enter Alignment with proposed Theme please!");
	   document.f.Project_fit.focus();
	   return false;
	}
    if(!is_Memo(document.f.Project_col_nature.value)){
	   alert("Enter Nature of the proposed collaboration please!");
	   document.f.Project_col_nature.focus();
	   return false;
	}
    /*if(!is_Comment(document.f.Lead_HEI_Supervisor.value)){
	   alert("Enter valid Lead HEI supervisor please!");
	   document.f.Lead_HEI_Supervisor.focus();
	   return false;
	}*/
    if(!is_Email(document.f.Email_address.value)){
	   alert("Enter valid Email Address please!");
	   document.f.Email_address.focus();
	   return false;
	}
    if(!is_Memo(document.f.Postal_address.value)){
	   alert("Enter valid Address please!");
	   document.f.Postal_address.focus();
	   return false;
	}
    if(!is_Phone(document.f.Contact_telephone.value)){
	   alert("Enter valid Contact telephone please!");
	   document.f.Contact_telephone.focus();
	   return false;
	}    
    if(!is_Memo(document.f.HEI_1_RAE.value)){
	   alert("Enter valid Relevant Academic Experience please!");
	   document.f.HEI_1_RAE.focus();
	   return false;
	}
    if(!is_Comment(document.f.HEI_2_Supervisor.value)){
	   alert("Enter valid 2nd HEI supervisor please!");
	   document.f.HEI_2_Supervisor.focus();
	   return false;
	}
    if(!is_Memo(document.f.arrangement_for_supervision.value)){
	   alert("Enter valid Arrangements for supervision please!");
	   document.f.arrangement_for_supervision.focus();
	   return false;
	}
    
    var swben = 0;    
    if(document.f.ARDER.checked){            
        if(!is_Memo(document.f.ARDER_Details.value)){
	        alert("You have checked Additional R&D expenditure in the region.\nEnter Details please!");
            document.f.ARDER_Details.focus();
	        return false;
	    }        
        ++swben;
    }
    if(document.f.HODNTP.checked){
        if(!is_Memo(document.f.HODNTP_Details.value)){
	        alert("You have checked Helping an organisation develop a new technology or process.\nEnter Details please!");
            document.f.HODNTP_Details.focus();
	        return false;
	    }                
        ++swben;        
    }
    if(document.f.ISTW.checked){
        if(!is_Memo(document.f.ISTW_Details.value)){
	        alert("You have checked Improving systems or techniques within the region.\nEnter Details please!");        
            document.f.ISTW_Details.focus();
	        return false;
	    }                
        ++swben;        
    }
    if(document.f.EISW.checked){
        if(!is_Memo(document.f.EISW_details.value)){
	        alert("You have checked Enhancing information about the SW.\nEnter Details please!");
            document.f.EISW_details.focus();
	        return false;
	    }                
        ++swben;        
    }
    if(document.f.EISW_Other.checked){
        if(!is_Memo(document.f.EISW_Other_Details.value)){
	        alert("You have checked Other.\nEnter Details please!");
            document.f.EISW_Other_Details.focus();
	        return false;
	    }        
        ++swben;        
    }
    if(!mand_off && (swben == 0)){
	        alert("Please select at least one SW Economic Benefit that apply");
            document.f.ARDER.focus();
            return false;    
    }
    if(!is_Memo(document.f.Benefits_ext_partner.value)){
	   alert("Describe What benefits are there for the external partner please!");
	   document.f.Benefits_ext_partner.focus();
	   return false;
	}
    if(document.f.EP_HEI_prev[0].checked){
      if(!is_Memo(document.f.Ext_partner_worked_HEI.value)){
	    alert("Describe details of partners Previous collaborations please!");
	    document.f.Ext_partner_worked_HEI.focus();
	    return false;
	  }    
    }
    if(!is_Memo(document.f.Potential_collabor.value)){
	   alert("Describe what potential is there for future research collaboration please!");
	   document.f.Potential_collabor.focus();
	   return false;
	}
        
    return true;    
}

function OnPartSave()  
{
    mand_off = true;
    if(!ValidateForm())
        return false;

    if(!confirm("Your application form will be saved for later retrieval.\nProceed?"))
        return false;

	document.f.action = "formadd.php?s_partial=1";
	document.f.submit();
	return false;
}

function OnSave()  
{
    mand_off = false;
    if(!ValidateForm())
        return false;

    if(!confirm("Your application form will be sent to GWR.\nProceed?"))
        return false;

	document.f.action = "formadd.php";
	document.f.submit();
	return false;
}

function OnPrint()  
{
    print();
	return false;
}

function OnOpen()
{
    document.location.href = "studentship_open.php";
}

function OnReview()  
{
	document.f.action = "formadd.php?s_temp=1";
	document.f.submit();
	return false;    
}

function setTTLCp()
{

    prc = (parseFloat(document.f.Total_contr_partners.value) + parseFloat(document.f.Total_contr_suppartners.value));
    prc = ''+prc;
    if(prc.indexOf('.')!=-1){
      prc = prc+'00';
      prc = prc.substring(0,prc.indexOf('.'))+'.'+prc.substring(prc.indexOf('.')+1,prc.indexOf('.')+3);
    }else{
      prc = prc+'.00';
    }
    document.f.TTLCP.value = prc;
    return false;
}