// JavaScript Document
function ce( t ) { return document.createElement( t ); }
function ge( t ) { return document.getElementById( t ); }
function insertAfter(node, referenceNode) {
  referenceNode.parentNode.insertBefore(node, referenceNode.nextSibling);
}
function isdefined( variable) {
	return (typeof(variable) == "undefined")?  false: true;
}
function isnumber( variable) {
	return (typeof(variable) == "number")?  false: true;
}
function detect() {
	var agent 	= navigator.userAgent.toLowerCase();
	// detect platform
	this.isMac		= (agent.indexOf('mac') != -1);
	this.isWin		= (agent.indexOf('win') != -1);
	this.isWin2k	= (this.isWin && (
			agent.indexOf('nt 5') != -1));
	this.isWinSP2	= (this.isWin && (
			agent.indexOf('xp') != -1 || 
			agent.indexOf('sv1') != -1));
	this.isOther	= (
			agent.indexOf('unix') != -1 || 
			agent.indexOf('sunos') != -1 || 
			agent.indexOf('bsd') != -1 ||
			agent.indexOf('x11') != -1 || 
			agent.indexOf('linux') != -1);
	
	// detect browser
	this.isSafari	= (agent.indexOf('safari') != -1);
	this.isSafari2 = (this.isSafari && (parseFloat(agent.substring(agent.indexOf("applewebkit/")+"applewebkit/".length,agent.length).substring(0,agent.substring(agent.indexOf("applewebkit/")+"applewebkit/".length,agent.length).indexOf(' '))) >=  300));
	this.isOpera	= (agent.indexOf('opera') != -1);
	this.isNN		= (agent.indexOf('netscape') != -1);
	this.isIE		= (agent.indexOf('msie') != -1);
	this.isFirefox	= (agent.indexOf('firefox') != -1);
	
	// itunes compabibility
	this.isiTunesOK	= this.isMac || this.isWin2k;
	
	this.getClientWidth = function() {
		return(window.innerWidth||
			  (document.documentElement && document.documentElement.clientWidth)||
			  (document.body && document.body.clientWidth)||
			  0);
	}
}
browser = new detect();	
browser.getClientHeight = function() {
	return(window.innerHeight||
		  (document.documentElement && document.documentElement.clientHeight)||
		  (document.body && document.body.clientHeight)||
		  0);
}
browser.getPageScrollTop = function() {
	return(document.documentElement && document.documentElement.scrollTop)||
		  (document.body && document.body.scrollTop)||
		  0;
}
browser.getPageScrollLeft = function() {
	return(document.documentElement && document.documentElement.scrollLeft)||
		  (document.body && document.body.scrollLeft)||
		  0;
}
browser.getPageSize = function() {
	var xScroll,yScroll;
	if(window.innerHeight && window.scrollMaxY)	{
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}	else if(document.body.scrollHeight > document.body.offsetHeight)	{
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}	else	{
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth,windowHeight;
	if(self.innerHeight)	{
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if(document.documentElement && document.documentElement.clientHeight) {
		windowWidth  = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if(document.body) {
		windowWidth  = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	var pageHeight,pageWidth;
	
	if(yScroll < windowHeight) pageHeight = windowHeight;
	else pageHeight = yScroll;
	
	if(xScroll < windowWidth) pageWidth = windowWidth;
	else pageWidth = xScroll;
	
	scrollleft = browser.getPageScrollLeft();
	scrolltop = browser.getPageScrollTop();
	return { pageWidth:pageWidth, pageHeight:pageHeight, 
			 windowWidth:windowWidth, windowHeight:windowHeight, 
			 scrollLeft:scrollleft, scrollTop:scrolltop};
}
browser.getPosition = function( element ) 	{
	var l = t = 0;
	while( element != null ) {
		l += element.offsetLeft;
		t += element.offsetTop;
		element = element.offsetParent
	}
	return {left:l, top:t};
};

//	Drag
function drag(o)	{		

	var src = o.src || '';
	var identy = o.identy || '';
	if( !src ) return;
	if( identy == '' ) return;
	var draggable = o.draggable || src;
	
	var draginit = function(e) {
		var htype = '-moz-grabbing';
		if (e == null) { e = window.event; htype = 'move';} 
		var target = e.target != null ? e.target : e.srcElement;
		 target = (target.nodeType == 1 || target.nodeType == 9) ? target : target.parentNode;
		//cursor = target.style.cursor;
		if ( target.className == identy || target.id == identy ) {
			//target.style.cursor = htype;
			target = draggable;
			dragging = true;
			dragXoffset = e.clientX - parseInt(draggable.style.left);
			dragYoffset = e.clientY - parseInt(draggable.style.top);
			src.onmousemove = function(e) {
				if (e == null) { e = window.event } 
				  if (e.button <=1 && dragging ) {
					 draggable.style.left = e.clientX - dragXoffset+'px';
					 draggable.style.top = e.clientY - dragYoffset+'px';
					 return false;
				  }
			}
			src.onmouseup = function() {
				src.onmousemove = null;
				src.onmouseup = null;
				//src.style.cursor = 'move';
				dragging = false;
			}
			return false;
		}
	}
	
	if(browser.isIE) src.attachEvent("onmousedown", draginit);
	else src.addEventListener("mousedown", draginit, false);
}	
//	Popup creater.
function dialog(o) {
	
		var g_template_img = baseurl+'sites/all/themes/zecurion/images/';
		o = o || {};
		var page = browser.getPageSize();

		var title = o.title || ''; 
		var width = o.width || 400;
		var height = o.height || 300;
		var ismodel = o.ismodel || false;
		var isdrag = o.isdrag || false;
		var onclose = o.onclose || 'dialog.close()';
		var content = o.content || '<span class="warnning" >Loading...</span>';
		var left = o.left || ((page.windowWidth/2) - (width/2));
		var top = o.top || ((page.windowHeight/2) - (height/2));
		var popupBgClass = !isdefined( o.bgclass ) ? 'content-area' : o.bgclass;
		this.loaded = false;
		
		left = left + page.scrollLeft;
		top  = top + page.scrollTop;
		
		bgid = 'dialog_bg_'+Math.random();
		pop  = 'dialog_pop_'+Math.random();
		popcontainer  = 'dialog_container_'+Math.random();	
		if( ismodel == true ) {	//	Gray background
			this.bg = ce('div');		
			this.bg.id = bgid;							
			this.bg.style.width = page.pageWidth; 
			this.bg.style.height = page.pageHeight; 
			this.bg.className = (browser.IsSafari) ? "popupBackgroundSafari" : "popupBackground";
			hideAllElement('select', this.bg, 1);
			document.body.appendChild(this.bg); 
		}
		//	window
		/*this.table = ce("TABLE");
		this.tbody = ce("TBODY");
		this.tr = ce("TR");
		this.td = ce("TD");*/

		this.win = ce('div');
		this.win.id = pop;
		this.win.className = 'dialog';
		this.win.style.width = width+'px';		
		//this.win.style.height = height+'px';
		this.win.style.left = left +'px';		
		this.win.style.top = top +'px';
		this.win.style.position = "absolute";		
		//	title bar
		this.tit = ce('div');
		this.tit.style.width = width+'px';
		this.tit.id = 'titlebar';			
		var header = '<div class="close"><a href="javascript:'+onclose+';" ><img id="popup_close_button" src="'+g_template_img+'btn_popupclose.gif" border="0"></a></div>';	
		this.tit.innerHTML = header;
		this.win.appendChild(this.tit);
		
		//Inner Window	
		this.innerwin = ce('div');
		this.win.appendChild(this.innerwin);	
		//this.innerwin.className = 'content-area';
		this.innerwin.className = popupBgClass; 

		//	title
		this.titlediv = ce('div');
		this.titlediv.className = 'title';
		this.innerwin.appendChild(this.titlediv);	
		this.titlediv.innerHTML = title;
		this.br = ce('br');
		this.br.className = 'clear';
		this.innerwin.appendChild(this.br);	
		//	Content area		
		this.con = ce('div');
		this.con.id = popcontainer;
		this.con.style.width = width+'px';
		this.con.style.height = height+'px';
		this.innerwin.appendChild(this.con);	
		//this.con.className = 'content-area';

		this.con.innerHTML = content;
		//this.con.onresize = function() {};

		this.show = function() {
			hideAllElement('select', this.win);
			document.body.appendChild(this.win);
			$('#popup_close_button').attr('src', g_template_img+'btn_popupclose.gif');
			if( isdrag ) {
				this.tit.style.cursor = 'move';
				this.drag = new drag( {src:this.tit, draggable:this.win, identy:this.tit.id} )
			}
		}

		this.sethtml = function(html) {
			$(this.con).css('height','auto');
			$(this.con).html(html);
			this.loaded = true;
			this.resizeDialog();
			//this.resetHeight( parseInt($(this.con).offsetHeight) );
		}
		this.setHeight = function(height){
			if(!browser.isIE){
				height = this.con.offsetHeight + parseInt(height);					
				$(this.con).css('height', height + 'px'); 
			}
		}
		this.resizeDialog = function(){				
			$(this.con).css('height','auto');
			($(this.con).attr('offsetHeight'));
			this.resetHeight( parseInt($(this.con).attr('offsetHeight')) );
		}
		this.resetHeight = function(height){
			height = parseInt(height);					
			$(this.con).css('height', height + 'px'); 
		}
		this.resize = function() {
			if(!browser.isIE){	
				if( this.con.clientHeight < this.win.clientHeight )
					$(this.con).css('height', this.win.clientHeight);
			}
		}

		this.gethtml = function() {
			return this.con.innerHTML;
		}		
		this.close = function() {
			if( this.loaded ) {
				showAllElement('select');
				if( this.bg ) this.bg.parentNode.removeChild(this.bg);
				if( this.win ) this.win.parentNode.removeChild(this.win);
			}
		}

}	


/*
 * hides give objects (for IE only)
 */
function hideAllElement( elmID, overDiv , greypopup) {
	if( document.all )	{
		for( i = 0; i < document.all.tags( elmID ).length; i++ )	{
			thispopup = 0;
			obj = document.all.tags( elmID )[i];
			
			if( !obj || !obj.offsetParent ) continue;
			// Find the element's offsetTop and offsetLeft relative to the BODY tag.
			objLeft   = obj.offsetLeft;
			objTop    = obj.offsetTop;
			objParent = obj.offsetParent;
			while( objParent.tagName.toUpperCase() != "BODY" )	{
				objLeft  += objParent.offsetLeft;
				objTop   += objParent.offsetTop;
				objParent = objParent.offsetParent;
				//alert(objParent.className);
				//if(objParent.id == overDiv.id){
					thispopup = 1;	
					break;
				//}
				
			}
			
			//if(thispopup == 1)	continue;
			if(greypopup){
				obj.style.visibility = "hidden";}
			else{
				objHeight = obj.offsetHeight;
				objWidth = obj.offsetWidth;
				if(( overDiv.offsetLeft + overDiv.offsetWidth ) <= objLeft );
				else if(( overDiv.offsetTop + overDiv.offsetHeight ) <= objTop );
				else if( overDiv.offsetTop >= ( objTop + objHeight ));
				else if( overDiv.offsetLeft >= ( objLeft + objWidth ));
				else	obj.style.visibility = "hidden";
			}
		}
	}
}
 
/*
 * unhides give objects (for IE only)
 */

function showAllElement( elmID )	{
	if( document.all )	{
		for( i = 0; i < document.all.tags( elmID ).length; i++ ) {
			obj = document.all.tags( elmID )[i];				
			if( !obj || !obj.offsetParent )
				continue;
			obj.style.visibility = "";
		}
	}
}

//	Trim
function trim(str)	{
	if(typeof(str) != "string" ) return "";
	var	str = str.replace(/^\s\s*/, ''),
	ws = /\s/,
	i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
	
	/* Function will remove the left and right white spaces within the string */
	return pmString.replace( /^ +/, "" ).replace( / +$/, "" );
}

//#-- Check valid email
function isValidEmail(pmEmail)	{
	/* Function will check whether the given email is valid or not. */
	if (!pmEmail) return false;
	pmEmail = trim(pmEmail);
	pmEmail = pmEmail.replace(/\r\n|\r|\n/g, ''); 
	
	if (isRegExpSupported()) {
		var vPattern = "^[A-Za-z0-9](([_\\.\\-]?[a-zA-Z0-9_]+)*)@([A-Za-z0-9]+)(([\\_.\\-]?[a-zA-Z0-9]+)*)\\.([A-Za-z]{2,})$";
		var vRegExp = new RegExp(vPattern);
		return (vRegExp.test(pmEmail));
	} else	{
		if(pmEmail.indexOf('@') == -1 || pmEmail.indexOf('.') == -1 || pmEmail.indexOf(' ') != -1) return false;
		else {
			var vSplit = pmEmail.split("@");
			if(vSplit.length > 2) return false;
			else 	{
				var vDomain = vSplit[1].split('.');
				var vLength = vDomain.length;
				for(var vLoop = 0; vLoop < vLength; vLoop++)
					if(vDomain[vLoop].length <= 0)	return false;
				return true;
			}
		} 
	}
}

function isRegExpSupported() {
	/* Function will check whether the regular expression supported by the browser */
	if (window.RegExp)	{
		var vTempStr = "a";
		var vTempReg = new RegExp(vTempStr);
		return (vTempReg.test(vTempStr));
	}
	return false;
} 
//#-- Added by kamalakannan

function hide_reg_msg()
{
	$("#com_err").html('');$("#reg_err").css("opacity","1");
	$("#com_err").hide();
}

function check_reg()
{ 
	
	if($("#fname").val()=="")
	{
		$("#reg_err").html("Please enter First name");
		$("#reg_err").show();
		$("#edit-fname").focus();
	//	$("#reg_err").fadeTo(4000, 0,hide_reg_msg);
		return false;
	}
	else if($("#edit-lname").val()=="")
	{
		$("#reg_err").html("Please enter Last name");
		$("#reg_err").show();
		return false;
	}
		else if($("#edit-email").val()=="")
	{
		$("#reg_err").html("Please enter email address");
		$("#reg_err").show();
		return false;
	}
	else if($("#edit-pwd").val()=="")
	{
		$("#reg_err").html("Please enter password");
		$("#reg_err").show();
		return false;
	}
	else if($("#edit-cpwd").val()=="")
	{
		$("#reg_err").html("Please enter confirm password");
		$("#reg_err").show();
		return false;
	}
	else if($("#edit-pwd").val() != $("#edit-cpwd").val())
	{
		$("#reg_err").html("Password mismatch");
		$("#reg_err").show();
		return false;
	}
	else
	{
		$("#reg_err").html("");
		return true;
	}
}

function imgmaxdim(img, max)
{
	if( !isdefined( img.tim ) ) img.tim = 0;
	if( img.tim < 10 && img.width == 0 )
	{
		img.tim++;
		setTimeout( function() { imgmaxdim( img, max ); }, 1000 );
		return;
	}
	var r = 1;
	var left = parseInt( img.offsetLeft + 0 );
	var top = parseInt( img.offsetTop + 0 );
	if( !isdefined( img.nheight ) )
	{
		img.nheight = img.height;
		img.nwidth = img.width;
	}
	var w = img.nwidth;
	var h = img.nheight;
	if( w == -1 )
	{
		debug( 'reload', img.width );
		setTimeout( function() { imgmaxdim( img, max ) }, 1000 );
		return;
	}
	var m = w > h ? w : h;
	if( m > max ) r = max/m;

	img.width = Math.floor( w*r );
	img.height = Math.floor( h*r );
	var w2 = img.width;
	var h2 = img.height;
	var m2 = w2 > h2 ? w2 : h2;
	if( m2 > max )
	{
//		debug( 'force' );
		img.style.width = Math.floor( w*r );
		img.style.height = Math.floor( h*r );
	}
//	debug( img.src, w, h, r, 'x', img.width, img.height );
	if( left <= 0 ) 
	{
		img.style.left = Math.floor((max-img.height)/2);
		img.style.left = 0;
	}
	if( top < 0 )
	{
		img.style.position = 'relative';
		img.style.top = 0;
	}
	var o = img.parentNode;
	while( o != document.body && o.parentNode != null && o.onmouseover == null ) o = o.parentNode;
	if( isdefined( o.onmouseover ) ) o.title = img.title;
	img.isloaded = true;
}
