﻿
var extension = false;
var setwindowId = -1;
var __oldAction;
var __oldPostBack;

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{   
	    if (GetCookie(oRoot.id + "x") != null)
	    {
	        oRoot.style.left = GetCookie(oRoot.id + "x");
	        oRoot.style.top = GetCookie(oRoot.id + "y");
	    }
	    
	    
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		
		SetCookie(Drag.obj.root.id + "x",nx);
		SetCookie(Drag.obj.root.id + "y",ny);
		
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
				
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
  endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
    return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}

function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = new Date();
  var path = "/";
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  
  expires.setFullYear(expires.getFullYear() + 2);
  
  document.cookie = name + "=" + escape (value) +
   ((expires == null) ? "" : ("; expires=" + expires.toGMTString()));
}

function SetWindowSize(oRoot, oContent, height, width, cheight, cwidth){
            
            
    if (GetCookie(oRoot.id + "windowSize") != null)
    {
        if (GetCookie(oRoot.id + "windowSize") == "closed")
        {   // Open
            oRoot.style.height = GetCookie(oRoot.id + "windowSizeHeight");
            oRoot.style.width = GetCookie(oRoot.id + "windowSizeWidth");
            
            oRoot.style.setAttribute("height",GetCookie(oRoot.id + "windowSizeHeight"));
            oRoot.style.setAttribute("width",GetCookie(oRoot.id + "windowSizeWidth"));
            
            oContent.style.visibility = "visible";
            
            SetCookie(oRoot.id + "windowSize","open");
        }
        else
        {   // Close
            var oheight = oRoot.style.height;
            var owidth = oRoot.style.width;
            
            oRoot.style.height = cheight;
            oRoot.style.width = cwidth; 
            
            oRoot.style.setAttribute("height",cheight);
            oRoot.style.setAttribute("width",cwidth);
            
            oContent.style.visibility = "hidden";
            
            SetCookie(oRoot.id + "windowSize","closed");
            SetCookie(oRoot.id + "windowSizeHeight",oheight);
            SetCookie(oRoot.id + "windowSizeHeight",owidth);
        }
    }
    else
    {
         // Close
            var oheight = oRoot.style.height;
            var owidth = oRoot.style.width;
            //var coheight = oRoot.style.height;
            //var cowidth = oRoot.style.width;
            
            oRoot.style.height = cheight;
            oRoot.style.width = cwidth;
            oRoot.style.setAttribute("height",cheight);
            oRoot.style.setAttribute("width",cwidth);
            
            oContent.style.visibility = "hidden";
            
            SetCookie(oRoot.id + "windowSize","closed");
            SetCookie(oRoot.id + "windowSizeHeight",oheight);
            SetCookie(oRoot.id + "windowSizeHeight",owidth);
    }   

}

function SetWindowSizeInit(oRoot, oContent, height, width, cheight, cwidth){
    
    
     
    if (GetCookie(oRoot.id + "windowSize") != null)
    {
        if (GetCookie(oRoot.id + "windowSize") == "closed")
        {
            oRoot.style.height = cheight;
            oRoot.style.width = cwidth;
            oRoot.style.setAttribute("height",cheight);
            oRoot.style.setAttribute("width",cwidth);

            oContent.style.visibility = "hidden";
        }
    }
    
}



function changeext()
{
	if (extension)
		extension = false;
	else
		extension = true;
	
	document.frm.SMS.focus();
}

