﻿/*
----------------------------------------------------------
	Initialize Namespace
----------------------------------------------------------
*/
window.Milou = {Util : {},Form : {},Admin : {},Effects : {}};

Milou.Class = {
	create: function() {
		return function() {
		this.initialize.apply(this, arguments);
		}
	}
};

Milou.Util.Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
};

Milou.Form.Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
};

/*
----------------------------------------------------------
	Milou.Constants
		-Milou.Constants.initialize()
		-Milou.Constants.VALIDATE_LENGTH
----------------------------------------------------------
*/

Milou.Constants = {
	initialize: function() {
		this.VALIDATE_LENGTH = "Length";
		this.VALIDATE_EMAIL = "Email";
		this.VALIDATE_NUMBER = "Number";
		this.DEBUG_MODE = false;
		this.VALIDATE_EMPTY = "Empty";
	}
};


/*
----------------------------------------------------------
	Milou.Util.User
		-Milou.Util.User.browser
		-Milou.Util.User.version
		-Milou.Util.User.OS
----------------------------------------------------------
*/
Milou.Util.User = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser"; 
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
};


/*
----------------------------------------------------------
	Milou.Util.Image
		-Milou.Util.Image.SwapString(strElement,strSrc)
		-Milou.Util.Image.SwapObject(objElement,strSrc)
		-Milou.Util.Image.FixPng(imgObj)
----------------------------------------------------------
*/
Milou.Util.Image = {
	SwapString: function (strElement,strSrc) {
		if($(strElement))
		   $(strElement).src = strSrc;
	},
	SwapObject: function (objElement,strSrc) {
		if(objElement)
		   objElement.src = strSrc;
	},
	FixPng: function(imgObj) {
		if(Milou.Util.User.browser != "Explorer" && imgObj.src.indexOf("png")<0)
			return;
		
		if ((Milou.Util.User.version >= 5.5) && (Milou.Util.User.version < 7) && (document.body.filters)) 
		{
		   var imgID = (imgObj.id) ? "id='" + imgObj.id + "' " : ""
		   var imgClass = (imgObj.className) ? "class='" + imgObj.className + "' " : ""
		   var imgTitle = (imgObj.title) ? 
						 "title='" + imgObj.title  + "' " : "title='" + imgObj.alt + "' "
		   var imgStyle = "display:inline-block;" + imgObj.style.cssText
		   var strNewHTML = "<span " + imgID + imgClass + imgTitle
					  + " style=\"" + "width:" + imgObj.width 
					  + "px; height:" + imgObj.height
					  + "px;" + imgStyle + ";"
					  + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
					  + "(src=\'" + imgObj.src + "\', sizingMethod='scale');\"></span>"
		   imgObj.outerHTML = strNewHTML	  
		}
	}
};

/*
----------------------------------------------------------
	Milou.Util.Cookie
		-Milou.Util.Cookie.Set(name,value,days)
		-Milou.Util.Cookie.Get(name)
		-Milou.Util.Cookie.Delete(name)
----------------------------------------------------------
*/
Milou.Util.Cookie = {
	Set: function (name,value,days) {
		if (days) {
		    var date = new Date();
		    date.setTime(date.getTime()+(days*24*60*60*1000));
		    var expires = "; expires="+date.toGMTString();
	    }
	    else var expires = "";
	    document.cookie = name+"="+value+expires+"; path=/";
	},
	Get: function (name) {
		var nameEQ = name + "=";
	    var ca = document.cookie.split(';');
	    for(var i=0;i < ca.length;i++) {
		    var c = ca[i];
		    while (c.charAt(0)==' ') c = c.substring(1,c.length);
		    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	    }
	    return null;
	},
	Delete: function (cookie_name) {
		var _ntx = ((new Date()).getTime()).toString();
		document.cookie = cookie_name+'=' + _ntx + '; path=/';
		document.cookie = cookie_name+'=; expires=' + (new Date(0)).toString() + '; path=/';
	}
};

