/*
function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );


if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}
	
// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
*/

/*	
if (parent.lefty.trigger == 1) 
{
	if (typeof(parent.lefty.tofield) == "undefined") {
        	to2 = ""
	} else {
        	to2 = parent.lefty.tofield
	}

	if (typeof(parent.lefty.message) == "undefined") {
        	msg = ""
	} else {
        	msg = parent.lefty.message
	}

	if (typeof(parent.lefty.chars) == "undefined") {
        	rmL = "0"
	} else {
	       rmL = parent.lefty.chars
	}

	if (typeof(parent.lefty.conts) == "undefined") {
		ncts = "0"
	} else {
		ncts = parent.lefty.conts
	}

	if (typeof(parent.lefty.day) == "undefined") {
        	dayz = "0"
	} else {
        	dayz = parent.lefty.day
	}

	if (typeof(parent.lefty.hours) == "undefined") {
        	hourz = "0"
	} else {
        	hourz = parent.lefty.hours
	}

	if (typeof(parent.lefty.minutes) == "undefined") {
        	minz = "0"
	} else {
        	minz = parent.lefty.minutes
	}

	if (typeof(parent.lefty.sms_msgtype) == "undefined") {
		msgt = "SMS_TEXT"
	} else {	
		msgt = parent.lefty.sms_msgtype
	}

	if (typeof(parent.lefty.deliv_ack) == "undefined") {
        	dack = "0"
	} else {
		dack = parent.lefty.deliv_ack 
	}

	if (typeof(parent.lefty.concat) == "undefined") {
	    concatz = "0"
	} else {
       	concatz = parent.lefty.concat
	}

	if (typeof(parent.lefty.sender_id) == "undefined") {
  		sid = ""
	} else {
    	sid = parent.lefty.sender_id
	}

	if (typeof(parent.lefty.mo_sender_id) == "undefined") {
        mo_sid = ""
    } else {
        mo_sid = parent.lefty.mo_sender_id
    }

	if (typeof(parent.lefty.lmax_cr) == "undefined") {
		mmax_cr = ""
	} else {
		mmax_cr = parent.lefty.lmax_cr
	}
}
*/
function bUnload()
{
//name, value, expires, path, domain, secure 

	if (window.document.theform.skipclean.value != 1) {
		Set_Cookie( 'to', window.document.theform.to.value, 1, '/', '', '' );
		Set_Cookie( 'message', window.document.theform.message.value, 1, '/', '', '' );
		Set_Cookie( 'chars', window.document.theform.remLen.value, 1, '/', '', '' );
		Set_Cookie( 'numconts', window.document.theform.numconts.value, 1, '/', '', '' );
		Set_Cookie( 'concat', window.document.theform.concat.value, 1, '/', '', '' );
		Set_Cookie( 'day', window.document.theform.day.value, 1, '/', '', '' );
		Set_Cookie( 'hours', window.document.theform.hours.value, 1, '/', '', '' );
		Set_Cookie( 'minutes', window.document.theform.minutes.value, 1, '/', '', '' );
		Set_Cookie( 'sms_msgtype', window.document.theform.sms_msgtype.value, 1, '/', '', '' );
		Set_Cookie( 'max_credits', window.document.theform.max_credits.value, 1, '/', '', '' );
		Set_Cookie( 'deliv_ack', window.document.theform.deliv_ack.value, 1, '/', '', '' );
		Set_Cookie( 'sender_id', window.document.theform.sender_id.value, 1, '/', '', '' );
	} else {
		Delete_Cookie( "max_credits", "/");
                Delete_Cookie( "deliv_ack", "/");
                Delete_Cookie( "sms_msgtype", "/");
                Delete_Cookie( "minutes", "/");
                Delete_Cookie( "hours", "/");
                Delete_Cookie( "day", "/");
                Delete_Cookie( "concat", "/");
                Delete_Cookie( "numconts", "/");
                Delete_Cookie( "chars", "/");
                Delete_Cookie( "message", "/");
                Delete_Cookie( "to", "/");
	}
//	Set_Cookie( 'mt_sender_id', window.document.theform.mt_sender_id.value, 1, '/', '', '' );
//	Set_Cookie( '', , 1, '/', '', '' );
//	Set_Cookie( '', , 1, '/', '', '' );
/*
	parent.lefty.tofield = window.document.theform.to.value
	parent.lefty.message = window.document.theform.message.value
	parent.lefty.chars = window.document.theform.remLen.value
        parent.lefty.conts = window.document.theform.numconts.value
        parent.lefty.day = window.document.theform.day.value
    	parent.lefty.hours = window.document.theform.hours.value
    	parent.lefty.minutes = window.document.theform.minutes.value
    	parent.lefty.sms_msgtype = window.document.theform.sms_msgtype.value
        parent.lefty.mt_sender_id = window.document.theform.mt_sender_id.value

*/

}

