﻿// JScript File
function ctr(id)
{
    return document.getElementById(id);
}

function ProcessError()
{
	var Error = document.forms[0].hdnError;
	if (Error.value != '')
	{	alert(Error.value);return -1;}
	else return 0;
}
function ProcessConfirmation()
{
	var hdnConfirmation = ctr('hdnConfirmation');
	if (hdnConfirmation != null && hdnConfirmation.value != '')
	{
		{
			if (confirm(hdnConfirmation.value)){OnConfirmation(); return true;}
			else return false;
		}
	}else
	{
		return 1;
	}
}

function ClearValue(id)
{   
    var control = ctr(id);
    if(control.value){control.value = '';};
}

function ClearOrder(clearall)
{
    ctr('cmbLateral').selectedIndex = 0;
    ctr('cmbDelivery').selectedIndex = 0;
    ctr('cmbDelivery').options.length=0;
    ctr('cmbAction').selectedIndex = 0;
    ctr('txtNewFlow').value = '';
    ctr('cmbDeliveryUnits').selectedIndex = 0;

    if(clearall)
    {  
        ctr('cmbOption').selectedIndex = 0;
        ctr('txtDeliveryDate').value = '';
    }
    ToggleMode()
}

function ShowHideDiv(ctrl, id)
{
    var div = ctr(id)
    if (div.style.display == 'none')
    {
        div.style.display = "";
        ctrl.src = "images/minimize.gif";
    }
    else
    {
        div.style.display = "none";
        ctrl.src = "images/maximum.gif";
    }
}

function ToggleMode()
{   
    var p = document.forms[0];
    var hdnMode = ctr('hdnMode');
    
    var MOD = (hdnMode.value == 'true');
    if (p.id == "frmOrderAccountInfo")
    {
        ctr('divAccountStats').disabled = MOD;
        ctr('divAssess').disabled = MOD;
    }
    else if (p.id == "frmWaterOrder")
    {
        var index = ctr('cmbOption').selectedIndex;
        var MAX = !(index > 0 && ctr('cmbOption').options[index].value != 'Max');
        var OFF = !(index > 0 && ctr('cmbOption').options[index].value != 'Off');
        ctr('divWaterOrder').disabled = MOD;
        ctr('cmbOption').disabled = MOD;
        ctr('cmbLateral').disabled = MAX || OFF;
        ctr('lblLateral').disabled = MAX || OFF;
        ctr('cmbDelivery').disabled = MAX || OFF;
        ctr('lblDelivery').disabled = MAX || OFF;
        ctr('cmbAction').disabled = MAX || OFF;
        ctr('lblAction').disabled = MAX || OFF;
        ctr('lblDeliveryDate').disabled = (index <= 0);        
        ctr('txtDeliveryDate').disabled = (index <= 0);
        ctr('btnPlaceOrder').disabled = MOD;
        ctr('btnClearOrder').disabled = MOD;
        index = ctr('cmbAction').selectedIndex;

        //check if only one valid Lateral option exists
        if(!MAX && !OFF && ctr('cmbLateral').options.length == 2 && ctr('cmbLateral').selectedIndex != 1)
        {
            ctr('cmbLateral').selectedIndex = 1;
            __doPostBack('cmbLateral','');
        }
        //check if only one valid Delivery option exists
        if(!MAX && !OFF && ctr('cmbDelivery').options.length == 2 && ctr('cmbDelivery').selectedIndex != 1)
        {
            ctr('cmbDelivery').selectedIndex = 1;
            fieldFocus(ctr('cmbAction'));
        }      
        if(index > 0 && ctr('cmbAction').options[index].value == 'Change')
        {
            if(ctr('txtNewFlow').value == 'Off' || ctr('txtNewFlow').value == 'Max'){ctr('txtNewFlow').value = '';};
            ctr('lblNewFlow').disabled = false;
            ctr('txtNewFlow').disabled = false;
            ctr('cmbDeliveryUnits').disabled = false;
            ctr('lblDeliveryUnits').disabled = false;
            ctr('txtNewFlow').focus();
            ctr('txtNewFlow').select();            
        }
        else if(index > 0 && ctr('cmbAction').options[index].value == 'Off')
        {
            ctr('txtNewFlow').value = 'Off';
            ctr('lblNewFlow').disabled = true;
            ctr('txtNewFlow').disabled = true;
            ctr('cmbDeliveryUnits').disabled = true;
            ctr('lblDeliveryUnits').disabled = true;
            ctr('txtDeliveryDate').focus();
            ctr('txtDeliveryDate').select();          
        }
        else if(index > 0 && ctr('cmbAction').options[index].value == 'Max')
        {
            ctr('txtNewFlow').value = 'Max';
            ctr('lblNewFlow').disabled = true;
            ctr('txtNewFlow').disabled = true;
            ctr('cmbDeliveryUnits').disabled = true;
            ctr('lblDeliveryUnits').disabled = true;           
            ctr('txtDeliveryDate').focus();
            ctr('txtDeliveryDate').select();          
        }
        else if(index <= 0)
        {
            ctr('txtNewFlow').value = '';        
            ctr('txtNewFlow').disabled = true;
            ctr('lblNewFlow').disabled = true;
            ctr('lblDeliveryUnits').disabled = true;
            ctr('cmbDeliveryUnits').disabled = true;
        }        
    }
    changeTitle();
}

