// JavaScript Document

function doPostToLogin()
{
	/*This handles redirection to the home page
	from any where in the system
	*/	
	window.location = "index.php";
}

/* This function perform ajax functions alongside 
The ajax class that will be written to handle user request
*/

/* The following variables will be needed for the execution of the ajax stuff*/

var http = createObject();
var nocache = 0;


function createObject()
{
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer")
	{
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		request_type = new XMLHttpRequest();
	}
	return request_type;
}


function insertSenderID(type)
{
	// Optional: Show a waiting message in the layer with ID login_response

	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	//var pid= document.getElementById('snrtype').value;
	var pid = type;
	if(pid=='brand')
	{
		document.getElementById('brand').value = "Please wait..."
		document.getElementById('brand').readOnly=false;
	}
	else
	{
		document.getElementById('brand').value = "Loading phone No..."
		document.getElementById('brand').readOnly=true;	
	}
	
	// Set te random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	http.open('get', 'ajax/insertsenderid.php?pid='+pid+'&nocache = '+nocache);
	http.onreadystatechange = InsertReplySenderId;
	http.send(null);
}

function InsertReplySenderId() 
{
	if(http.readyState == 4)
	{ 
		var response = http.responseText;
		document.getElementById('brand').value =response;
	}
}

/* This function counts the recipients added to the
*Recipient box
*/

function countRecipient()
{
		var a = document.getElementById('dnr').value;
		if(a=="")
		{
				document.getElementById('rec').innerHTML= 0;
		}
		else
		{
		var arr = a.split(",");
		document.getElementById('rec').innerHTML= arr.length;
		}
}

function textCounter(field,counter,maxlimit,linecounter) {
	// text width//
	var fieldWidth =  parseInt(field.offsetWidth);
	var charcnt = field.value.length;        

	// trim the extra text
	if (charcnt > maxlimit) { 
		field.value = field.value.substring(0, maxlimit);
	}

	else { 
	// progress bar percentage
	var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
	document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/100)+"px";
	//document.getElementById(counter).innerHTML="Limit: "+percentage+"%"
	if(charcnt<=160)
	{
		document.getElementById(counter).innerHTML="page 1"
		document.getElementById('charCount').innerHTML=charcnt + " of 160 characters (1)"
	}
	if(charcnt >160 && charcnt<=320)
	{
		document.getElementById(counter).innerHTML="page 2"
		document.getElementById('charCount').innerHTML=(charcnt-160) + " of 160 characters 160(2)"
	}
	if(charcnt>320 && charcnt<=480)
	{
		document.getElementById(counter).innerHTML="page 3"
		document.getElementById('charCount').innerHTML=(charcnt-320) + " of 160 characters(3)"
	}
	if(charcnt >480 && charcnt<=640)
	{
		document.getElementById(counter).innerHTML="page 4"
		document.getElementById('charCount').innerHTML=(charcnt-480) + " of 160 characters(4)"
	}
	if(charcnt > 640 && charcnt <=800)
	{
		document.getElementById(counter).innerHTML="page 5"
		//if(charcnt<=800)
		//{
		document.getElementById('charCount').innerHTML=(charcnt-640) + " of 160 characters(5)"
		//}
	}
	// color correction on style from CCFFF -> CC0000
	setcolor(document.getElementById(counter),percentage,"background-color");
	}
}

function setcolor(obj,percentage,prop){
	obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function loadContacts()
{
	var gid = document.getElementById('gid').value;
	
	if(gid=="")
	{
		alert("Please select a group");
		
	}
	else{
		document.getElementById('contact_wait').innerHTML = "Please wait...";
	nocache = Math.random();
	// Pass the login variables like URL variable
	http.open('get', 'ajax/insertsenderid.php?gid='+gid+'&nocache = '+nocache);
	http.onreadystatechange = loadC;
	http.send(null);
	}
}

function loadC() 
{
	if(http.readyState == 4)
	{ 
		document.getElementById('contact_wait').innerHTML = "";
		var response = http.responseText;
		document.getElementById('dnr').value =document.getElementById('dnr').value+ ","+response;
	}
}