function setFields()
{
if (Get_Cookie('to')) {
	window.document.theform.to.value = Get_Cookie('to');
} else {
	window.document.theform.to.value = "";
}
if (Get_Cookie('message')){
        window.document.theform.message.value = Get_Cookie('message');
} else {

	window.document.theform.message.value = "";

}
if (Get_Cookie('remLen')){
        window.document.theform.remLen.value = Get_Cookie('remLen');
} else {
	window.document.theform.remLen.value = 0;

}
if (Get_Cookie('day')){
        window.document.theform.day.value = Get_Cookie('day');
} else {
window.document.theform.day.value = 0;

}
if (Get_Cookie('hours')){
        window.document.theform.hours.value = Get_Cookie('hours');
} else {
window.document.theform.hours.value = 0;

}
if (Get_Cookie('minutes')){
        window.document.theform.minutes.value = Get_Cookie('minutes');
} else {
window.document.theform.minutes.value = 0;

}
if (Get_Cookie('sms_msgtype')){
        window.document.theform.sms_msgtype.value = Get_Cookie('sms_msgtype');
} else {
 window.document.theform.sms_msgtype.value = "SMS_TEXT";

}
if (Get_Cookie('concat')){
        window.document.theform.concat.value = Get_Cookie('concat');
} else {
//window.document.theform.concat.value = 1;

}
if (Get_Cookie('max_credits')){
        window.document.theform.max_credits.value = Get_Cookie('max_credits');
} else {
window.document.theform.max_credits.value = 0;

}
if (Get_Cookie('deliv_ack')==1){
        window.document.theform.deliv_ack.checked=true;
} else {


}
/*
function unsetFields() {
	//or just delete them
        Set_Cookie( 'to', '', 1, '/', '', '' );
        Set_Cookie( 'message', '', 1, '/', '', '' );
	Set_Cookie( 'remLen', 0, 1, '/', '', '' );
        Set_Cookie( 'chars', 0, 1, '/', '', '' );
        Set_Cookie( 'numconts', 0, 1, '/', '', '' );
        Set_Cookie( 'concat', 1, 1, '/', '', '' );
        Set_Cookie( 'day', 0, 1, '/', '', '' );
        Set_Cookie( 'hours', 0, 1, '/', '', '' );
        Set_Cookie( 'minutes', 0, 1, '/', '', '' );
        Set_Cookie( 'sms_msgtype', 'SMS_TEXT'.value, 1, '/', '', '' );
        Set_Cookie( 'max_credits', 0, 1, '/', '', '' );
        Set_Cookie( 'deliv_ack', 0, 1, '/', '', '' );
        Set_Cookie( 'sender_id', '', 1, '/', '', '' );


}
*/

/*

	window.document.theform.to.value = to2
	window.document.theform.message.value = msg
    	window.document.theform.remLen.value = rmL
        window.document.theform.day.value = dayz
        window.document.theform.hours.value = hourz
        window.document.theform.minutes.value = minz
        window.document.theform.sms_msgtype.value = msgt
        window.document.theform.concat.value = concatz
        window.document.theform.max_credits.value = mmax_cr

        if (dack == 1)
                window.document.theform.deliv_ack.checked=true
*/

window.document.theform.remLen.value = window.document.theform.message.value.length

}



function bodyUnload() 
{
    parent.lefty.tofield = window.document.theform.to.value
    parent.lefty.message = window.document.theform.message.value
    parent.lefty.chars = window.document.theform.remLen.value
	parent.lefty.conts = window.document.theform.numconts.value
	parent.lefty.day = window.document.theform.day.value
    parent.lefty.hours = window.document.theform.hours.value
    parent.lefty.minutes = window.document.theform.minutes.value
    parent.lefty.sms_msgtype = window.document.theform.sms_msgtype.value
	parent.lefty.mt_sender_id = window.document.theform.mt_sender_id.value

	if (typeof(window.document.theform.mo_sender_id) != "undefined")
	{
		if (window.document.theform.sender_id_type.value == "mt" && window.document.theform.mo_sender_id.value)
		{
			parent.lefty.sender_id = window.document.theform.mt_sender_id.value
			window.document.theform.sender_id_type.value = "mt"
		}
		else
		{
			parent.lefty.sender_id = window.document.theform.mo_sender_id.value
			window.document.theform.sender_id_type.value = "mo"
		}

	}
	else
	{
		parent.lefty.sender_id = window.document.theform.mt_sender_id.value
		window.document.theform.sender_id_type.value = "mt"
	}

	parent.lefty.sender_id_type = window.document.theform.sender_id_type.value
	parent.lefty.concat = window.document.theform.concat.value
	parent.lefty.lmax_cr = window.document.theform.max_credits.value

	if (window.document.theform.deliv_ack.checked == true) 
		parent.lefty.deliv_ack = 1
	else
		parent.lefty.deliv_ack = 0

	parent.lefty.trigger = 1;
}