function ProcessOption()
{
    var index = ctr('cmbOption').selectedIndex;
    var MOD = (ctr('hdnMode').value == 'true');
    var MAX = (index > 0 && ctr('cmbOption').options[index].value == 'Max');
    var OFF = (index > 0 && ctr('cmbOption').options[index].value == 'Off');    
    var SPE = (index > 0 && ctr('cmbOption').options[index].value == 'Specific');
    if (!MOD && (MAX || OFF))
    {
        ClearOrder(false);
        ctr('txtDeliveryDate').focus();
        ctr('txtDeliveryDate').select();
    }
    else if (!MOD && SPE)
    {        
        ClearOrder(false);
        ctr('cmbLateral').focus()
    }   
}

function ProcessAction()
{
    var index = ctr('cmbOption').selectedIndex;
    var MOD = (ctr('hdnMode').value == 'true');
    var MAX = (index > 0 && ctr('cmbOption').options[index].value == 'Max');
    var OFF = (index > 0 && ctr('cmbOption').options[index].value == 'Off');
    var SPE = (index > 0 && ctr('cmbOption').options[index].value == 'Specific');
    if (!MOD && (MAX || OFF))
    {
        ClearOrder(false);
        ctr('txtDeliveryDate').focus();
        ctr('txtDeliveryDate').select();
    }
    else if (!MOD && SPE)
    {        
        ClearOrder(false);
        ctr('cmbLateral').focus();
    }   
}

function checkNumber(control,decplaces)
{
	if (isNumeric(control.value))
	{
		var decimalpos = -1;
		var OriginalText = control.value;
		decimalpos = OriginalText.indexOf(".");
		if (decplaces == 0)
		{
			if (decimalpos!== -1)
				control.value = control.value.substr(0,decimalpos);
		}
		else if (decimalpos == -1 && decplaces != 0)
			control.value = control.value + "." + padding.substr(0, decimalpos + decplaces + 1);
		else if (control.value.length - decimalpos - 1 > decplaces )
			control.value = control.value.substr(0, decimalpos + decplaces + 1);
		else if (control.value.length - decimalpos - 1 < decplaces)
			control.value = control.value + padding.substr(0,decplaces - control.value.length + decimalpos + 1);
		else if (control.value.length - decimalpos - 1 < decplaces)
			control.value = control.value + padding.substr(0,decplaces - control.value.length + decimalpos + 1);
		postornot = true;
		
		if(control.getAttribute("MaxValue") != null)
		{
			postornot = ChkMaxValue(control,control.getAttribute("MaxValue"));
		};
	}
	else
	{
		alert("This field will only accept numeric values");
		postornot = false;
	}
	return postornot;
}

function checkDate(control)
{
    if(!isDate(control.value))
    {
        fieldFocus(control);
        alert('Incorrect date format. Format: mm/dd/yyyy');
    }
    else
    {
        control.value=FormatDate(control.value);
    }
}

