/* UI */
function initmenu(menuid){
	var suboffset=-25;
	var uls=document.getElementById(menuid).getElementsByTagName("ul");
	for (var t=0; t<uls.length; t++){
		uls[t].parentNode.onmouseover=function(){
			this.getElementsByTagName("ul")[0].style.left=this.parentNode.offsetWidth+suboffset+"px";
			this.getElementsByTagName("ul")[0].style.display="block";
		}
		uls[t].parentNode.onmouseout=function(){
			this.getElementsByTagName("ul")[0].style.display="none";
		}
	}
}
function openWin(url,h,w){
	if(h==undefined) h = 300;
	if(w==undefined) w = 600;
	return window.open(url, '', 'menubar=0,resizable=1,width='+w.toString()+',height='+h.toString());	
}
function showDialog(_urlOrHtml, _title, _width, _onHideHandler){
	if(_onHideHandler == undefined || _onHideHandler == null) 
		_onHideHandler = function(){};
	Modalbox.show(_urlOrHtml, {title: _title, width: _width, autoFocusing: false, afterHide: _onHideHandler});
}
function showMessage(msg, onHide){
	showDialog('<div><p>'+msg+'</p><br /><div align="right"><button class="button" onclick="Modalbox.hide();">OK</button></div></div>', "Message", 400, onHide); 
}
function showErrorMessage(msg, onHide){
	var s = '<table style="width: 100%;"><tr><td valign="top"><img src="image/erricon47.gif" /></td><td valign="top"><p>'+msg+'</p></tr></table><br /><div align="right"><button class="button" onclick="Modalbox.hide();">OK</button></div></div>';
	showDialog(s, "Error", 400, onHide); 
}
function showVidPreview(id){ 
	showDialog("dialog/videoplayer.php?id="+id, "Video", 450); 
}
function showUserHistory(id){ 
	showDialog("dialog/userhistory.php?id="+id, "User History", 400); 
}
function showHelp(topicName){
	var helpwin = window.open('help/help.php?helppage=' + topicName, 'helpwin', 'menubar=0,resizable=0,width=425,height=450');
	try{helpwin.moveTo(0,0);}catch(e){}
}
function previewImage(url){
		var d = new Element("div", { id: 'evp_imgoverlay', style: 'display: none' });
		var img = new Element("img");
		img.onload=function(){
			d.insert(img);
			$(document.body).insert(d);
			doOverlay();
			var r = document.viewport.getDimensions();
			var r2 = d.getDimensions();
			var x = (r.width - r2.width) / 2;
			var y = (r.height - r2.height) / 2;
			d.setStyle({ top: y+'px', left: x+'px' });
			d.onclick=function(){
				$("evp_imgoverlay").remove();
				killOverlay();
			}		
			Effect.Grow('evp_imgoverlay', { duration: 0.5 });
		}
		img.src = url;
}
function doOverlay() {
	if($("evp_overlay") == undefined){
		var ov = new Element("div", { id: 'evp_overlay' });
		ov.setOpacity(0.7);
		if(Prototype.Browser.IE && !navigator.appVersion.match(/\b7.0\b/)) { 
			$$('html, body').invoke('setStyle', {width: "100%", height: "100%", overflow: "hidden"});
			$$("select").invoke('setStyle', {'visibility': "hidden"});
		}
		$(document.body).insert(ov, 'below');
	}
}
function killOverlay(){
	if($("evp_overlay") != undefined){
		if(Prototype.Browser.IE && !navigator.appVersion.match(/\b7.0\b/)) { 
			$$('html, body').invoke('setStyle', {width: "", height: "", overflow: "auto"});
			$$("select").invoke('setStyle', {'visibility': "visible"});
		}
		$("evp_overlay").remove();
	}
}
function toggleExpandArea(img, id){
	if($(id).visible()){ img.src = "image/expand.gif"; }
	else{ img.src = "image/collapse.gif"; }
	Effect.toggle(id, 'blind', { duration: 0.3 });
}
function showElements(ids){
	for(var i = 0; i < ids.length; i++)
		$(ids[i]).show();
}
function hideElements(ids){
	for(var i = 0; i < ids.length; i++)
		$(ids[i]).hide();
}
function disableFields(ids){
	for(var i = 0; i < ids.length; i++){
		if($(ids[i]) != undefined) 
			$(ids[i]).disabled=true;
	}
}
function enableFields(ids){
	for(var i = 0; i < ids.length; i++){
		if($(ids[i]) != undefined) 
			$(ids[i]).disabled=false;
	}
}
function setSelect(id, matchVal){
	var sel = $(id);
	for(var i = 0; i < sel.length; i++){
		if (sel[i].value == matchVal){
			sel[i].selected = true;
			break;
		}
	}
}

