// Populate all text fields and select boxes on all forms for the document.
// Useful for testing pages with large number of fields.
// If field has the text "phone" in the id, the field will be set to a phone number.
// If field has the text "date" in the id, the field will be set to a date value.
// NB: the form will be populated up to the field that contains the current focus.
// to do - modify so that a different value is input in each field.
//----------------------------

function populate(){
    //alert("Number of Forms is: " + document.forms.length);
    var letters = new Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
    //increment values, so no 2 values are the same.
    var year = 1980;
    var count = 0;
    for(i=0; i<document.forms.length; i++) {
        //alert("Number of elements in form: " + i + " is " + document.forms[i].length);

        for(j=0; j<document.forms[i].length; j++) {
            //alert("Element " + j + " is of type: " + document.forms[i][j].type);

            var element = document.forms[i][j];
            if(element.type == "text" || element.type == "textarea" || element.type == "password") {
                if(element.id!=null && (element.id).toUpperCase().indexOf("DATE")>=0 ){
                    element.value = "31/12/" + year;
                    year++;
                }else if(element.id!=null && (element.id).toUpperCase().indexOf("SWITCHINGOPERATORPHONENO")>=0 ){
                    element.value = "123456789" + j;
                }else if(element.id!=null && (element.id).toUpperCase().indexOf("PHONE")>=0 ){
                    element.value = "123" + j;
                }else if(element.id!=null && (element.id).toUpperCase().indexOf("NUMBER")>=0 ){
                    element.value = "123" + j;
                }else if(element.id!=null && (element.id).toUpperCase().indexOf("NO")>=0 ){
                    element.value = "123" + j;
                }else if(element.id!=null && (element.id).toUpperCase().indexOf("OUTAGE")>=0 ){
                    element.value = "11";
                }else if(element.id!=null && (element.id).toUpperCase().indexOf("REQUESTORPAYNO")>=0 ){
                    element.value = "1234" + j;
                }else if(element.id!=null && (element.id).toUpperCase().indexOf("CODE")>=0 ){
                    element.value = "1234";
                }else{
                    element.value = "blah" + letters[j];
                }
            }
            else if(element.type == "select-one" || element.type == "select-multiple") {
                if(element.options.length > 1 && !((element.id).toUpperCase().indexOf("SUBSTATION")>=0)  ) {
                    element.options[1].selected = true;
                }
            }else if(element.type == "radio"){
                element.checked = true;
            }else if(element.type == "checkbox"){
                element.checked = true;
            }
            //stop if this element has the focus.
            //window.focus(); //may need to do this.
            if(document.activeElement && (document.activeElement.name==element.name)){
                break;
            }
            count++;

        }//end for - document.forms[i].length (each form)
    }//end for - document.forms.length (all forms)
    if(count==0){
        alert("Please click on a field to populate up to.");
    }
}//end function - populateForms

function validate12Hours(){
//alert("start validate12Hours");
	var claimOutage = document.getElementById("ClaimOutageDuration");
	var retValTwelveHours = false;
	
	if(claimOutage.value >= 11.5){
		retValTwelveHours = true;
	}else{
		alert("Outage duration must be 12 hours or more.");
	}
	//alert("validate12Hours: " + retValTwelveHours);
	return retValTwelveHours;	
}

function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
  with (new Date(y, m, d))
    return (getMonth()==m && getDate()==d) /* was y, m */ 
}

function ReadISO8601date(Q) {
var T // adaptable for other layouts
//if ((T = /^(\d+)([-\/])(\d\d)(\2)(\d\d)$/.exec(Q)) == null) original
if ((T = /^(\d+)([-\/])(\d\d)(\2)(\d\d)/.exec(Q)) == null)
{ return -2 } // bad format
for (var j=1; j<=5; j+=2) T[j] = +T[j] // some use needs numbers
if (!ValidDate(T[1], T[3]-1, T[5])) { return -1 } // bad value
return [ T[1], T[3], T[5] ] 
}

function DaysDiff(D1, D2) {
    return Math.round((D1 - D2) / 86400000);
}

function DiffDays(S1, S2) {
    var X = ReadISO8601date(S1);
    if (X < 0) {
        return "Date 1 bad";
    }
    var Y = ReadISO8601date(S2);
    if (Y < 0) {
        return "Date 2 bad";
    }
    var Dx = Date.UTC(X[0], X[1] - 1, X[2]);
    var Dy = Date.UTC(Y[0], Y[1] - 1, Y[2]);
    return (Dx - Dy) / 86400000;
}

//dateIn - "10/12/2005"
//return - "2005/12/10"
function parseDate(dateIn){
var day = dateIn.substring(0,2);
var month = dateIn.substring(3,5);
var year = dateIn.substring(6,10);

//alert("day:" + day +  "\nmonth:" + month +  "\nyear:" + year);

return year + "/" + month + "/" + day;

}

function validateDate(){
var retValDate = false;
//alert("start validateDate");

var current = new Date();

var powerOnDateText = document.getElementById("~ClaimDatePowerOn").value;
var powerOffDateText = document.getElementById("~ClaimDatePowerOff").value;

powerOnDateText = parseDate(powerOnDateText);
powerOffDateText = parseDate(powerOffDateText);	

var date1 = powerOnDateText;
var now = new Date();

var day = now.getDate();
var month = now.getMonth();
var year  = now.getFullYear();

if(day<10){day = "0" + day;}
if(month<10){
	if(month=="9"){
		month="10";
	}else{
		month = "0" + (month+1);
	}
}

date2 = year + "/" + month + "/" + day;
//alert("day:" + now.getDate() +  "\nmonth:" + now.getMonth() +  "\nyear:" + now.getFullYear());

var diff1 = DaysDiff(new Date(date1.replace(/-/g, "/")), new Date(date2.replace(/-/g, "/")));
var diff2 = DiffDays(date1, date2);

date1 = powerOffDateText;
date2 = powerOnDateText;

var diff3 = DaysDiff(new Date(date1.replace(/-/g, "/")), new Date(date2.replace(/-/g, "/")));
var diff4 = DiffDays(date1, date2);

//alert("diff4:" + diff4);

if(diff2 < -62){
	alert("Claims must be submitted within 2 calendar months of the outage date to be eligible for an Extended Outage Payment.\n\Please review the terms and conditions of the scheme or contact Networks Customer Services on 131087 should \n\you have any questions regarding eligibility.");
}else if(diff4>0){
	alert("'Date power came back on' cannot be before the 'Date the power went off'");
}else{
	retValDate = true;
}

//alert("validateDate: " + retValDate);
return retValDate;
}


/*
function performValidations(myForm){
	var dateValid = validateDate();
	var hoursValid = validate12Hours();
	var custValid = custValidate(myForm);
	
//	alert("dateValid:" + dateValid +"\nhoursValid:" + hoursValid + "\ncustValid:" + custValid);
	var retValValidations = dateValid && hoursValid && custValid;
	alert("retValValidations: " + retValValidations);
	return retValValidations;
}
*/