/*
----------------------------------------------------------
	Milou.Util.General
		-Milou.Util.General.MailTo(strTo,strHost)
		-Milou.Util.General.GetStringToObject(strObject)
----------------------------------------------------------
*/
Milou.Util.General = {
	MailTo: function(strTo,strHost)
	{
		var ppclink = strTo;
		var ppchost = strHost; 
		document.write("<a href=" + "mail" + "to:" + ppclink + "&#64;" + ppchost + ">" + ppclink+ "&#64;" + ppchost + "</a>") 
	},
	GetStringToObject: function(strObject) {
		var convertString = strObject.slice(1, strObject.length - 1);
		var items = convertString.split(",");
		var obj = new Object();
		for(var i=0; i<items.length; i++)
		{
			var props = items[i].split(":");
			obj[props[0]] = props[1].slice(1, props[1].length - 1);
		}
		return obj;
	},
	log: function(strMessage) {
		if (!Milou.Util.General.log.window_ || Milou.Util.General.log.window_.closed) {
			var win = window.open("", null, "width=400,height=200," +
								  "scrollbars=yes,resizable=yes,status=no," +
								  "location=no,menubar=no,toolbar=no");
			if (!win) return;
			var doc = win.document;
			doc.write("<html><head><title>Debug Log</title><style>body{font-family: Arial, Verdana, Helvetica, sans-serif;font-size: 11px;}</style></head><body></body></html>");
			doc.close();
			Milou.Util.General.log.window_ = win;
		}
		var logLine = Milou.Util.General.log.window_.document.createElement("div");
		logLine.appendChild(Milou.Util.General.log.window_.document.createTextNode(strMessage));
		Milou.Util.General.log.window_.document.body.appendChild(logLine);
	}
};


/*
----------------------------------------------------------
	Milou.Util.DOM
		-Milou.Util.DOM.CenterElement(element, parent)
		-Milou.Util.DOM.LayerVisibility(strLayer,strAction)
		-Milou.Util.DOM.LayerDisplay(strLayer,strAction)
		-Milou.Util.DOM.CheckedValue(strRadioName)
		-Milou.Util.DOM.Watermark(objElement)
		-Milou.Util.DOM.CurrentPage();
		-Milou.Util.DOM.GetPage(strUrl);
----------------------------------------------------------
*/
Milou.Util.DOM = {
	LayerVisibility: function(strLayer,strAction) {
		var objLayer = $(strLayer);
	    if(objLayer)
	        objLayer.style.visibility = strAction;
    },
    LayerDisplay: function(strLayer,strAction) {
	    var objLayer = $(strLayer);
	    if(objLayer)
	        objLayer.style.display = strAction;
    },
    CheckedValue: function (strRadioName) {
	    var radioObj = document.getElementsByName(strRadioName);
	    if(!radioObj)
		    return "";
	    var radioLength = radioObj.length;
	    if(radioLength == undefined)
		    if(radioObj.checked)
			    return radioObj.value;
		    else
			    return "";
	    for(var i = 0; i < radioLength; i++) {
		    if(radioObj[i].checked) {
			    return radioObj[i].value;
		    }
	    }
	    return "";
    },
    Watermark: function(objElement) {
		if(objElement.value==objElement.defaultValue)
			objElement.value='';
		else if(objElement.value=='')
			objElement.value=objElement.defaultValue;
	},
	CurrentPage: function() {
		var strUrl = window.location.href;
		var strPage = strUrl.substr(strUrl.lastIndexOf('/')+1);
		strPage = strPage.substr(0,strPage.indexOf('.'))
		return strPage;
	},
	CurrentPageUrl: function() {
		var strUrl = window.location.href;
		var strPage = strUrl.substr(strUrl.lastIndexOf('/')+1);
		strPage = strPage.substr(0,strPage.indexOf('.')+5)
		return strPage;
	},
	CurrentUrl: function() {
		var strUrl = window.location.href;
		var strPage = strUrl.substr(strUrl.lastIndexOf('/')+1);
		return strPage;
	},
	CurrentUrlAddParam: function(strParams) {
		var strUrl = window.location.href;
		var strPage = strUrl.substr(strUrl.lastIndexOf('/')+1);
		if(strPage.indexOf(strParams) == -1)
		{
			if(strPage.indexOf("?") > -1)
				strPage = strPage + "&" + strParams;
			else
				strPage = strPage + "?" + strParams;
		}
		return strPage;
	},
	GoTo: function(strUrl) {
		document.location.href=strUrl;
	},
	SwapCssClass: function(objSrc,strNewClass) {
		if(objSrc)
			objSrc.className = strNewClass;
	}
};


