// JavaScript Document
var standarderror	= 'Couldn\'t communicate with server, please try again later. If the problem persists contact the site administrator.';
var divarray 		= new Array();
var divcount 		= 0; 
var selector		= null;
var confirmselector = null;
var messageselector = null;
$(document).ready(function(){
	$(".close_notification").click( 
		function () {
			$(this).hide();
			$(this).parent().fadeTo('fast', 0, function () { 
				$(this).slideUp('fast');
			});
			return false;
		}
	);	
	$('#modal_close').click(function(){$.unblockUI()});
});
function waitElement(which,what){
	$('#tooltip').hide();
	if(!$('#'+which).is(':visible'))$('#'+which).show();
	var pos		= {
		align: 		$('#'+which).css("float"),
		position: 	$('#'+which).css("position"),
		left:		$('#'+which).css("left"),
		top: 		$('#'+which).css("top"),
		right:		$('#'+which).css("right"),
		bottom:		$('#'+which).css("bottom")
	}
	var style = '';
	if(pos.align!='none' && pos.align!='auto')style += 'float:'+pos.align+';';
	if(pos.position!='none' && pos.position!='auto')style += 'position:'+pos.position+';';
	if(pos.left!='none' && pos.left!='auto')style += 'left:'+pos.left+';';
	if(pos.top!='none' && pos.top!='auto')style += 'top:'+pos.top+';';
	if(pos.right!='none' && pos.right!='auto')style += 'right:'+pos.right+';';
	if(pos.bottom!='none' && pos.bottom!='auto')style += 'bottom:'+pos.bottom+';';
	divarray[which] = new Object();
	divarray[which].element = $('#'+which).clone(true);
	divarray[which].count = divcount++;
	if(!what)what = "wait_element";
	wait = "<div class='"+what+"' id='WAIT_"+divarray[which].count+"' align='absmiddle' style='"+style+"' />";
	$('#'+which).replaceWith(wait);
}
function restoreElement(which) {
	if(typeof(divarray[which])!='undefined'){
		$('#WAIT_'+divarray[which].count).replaceWith(divarray[which].element);
		delete divarray[which];
	}
}
function showWait(){
	$('.hdcms-loading').show();
}
function hideWait(){
	$('.hdcms-loading').hide();	
}
function waitOverlay(s){
	selector = s;
	$(selector).block({
			message: null,
			overlayCSS:{
				backgroundColor: '#ffffff',
				backgroundImage:  'url(images/loader.gif)',
				backgroundRepeat: 'repeat-x',
				backgroundPosition: '0px -8px',
				backgroundColor: 'transparent',
				borderWidth: '0px',
				opacity: 1
			}
	});
}

function removeWaitOverlay(selector){
	/*
	$(window).unbind('resize').unbind('scroll');
	$('#waitoverlay_holder').fadeOut('slow');	
	*/
	$(selector).unblock();
}
function clearerrors(){
	$('#jpopup_message').hide();	
}
function popupconfirm(s,message,callback,param){
	$('#hdcms-question a.hdcms-save').unbind('click');
	confirmselector = s;
	$('#hdcms-question strong').empty().append(message).show();
	var width	= $(selector).outerWidth();
	var height	= $(selector).outerHeight();
	var cwidth	= $('#hdcms-question').outerWidth();
	var cheight	= $('#hdcms-question').outerHeight();
	var l		= Math.round((width - cwidth)/2);
	var t		= Math.round((height - cheight)/2);
	$(confirmselector).block(
		{
			message: $('#hdcms-question'),
			css: {
				borderWidth: '0px',
				background: 'transparent',
				width: cwidth,
				height: cheight,
				left: l+'px',
				top: t+'px',
				cursor:'default'
			},
			overlayCSS: {
				opacity: 0.4	
			},
			fadeOut: 0
		}
	);
	$('#hdcms-question a.hdcms-save').bind('click',function(){
		cancelPopupConfirm();
		callback(param);														
	});
}