/* AJAX */
function showProgress(){
	var l = $("loaderDiv");
	if(l != undefined){
		var r = document.viewport.getDimensions();
		var r2 = l.getDimensions();
		var x = (r.width - r2.width) / 2;
		var y = (r.height - r2.height) / 2;
		l.setStyle({ top: y+"px", left: x+"px" });
		l.style.display = "block";
	}
}
function hideProgress(){
	$("loaderDiv").style.display = "none";
}
function callAjaxFunction(parameters, _onSuccess, _onFailure, progressBar){
	if(progressBar == undefined) progressBar = $("loaderDiv");
	new Ajax.Request('/includes/ajaxfunctions.php', {
			parameters: parameters, 
			method: 'post', 
			onSuccess: function(transport) { if(_onSuccess) _onSuccess(transport); }, 
			onFailure: function(transport) { if(_onFailure) _onFailure(transport); }, 
			onCreate: function() { showProgress(); }, 
			onComplete: function() { hideProgress(); }, 
			onException: function() { hideProgress (); }
	});
}
function postForm(formId, _onSuccess, _onFailure, progressBar){
	if(progressBar == undefined) progressBar = $("loaderDiv");
	new Ajax.Request('/includes/ajaxPosts.php', {
			parameters: $(formId).serialize(), 
			method: 'post', 
			onSuccess: function(transport) { if(_onSuccess) _onSuccess(transport); }, 
			onFailure: function(transport) { if(_onFailure) _onFailure(transport); }, 
			onCreate: function() { showProgress(); }, 
			onComplete: function() { hideProgress(); }, 
			onException: function() { hideProgress (); }
	});
}

/* UTIL */
function isValidEmail(email){
	var regex = /^((\w|[-_])+(\w|[-_.])*(\w|[-_])+|(\w|[-_]){1})@((\w|[-])+(\w|[-.])*(\w|[-])+|(\w|[-]){1})\.[a-zA-Z]{2,}$/gi;
	return regex.test(email);
}
function isValidPassword(pwd){
	// min 6 chars, no spaces, alphamumeric or $,#,@
	var regex = /^([a-zA-Z0-9@#$]{6,})$/g;
	return regex.test(pwd);
}
function isValidDateString(dateString){
	return !(isNaN(Date.parse(dateString)));
}
function showImagePicker(vidID,outletID,netVidID,netOutletID){
	document.getElementById('gridarea').style.display="none";
	document.getElementById('imagePicker').style.display="block";
	document.getElementById('searcharea').style.display="none";
	
	var so = new SWFObject("flash/EVPSnapDefault.swf", "", "600", "670", "9");
	so.addVariable("oid", outletID ); 
	so.addVariable("vid", vidID);
	so.addVariable("netoid", netOutletID ); 
	so.addVariable("netvid", netVidID);	
	so.addVariable("isDev", "0");
	so.addParam("allowscriptaccess", "always");	
	so.addParam("wmode", "transparent");	
	so.write("imagePicker");		
}
function hideImagePicker(){
	document.getElementById('gridarea').style.display="block";
	document.getElementById('imagePicker').style.display="none";
	document.getElementById('imagePicker').innerHTML="";
	document.getElementById('searcharea').style.display="block";
}