var IsIe  = (document.all && document.getElementById);
var IsMoz = (!document.all && document.getElementById);
var errorMessage = '';

function imageholderclass()
{
	this.over=new Array();
	this.down=new Array();
	this.src=new Array();
	this.store=store;
	
	function store(src, down, over){
		var AL=this.src.length;
		this.src[AL]=new Image(); this.src[AL].src=src;
		this.over[AL]=new Image(); this.over[AL].src=over;
		this.down[AL]=new Image(); this.down[AL].src=down;
	}
}

var ih = new imageholderclass();
var mouseisdown=0;

function preloader(t){
	for(i=0;i<t.length;i++){
		if(t[i].getAttribute('srcover')||t[i].getAttribute('srcdown')){
			
			storeimages(t[i]);
			var checker='';
			checker=(t[i].getAttribute('srcover'))?checker+'A':checker+'';
			checker=(t[i].getAttribute('srcdown'))?checker+'B':checker+'';
			
			switch(checker){
			case 'A' : mouseover(t[i]);mouseout(t[i]); break;
			case 'B' : mousedown(t[i]); mouseup2(t[i]); break;
			case 'AB' : mouseover(t[i]);mouseout(t[i]); mousedown(t[i]); mouseup(t[i]); break;
			default : return;			
			}
			
			if(t[i].src){t[i].setAttribute("oldsrc",t[i].src);}
		}
	}
}
function mouseup(t){
	var newmouseup;
	if(t.onmouseup){
		t.oldmouseup=t.onmouseup;
		newmouseup=function(){mouseisdown=0;this.src=this.getAttribute("srcover");this.oldmouseup();}

	}
	else{newmouseup=function(){mouseisdown=0;this.src=this.getAttribute("srcover");}}
	t.onmouseup=newmouseup;
}

function mouseup2(t){
	var newmouseup;
	if(t.onmouseup){
		t.oldmouseup=t.onmouseup;
		newmouseup=function(){mouseisdown=0;this.src=this.getAttribute("oldsrc");this.oldmouseup();}
		}
	else{newmouseup=function(){mouseisdown=0;this.src=this.getAttribute("oldsrc");}}
	t.onmouseup = newmouseup;
}

function mousedown(t){
	var newmousedown;
	if(t.onmousedown){
		t.oldmousedown=t.onmousedown;
		newmousedown=function(){if(mouseisdown==0){this.src=this.getAttribute("srcdown");this.oldmousedown();}}
	}
	else{newmousedown=function(){if(mouseisdown==0){this.src=this.getAttribute("srcdown");}}}
	t.onmousedown=newmousedown;
}

function mouseover(t){
	var newmouseover;
	if(t.onmouseover){
		t.oldmouseover=t.onmouseover;
		newmouseover=function(){this.src=this.getAttribute("srcover");this.oldmouseover();}
	}
	else{newmouseover=function(){this.src=this.getAttribute("srcover");}}
	t.onmouseover=newmouseover;
}

function mouseout(t){
	var newmouseout;
	if(t.onmouseout){
		t.oldmouseout=t.onmouseout;
		newmouseout=function(){this.src=this.getAttribute("oldsrc");this.oldmouseout();}
	}
	else{newmouseout=function(){this.src=this.getAttribute("oldsrc");}}
	t.onmouseout=newmouseout;
}

function storeimages(t){
	var s=(t.getAttribute('src'))?t.getAttribute('src'):'';
	var d=(t.getAttribute('srcdown'))?t.getAttribute('srcdown'):'';
	var o=(t.getAttribute('srcover'))?t.getAttribute('srcover'):'';
	ih.store(s,d,o);
}

function preloadimgsrc(){
	if(!document.getElementById) return;
	var it=document.getElementsByTagName('IMG');
	var it2=document.getElementsByTagName('INPUT');
	preloader(it);
	preloader(it2);
}

function addEvent(obj, evType, fn)
{ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, true); 
		return true; 
	} else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} else { 
		return false; 
	} 
}

function gEl(id)
{
	return document.getElementById(id);
}
function splashLogin()
{
	gEl('splash-login').style.display = 'block';
	if (gEl('nav-login') != null)
		updateClass('nav-login', 'selected');
	else
		setTimeout("splashLogin()", 1000);
		
}
function cleanIt(elm, isDef)
{
	if (elm.value == isDef)
	        elm.value = "";
}

function HighlightText(id)
{
	if (document.selection) 
	{
		var r1 = document.body.createTextRange();
		r1.moveToElementText(gEl(id));
		r1.setEndPoint("EndToEnd", r1);
		r1.moveStart('character', 0);
		r1.moveEnd('character', 1);
		r1.select();
	} else {
		s = window.getSelection();
		var r1 = document.createRange();
		r1.setStartBefore(gEl(id));
		r1.setEndAfter(gEl(id));
		s.addRange(r1);
	}
}
function textToPass(id)
{
	gEl('em_' + id).style.display = 'none';
	gEl(id).style.display = '';
	gEl(id).focus();
}
function updateClass(id, name)
{
	if (!name)
		name = 'txt';
	gEl(id).className = name;
}
	