function popup(div,title,message){
	$('#'+div+' #modal_confirm').remove();
	$('#'+div+' #modal_title').empty().append(title);
	$('#'+div+' #modal_message').empty().append(message);
	width	= $('#'+div).outerWidth();
	$.blockUI({message: $('#'+div),css: {top: '100px',width:width,borderWidth: 0,cursor: 'default'},overlayCSS: {opacity: 0.7}});
}
function jpop(div,title,message,align,decay){
	$('#'+div).jpopup({title: title,message: message,align: align,decay:decay});
}
function confirmBox(msg,callback,param){
	$('#hdcms-question strong').empty().append(msg);
	width	= $('#hdcms-question').outerWidth();
	$('#hdcms-confirm-ok').bind('click',function(){
		$('#hdcms-confirm-ok').unbind('click');
		$.unblockUI();
		if(callback)callback(param);											 
	});
	$.blockUI({message: $('#hdcms-question'),
			   css: {borderWidth: 0,cursor: 'default',width: width},
			   overlayCSS: {opacity:0.7}
	});
}
function cancelModalConfirm(){
	$('#hdcms-confirm-ok').unbind('click');
	$.unblockUI();
	
}
function cancelPopupConfirm(){
	$(confirmselector).unblock();
	confirmselector = null;
}
function slideblock(sel,url,params){
	showWait();
	sel.empty().load(url,params,function(){hideWait()});
	$.blockUI({
		message: sel,
		css: {
			top:				'100px',
			left:				'0px',
			borderWidth: 		'0px',
			width:				'100%',
			textAlign: 			'center',
			backgroundColor: 	'transparent',
			cursor: 			'default'
		},
		overlayCSS:{
			backgroundColor: 'transparent'
		}
	});	
	sel.draggable({handle: '.hdcms-handle:eq(0)'});
}
function slideBlockClose(){
	$('#hdcms-modal').draggable('destroy');
	$.unblockUI();
}
function checkNumberNotBlank(id,title,msg,align,decay,sdiv){
	if(typeof(sdiv)=='undefined')sdiv = true;
	if($.trim($('#'+id).val())=='')return true;
	return checkNumber(id,title,msg,align,decay,sdiv);
}
function checkNumber(id,title,msg,align,decay,sdiv){
	if(typeof(sdiv)=='undefined')sdiv = true;
	if(isNaN($.trim($('#'+id).val()))){
		$('#'+id).jpopup({title: title,message: msg,align: align,decay:decay});
		$('#'+id).focus();
		if(sdiv)scrollDiv(id);
		return false;
	}
	return true;
}
function checIntegerNotBlank(id,title,msg,align,decay,sdiv){
	if(typeof(sdiv)=='undefined')sdiv = true;
	if($.trim($('#'+id).val())=='')return true;
	return checkInteger(id,title,msg,align,decay,sdiv);
}
function checkInteger(id,title,msg,align,decay,sdiv){
	if(typeof(sdiv)=='undefined')sdiv = true;
	var v = $.trim($('#'+id).val());
	if(!IsNumeric(v) || v%1 != 0 ){
		$('#'+id).jpopup({title: title,message: msg,align: align,decay:decay});	
		$('#'+id).focus();
		if(sdiv)scrollDiv(id);
		return false;
	}
	return true;
}
function checkNotBlankInteger(id,title,msg,align,decay,sdiv){
	if($.trim($('#'+id).val())!==''){
		return checkInteger(id,title,msg,align,decay);	
	}
	return true;
}
function checkNotBlankNumber(id,title,msg,align,decay,sdiv){
	if($.trim($('#'+id).val())!==''){
		return checkNumber(id,title,msg,align,decay,sdiv);	
	}
	return true;
}
function checkString(id,title,msg,align,decay,sdiv){
	if(typeof(sdiv)=='undefined')sdiv = true;
	if($.trim($('#'+id).val())==''){
		$('#'+id).jpopup({title: title,message: msg,align: align,decay:decay});	
		$('#'+id).focus();
		if(sdiv)scrollDiv(id);
		return false;
	}
	return true;
}
function checkStringLength(id,title,msg,align,decay,size,sdiv){
	if(typeof(sdiv)=='undefined')sdiv = true;
	if($.trim($('#'+id).val()).length<size){
		$('#'+id).jpopup({title: title,message: msg,align: align,decay:decay});	
		$('#'+id).focus();
		if(sdiv)scrollDiv(id);
		return false;
	}
	return true;
}
function checkStringLengthExact(id,title,msg,align,decay,size,sdiv){
	if(typeof(sdiv)=='undefined')sdiv = true;
	var lgt = $.trim($('#'+id).val()).length;
	if(lgt!=size){
		$('#'+id).jpopup({title: title,message: msg,align: align,decay:decay});	
		$('#'+id).focus();
		if(sdiv)scrollDiv(id);
		return false;
	}
	return true;
}
function checkEqual(id1,id2,title,msg,align,decay,sdiv){
	if(typeof(sdiv)=='undefined')sdiv = true;
	if($.trim($('#'+id1).val())!=$.trim($('#'+id2).val())){
		$('#'+id2).jpopup({title: title,message: msg,align: align,decay:decay});	
		$('#'+id2).focus();
		if(sdiv)scrollDiv(id);
		return false;
	}
	return true;	
}
function checkValidEmail(id,title,msg,align,decay,sdiv){
	if(typeof(sdiv)=='undefined')sdiv = true;
	if(!checkValidEmailNoError($.trim($('#'+id).val()))){
		$('#'+id).jpopup({title: title,message: msg,align: align,decay:decay});	
		$('#'+id).focus();
		if(sdiv)scrollDiv(id);
		return false;
	}
	return true;
}
function checkValidEmailNoError(string){
	var reg	= /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	return reg.test(string);
}
function checkTime(id,title,msg,align,decay){
	var reg	= /^((1[0-2])|([1-9])):[0-5][0-9](am|pm)$/i;
	var e = $.trim($('#'+id).val());
	if(!reg.test(e)){
		$('#'+id).jpopup({title: title,message: msg,align: align,decay:decay});	
		$('#'+id).focus();
		return false;
	}
	return true;
}
function checkTimeConsecutive(id,id2,title,msg,align,decay){
	var time1 = $.trim($('#'+id).val());
	var t1 = {
		hour:	time1.substring(0,time1.indexOf(':')),
		minute: time1.substring(time1.indexOf(':')+1,time1.length-2),
		ap:		time1.substring(time1.length-2,time1.length)
	}
	var time2 = $.trim($('#'+id2).val());
	var t2 = {
		hour:	time2.substring(0,time2.indexOf(':')),
		minute: time2.substring(time2.indexOf(':')+1,time2.length-2),
		ap:		time2.substring(time2.length-2,time2.length)
	}
	if(t1.hour==12){
		if(t1.ap=='am')t1.hour = 0;	
	}else if(t1.ap=='pm'){
		t1.hour += Number(12);	
	}
	if(t2.hour==12){
		if(t2.ap=='am')t2.hour = 0;	
	}else if(t2.ap=='pm'){
		t2.hour += Number(12);	
	}
	var tdate1 = new Date();
	tdate1.setHours(t1.hour);
	tdate1.setMinutes(t1.minute);
	var tdate2 = new Date();
	tdate2.setHours(t2.hour);
	tdate2.setMinutes(t2.minute);
	if(tdate1>=tdate2){
		$('#'+id).jpopup({title: title,message: msg,align: align,decay:decay});	
		$('#'+id).focus();
		return false;
	}
	return true;
}
function checkSelect(id,title,msg,align,decay,sdiv){
	if(typeof(sdiv)=='undefined')sdiv = true;
	var v	= $.trim($('#'+id).val());
	if(v==0 || v==''){
		$('#'+id).jpopup({title: title,message: msg,align: align,decay:decay});	
		$('#'+id).focus();
		if(sdiv)scrollDiv(id);
		return false;
	}
	return true;
}
function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
function roundNumber(val,places){
	factor = Math.pow(10,places);
	val *= factor;
	val	= Math.round(val)/factor;
	len	= val.toString().length-1;
	pos	= val.toString().indexOf('.');
	if(pos==-1){
		val +='.';
		for(i=0;i<places;i++)val+='0';
		return val;
	}
	num = len-pos;
	for(i=num;i<places;i++)val+='0';
	return val;
}
function scrollDiv(id){
	var offs = $('#'+id).offset();
	window.scrollTo(0,offs.top-100);
}
function urldecode (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +   improved by: Orlando
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +      bugfixed by: Rob
    // %        note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // %        note 2: Please be aware that this function expects to decode from UTF-8 encoded strings, as found on
    // %        note 2: pages served as UTF-8
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
    if(str)return decodeURIComponent(str.replace(/\+/g, '%20'));
	return '';
}
function urlencode (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Joris
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: This reflects PHP 5.3/6.0+ behavior
    // %        note 2: Please be aware that this function expects to encode into UTF-8 encoded strings, as found on
    // %        note 2: pages served as UTF-8
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
    str = (str + '').toString();

    // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
    // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}
/*
if(typeof(console)=='undefined'){
	var console = new Object();
	console.log	= function(msg){
		var op = console.recursmsg(msg);
		alert(op);
	}
	console.recursmsg = function(msg){
		var op = '';
		for(var i in msg){
			if(msg.hasOwnProperty(i)){
				if(typeof(msg[i])=='object'){
					op = op + '\n [ '+i+' ] => ';
					op = op + console.recursmsg(msg[i]);
				}else{
					op = op+'\n [ '+i+' ] '+msg[i];		
				}
			}
		}
		return op;
	}
}
*/
$(function (){
    $.ajaxSetup({
        xhr: function(){
            if ($.browser.msie){
                return new ActiveXObject("Microsoft.XMLHTTP");
            }else{
                return new XMLHttpRequest();
			}
      	}
	})
});
$.fn.hasAttr = function(attr){ 
	var attribVal = this.attr(attr); 
	return (attribVal !== undefined) && (attribVal !== false) && $.trim(attribVal)!=''; 
};
$.fn.outerHtml = function(){
    return $("<div>").append($(this).clone()).html();
}