function replChars(tArea, aArray)
// this function replaces given characters in a textarea with an alternative (sequence of) character(s)
//
// tArea : the textarea
// aArray: an Array that contains pairs of a character to be replaced and the replacement string. 
{


	var iMax = aArray.length / 2;
	var reg = "";
	var sNew = tArea.value;
	
	// building the regular expression to check if one if the characters is present
	for (var i = 0; i < iMax; i++) {
		if (i > 0) 
			reg = reg + '|'; 
		reg = reg + aArray[i * 2];
	}
	
	reg = '(' + reg + ')';
	re = new RegExp(reg, 'g');		// this new regular expression does a global match (takes all)
		
	sNew = tArea.value.replace(re, 

		function (str, p1) 
		// this function is executed for every occurrence of a funny character. It loops
		// through the array to search for the replacement.
		{
			var strToCheck = "";
			for (i = 0; i < iMax; i++) {
				
				// only check the last character in the string. There might be a '\' in front of the character that has been used
				// for the regular expression. We don't need it now
				strToCheck = aArray[i * 2].substr(aArray[i * 2].length - 1)
					
				if (p1 == strToCheck)	// if this is the non SMS character return the alternative
					return aArray[i * 2 + 1];
			} 
		} ); 
		
	if (sNew != tArea.value)	// this rewrites the textarea, but only when the content has changed.
		tArea.value = sNew;
}
function resetTextCounter(karBox)
{
    karBox.value = '';
}
function smsCheckMsg(smsBox, karBox, refresh)
{
	if ((setwindowId != -1) && (refresh))
    {
	    clearTimeout(setwindowId);
	    setwindowId = -1;
	}

	
	// maximum length is the default value of karBox. In case of invalid value, 160 will be used.
	var maxLen = 160;
	if ((!isNaN(Number(karBox.defaultValue))) && (Number(karBox.defaultValue) >= 1)) {
		maxLen = parseInt(karBox.defaultValue, 10);
	}
		
   // replace tabs with spaces
	if (smsBox.value.search(/\t/) != -1)
		smsBox.value = smsBox.value.replace(/\t/, ' ');
		
	var corr = 0;   // correction value for CRLF and extension characters
	
	// count number of CRLF and set correction value
	//var ncrlf = smsBox.value.match(/\n/g);
	//if (ncrlf != null)
	//	corr = ncrlf.length;

	// replace non SMS characters by an equivalent or by nothing if no similar character exists in the GSM set
	//   first replace the funny characters that are similar to the ones in the extended character set
	
	var no = smsBox.value.match(/%SHORTCODE%/g);
	if (no != null) 
		corr = corr + no.length * 7;
	
	
	
	
	replChars(smsBox, ['¦', '|', 'ˆ', '^', '˜', '~']);

	if (extension) {
	
		// if the extended character set is used then the value of 'corr' has to be adjusted because an extended character
		// uses the space of two characters
		
		no = smsBox.value.match(/\[|\]|\^|\{|\}|~|\\|\||€/g);	// count number of extended characters
		if (no != null)
			corr = corr - no.length;									// adjust correction value if necessary
			
	} else {
	
		// if the extended character set is not used then those characters must be replaced
		replChars(smsBox, ["\\[", "(", "\\]", ")", "\\^", "", "\\[", "(", "\\]", ")", "~", "", "\\\\", "/", "\\|", "!", "€", "E"]);
	}
	
	// replace the characters that don't exist in both standard and extended GSM character set
	replChars(smsBox, ["`", "'", "", " ", "", " ", "‚", ",", "ƒ", "f", "„", "\"", "…", "...", "†", "+", "‡", "+", "‰", "",
		"Š", "S", "‹", "<", "Œ", "OE", "", "", "Ž", "Z", "", "", "", "", "‘", "'", "’", "'", "“", "\"", "”", "\"", "•", "*", 
		"–", "-", "—", "-", "™", "tm", "š", "s", "›", ">", "œ", "oe", "", "", "ž", "z", "Ÿ", "Y", " ", " ", "¢", "c", "¨", "\"",
		"©", "(c)", "ª", "a", "«", "<", "¬", "-", "®", "(r)", "¯", "-", "°", "", "±", "", "²", "2", "³", "3", "´", "'", "µ", "u",
		"¶", "", "·", "*", "¸", ",", "¹", "1", "º", "", "»", ">", "¼", "1/4", "½", "1/2", "¾", "3/4", "À", "A", "Á", "A", "Â", "A",
		"Ã", "A", "È", "E", "Ê", "E", "Ë", "E", "Ì", "I", "Í", "I", "Î", "I", "Ï", "I", "Ð", "D", "Ò", "O", "Ó", "O", "Ô", "O",
		"Õ", "O", "×", "x", "Ù", "U", "Ú", "U", "Û", "U", "Ý", "Y", "Þ", "", "á", "a", "â", "a", "ã", "a", "ç", "c", "ê", "e",
		"ë", "e", "í", "i", "î", "i", "ï", "i", "ð", "d", "ò", "o", "ó", "o", "ô", "o", "õ", "o", "÷", "", "ú", "u", "û", "u",
		"ý", "y", "þ", "", "ÿ", "ij"]);
		
	// replace occurences of multiple spaces by one space
	if (smsBox.value.search(/(\s{2,})/) != -1) {
		smsBox.value = smsBox.value.replace(/(\s{2,})/g, ' ');
	}
	

	smsBox.value.replace(/(hoi)/g, ' ');
	
	// remove text that exceeds maximum length
	if (smsBox.value.length > maxLen + corr)
		smsBox.value = smsBox.value.substr(0, maxLen + corr);
	
	
		
	// set the value of remaining characters	
	karBox.value = maxLen - smsBox.value.length + corr;
	
	if (refresh)
	{
	    SetNormalTimeOut();
	}
}

function insert(frm, word) 
{
    //__oldAction = frm.action;
    
	frm.value += word;
	frm.focus();
	//smsCheckMsg(document.frm., document.frm.kar, false);
}

function getForm()
{
    var theForm = document.getElementById('aspnetForm');
    
    __oldAction = theForm.action;
    __oldPostBack = __doPostBack;

    if (theForm == null)
    {
        alert('Form is dead');
    }
    
    return 
}

function SetNormalTimeOut()
{
    setwindowId = window.setTimeout(' window.location="?refresh=yes"; ', 30000);
    //setTimeout(function() { Anthem_InvokeControlMethod('myTimer', 'DoTick', [], function() { }, null) }, 5000);
}

function RemoveTimeOut()
{
    clearTimeout(setwindowId);
	setwindowId = -1;
}

function DoNothing()
{
    var theForm = document.getElementById('aspnetForm');
    
        //__doPostBack = function(eventTarget, eventArgument) {
        //    theForm.action = __oldAction;
        //    __oldPostBack(eventTarget, eventArgument)};  
    
    //alert('DoNothingCalled');
    
    theForm.action = __oldAction;
    
    //alert(__oldAction);
    //alert(theForm.action);
    
    return;
}

function checkdel() {
	
	    if (confirm("Are you sure you want to remove this item?")) {
		__doPostBack('DelBtn','');
	    }
	       else
		return false;
        }

function doBlink() {
  // Blink, Blink, Blink...
  var blink = document.all.tags("SPAN")
  for (var i=0; i < blink.length; i++)
    if (blink[i].className == "blink") {
    blink[i].style.visibility = blink[i].style.visibility == "" ? "hidden" : "" }
}

function startBlink() {
  // Make sure it is IE4
  if (document.all)
    setInterval("doBlink()",500)
}