function isDate(DateToCheck)
{
    if(DateToCheck==""){return true;}
    var m_strDate = FormatDate(DateToCheck);
    if(m_strDate==""){return false;}
    var m_arrDate = m_strDate.split("/");
    var m_MONTH = m_arrDate[0];
    var m_DAY = m_arrDate[1];
    var m_YEAR = m_arrDate[2];
    if(m_YEAR.length > 4){return false;}
    m_strDate = m_MONTH + "/" + m_DAY + "/" + m_YEAR;
    var testDate=new Date(m_strDate);
    if(testDate.getMonth()+1==m_MONTH){return true;} 
    else{return false;}
}

function isNumeric(ctrval)
{
	var ValidChars = "0123456789.-";
	var IsNumber = true;
	var Char;
				
	for (i = 0; i < ctrval.length && IsNumber == true; i++) 
	{ 
		Char = ctrval.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;			
}

function ChkMaxValue(control,maxvalue)
{
	if (parseFloat(control.value) > parseFloat(maxvalue))
	{
		alert("The maximum value for this field is " + maxvalue);
		control.value = control.getAttribute("OriginalValue");
		function fieldFocus() {control.focus();control.select();};
		setTimeout(fieldFocus , 100);
		return false;
	}
	else 
	{
		return true;
	}
}

function FormatDate(DateToFormat,FormatAs)
{
    if(DateToFormat==""){return"";}
    if(!FormatAs){FormatAs="mm/dd/yyyy";}

    var strReturnDate;
    FormatAs = FormatAs.toLowerCase();
    DateToFormat = DateToFormat.toLowerCase();
    var arrDate
    var arrMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
    var strMONTH;
    var Separator;

    while(DateToFormat.indexOf("st")>-1){
    DateToFormat = DateToFormat.replace("st","");
    }

    while(DateToFormat.indexOf("nd")>-1){
    DateToFormat = DateToFormat.replace("nd","");
    }

    while(DateToFormat.indexOf("rd")>-1){
    DateToFormat = DateToFormat.replace("rd","");
    }

    while(DateToFormat.indexOf("th")>-1){
    DateToFormat = DateToFormat.replace("th","");
    }

    if(DateToFormat.indexOf(".")>-1){
    Separator = ".";
    }

    if(DateToFormat.indexOf("-")>-1){
    Separator = "-";
    }


    if(DateToFormat.indexOf("/")>-1){
    Separator = "/";
    }

    if(DateToFormat.indexOf(" ")>-1){
    Separator = " ";
    }

    arrDate = DateToFormat.split(Separator);
    DateToFormat = "";
	    for(var iSD = 0;iSD < arrDate.length;iSD++){
		    if(arrDate[iSD]!=""){
		    DateToFormat += arrDate[iSD] + Separator;
		    }
	    }
    DateToFormat = DateToFormat.substring(0,DateToFormat.length-1);
    arrDate = DateToFormat.split(Separator);

    if(arrDate.length < 3){
    return "";
    }

    var MONTH = arrDate[0];
    var DAY = arrDate[1];
    var YEAR = arrDate[2];




    if(parseFloat(arrDate[1]) > 12){
    DAY = arrDate[1];
    MONTH = arrDate[0];
    }

    if(parseFloat(DAY) && DAY.toString().length==4){
    YEAR = arrDate[0];
    DAY = arrDate[2];
    MONTH = arrDate[1];
    }


    for(var iSD = 0;iSD < arrMonths.length;iSD++){
    var ShortMonth = arrMonths[iSD].substring(0,3).toLowerCase();
    var MonthPosition = DateToFormat.indexOf(ShortMonth);
	    if(MonthPosition > -1){
	    MONTH = iSD + 1;
		    if(MonthPosition == 0){
		    DAY = arrDate[1];
		    YEAR = arrDate[2];
		    }
	    break;
	    }
    }

    var strTemp = YEAR.toString();
    if(strTemp.length==2){

	    if(parseFloat(YEAR)>40){
	    YEAR = "19" + YEAR;
	    }
	    else{
	    YEAR = "20" + YEAR;
	    }

    }

	if(parseInt(MONTH)< 10 && MONTH.toString().length < 2){
	MONTH = "0" + MONTH;
	}
	if(parseInt(DAY)< 10 && DAY.toString().length < 2){
	DAY = "0" + DAY;
	}
	switch (FormatAs){
	case "dd/mm/yyyy":
	return DAY + "/" + MONTH + "/" + YEAR;
	case "mm/dd/yyyy":
	return MONTH + "/" + DAY + "/" + YEAR;
	case "dd/mmm/yyyy":
	return DAY + " " + arrMonths[MONTH -1].substring(0,3) + " " + YEAR;
	case "mmm/dd/yyyy":
	return arrMonths[MONTH -1].substring(0,3) + " " + DAY + " " + YEAR;
	case "dd/mmmm/yyyy":
	return DAY + " " + arrMonths[MONTH -1] + " " + YEAR;	
	case "mmmm/dd/yyyy":
	return arrMonths[MONTH -1] + " " + DAY + " " + YEAR;
	}

    return DAY + "/" + strMONTH + "/" + YEAR;;

}

function fieldFocus(control)
{
    function setFocus()
    {
        if(control.focus){control.focus()};
        if(control.select){control.select()};
    }
    setTimeout(setFocus, 100);   
}

function checkRequired()
{
    var p = document.forms[0];
    var index = ctr('cmbOption').selectedIndex;
    var hasOption = (index > 0);
    var Option = '';
    var Lateral = '';
    var Delivery = '';
    var Action = '';
    var Units = '';
    var Date = '';
    var Flow = '';
    //if not disabled, set values
    if(!ctr('cmbOption').disabled && index>-1){Option = ctr('cmbOption').options[index].value};
    if(!ctr('cmbDelivery').disabled && ctr('cmbDelivery').selectedIndex>-1){Delivery = ctr('cmbDelivery').options[ctr('cmbDelivery').selectedIndex].value};
    if(!ctr('cmbLateral').disabled && ctr('cmbLateral').selectedIndex>-1){Lateral = ctr('cmbLateral').options[ctr('cmbLateral').selectedIndex].value};
    if(!ctr('cmbAction').disabled && ctr('cmbAction').selectedIndex>-1){Action = ctr('cmbAction').options[ctr('cmbAction').selectedIndex].value};
    if(!ctr('cmbDeliveryUnits').disabled && ctr('cmbDeliveryUnits').selectedIndex>-1){Units = ctr('cmbDeliveryUnits').options[ctr('cmbDeliveryUnits').selectedIndex].value};
    if(!ctr('txtDeliveryDate').disabled){Date = ctr('txtDeliveryDate').value};
    if(!ctr('txtNewFlow').disabled){Flow = ctr('txtNewFlow').value};
    //add error for any missing fields
    var error = '';
    var ending = ' is a required field. \r';
    if(!hasOption){error += 'Option' + ending;};
    if(hasOption && Option=='Specific' && Lateral==''){error += 'Lateral' + ending;};
    if(hasOption && Option=='Specific' && Delivery==''){error += 'Delivery' + ending;};
    if(hasOption && Option=='Specific' && Action==''){error += 'Action' + ending;};
    if(hasOption && Option=='Specific' && Action=='Change' && Flow==''){error += 'New Flow' + ending;};
    if(hasOption && Option=='Specific' && Action=='Change' && Units==''){error += 'Delivery Units' + ending;};
    if(hasOption && Date==''){error += 'Delivery Date' + ending;};
    //check for any errors found
    if(error!='')
    {
        alert(error);
        return false;
    }
    else
    {
        return true;
    }
}

function changeTitle()
{
    if (parent.length > 0)
    {
        parent.ctr('lblTitle').innerHTML = ctr('hdnTitle').value;
    }
}

function handleEnter(control, keyevent, btnName)
{
    if (keyevent.keyCode==13)
    {
        keyevent.keyCode = 9;
        ctr(btnName).click();
    }
}

function Test()
{
    alert('this worked')
}