if(window.addEventListener){window.addEventListener("load", preloadimgsrc, false);} 
else{
	if(window.attachEvent){window.attachEvent("onload", preloadimgsrc);}
	else{if(document.getElementById){window.onload=preloadimgsrc;}}
}




/* tools */

function showBox(boxId, olId)
{
	gEl(olId).style.height = document.body.offsetHeight + 'px';
	show(olId);
	show(boxId);
	center(boxId);
}
function hideBox(boxId, olId)
{
	gEl(olId).style.display = 'none';
	gEl(boxId).style.display = 'none';
}

function show(element) { gEl(element).style.display = 'block'; }
function hide(element) { gEl(element).style.display = 'none'; }

function center(element)
{
	element = gEl(element);

	var my_width  = getWindowWidth();
	var my_height = getWindowHeight();

	element.style.position = 'absolute';
	element.style.zIndex   = 201;

	var scrollY = getScrollXY()[1];

	var elementDimensions = getDimensions(element);

	var setX = ( my_width  - elementDimensions.width  ) / 2;
	var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;

	setX = ( setX < 0 ) ? 0 : setX;
	setY = ( setY < 0 ) ? 0 : setY;

	element.style.left = setX + "px";
	element.style.top  = setY + "px";

	element.style.display  = 'block';
}
function getDimensions (element) 
{	
	var display = element.style.display;
	if (display != 'none' && display != null) // Safari bug
		return { width: element.offsetWidth, height: element.offsetHeight};

	// All *Width and *Height properties give 0 on elements with display none,
	// so enable the element temporarily
	var els = element.style;
	var originalVisibility = els.visibility;
	var originalPosition = els.position;
	var originalDisplay = els.display;
	els.visibility = 'hidden';
	els.position = 'absolute';
	els.display = 'block';
	var originalWidth = element.clientWidth;
	var originalHeight = element.clientHeight;
	els.display = originalDisplay;
	els.position = originalPosition;
	els.visibility = originalVisibility;
	return {width: originalWidth, height: originalHeight};
}

function getElementPos(container) {
	var offsetY = 0;
	var offsetX = 0;
	if (container.getBoundingClientRect) {
		var rect = container.getBoundingClientRect();
		offsetY = rect.top - 2;
		offsetX = rect.left - 2;
		var z = getScrollXY();
		offsetY += z[1];
		offsetX += z[0];
	} else {
		var parentElem = container;
		while (parentElem &&
		       parentElem != document.body &&
		       parentElem != document.documentElement) {
			offsetY += parentElem.offsetTop;
			offsetX += parentElem.offsetLeft;
			parentElem = parentElem.offsetParent;
		}
	}
	return [offsetX, offsetY];
}

function getElementWidth(target) 
{
	if (target.clientWidth) return target.clientWidth;
	else if (target.innerWidth) return target.innerWidth;
	else if (target.offsetWidth) return target.offsetWidth;
	else return 0;
}
function getElementHeight(target) {
	if (target.clientHeight) return target.clientHeight;
	else if (target.innerHeight) return target.innerHeight;
	else if (target.offsetHeight) return target.offsetHeight;
	else return 0;
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}
function getWindowWidth() {
	if (window.innerWidth && window.innerWidth != 0)
		return window.innerWidth;
	if (document.documentElement.clientWidth && document.documentElement.clientWidth != 0)
		return document.documentElement.clientWidth;
	if (document.body.clientWidth && document.body.clientWidth != 0)
		return document.body.clientWidth;
	return 0;
}

function getWindowHeight() {
	if (window.innerHeight && window.innerHeight != 0)
		return window.innerHeight;
	if (document.documentElement.clientHeight && document.documentElement.clientHeight != 0)
		return document.documentElement.clientHeight;
	if (document.body.clientHeight && document.body.clientHeight != 0)
		return document.body.clientHeight;
	return 0;
}



/* validation */
var PhoneRegExp = /^[\d]+$/;
var DateNumRegExp = /^(\d{1,2}).(\d{1,2}).(\d{2,4})$/;
var DateRegExp = /^([a-zA-Z]{3,10}) +(\d{1,2})\, *(\d{4})$/;
var UrlRegEx = /^http(s)?\:\/\//;
function js_trim(_v) 
{
	_v = _v.replace(/^\s+/, '');
	return _v.replace(/\s+$/, '');
}
function validateEmail(str)
{
	var emailRegEx = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	if(str.match(emailRegEx))
		return true;
	return false;
}
function validateUrl(str)
{
	str = js_trim(str);
	if(str.match(UrlRegEx))
		return true;
	return false;
}

