// @ver 2009.06.11 YYYY.MM.DD

// ------------------------------------------------------------------------------------
// single functions
// ------------------------------------------------------------------------------------
function IE_PngBG(element)
{
        if(element)
        {
                var imgSrc = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
                if(imgSrc)
                {
                        var imgUrl = imgSrc[1];
                        element.runtimeStyle.backgroundImage = 'none';
                        element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+imgUrl+"',sizingMethod='crop')";
                }
        }
}

function IE_PngAddExpressionEvents(element)
{
        if(element.attachEvent)
        {
                element.attachEvent('onload', function() {
                        IE_PngImgExpression(element)
                });

                element.attachEvent('onresize', function() {
                        IE_PngImgExpression(element)
                });
        }
}

function IE_PngImgExpression(element)
{
        var imgSrc = element.src.match(/.+\.png/i);
        if(imgSrc)
        {
                if(element.width && element.width > 0 && element.height > 0)
                {
                        element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+imgSrc+"',sizingMethod='scale')";
                        element.runtimeStyle.width =  element.width+'px';
                        element.runtimeStyle.height =  element.height+'px';
                        element.src = '/templates/landrover/images/blank_png.gif';
                        return true;
                }
        }
        else
        {
                return true;
        }
        return false;
}

function IE_PngIMG(element)
{
        if(element)
        {
                if(/MSIE (5\.5|6).+Win/.test(navigator.userAgent))
                {
                        if(element.tagName=='IMG')
                        {
                                var PngFixTimer_;
                                if(!IE_PngImgExpression(element))
                                {
                                        var pngfixCallback_ = function() {
                                                IE_PngImgExpression(element);
                                                clearTimeout(PngFixTimer_);
                                        };
                                        PngFixTimer_ = setTimeout(pngfixCallback_, 100);
                                }
                        }
                }
        }
}

function devShowProperties(obj, idObj)
{
	var result = "----------";
	result += "\n <br />";
  	for(var i in obj)
  	{
  		result += i + " = " + obj[i] + "\n <br />";
  	}
  	result += "----------";
  	result += "\n <br />";
  	document.getElementById(idObj).innerHTML += result;
}

function getObjectByMixedParam(mParam)
{
	var oResult = false;
	if(mParam)
	{
		if(typeof(mParam) == 'object')
		{
			oResult = mParam;
		}
		else if(typeof(mParam) == 'string')
		{
			try
			{
				oResult = document.getElementById(mParam);
			} catch(e){}
		}
	}
	return oResult;
}

function addEvent(mTarget, sEventName, fCallback)
{
	var bResult = false;
	var oTarget = getObjectByMixedParam(mTarget);
	if(oTarget)
	{
		if(oTarget.attachEvent)
		{
			oTarget.attachEvent('on' + sEventName, fCallback);
			bResult = true;
		}
		else if(oTarget.addEventListener)
		{
			oTarget.addEventListener(sEventName, fCallback, false);
			bResult = true;
		}
	}
	return bResult;
}

function showFlashBanner(mTarget)
{
	fCallback = function() {
		var oTarget = getObjectByMixedParam(mTarget);
		oTarget.innerHTML = oTarget.innerHTML;
	};
	addEvent(window, 'load', fCallback);
}

// ------------------------------------------------------------------------------------
// print version functions
// ------------------------------------------------------------------------------------
var canSetPrint = false;

function funcSetPrintCSS()
{
	var sCSS_Link = '/templates/landrover/css/print.css';
	var sCSS_Id = 'print_css';
	var oItem = getObjectByMixedParam(sCSS_Id);
	if(oItem && canSetPrint)
	{
		oItem.href = sCSS_Link;
	}

   	return false;
}

function funcOpenPage(thisURL, bShowPrintPreview)
{
	if(bShowPrintPreview)
	{
		window.open(thisURL, '_blank');
	}
	else
	{
		window.print();
	}
	return false;
}

if(window.opener && (document.location.hash == '#print'))
{
	if(window.opener.location.pathname == window.location.pathname)
	{
		canSetPrint = true;
		addEvent(window, 'load', funcSetPrintCSS);
	}
}

