function ConfDelete() {
	var p = confirm('Are you sure you want to delete this? There is no undo');
	if (p == true) { return true } else { return false }
}

function ChangeAccount(){
	if (document.getElementById('SelectAllAccounts').style.display == "none") {
		document.getElementById('SelectAllAccounts').style.display = "";
		
		document.getElementById('DD_Select').style.backgroundImage = "url('../images/arrow_up.gif')";
		
	} else {
		document.getElementById('SelectAllAccounts').style.display = "none";
		document.getElementById('DD_Select').style.backgroundImage = "url('../images/arrow_down.gif')";
	}
}

function HelpBubbles(maxNum, ShowTxt, HideTxt, ExtraAreas) {
	var Button = false;
	for (i = 0; i<=maxNum; i++) {
		try {
			if (document.getElementById("help"+i).style.display == "none") {
				document.getElementById("help"+i).style.display = "";
				
				if (!Button) { document.getElementById("HelpButton").innerHTML = HideTxt; Button = true; }
			} else {
				document.getElementById("help"+i).style.display = "none"
				if (!Button) { document.getElementById("HelpButton").innerHTML = ShowTxt; Button = true; }
			}
		} catch(err) { }
	
	}
	// extra areas to show
	var shwExtraAreas = ExtraAreas.split(";");
		for (i = 0; i<shwExtraAreas.length; i++) {
			try {
				document.getElementById(shwExtraAreas[i]).style.display = "";
			} catch(err) { }
		} 
}

function showmsg(id){
	var status = document.getElementById(id).style.display;
		if (status == "none") { status = "" } else { status = "none" }
			document.getElementById(id).style.display = status;
}

function clearText(field){
	if (field.defaultValue == field.value) field.value = '';
	else if (field.value == '') field.value = field.defaultValue;
}

/* ----  CHECK SMS ------- */
function CheckSMS(){
	if (!ValidSMS('message')) { return false }
	if (!ValidFromField('from')) { return false }
	return true;
}

