﻿function PlusMinus(id, plus)
{

 var _Text = id.split('_');
 var _QuantityField = document.getElementById("quantity_" + _Text[1]);
    
    
    var _TestQuantity = _QuantityField.value;
    if (_TestQuantity == '') _TestQuantity = 0;
    
 var _QuantityValue = parseFloat(_TestQuantity);
 
 if (plus == 1)
 {
  _QuantityValue += 1;
 }
 else
 {
    if (_QuantityValue > 0) _QuantityValue -= 1;
 } 

 _QuantityField.value = _QuantityValue;
 
 EnableUpdateButtons();
 
 return false;
 
}

function EnableUpdateButtons()
{
    
    var _Update1 = document.getElementById("update_1");
    
    if (_Update1 != null && _Update1.disabled)
    {
        _Update1.disabled = false;
        var _Update2 = document.getElementById("update_2");
        _Update2.disabled = false;
    }
    
}

function CheckFields(objArray)
{

    // this function is passed an array of validation objects
    var _OK = true;
    for (var c = 0; c < objArray.length; c++)
    {
              
        var _Field = document.getElementById(objArray[c].FieldID);
        if (_Field != null)
        {
            if (objArray[c].FieldType == 1) // is text field
            {
                if (_Field.value == '')
                {
                    
                    var _Warning = document.getElementById(objArray[c].FieldID+'Warning');
                    if (_Warning != null)
                    {
                        _Warning.innerHTML = 'Required Field!';
                    }
                    else
                    {
                        alert(objArray[c].FieldLabel + ' as a required field');
                    }
                    
                    _Field.focus();
                    _OK = false;
                    break
                }
            }        
        }
        
    }
    
    return _OK;
    
}

function LinkMouseHover(obj, colour)
{
    obj.style.color = colour;
    obj.style.textDecoration = 'underline';
    obj.style.cursor = 'hand';
}

function LinkMouseOut(obj, colour)
{
    obj.style.color = colour;
    obj.style.textDecoration = '';
    obj.style.cursor = '';
}

function MakeFieldArray(fieldLabel, fieldID, fieldType)
{
    var _Field = new Object();
    _Field.FieldLabel = fieldLabel;
    _Field.FieldID = fieldID;
    _Field.FieldType = fieldType;
    
    return _Field;
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function CheckPasswordsForReset(obj)
{

    
    var _P1 = document.getElementById('password1');
    var _P2 = document.getElementById('password2');
    
    if (_P1.value == '' || _P2.value == '')
    {
        RequiredField(_P1, '', 'Please enter a password');
    }
    else if (_P1.value != _P2.value)
    {
        RequiredField(_P1, '', 'The two passwords do not match');
    }
    else if (_P1.value.length < 6)
    {
        RequiredField(_P1, '', 'The password must be at least 6 characters long');
    }
    else
    {
        __doPostBack(obj, 'reset_password');
    }

}


function CheckEmailForReset(obj)
{
    
    var _Email = document.getElementById('email_address');
    var _EmailValue = trim(_Email.value);

    if (_EmailValue.length == 0)
    {
        RequiredField(_Email, 'Email Address')
        return;
    }
    else
    {
        var _IsOK = IsEmailValid(_EmailValue);
        if (! _IsOK)
        {
            RequiredField(_Email, _EmailValue, ' is not a valid email address')
            return;
        }
    }
    
    __doPostBack(obj, 'reminder');
    
}

function DoCheckAddress(obj, arg) 
{
    
    var _OK = GenericValidate('address1', 'Address1') 
    if (! _OK) return;
    
    var _OK = GenericValidate('town', 'Town') 
    if (! _OK) return;
    
    var _OK = GenericValidate('postcode', 'Postcode') 
    if (! _OK) return;
    
    
    __doPostBack(obj, arg)
    
}

function GenericValidate(controlID, failureMessage) 
{

    var _OK = true;
    
    var _Control = document.getElementById(controlID);
    var _ControlValue = trim(_Control.value);

    if (_ControlValue.length == 0)
    {
        RequiredField(_Control, failureMessage)
        _OK = false;
    }
    
    return _OK;
    
}

function DoCheckPersonInfo(obj, arg)
{
    
    // var _Fields = ['first', 'February', 'March'];
    var _FirstName = document.getElementById('first_name');
    var _FirstNameValue = trim(_FirstName.value);

    if (_FirstNameValue.length == 0)
    {
        RequiredField(_FirstName, 'First Name')
        return;
    }
    
    var _LastName = document.getElementById('last_name');
    var _LastNameValue = trim(_LastName.value);

    if (_LastNameValue.length == 0)
    {
        RequiredField(_LastName, 'Last Name')
        return;
    }
    
    var _Telephone = document.getElementById('telephone1');
    var _TelephoneValue = trim(_Telephone.value);

    if (_TelephoneValue.length == 0)
    {
        RequiredField(_Telephone, 'Telephone')
        return;
    }
    
    var _Email = document.getElementById('email_address');
    
    if (_Email != null) // check whether this version of the form contains and email address
    {
    
        var _EmailValue = trim(_Email.value);

        if (_EmailValue.length == 0)
        {
            RequiredField(_Email, 'Email Address')
            return;
        }
        else
        {
            var _IsOK = IsEmailValid(_EmailValue);
            if (! _IsOK)
            {
                RequiredField(_Email, _EmailValue, ' is not a valid email address')
                return;
            }
        }
        
    }
    
    __doPostBack(obj, arg)
        
}

function DoCheckSignin(obj)
{
    
    var _Email = document.getElementById('email_address');
    var _EmailValue = trim(_Email.value);

    if (_EmailValue.length == 0)
    {
        RequiredField(_Email, 'Email Address')
        return;
    }
    else
    {
        var _IsOK = IsEmailValid(_EmailValue);
        if (! _IsOK)
        {
            RequiredField(_Email, _EmailValue, ' is not a valid email address')
            return;
        }
    }
    
    
    var _Password = document.getElementById('password');
    var _PasswordValue = trim(_Password.value);

    if (_PasswordValue.length == 0)
    {
        RequiredField(_Password, _PasswordValue)
        return;
    }
    
    __doPostBack(obj, 'signin')
    
}

function IsEmailValid(emailAddress) 
{
     //var emailReg = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
     //var regex = new RegExp(emailReg);
     return true;
}
  

function RequiredField(field, message, def)
{
    def = def || ' is a required field'
    
    alert(message + def);

    try
    {
        if (field.type != 'hidden') 
		{
			field.focus();
			field.select();
		}
    }
    catch(err)
    {
        
    }

}
