// JavaScript Functions

//clear the default text in a text area
function clearText(thefield)
{
	if (thefield.value=="Enter your details here")
	thefield.value = ""
}

function clearDynamicText(thefield,cleartext)
{
	if (thefield.value==cleartext)
	thefield.value = ""
}


//check to see if a textfield has been completed, also TextArea validation for default text
function validRequired(formField,fieldLabel)
{
	var result = true;
	if ((formField.value == "") || (formField.value == "Enter your details here"))
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	return result;// JavaScript Functions




//function to check email check
function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
   
  return result;

}

//checkbox validation
function checkbox(formfield,fieldlabel)
{
   if (formfield.checked)
   {
   return true;
   }
   else
   {
   alert ("Please check the checkbox before proceeding");
   return false;
   }
}

//drop down validation
function validDropdown(value,fieldlabel) 
{
    if (value =='' ) 
    { 
    alert ('Please choose an option from the "' + fieldlabel + '"before proceeding');
    return false;
    }
    else 
    {
    return true;
    }
} 

//Little function to assign a querystring value
function editRow(url)
{

window.location = url; 
}

function editRowRight(url)
{
window.location.target="right";
window.location = url; 
}


//checking for an integer
   function integerCheck(formfield,fieldLabel,range)
   {
       
       if(isNaN(formfield.value)) 
       { 
         alert('Please enter a valid number into the box'); 
         formfield.focus(); 
         return false; 
       }
       
       if((formfield.value)>range) 
       { 
         alert('Please enter a valid percentage figure between 1 & 100 into the  box'); 
         formfield.focus(); 
         return false; 
       }
       return true;
   }
   
 //checking for good file types
 //If updating an existing reference to a file, then not necessariy needing a file attached
function validateFileExtensionEdit(fld) {
    if (fld.value.length > 4){
   
    //add your valid file types to this list
	if(!/(\.bmp|\.gif|\.jpg|\.doc|\.xls|\.txt|\.pdf|\.zip|\.rar|\.csv)$/i.test(fld.value)){
		
		alert("You cannot currently upload this type of file. Valid file types include (bmp, gif, jpg, doc, xls, txt, pdf, zip, rar, csv)");
		fld.focus();
		return false;
	}
	return true;
	
	}
	return true;
}
//new documents need to have a file attached
function validateFileExtensionNew(fld) {

    if (!fld.value.length > 4){
	    alert("Please choose a document to upload.");
	    fld.focus();
	    return false;
    }	
	if(!/(\.bmp|\.gif|\.jpg|\.doc|\.xls|\.txt|\.pdf|\.zip|\.rar|\.csv)$/i.test(fld.value)){
		    alert("You cannot currently upload this type of file. Valid file types include (bmp, gif, jpg, doc, xls, txt, pdf, zip, rar, csv)");
		fld.focus();
		    return false;	
	}
    return true;
}

function confirmSubmit()
{
var agree=confirm("Are you sure you want to delete the project?");
if (agree)
	return true ;
else
	return false ;
}


//----------------------------------------------------------------------------------------------
/*Just add water Scripts*/
//------------------------------------------------------------------------------------------------

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr,message){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The " + message + " format should be : dd/mm/yyyy" )
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month for the " + message)
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day for the " + message)
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear + "for the " + message)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date for the " + message)
		return false
	}
return true
}

function validDate(datefield,label)
{
	var dt=datefield;
	var message = label;
	
	if (datefield.value.length > 0)
	{
	    if (isDate(dt.value,message)==false)
	    {
		    dt.focus()
		    return false;
	    }
	}
    return true;
 }

function validRequiredDate(datefield,label)
{
	var dt=datefield;
	var message = label;
	if (isDate(dt.value,message)==false)
	    {
		    dt.focus()
		    return false;
	    }
    return true;
 }
 //make sure dates are logically chronological
 function dateComparison(date1,date2)
 {
     //alert("in function");
     var day1 = parseInt(date1.value.substring(0,2),10);
     var day2 = parseInt(date2.value.substring(0,2),10);
     var mon1 = parseInt(date1.value.substring(3,5),10);
     var mon2 = parseInt(date2.value.substring(3,5),10);
     var yr1 = parseInt(date1.value.substring(6,10),10);
     var yr2 = parseInt(date2.value.substring(6,10),10);  
    
     var startDate = new Date(yr1,mon1,day1);
     var endDate = new Date (yr2,mon2,day2);
     if (startDate>endDate)
         {
         alert("Your policy start date is earlier than when you sold the policy");
         date2.focus()
         return false;
         }
     return true;
 }

// end of dehydrated scripts
//----------------------------

}

//------------------------------------------------------------------------------
// Calling the individual validation functions based on the forms requirements.
//------------------------------------------------------------------------------

 function validateContactForm(theForm)
	{
		if (!validRequired(theForm.name,"Name"))
			return false;
		if (!validEmail(theForm.email,"Email Address",false))
			return false;
		if (!validRequired(theForm.message,"Message"))
			return false;
		
		return true;
}

//------------------------------------------------------------------------------
// Calling the individual validation functions based on the forms requirements.
//------------------------------------------------------------------------------

 function validateLoginForm(theForm)
	{
		if (!validRequired(theForm.Username,"Username"))
			return false;
		if (!validRequired(theForm.Password,"Password"))
			return false;
		
		return true;
}