function ValidSMS(Field){
try {
	var AllField = String(document.getElementById(Field).value).replace(/«#[0-9][0-9]#»/g, "");
	var ValidChars = /[^a-zA-Z0-9 !"#$%&'()*+,-.\/:;<=>?@\n\r£¥_¿ÄÅÆÇÉÑÖØÜßàäåæèéìñòöøùü]/g;
	var errorchars = String(AllField).match(ValidChars).join(" ");
	if (errorchars != null) {
		var ChkMsg = invalidTextMsg.replace(/#Error#/i, errorchars);
		alert(ChkMsg);
		
		return false;
	}
		try { msgTextArea(); } catch(err) { TextArea(Field); }
		return true;
	} catch(err) { 
		try { msgTextArea(); } catch(err) { TextArea(Field); }
		return true 
	}
}

function ValidFromField(Field){
try {
	var AllField = String(document.getElementById(Field).value);
	var ValidChars = /[^a-zA-Z0-9 ?.-]/g;
	var errorchars = String(AllField).match(ValidChars).join(" ");
	if (errorchars != null) {
		var ChkMsg =  invalidTextFrom.replace(/#Error#/i, errorchars);
		alert(ChkMsg);
		return false;
	}

	return true;
} catch(err) { return true }
}

function TextArea(Field) {
	setTimeout("TextAreaDelayed('"+Field+"')", 1);
}


function TextAreaDelayed(Field){
if (String(Field) == "undefined") { Field = "message"; }
var chars= "characters";
	if (String(Field).indexOf("2") != "-1") { chars = "characters2" }
	if (String(Field).indexOf("3") != "-1") { chars = "characters3" }

   	if(document.getElementById(Field).value.length>=maxlen) {
          document.getElementById(Field).value = document.getElementById(Field).value.substring(0, maxlen);
 	} 
 document.getElementById(chars).innerHTML = (document.getElementById(Field).value.length);
 try {
	document.getElementById("totalSMS").innerHTML = Math.ceil((document.getElementById('message').value.length)/160);	
 } catch(err) { }

}

function ChangeFromField(chkfield) {
	setTimeout('ChangeFromField_Dlyd(\''+chkfield+'\')', 1);
}

function ChangeFromField_Dlyd(chkfield){
	// all numeric: 12 digits, Alpha: 11
	if (String(chkfield) == "undefined") {
		chkfield = "from";
	}
	ValidFromField(chkfield);
	var fromfield = String(document.getElementById(chkfield).value);

	if (!isNumeric(fromfield)) {
		if(fromfield.length > 11) {
          document.getElementById(chkfield).value = document.getElementById(chkfield).value.substring(0, 11);
		  document.getElementById(chkfield).focus();
 		} 
	}

}
function setCaret (textObj) {
	if (textObj.createTextRange) {
		textObj.caretPos = document.selection.createRange().duplicate();
	}
	setTimeout('setCaret_delyd()', 1);
}

function setCaret_delyd () {
	msgTextArea();
	updatephone();
}

function msgTextArea() {
   	if(txtmsglength()>maxlen) {
          document.getElementById('message').value = String(document.getElementById('message').value).substring(0, maxlen);
		  updatephone();
		  document.getElementById('message').focus();
 	} 
 document.getElementById("characters").innerHTML = txtmsglength();
 
 
 try { 
 	document.getElementById("totalSMS").innerHTML = Math.ceil(txtmsglength()/160) 
	 if ((!onemsgalert) && (Math.ceil(txtmsglength()/160) > 1)) { 
		 onemsgalert = true; 
		 alert(TwoMsgAlert); 
	 }	
	} catch(err) { }
	
}

function txtmsglength() {
	//alert("txtmsglength");
	var textmessage = String(document.getElementById('message').value);
	var allPersonalised = String(textmessage).match(/«#[0-9][0-9]#»/gi);
	var TextLength = textmessage.length;
	
	if (allPersonalised != null) {
	// remove personalised fields from message
	textmessage = textmessage.replace(/«#[0-9][0-9]#»/gi, "");
	TextLength = textmessage.length;
	
	// add personalised length to message (longer of: prefix + maxlength || alt text)
	for (i=0; i<allPersonalised.length; i++){
		var pos = parseFloat(allPersonalised[i].substring(2,4))-1;
	
		// count existing nodes to get this number
		var XMLData = "<api>"+document.getElementById("pzlData").value+"</api>";

		if (window.ActiveXObject) {		// code for IE
			  var doc=new ActiveXObject("Microsoft.XMLDOM");
			  doc.async="false";
			  doc.loadXML(XMLData);
		} else {	// code for Firefox etc
			  var parser=new DOMParser();
			  var doc=parser.parseFromString(XMLData,"text/xml");
		}
		
	
	try {
		var maxlengthamount = doc.getElementsByTagName("maxlength")[pos].childNodes[0].nodeValue;
	} catch(err) { maxlengthamount = ""; }
	try {
		var dbfieldname = doc.getElementsByTagName("dbfield")[pos].childNodes[0].nodeValue;
	} catch(err) { dbfieldname = ""; }
	try {
		var prefixlength = String(doc.getElementsByTagName("prefix")[pos].childNodes[0].nodeValue);//.length;
			prefixlength = prefixlength.length;
	} catch(err) { prefixlength = 0; }
	try {
		var altlength = doc.getElementsByTagName("alt")[pos].childNodes[0].nodeValue;
			altlength = altlength.length;
	} catch(err) { altlength = 0; }

		// here		
		if ((parseFloat(prefixlength)+parseFloat(maxlengthamount)) < parseFloat(altlength)) { 
			TextLength += parseFloat(altlength);
		} else { 
			TextLength += parseFloat(prefixlength)+parseFloat(maxlengthamount);
		}
		doc = null;
		} // for loop
		} // allPersonalised != null
	return TextLength;
}

function updatephone(){
	try {
		document.getElementById("fromfield").innerHTML = document.getElementById("from").value;
	} catch(err) {
		document.getElementById("fromfield").innerHTML = document.getElementById("shortcode").value;
	}
	document.getElementById("msg").innerHTML = document.getElementById("message").value;

}
function MSGFromField() {
	setTimeout('MSGFromField_dlyd()', 1);
}

function MSGFromField_dlyd() {
	updatephone();
	ChangeFromField('from');
}
/* ----------------- PERSONALISE -------------------- */
function addprefixexample(){
	document.getElementById("prefixeg").innerHTML = document.getElementById("prefix").value;
	document.getElementById("prefixField").innerHTML = document.getElementById("personalise").options[document.getElementById("personalise").selectedIndex].text.toLowerCase();
}

/* --------------- OTHER FUNCTIONS ----------------- */


function IsNumericField(chkfield){
	
	var fromfield = String(document.getElementById(chkfield).value);
	
	mobilelength = fromfield.substring(0,1) == "1" ? 11 : 12;

	if (isNumeric(fromfield)) {
		if(fromfield.length > mobilelength) {
          document.getElementById(chkfield).value = document.getElementById(chkfield).value.substring(0, mobilelength);
		  document.getElementById(chkfield).focus();
 		} 
	} else {
		document.getElementById(chkfield).value = document.getElementById(chkfield).value.substring(0, String(document.getElementById(chkfield).value).length-1);
		document.getElementById(chkfield).focus();
	}


}

function isNumeric(inputStr){
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (oneChar < "0" || oneChar > "9") { return false }
	}
	return true
}

function SubmitDate(){
	// check valid date
	var date1 = document.getElementById("DateLower").value; 
	var date2 = document.getElementById("DateLimit").value;
	var RTN = true
	try {
	if (document.getElementById("LCID").value == 1033) { 
	var d1 = new Date(parseFloat(date1.substring(6,10)),parseFloat(date1.substring(0,2))-1,parseFloat(date1.substring(3,5)));
	var d2 = new Date(parseFloat(date2.substring(6,10)),parseFloat(date2.substring(0,2))-1,parseFloat(date2.substring(3,5)));
		if (String(parseFloat(date1.substring(6,10))+""+parseFloat(date1.substring(0,2))+""+parseFloat(date1.substring(3,5)))	!= String(d1.getFullYear()+ ""+ (d1.getMonth()+1)+ "" + d1.getDate()) ) { RTN = false };
		if (String(parseFloat(date2.substring(6,10))+""+parseFloat(date2.substring(0,2))+""+parseFloat(date2.substring(3,5)))	!= String(d2.getFullYear()+ ""+ (d2.getMonth()+1)+ "" + d2.getDate()) ) { RTN = false };
		
	 } else {
	var d1 = new Date(date1.substring(6,10),parseFloat(date1.substring(3,5))-1,date1.substring(0,2));
	var d2 = new Date(date2.substring(6,10),parseFloat(date2.substring(3,5))-1,date2.substring(0,2));
		if (String(date1.substring(6,10)+""+date1.substring(3,5)+""+date1.substring(0,2))	!= String(d1.getFullYear()+ ""+ ((d1.getMonth()+1) < 10 ? "0" : "")+(d1.getMonth()+1)+ "" + (d1.getDate() < 10 ? "0" : "")+d1.getDate()) ) { RTN = false };
		if (String(date2.substring(6,10)+""+date2.substring(3,5)+""+date2.substring(0,2))	!= String(d2.getFullYear()+ ""+ ((d2.getMonth()+1) < 10 ? "0" : "")+(d2.getMonth()+1)+ "" + (d2.getDate() < 10 ? "0" : "")+d2.getDate()) ) { RTN = false };
	
	} 
		
		if (isNaN(d1)) { RTN = false; }
		if (isNaN(d2)) { RTN = false; }
		
		if (d1 > d2) { RTN= false; }
	} catch(err) {
		
		RTN = false;
	}
	// 02/20/2001
	if (!RTN) {
		alert("Please enter a valid date range");
	} else {
		document.getElementById("LowerValue").value = d1.valueOf();
		document.getElementById("LimitValue").value = d2.valueOf();
	}
return RTN;
}

function GetXmlHttpObject() {
var xmlHttp=null;
	try {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	 } catch (e) {
	  	// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
function doHighlight(bodyText, searchTerm) {
  highlightStartTag = "<font style='background-color:yellow;'>";
  highlightEndTag = "</font>";

  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {

      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a  block
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
      }
    }
  }
  return newText;
  
}

function get_Inbound(runURL){
	xmlHttp=GetXmlHttpObject();
	
	if ((String(runURL) != "undefined") && (String(runURL) != "")) {
		var url = runURL;
	} else {
		var url="View_InboundTexts.asp?AccountID="+document.getElementById("AccountID").value+"&MSISDN="+document.getElementById("orgMobile").value;		
	}
		xmlHttp.onreadystatechange=get_Inbound_SC;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null)
}

function get_Sent(runURL){
	xmlHttp_sent=GetXmlHttpObject();
			
	if ((String(runURL) != "undefined") && (String(runURL) != "")) {
		var url = runURL;
	} else {
		var url="View_SentTexts.asp?AccountID="+document.getElementById("AccountID").value+"&MSISDN="+document.getElementById("orgMobile").value;		
	}
		
		xmlHttp_sent.onreadystatechange=get_Sent_SC;
		xmlHttp_sent.open("GET",url,true);
		xmlHttp_sent.send(null)
}

function get_Inbound_SC() { 
if (xmlHttp.readyState==4)
	{
		var xmlDoc=xmlHttp.responseXML.documentElement;
		var dataInfo = xmlDoc.getElementsByTagName("Output")[0].childNodes[0].nodeValue;
			document.getElementById("EU_Keywords").innerHTML = dataInfo;
	}
}

function get_Sent_SC() { 
if (xmlHttp_sent.readyState==4)
	{
		var xmlDoc=xmlHttp_sent.responseXML.documentElement;
		var dataInfo = xmlDoc.getElementsByTagName("Output")[0].childNodes[0].nodeValue;
			document.getElementById("EU_Sent").innerHTML = dataInfo;
	}
}
function chgPattern(){
	if (document.getElementById("Pattern").value == "random") {
		document.getElementById("Pattern2").style.display = 'none';
		document.getElementById("Pattern1").style.display = '';
	} else {
		document.getElementById("Pattern1").style.display = 'none';
		document.getElementById("Pattern2").style.display = '';
	}

}

function SubmitLimit(){
	var RTN = true;
	var Lower = document.getElementById("Limit_Lower").value; 
	var Upper = document.getElementById("Limit_Upper").value;
	var al = "";
	if (Lower.length <1) 	{ RTN = false;  }
	if (Upper.length <1) 	{ RTN = false;  }
	if (parseFloat(Lower) > parseFloat(Upper)) 		{ RTN= false; }
	
	if (!RTN) {
		alert("Please enter a valid limit range");
	}
	return RTN;
}

function SubmitPattern(){
	RTN = true;
	if (String(document.getElementById("Pattern").value) == "random") {
		if (String(document.getElementById("randomnumber").value).length <1) 	{ RTN = false;  }
	}
	if (!RTN) {
		alert("Please enter the number of people to select");
	} 
	return RTN
}


function SubmitLocation(){
	RTN = true;
		if (String(document.getElementById("location").value).toLowerCase().indexOf("areacode") != "-1") 	{ RTN = false;  }
		if (String(document.getElementById("location").value).toLowerCase().indexOf(" or city") != "-1") 	{ RTN = false;  }
		if (String(document.getElementById("location").value).length <1) 	{ RTN = false;  }
	if (!RTN) {
		alert("Please enter a location");
	} 
	return RTN
}


function lookupLocation(){
//alert(String(document.getElementById("location").value).length < 1)
	var AllPlaces = String(document.getElementById("location").value);
	if (AllPlaces.indexOf(";") != "-1") {
		AllPlaces = AllPlaces.substring(AllPlaces.lastIndexOf(";")+1).replace(/^[ \t\r\n]+/, "").replace(/[ \t\r\n]+$/, "");
	}

	xmlHttp=GetXmlHttpObject();
		if (String(document.getElementById("location").value).length < 1) {
			document.getElementById('Areas').style.display='none';
		} else {
	
		var url='../code/FindLocations.asp?location='+AllPlaces+'&MediaID='+String(document.getElementById("MediaID").value)+'&datasource='+String(document.getElementById("strDataSource").value)+'&PrevLoc='+String(document.getElementById("location").value).substring(0, String(document.getElementById("location").value).lastIndexOf(";"));
		
			xmlHttp.onreadystatechange=LocationChanged;
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null)
		}
	
}

function LocationChanged() 
{ 
if (xmlHttp.readyState==4)
	{
		
		var xmlDoc=xmlHttp.responseXML.documentElement;
		var svLocation = xmlDoc.getElementsByTagName("pdata")[0].childNodes[0].nodeValue;
		var setH = (5+(14*parseFloat(xmlDoc.getElementsByTagName("results")[0].childNodes[0].nodeValue)));
		document.getElementById("AreasData").style.height = setH+"px";
		document.getElementById('Areas').style.display='';
		document.getElementById("AreasData").innerHTML = svLocation;

	}
}



function clearLocation(){
	document.getElementById('Areas').style.display='none';
	document.getElementById('location').value='';
	clearText(document.getElementById('location'))
}

function useLocation(data) {
	if (String(document.getElementById('location').value).indexOf(";") == "-1") {
		document.getElementById('location').value=data;
	} else {
		document.getElementById('location').value= String(document.getElementById("location").value).substring(0, String(document.getElementById("location").value).lastIndexOf(";")) +";"+data;
	}
	document.getElementById('Areas').style.display='none';
}