// ------------------------------------------------------------------------------------
// form validators
// ------------------------------------------------------------------------------------
CFormValidator = function()
{
        var _this = this;
        this.arValidators = new Array();
        this.arClearCaption = new Array();
        this.arErrObjects = new Array();
        this.formObj = null;
        this.wasFocus = false;
        this.formName = false;
        this.addFormEvent = false;
        this.callbackIdx = 0;
        this.beforeValidateCallback = new Array();
        this.afterValidateCallback = new Array();
        this.rsTimer = false;
        this.iTimeout = 1000;
        this.submitValidateProcess = false;
        this.bCurValidateResult = false;

	// *********************************************************************
        // private methods
	// *********************************************************************
	this.__init = function()
	{
                if(_this.formName)
                {
                        if(document.forms[_this.formName])
                        {
                                _this.formObj = document.forms[_this.formName];
                                if(_this.addFormEvent)
                                {
					_this.formObj.onsubmit = function() {
						return _this.validate();
					};
                                        //_this.addEvent(_this.formObj, 'onsubmit', _this.validate);
                                }
                        }
                }
	}

	this.__registerErrMessageObj = function(errObj)
	{
		if(errObj && typeof(errObj) == 'object')
		{
			var idx = this.arErrObjects && this.arErrObjects.length ? this.arErrObjects.length : 0;
			this.arErrObjects[idx] = errObj;
		}
	}

	this.__resetErrorObject = function()
	{
		if(this.arErrObjects && this.arErrObjects.length > 0)
		{
                        for(var i in this.arErrObjects)
                        {
                        	var obj = this.arErrObjects[i];
                        	if(typeof(obj) == 'object')
                        	{
                        		try
                        		{
                        			obj.parentNode.removeChild(obj);
                        		} catch (e) {}
                        	}
                        }
		}
		this.arErrObjects = new Array();
	}

	this.__getObjectByMixedParam = function(mParam)
	{
		var oResult = null;
		if(mParam)
		{
			if(typeof(mParam) == 'object')
			{
				oResult = mParam;
			}
			else if(typeof(mParam) == 'string')
			{
				try
				{
					oResult = document.getElementById(mParam);
				} catch(e){}
			}
		}
		return oResult;
	}

	// *********************************************************************
        // main public methods
	// *********************************************************************
	this.addEvent = function(el, evname, func, capture)
	{
		if(el.attachEvent)
		{
			el.attachEvent('on' + evname, func);
		}
		else if(el.addEventListener)
		{
			el.addEventListener(evname, func, false);
		}
	}

        this.init = function(sFormName, bAddEvent)
        {
        	if(sFormName && typeof(sFormName) == 'string')
        	{
        		this.formName = sFormName;
        		_this.addFormEvent = bAddEvent ? true : false;
        		this.addEvent(window, 'load', _this.__init);
        	}
        }

        this.addBeforeValidateCallback = function(funcCallback)
        {
		if(funcCallback && typeof(funcCallback) == 'function')
		{
                	var idx = this.beforeValidateCallback && this.beforeValidateCallback.length ? this.beforeValidateCallback.length : 0;
                	this.beforeValidateCallback[idx] = {'funcCallback': funcCallback};
		}
	}

        this.onBeforeValidateCallback = function()
        {
                _this.submitValidateProcess = true;
                if(_this.beforeValidateCallback && _this.beforeValidateCallback.length > 0)
                {
                        for(var i in _this.beforeValidateCallback)
                        {
                                _this.beforeValidateCallback[i]['funcCallback']();
                        }
                }
	}

        this.addAfterValidateCallback = function(funcCallback)
        {
		if(funcCallback && typeof(funcCallback) == 'function')
		{
                	var idx = this.afterValidateCallback && this.afterValidateCallback.length ? this.afterValidateCallback.length : 0;
                	this.afterValidateCallback[idx] = {'funcCallback': funcCallback};
		}
	}

        this.onAfterValidateCallback = function()
        {
                _this.submitValidateProcess = false;
                if(_this.afterValidateCallback && _this.afterValidateCallback.length > 0)
                {
                        for(var i in _this.afterValidateCallback)
                        {
                                _this.afterValidateCallback[i]['funcCallback']();
                        }
                }
	}

        this.addValidatorCallback = function(funcCallback)
        {
		if(funcCallback && typeof(funcCallback) == 'function')
		{
			var idx = this.callbackIdx++;
			this.arValidators[idx] = {'funcCallback': funcCallback};
		}
        }

	this.addRealtimeValidatorCallback = function(mField, sEventName, funcCallback)
	{
		if(mField && sEventName && typeof(sEventName) == 'string' && funcCallback && typeof(funcCallback) == 'function')
		{
			var oField = _this.__getObjectByMixedParam(mField);
			if(oField)
			{
				_this.addEvent(oField, sEventName, funcCallback);
			}
		}
	}

        this.validate = function()
        {
                var result = true;
                _this.wasFocus = false;
                _this.__resetErrorObject();
                _this.bCurValidateResult = result;
                _this.onBeforeValidateCallback();
                if(_this.formObj && _this.arValidators.length > 0)
                {
                        for(var i in _this.arValidators)
                        {
                                var resCallback = _this.arValidators[i]['funcCallback']();
                                if(resCallback === false)
                                {
                                        result = false;
                                }
                        }

                        if(result)
                        {
                                var obj_ = null;
                                for(var i in _this.arClearCaption)
                                {
                                        if(obj_ = document.getElementById(_this.arClearCaption[i]['id']))
                                        {
                                                if(obj_.value == _this.arClearCaption[i]['mess'])
                                                {
                                                        obj_.value = '';
                                                }
                                        }
                                }
                        }
                }
                _this.bCurValidateResult = result;
                _this.onAfterValidateCallback();
                return result;
        }

        this.validateInline = function()
        {

                var result = true;
                _this.wasFocus = false;
                _this.__resetErrorObject();
                _this.bCurValidateResult = result;
                _this.onBeforeValidateCallback();
                if(_this.arValidators.length > 0)
                {
                        for(var i in _this.arValidators)
                        {
                        	if(typeof(_this.arValidators[i]['funcCallback']) == 'function')
                        	{
                                	var resCallback = _this.arValidators[i]['funcCallback']();
                                	if(resCallback === false)
                                	{
                                        	result = false;
                                	}
                                }
                        }

                        if(result)
                        {
                                var obj_ = null;
                                for(var i in _this.arClearCaption)
                                {
                                        if(obj_ = document.getElementById(_this.arClearCaption[i]['id']))
                                        {
                                                if(obj_.value == _this.arClearCaption[i]['mess'])
                                                {
                                                        obj_.value = '';
                                                }
                                        }
                                }
                        }
                }
                _this.bCurValidateResult = result;
                _this.onAfterValidateCallback();
                return result;
        }

        this.trim_ = function(str)
        {
                str = str.replace(/(^ *)|( *$)/,"");
                return str;
        }

	// *********************************************************************
        // public error message methods
	// *********************************************************************
	this.showErrorMessage = function(mErrContainer, errMessage, callbackFunc)
	{
		var oErrContainer = _this.__getObjectByMixedParam(mErrContainer);
		if(oErrContainer && typeof(oErrContainer) == 'object')
		{
			var obj = null;
			if(callbackFunc && typeof(callbackFunc) == 'function')
			{
				callbackFunc(oErrContainer, errMessage);
			}
			else
			{
				errMessage = errMessage && typeof(errMessage) == 'string' ? errMessage : '';
				try
				{
					obj = document.createElement('div');
					obj.className = 'form-validator-err-container';
					obj.innerHTML = errMessage;
					oErrContainer.parentNode.appendChild(obj);
					_this.__registerErrMessageObj(obj);
				} catch(e) {}
			}
		}
	}

	// *********************************************************************
        // public custom reset form funcs
	// *********************************************************************
        this.customizeReset = function(formName)
        {
                if(formName)
                {
                        var formObj = null;
                        if(formObj = document.forms[formName])
                        {
                                for(var i=0; i<formObj.length; i++)
                                {
                                        if((formObj.elements[i].type == 'text' || formObj.elements[i].type == 'textarea') && !formObj.elements[i].readOnly && !formObj.elements[i].disabled)
                                        {
                                                formObj.elements[i].value = '';
                                        }
                                }

                                this.resetCaptions();
                                return false;
                        }
                }
        }

	// *********************************************************************
        // public focus funcs
	// *********************************************************************
        this.focusIfRequiredArray = function(obj_id, objRequired)
        {
                var obj = document.getElementById(obj_id);
                var result = true;
                var isOk = false;
                if(obj && typeof(objRequired) == 'object')
                {
                        var obj_ = null;
                        for(var key_ in objRequired)
                        {
                                if(obj_ = document.getElementById(objRequired[key_]))
                                {
                                        if(this.trim_(obj_.value) != '')
                                        {
                                                isOk = true;
                                        }
                                }
                        }

                        if(!isOk)
                        {
                                result = false;
                                if(!this.wasFocus)
                                {
                                        this.wasFocus = true;
                                        obj.focus();
                                }
                        }
                }
                return result;
        }

        this.focusIfRequiredArrayAll = function(objRequired)
        {
                var result = true;
                if(typeof(objRequired) == 'object')
                {
                        var obj_ = null;
                        for(var key_ in objRequired)
                        {
                        	if(!this.focusIfRequired(objRequired[key_]))
                        	{
                         		result = false;
                        	}
                        }
                }
                return result;
        }

        this.focusIfRequired = function(obj_id)
        {
                var obj = document.getElementById(obj_id);
                var result = true;
                if(obj)
                {
                        if(this.trim_(obj.value) == '')
                        {
                                result = false;
                                if(!this.wasFocus)
                                {
                                        this.wasFocus = true;
                                        obj.focus();
                                }
                        }
                }
                return result;
        }

        this.focusObject = function(obj)
        {
                if(obj)
                {
                        if(!this.wasFocus)
                        {
                                this.wasFocus = true;
                                obj.focus();
                        }
                }
        }

	// *********************************************************************
        // public field inner captions
	// *********************************************************************
        this.resetCaptions = function()
        {
                var obj_ = null;
                for(var i in this.arClearCaption)
                {
                        if(obj_ = document.getElementById(this.arClearCaption[i]['id']))
                        {
                                obj_.focus();
                                obj_.blur();
                        }
                }
        }

        this.addSuccessPostClearCaption = function(obj_id, mess)
        {
                var idx = this.arClearCaption && this.arClearCaption.length ? this.arClearCaption.length : 0;
                this.arClearCaption[idx] = {'id': obj_id, 'mess': mess};
        }

        this.addInFieldCaption = function(obj_id, mess, default_css, if_empty_css, funcCallback)
        {
		this.addEvent(window, 'load', function() {
                        var obj = document.getElementById(obj_id);
                        if(obj)
                        {
                                _this.addValidatorCallback(funcCallback);
                                if(obj.value == '')
                                {
                                        obj.value = mess;
                                        if(if_empty_css)
                                        {
                                                obj.className =  if_empty_css;
                                        }
                                }

                                obj.onblur = function()
                                {
                                        if(obj.value == '')
                                        {
                                                obj.value = mess;
                                                if(if_empty_css)
                                                {
                                                        obj.className =  if_empty_css;
                                                }
                                        }
                                }

                                obj.onfocus = function()
                                {
                                        if(obj.value == mess)
                                        {
                                                obj.value = '';
                                                if(default_css)
                                                {
                                                        obj.className =  default_css;
                                                }
                                        }
                                }

                                _this.addSuccessPostClearCaption(obj_id, mess);
                        }
		});
        }

        this.checkInFieldCaption = function(obj_id, mess, isRequired, setElFocus)
        {
                var obj = document.getElementById(obj_id);
                var result = true;
                if(obj)
                {
                        if(obj.value == mess && isRequired == 'Y')
                        {
                                result = false;
                                if(setElFocus && !this.wasFocus)
                                {
                                        this.wasFocus = true;
                                        obj.focus();
                                }
                        }
                        else if(isRequired == 'Y' && this.trim_(obj.value) == '')
                        {
                                result = false;
                                if(setElFocus && !this.wasFocus)
                                {
                                        this.wasFocus = true;
                                        obj.focus();
                                }
                        }
                }
                return result;
        }

	// *********************************************************************
        // public field absolute captions
	// *********************************************************************
	this.addAbsFieldCaption	= function(mField, mCaption, sAddCaptionCSS)
	{
		if(mField && mCaption)
		{
			this.addEvent(window, 'load', function() {
				var oField = _this.__getObjectByMixedParam(mField);
				var oCaption = _this.__getObjectByMixedParam(mCaption);
				if(oField && oCaption)
				{
					if(sAddCaptionCSS && typeof(sAddCaptionCSS) == 'string')
					{
						oCaption.className += ' '+sAddCaptionCSS;
					}

					if(!oField.value)
					{
						oCaption.style.display = 'block';
					}
					else
					{
						oCaption.style.display = 'none';
					}

					_this.addEvent(oField, 'blur', function() {
						if(!oField.value)
						{
							oCaption.style.display = 'block';
						}
						else
						{
							oCaption.style.display = 'none';
						}
					});

					_this.addEvent(oField, 'focus', function() {
						oCaption.style.display = 'none';
					});

					_this.addEvent(oCaption, 'click', function() {
						oField.focus();
					});

				}
			});
		}
	}

	// *********************************************************************
        // public validate funcs
	// *********************************************************************
        this.checkPhoneCode = function(fieldID, setFocus)
        {
                var res = true;
                var valObj = document.getElementById(fieldID);
                if(valObj)
                {
                        var val = valObj.value;
                        if(val.length > 0)
                        {
                        	res = /^[0-9\+\(\) ]+$/.test(val);
	                        if(!res && setFocus)
        	                {
                	                this.focusObject(valObj);
                        	}
                        }
                }
                return res;
        }

        this.checkPhoneNumber = function(fieldID, setFocus)
        {
                var res = true;
                var valObj = document.getElementById(fieldID);
                if(valObj)
                {
                        var val = valObj.value;
                        if(val.length > 0)
                        {
	                        res = /^[0-9-, ]+$/.test(val);
        	                if(!res && setFocus)
                	        {
                        	        this.focusObject(valObj);
	                        }
	                 }
                }
                return res;
        }

        this.checkEmail = function(fieldID, setFocus)
        {
                var res = true;
                var valObj = document.getElementById(fieldID);
                if(valObj)
                {
                        var val = valObj.value;
                        if(val.length > 0)
                        {
	                        res = /^\w+([-+.]\w+)*@[a-zA-Z0-9]+([-.][a-zA-Z0-9]+)*\.[a-zA-Z0-9]+([-.][a-zA-Z0-9]+)*$/.test(val);
        	                if(!res && setFocus)
                	        {
                        	        this.focusObject(valObj);
	                        }
	                 }
                }
                return res;
        }

        this.checkMinLength = function(obj_id, minLength, setElFocus)
        {
                var obj = document.getElementById(obj_id);
                var result = true;
                if(obj)
                {
                        if(obj.value.length < parseInt(minLength))
                        {
                                result = false;
                                if(setElFocus && !this.wasFocus)
                                {
                                        this.wasFocus = true;
                                        obj.focus();
                                }
                        }
                }
                return result;
        }

        this.checkMaxLength = function(obj_id, maxLength, setElFocus)
        {
                var obj = document.getElementById(obj_id);
                var result = true;
                if(obj)
                {
                        if(obj.value.length > parseInt(maxLength))
                        {
                                result = false;
                                if(setElFocus && !this.wasFocus)
                                {
                                        this.wasFocus = true;
                                        obj.focus();
                                }
                        }
                }
                return result;
        }

        this.checkFixedLength = function(obj_id, fixedLength, setElFocus)
        {
                var obj = document.getElementById(obj_id);
                var result = true;
                if(obj)
                {
                        if(obj.value.length != parseInt(fixedLength))
                        {
                                result = false;
                                if(setElFocus && !this.wasFocus)
                                {
                                        this.wasFocus = true;
                                        obj.focus();
                                }
                        }
                }
                return result;
        }

        this.checkEqualFields = function(mField1, mField2, setElFocus)
        {
                var oField1 = _this.__getObjectByMixedParam(mField1);
                var oField2 = _this.__getObjectByMixedParam(mField2);
                var result = true;
                if(oField1 && oField2)
                {
                        if(oField1.value != oField2.value)
                        {
                                result = false;
                                if(setElFocus && !this.wasFocus)
                                {
                                        this.wasFocus = true;
                                        oField2.focus();
                                }
                        }
                }
                return result;
        }
}