function popFields() 
{
    window.document.theform.to.value = to2
    window.document.theform.message.value = msg
    window.document.theform.remLen.value = rmL
	window.document.theform.day.value = dayz
	window.document.theform.hours.value = hourz
	window.document.theform.minutes.value = minz
	window.document.theform.sms_msgtype.value = msgt
	window.document.theform.concat.value = concatz
	window.document.theform.max_credits.value = mmax_cr

	if (dack == 1) 
		window.document.theform.deliv_ack.checked=true
}

function countSpecialChars(str) {

                 var rg = /[{}\[\]^|\\]/g; //these chars are escaped so they count as 2 in the final message.
                 var occur = str.match(rg);
                 if (!occur) {

                        return 0;

                 } else {

                   return occur.length;

                 }

}


function textCounter(field, countfield, maxlimit) 
{
  if ( field.value.length > maxlimit )
  {
    field.value = field.value.substring( 0, maxlimit );
    alert( 'Message characters cannot exceeed 160 characters in length.' );
    return false;
  }
  else
    countfield.value = field.value.length + countSpecialChars(field.value);
}

function popWin(url,w,h) 
{
	new_window = window.open(url,'x','toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=' + w + ',height=' + h);
    //new_window.focus();
}

function genTemplate(e)
{
	if (window.document.theform.numconts.value == 0)
	{
		window.alert("Please note that you have not selected any recipients from your address book.");
	}
	window.document.theform.message.value = window.document.theform.message.value + "#"+e+"#"
	window.document.theform.message.focus();
	return false;
}

function highlight(field) {
	field.focus();
	field.select();
}

function toConfirm()
{
	days = parseInt(window.document.theform.day.value);
	hours = parseInt(window.document.theform.hours.value);
	minutes = parseInt(window.document.theform.minutes.value);
	max_delay = 7*24*60*60;
	delay_set= (days*24*60*60) + (hours*60*60) + (minutes*60);
	if(  delay_set > max_delay)
	{
		alert("Messages may not be delayed by more than 7 hours.");
        	window.document.theform.hours.focus();
        	return false;
	}
 
    if (window.document.theform.to.value == "" && window.document.theform.numconts.value == 0)
    {
        alert("Please select at least one recipient.");
        window.document.theform.to.focus();
        return false;
    }

    if (window.document.theform.message.value == "")
    {
        alert ("Please enter a message.")
        window.document.theform.message.focus();
        return false;
    }

	if (typeof(window.document.theform.mo_sender_id) != "undefined")
		window.document.theform.sender_id_type.value = window.document.theform.mo_sender_id.value

    //bodyUnload();
    bUnload();
    window.document.theform.confirm.value = "msgconf";

	//if (parent.lefty.document.forms[0].submitcount.value == 0)
	if (window.document.forms[0].submitcount.value == 0)
	//if (1==1)
	{
	    popWin('/central/communicator/compose/msgconfirm.php?mess_sent=' + escape(window.document.theform.message.value) ,500,550);
	}
	else
	{
		window.alert("You have already clicked \"Send\". Thanks!");
		return false;
	}
}

function IsNumericTo(oElem)
{
   var strString = oElem.value;
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   for (i = 0; i < strString.length && blnResult == true; i++)
   {
      strChar = strString.charAt(i);
      	if (strValidChars.indexOf(strChar) == -1)
        {
        	blnResult = false;
			window.alert("The 'To' field accepts only numbers.");
			highlight(oElem);
			//oElem.focus();
        }
   }
   return blnResult;

}

function IsNumericFrom(oElem)
{
   var strString = oElem.value;
   var strValidChars = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   for (i = 0; i < strString.length && blnResult == true; i++)
   {
      strChar = strString.charAt(i);
      	if (strValidChars.indexOf(strChar) == -1)
        {
        	blnResult = false;
			window.alert("The 'From' field accepts only numbers and characters.");
			highlight(oElem);
			//oElem.focus();
        }
   }
   return blnResult;

}


function IsNumericC(oElem)
{
   var strString = oElem.value;
   var strValidChars = "0123456789.";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   for (i = 0; i < strString.length && blnResult == true; i++)
   {
      strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1)
        {
            blnResult = false;
            window.alert("This field only accepts numbers.");
            highlight(oElem);
            //oElem.focus();
        }
   }
   return blnResult;

}

function actSend()
{
	window.alert("You need to activate your account before you can send any messages.");
	return false;
}