function validateDate(s_date) 
{
	// check format
	if (!DateRegExp.test(s_date))
		return false;

	// check allowed ranges	
	if (RegExp.$2 > 31 || RegExp.$3 < 2000 )
		return false;
	var mNum = -1;
	var _c = RegExp.$1;
	for (var i in em_Months)
	{
		if (_c.toLowerCase() == em_Months[i])
		{
			mNum = i;
			break;
		}
	}

	if (mNum < 0)
		return false;

	// check number of day in month
	var dt_test = new Date(Number(RegExp.$3), mNum, Number(RegExp.$2));
	if (dt_test.getMonth() != mNum)
		return false;
	return true;
}

function validateDateNum(s_date)
{
	// check format
	if (!DateNumRegExp.test(s_date))
		return false;

	// check allowed ranges	
	if (RegExp.$1 > 12 || RegExp.$2 > 31 )
		return false;
	
	// check number of day in month
	var dt_test = new Date(RegExp.$3, Number(RegExp.$1-1), RegExp.$2);
	if (dt_test.getMonth() != Number(RegExp.$1-1))
		return false;
	return true;
}
function validateDates()
{
	var _mnA = parseInt(gEl("dtMonthA").value);
	var _dyA = parseInt(gEl("dtDayA").value);
	var _yrA = parseInt(gEl("dtYearA").value);

	var _mnB = parseInt(gEl("dtMonthB").value);
	var _dyB = parseInt(gEl("dtDayB").value);
	var _yrB = parseInt(gEl("dtYearB").value);

	if ((_mnA != 0 && _dyA != 0 && _yrA !=0) && validateDateNum(_mnA + "/" + _dyA + "/" + _yrA) == false)
	{
		alert('Not valid From Date');
		return false;
	}
	if ((_mnB != 0 && _dyB != 0 && _yrB !=0) && validateDateNum(_mnB + "/" + _dyB + "/" + _yrB) == false)
	{
		alert('Not valid To Date');
		return false;
	}
	return true;
}
function validateDatesInterval(startDate, endDate)
{
	
	if (!endDate || endDate == '')
		return true;
	var s_date_exp = DateNumRegExp.test(startDate);
	var s_date = new Date(RegExp.$3, Number(RegExp.$1-1), RegExp.$2);
	
	var e_date_exp = DateNumRegExp.test(endDate);
	var e_date = new Date(RegExp.$3, Number(RegExp.$1-1), RegExp.$2);
	
	if (((s_date.getTime()) * 1) > ((e_date.getTime()) * 1))
		return false;

	return true;
}

function CheckIp()
{
	var ip = js_trim(gEl('ip').value);
	if (ip == '')
	{
		alert('Input IP');
		return;
	}
	
	gEl('loading').style.visibility = 'visible';
	var request = 'IsValidate=1&IsAjax=1&Ip=' + ip;
	AjaxRequestCall('/rbl_validate.php', request, 'dina-content', 'post', 'CheckIpCb')
}

function CheckIpCb()
{
	gEl('loading').style.visibility = 'hidden';
}


function doReg()
{
	var first_name = gEl('reg-first_name').value;
	var last_name = gEl('reg-last_name').value;
	var email = gEl('reg-email').value;
	
	if ( (js_trim(first_name) == '' && js_trim(last_name) == '') || js_trim(email) == '' )
	{
		var m = (errorMessage != '') ? errorMessage : 'Please Complete all fields'
		alert(m);
		return;
	}
	
	gEl('success-message').style.visibility='visible';
	
	var request = 'IsSubmit=1&IsAjax=1&first_name=' + first_name + '&last_name=' + last_name + '&email=' + email;
	AjaxRequestCall('/index.php', request, 'null', 'post', 'ThanksFor')
}


var thnksFade = 100;
function ThanksFor()
{
	var el = gEl('success-message');
	el.style.visibility='visible';
	
	if(IsIe)
		el.filters.alpha.opacity = 100;
	else
	{
		el.style.MozOpacity = 1;
		el.style.opacity = 1;
	}
	thnksFade = 100;
		
	gEl('reg-first_name').value = '';
	gEl('reg-last_name').value = '';
	gEl('reg-email').value = '';
	
	window.setTimeout("fadeOut('success-message')", 3000);
}

function fadeOut(elId)
{
	var el = gEl(elId)
	thnksFade -= 5;
	if (thnksFade < 0)
	{
		el.style.visibility='hidden';
		return;
	}
	if(IsIe) 
		el.filters.alpha.opacity = thnksFade;
	else
	{
		el.style.MozOpacity = thnksFade / 100;
		el.style.opacity = thnksFade / 100;
	}
		
	window.setTimeout("fadeOut('" + elId + "')", 50);
}

function PageInit()
{
	gEl('reg-last_name').focus();
	sendEvent('reg-last_name');
	sendEvent('reg-first_name');
	sendEvent('reg-email');
}

function sendEvent(id)
{
	gEl(id).onkeydown = function(e) 
	{
		e = (e) ? e : ((window.event) ? window.event : "");
		if ((e.which == 13) || (e.keyCode == 13)) // Return key
			doReg();
		return true;
	}
}
addEvent(window, 'load', PageInit);