/*
----------------------------------------------------------
	Milou.Util.Copy
		- Milou.Util.Copy.Set(strKey,strValue)
		- Milou.Util.Copy.Get(strKey)
----------------------------------------------------------
*/
Milou.Util.Copy = {
	initialize: function() {
		this.objCopy = new Hash();
	},
	Set: function(strKey,strValue) {
		this.objCopy.set(strKey,strValue);
    },
    Get: function(strKey) {
		return this.objCopy.get(strKey);
    }
}


/*
----------------------------------------------------------
	Milou.Util.Flash
		- Milou.Util.Flash.FsCommand(strFlashName)
----------------------------------------------------------
*/
Milou.Util.Flash = {
	FsCommand: function(strFlashName) {
		document.write('<script type="text/javascript">\n');
		document.write('var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;\n')
		document.write('function '+strFlashName+'_DoFSCommand(command, args)\n');
		document.write('{\n');
		document.write('var '+strFlashName+'Obj = InternetExplorer ? '+strFlashName+' : document.'+strFlashName+';\n');
		document.write('eval(command+"("+args+")")\n');
		document.write('}\n');
		document.write('</script>\n\n');
		
		if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) 
		{
			document.write('<SCRIPT LANGUAGE=VBScript\> \n');
			document.write('on error resume next \n');
			document.write('Sub '+strFlashName+'_FSCommand(ByVal command, ByVal args)\n');
			document.write(' call '+strFlashName+'_DoFSCommand(command, args)\n');
			document.write('end sub\n');
			document.write('</SCRIPT\> \n');
		}
	}
}

/*
----------------------------------------------------------
	Milou.Form.Validation
----------------------------------------------------------
*/
Milou.Form.Validation = Class.create();

Milou.Form.Validation.prototype = {
	initialize: function(objValidation) {
	    this.OnError = objValidation.OnError;
	    this.OnSuccess = objValidation.OnSuccess;
	    this.Element = new Array();
	    this.ElementIndex = 0;
	},
	AddElement: function(objNewElement) {
		var objTemp = new Object();
		objTemp.ValidationType = objNewElement.ValidationType;
		objTemp.ElementName = objNewElement.ElementName;
		objTemp.Message = objNewElement.Message;
		objTemp.Criteria = objNewElement.Criteria;
		this.Element[this.ElementIndex] = objTemp;
		this.ElementIndex++;
	},
	Validate: function() {
		for(var i=0; i<this.Element.size();i++)
		{
			var objElement = this.Element[i];
			var bolOnSuccess = eval("Milou.Form.Validation.Validate."+objElement.ValidationType+"(objElement.ElementName,objElement.Criteria)");
			
			if(!bolOnSuccess)
			{
				eval(this.OnError+"({Message:'"+objElement.Message+"',ElementName:'"+objElement.ElementName+"'})");
				return false;
			}
		}
		eval(this.OnSuccess+"()");
	}
}

Milou.Form.Validation.Validate = {
	Length: function(strElement,objCriteria) {
		if($F(strElement).length < objCriteria.Min || $F(strElement).length > objCriteria.Max)
			return false;
		else
			return true;
    },
	Empty: function(strElement,objCriteria) {
		if($F(strElement).empty())
			return false;
		else
			return true;
    },
    Email: function(strElement,objCriteria) {
		var strReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(strReg.test($F(strElement)) == false)
			return false;
		else
			return true;
    },
    Number: function(strElement,objCriteria) {
		var strReg = /^\d+$/;
		if(strReg.test($F(strElement)) == false)
			return false;
		else
			return true;
    }
}
