/*
'Author					: Pierre Sutherland
'Date Created			: 17-Jan-2005
'Last Changed by		: Stefan Kruger
'Date Changed			: 05-Jul-2007
'Version				: 1.0.05072007
'Comments:
'Date:			Desc:															
---------------------------------------------------------------------------------------------
'14-Feb-2005	Trying to get the 'select an option'- at top of drop down box
'05-Jul-2007	Fax & e-mail template changes

'ToDoList:
'Date:		Desc:															Status:
'--------------------------------------------------------------------------------------------
*/

var SELECTED_INDEX_VALUE;

//==========================GROUP MODIFICATIONS=======================================

//Check whether a field has been selected and write to textfield:
function checkEdit(code){
	if (document.frmEdit.selEdit.selectedIndex > 0){
		//Set textboxes value equal to the selected index
		document.frmEdit.txtNewName.value = document.frmEdit.selEdit.options[document.frmEdit.selEdit.selectedIndex].text;
		SELECTED_INDEX_VALUE = document.frmEdit.selEdit.options[document.frmEdit.selEdit.selectedIndex].value;
	}else{
		document.frmEdit.txtNewName.value = "";
	}
}

//Populate data for selected comms group
function checkEditCommsGroup(groupID){
	if (groupID > 0){
		var groupName = document.frmEdit.selEdit.options[document.frmEdit.selEdit.selectedIndex].text;
		
		/*
		//get export filename
		page = "modules/sms/bin/commsModule.asp";
		dataToSend = "submitted=true&action=GetDetails&MainID=" + groupID;
		
		var postLoad = function (oXML) {
			var filename = oXML.responseText;
			//Set textboxes value equal to the selected index
			document.frmEdit.txtNewName.value = groupName;
			//document.frmEdit.txtFilename.value = filename;
			SELECTED_INDEX_VALUE = groupID;
			hideLoading();
			oXML = null;
		}
		
		hiddenSubmit(page,dataToSend,postLoad,0,null,true);*/
		
		document.frmEdit.txtNewName.value = groupName;
		
		/*
		var filename = hiddenSubmit(page,dataToSend
		//Set textboxes value equal to the selected index
		document.frmEdit.txtNewName.value = groupName;
		document.frmEdit.txtFilename.value = filename;
		SELECTED_INDEX_VALUE = groupID;
		*/
	} else {
		document.frmEdit.txtNewName.value = "";
		/*document.frmEdit.txtFilename.value = "";*/
	}
}

//Alter the table with the new name received from textbox
function doEdit(code){
	if (document.frmEdit.selEdit.selectedIndex > 0){
		SELECTED_INDEX_VALUE = document.frmEdit.selEdit.options[document.frmEdit.selEdit.selectedIndex].value;
		var page = "modules/sms/bin/commsModule.asp";
		var dataToSend = "submitted=true&action=doEdit&code=" + encodeURIComponent(code) + "&desc=" + encodeURIComponent(document.frmEdit.txtNewName.value) + "&userid=" + encodeURIComponent(SELECTED_INDEX_VALUE);
		/*if (code == 1) { //Changed this from the erroneous "code=1"
			dataToSend = dataToSend + "&filename=" + document.frmEdit.txtFilename.value;
			document.frmEdit.txtFilename.value = "";
		}*/
		var postLoad = function (oXML) {
			var returnVal = oXML.responseText;
			if (returnVal != 1) {
				alert("Entry could not be updated. A entry with this value already exists.");
			}
			refillComboBox(code,frmEdit.selEdit);
			refillComboBox(code,frmDelete.selDelete);
			document.frmDelete.selDelete.selectedIndex = 0;
			document.frmEdit.txtNewName.value = "";
			hideLoading();
			oXML = null;
		}
		hiddenSubmit(page,dataToSend,postLoad,0,null,null,true);
		
		/*
		var returnVal = hiddenSubmit(page,dataToSend);
		if (returnVal != 1) {
			alert("Entry could not be updated. A entry with this value already exists.");
		}
		refillComboBox(code,frmEdit.selEdit);
		refillComboBox(code,frmDelete.selDelete);
		document.frmDelete.selDelete.selectedIndex = 0;
		document.frmEdit.txtNewName.value = "";
		*/
	}
}

//Add the name received from textbox to the table:
function doAdd(code){
	if (document.frmAdd.txtAdd.value != ''){
		var page = "modules/sms/bin/commsModule.asp";
		var dataToSend = "submitted=true&action=doAdd&code=" + encodeURIComponent(code) + "&desc=" + encodeURIComponent(document.frmAdd.txtAdd.value) + "";
		/*if (code == 1) {//Changed this from the erroneous "code=1"
			dataToSend = dataToSend + "&filename=" + document.frmAdd.txtFilename.value;
			document.frmEdit.txtFilename.value = "";
			document.frmAdd.txtFilename.value = "";
		}*/
		
		var postLoad = function (oXML) {
			var returnVal = oXML.responseText;
			//alert(returnVal);
			if (returnVal != 1) {
				alert("Entry could not be added, because an entry with this value already exists.\nThis entry has now been set as active.");
			}

			refillComboBox(code,frmEdit.selEdit);
			refillComboBox(code,frmDelete.selDelete);

			document.frmDelete.selDelete.selectedIndex = 0;
			document.frmEdit.selEdit.selectedIndex = 0;
			
			document.frmAdd.txtAdd.value = "";
			document.frmEdit.txtNewName.value = "";
			hideLoading();
			oXML = null;
		}
		hiddenSubmit(page,dataToSend,postLoad,0,null,null,true);
		
		/*
		var returnVal = hiddenSubmit(page,dataToSend);
		if (returnVal != 1) {
			alert("Entry could not be added. A entry with this value already exists.");
		}

		refillComboBox(code,frmEdit.selEdit);
		refillComboBox(code,frmDelete.selDelete);

		document.frmDelete.selDelete.selectedIndex = 0;
		document.frmEdit.selEdit.selectedIndex = 0;
		
		document.frmAdd.txtAdd.value = "";
		document.frmEdit.txtNewName.value = "";
		*/
	} else {
		alert("Please enter a name for addition.");
	}
}

//Add the name received from textbox to the table:
function doDelete(code){
	if (document.frmDelete.selDelete.selectedIndex > 0){
		var page = "modules/sms/bin/commsModule.asp";
		var dataToSend = "submitted=true&action=doDelete&code=" + code + "&id=" + document.frmDelete.selDelete.options[document.frmDelete.selDelete.selectedIndex].value + "";
		
		var postLoad = function (oXML) {
			var returnVal = oXML.responseText;
			refillComboBox(code,frmEdit.selEdit);
			refillComboBox(code,frmDelete.selDelete);
			
			document.frmDelete.selDelete.selectedIndex = 0;
			document.frmEdit.selEdit.selectedIndex = 0;

			document.frmEdit.txtNewName.value = "";
			hideLoading();
			oXML = null;
		}
		hiddenSubmit(page,dataToSend,postLoad,0,null,null,true);
		/*
		var returnVal = hiddenSubmit(page,dataToSend);
		
		refillComboBox(code,frmEdit.selEdit);
		refillComboBox(code,frmDelete.selDelete);
		
		document.frmDelete.selDelete.selectedIndex = 0;
		document.frmEdit.selEdit.selectedIndex = 0;

		document.frmEdit.txtNewName.value = "";
		*/
	}
}

//==========================END GROUP MODIFICATIONS===================================

//===========================SMS CAMPAIGN FUNCTIONS===================================

function debugSMS(){
	//var page = "http://cfourie:expert@smsgw1.gsm.co.za/xml";
	var page = "http://smsgw1.gsm.co.za/xml";
	var data = "";
	var postLoad = function (oXML) {
		alert(oXML.status);
		//alert(oXML.responseText);
		//alert(oXML.responseXML);
		
		hideLoading();
		oXML = null;
	}
	hiddenSubmit(page,data,postLoad,0,null,null,true);
}

//Execute the campaign
function executeCampaign(campID){

	//debugSMS();return;

	//var page = "other/SMScaller/callService.aspx";
	var page = "SMScaller/callService.aspx";
	var data = "submitted=true&action=Log1&campID=" + campID;
	//data = "submitted=true&action=SMSfQ&campID=" + campID
	//alert("Page : " + page + "\nData : " + data);
	//alert(data)
	//return
	//var resp = "Campaign executed successfully!"

	//var encMessage = message.replace(/ /g,"+")
	//var previous = pagecontent.innerHTML
	//pagecontent.innerHTML = loadingstr
	//var resp
	
	//alert("Disabled");return;
	
	var postLoad = function (oXML) {
		var resp = oXML.responseText;
		var errCode = resp.split(",");
		if (parseInt(errCode[0]) == 0){
			try {
				document.getElementById("status" + campID).innerHTML = "Executed";
			}
			catch (ex) {
				//alert("error updating status div : " + ex.message);
			}
			resp = errCode[1];
		}
		//genericSubmit(page,data)
		//pagecontent.innerHTML = previous
		alert(resp);
		hideLoading();
		oXML = null;
	}
	hiddenSubmit(page,data,postLoad,0,null,null,true);
	
	/*
	var resp = hiddenSubmit(page,data);
	var errCode = resp.split(",");
	if ((parseInt(errCode[0]) == 0)){
			try {eval("status" + campID + ".innerHTML = 'Executed'");}
			catch (ex) {}
			resp = errCode[1];
	}
	//genericSubmit(page,data)
	//pagecontent.innerHTML = previous
	alert(resp);
	//pagecontent.innerHTML = resp
	//return
	//alert(resp)
	*/
}

function showCampLog(campID){
	var page = "modules/sms/campaignLog.asp"
	var data = "campaignID=" + campID
	genericSubmit(page,data,null,null,'topcontent',completedSingleLoad,regularloadingstring,false);
	//genericSubmit(page,data)
}

/*
function textCounter(field, countfield, maxlimit) { 
	if (field.value.length > maxlimit){
		field.value = field.value.substring(0, maxlimit); 
		// otherwise, update 'characters left' counter 
	}else{
		countfield.value = maxlimit - field.value.length;
	}
}*/

function textCounter(field, countfield, defaultlimit) {
	var theform = document.frmAddCamp
	if (!(theform)) {
		theform = document.frmEditCamp
	}
	var totallimit = defaultlimit 
	var currentvalue = theform.txtBrand.value.length + theform.txtModel.value.length + theform.txtMessageDesc.value.length + theform.txtPrice.value.length + theform.txtMinQuant.value.length + theform.txtCampNumber.value.length
	if (currentvalue > totallimit){
		field.value = field.value.substring(0, field.value.length - 1); 
		// otherwise, update 'characters left' counter 
	}else{
		countfield.value = totallimit - currentvalue;//field.value.length; 
	}
} 

//		//Send an sms
//		function sendSMS(number,message){
//
//			//var url   = "http://xml2sms.gsm.co.za/send/?username=cfourie&password=cfourie&number=2782699814&message=hello"
//			var urlStart   = "http://xml2sms.gsm.co.za/send/?username=cfourie&password=cfourie&number="
//			var urlMessage = "&message="
//			var url = urlStart + number + urlMessage + message
//			var data = "url=" + encodeURIComponent(url)
//			genericSubmit("modules/sms/stefansetoets.asp",data)
//
//		//		var xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP");
//		//		xmlhttp2.Open("POST", url , false);
//				//if (data != "") {
//				//	xmlhttp2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//				//	var myregex = / /g
//				//	data = data.replace(myregex,"%20")
//				//}
//		//		alert(page)
//		//		xmlhttp2.Send(/*data*/)
//		//
//		//		alert(xmlhttp2.responseText)
//		//		innerHTML.pagecontent = xmlhttp2.responseText
//			//page = "http://www.furnexonline.co.za"
//			
//			//var res = hiddenSubmit(page,data)
//		}

//Redirect to the add new campaign page
function gotoAddNew(){
	page = "modules/sms/campaignAdd.asp"
	data = "submitted=true&addNew=true"
	//genericSubmit(page,data)
	genericSubmit(page,data,null,null,'topcontent',completedSingleLoad,regularloadingstring,false);
}

//Add a new campaign
function doAddNewCamp(){
	if (document.frmAddCamp.txtName.value != ''){
		if (document.frmAddCamp.txtDesc.value != ''){
			if (document.frmAddCamp.selCode.value != 0){
				if (document.frmAddCamp.txtEmail.value != ''){
					if (document.frmAddCamp.txtBrand.value != ''){
						if (document.frmAddCamp.txtModel.value != ''){
							if(document.frmAddCamp.txtMessageDesc.value != ''){
								if (document.frmAddCamp.txtPrice.value != '' && !(isNaN(document.frmAddCamp.txtPrice.value))){
									if (document.frmAddCamp.txtMinQuant.value != '' && !(isNaN(document.frmAddCamp.txtMinQuant.value))){
										if (document.frmAddCamp.txtCampEndDate.value != ''){
											if (document.frmAddCamp.txtCampNumber.value != ''){
												if (document.frmAddCamp.txtDispatchDate.value != ''){
													var dquote = /\'/g
													//alert(document.frmAddCamp.txtDispatchDate + "*"); return
													var name	= document.frmAddCamp.txtName.value.replace(dquote,"''")
													var code	= document.frmAddCamp.selCode.options[document.frmAddCamp.selCode.selectedIndex].value
													/*var message	= document.frmAddCamp.txtMessage.value*/
													var brand	= document.frmAddCamp.txtBrand.value.replace(dquote,"''")
													var model	= document.frmAddCamp.txtModel.value.replace(dquote,"''")
													var mdesc	= document.frmAddCamp.txtMessageDesc.value.replace(dquote,"''")
													var price	= document.frmAddCamp.txtPrice.value
													var min		= document.frmAddCamp.txtMinQuant.value
													var enddate	= document.frmAddCamp.txtCampEndDate.value
													var campnum	= document.frmAddCamp.txtCampNumber.value.replace(dquote,"''")
													/*NEW*/
													//var fckFaxContent = FCKeditorAPI.GetInstance('fckFaxTemplate').GetXHTML();
													//var fckEmailContent = FCKeditorAPI.GetInstance('fckEmailTemplate').GetXHTML();
													//document.frmAddCamp.txtFaxTemplate.value = fckFaxContent;
													//document.frmAddCamp.txtEmailTemplate.value = fckEmailContent;
													//var faxtemplate	= document.frmAddCamp.txtFaxTemplate.value;
													//var emailtemplate = document.frmAddCamp.txtEmailTemplate.value;
													/*END NEW*/
													var email	= document.frmAddCamp.txtEmail.value.replace(dquote,"''")
													var desc	= document.frmAddCamp.txtDesc.value.replace(dquote,"''")
													var date	= document.frmAddCamp.txtDispatchDate.value
													//alert(document.frmAddCamp.txtDispatchDate.value)
													var time	= document.frmAddCamp.selExecHour.options[document.frmAddCamp.selExecHour.selectedIndex].value + ":"
													time		= time + document.frmAddCamp.selExecMin.options[document.frmAddCamp.selExecMin.selectedIndex].value + ":00"
													
													if (document.frmAddCamp.selExecHour.options[document.frmAddCamp.selExecHour.selectedIndex].value >= 12){
														time = time + "PM"	
													}else{
														time = time + "AM"	
													}
													
													var page = "modules/sms/bin/commsModule.asp"
													var data = "submitted=true&action=doAddCampaign&name=" + encodeURIComponent(name) + "&code=" + encodeURIComponent(code) 
													data = data + "&email=" + encodeURIComponent(email) + "&date=" + encodeURIComponent(date) + "&time=" + encodeURIComponent(time) + "&desc=" + encodeURIComponent(desc)
													data = data + "&brand=" + encodeURIComponent(brand) + "&model=" + encodeURIComponent(model) + "&mdesc=" + encodeURIComponent(mdesc) + "&price=" + encodeURIComponent(price)
													data = data + "&min=" + encodeURIComponent(min) + "&enddate=" + encodeURIComponent(enddate) + "&campnum=" + encodeURIComponent(campnum)
													/*NEW*/
													//data = data + "&faxtemplate=" + encodeURIComponent(faxtemplate) + "&emailtemplate=" + encodeURIComponent(emailtemplate)
													/*END NEW*/
													/*alert(data)
													return;*/
													hiddenSubmit(page,data,null,4,null,null,true);
													
													/*var postLoad = function(oXML){
														alert(oXML.responseText);
														oXML = null;
													}
													hiddenSubmit(page,data,postLoad,0,null,true);*/
													
													
													//var ret = hiddenSubmit(page,data);
													
													
													//alert(ret)
													//pagecontent.innerHTML = ret
													//alert("hi")
													page = "modules/sms/campaignInfo.asp"
													data = "submitted=true&action=AddedCamp"
													
													genericSubmit(page,data,null,null,'topcontent',completedSingleLoad,regularloadingstring,false);
													//genericSubmit(page,data)
												}
												else {
													alert("Please select a dispatch date.")
												}
											}
											else {
												alert("Please enter a campaign number.")
											}
										}
										else {
											alert("Please select a end date.")
										}
									}
									else {
										alert("Please enter a valid number as a minimum quantity.")
									}
								}
								else {
									alert("Please enter valid number as a price.")
								}
							}
							else {
								alert("Please enter a message description.")
							}
						}
						else {
							alert("Please enter a model.")
						}
					}
					else {
						alert("Please enter a brand.")
					}
				}
				else {
					alert("Please enter a reply e-mail address.")
				}
			}
			else {
				alert("Please select a campaign code.")
			}
		}
		else {
			alert("Please enter a description.")
		}
	}
	else {
		alert("Please enter a name.")
	}
}

//Add a new campaign
function doUpdateCamp(campID){
	if (document.frmEditCamp.txtName.value != ''){
		if (document.frmEditCamp.txtDesc.value != ''){
			if (document.frmEditCamp.selCode.value != 0){
				if (document.frmEditCamp.txtEmail.value != ''){
					if (document.frmEditCamp.txtBrand.value != ''){
						if (document.frmEditCamp.txtModel.value != ''){
							if(document.frmEditCamp.txtMessageDesc.value != ''){
								if (document.frmEditCamp.txtPrice.value != '' && !(isNaN(document.frmEditCamp.txtPrice.value))){
									if (document.frmEditCamp.txtMinQuant.value != '' && !(isNaN(document.frmEditCamp.txtMinQuant.value))){
										if (document.frmEditCamp.txtCampEndDate.value != ''){
											if (document.frmEditCamp.txtCampNumber.value != ''){
												if (document.frmEditCamp.txtDispatchDate.value != ''){
													var dquote = /\'/g
													var name	= document.frmEditCamp.txtName.value.replace(dquote,"''")
													var code	= document.frmEditCamp.selCode.options[document.frmEditCamp.selCode.selectedIndex].value
													/*var message	= document.frmEditCamp.txtMessage.value*/
													var brand	= document.frmEditCamp.txtBrand.value.replace(dquote,"''")
													var model	= document.frmEditCamp.txtModel.value.replace(dquote,"''")
													var mdesc	= document.frmEditCamp.txtMessageDesc.value.replace(dquote,"''")
													var price	= document.frmEditCamp.txtPrice.value
													var min		= document.frmEditCamp.txtMinQuant.value
													var enddate	= document.frmEditCamp.txtCampEndDate.value
													var campnum	= document.frmEditCamp.txtCampNumber.value.replace(dquote,"''")
													/*NEW*/
													//var fckFaxContent = FCKeditorAPI.GetInstance('fckFaxTemplate').GetXHTML();
													//var fckEmailContent = FCKeditorAPI.GetInstance('fckEmailTemplate').GetXHTML();
													//document.frmEditCamp.txtFaxTemplate.value = fckFaxContent;
													//document.frmEditCamp.txtEmailTemplate.value = fckEmailContent;
													//var faxtemplate	= document.frmEditCamp.txtFaxTemplate.value
													//var emailtemplate = document.frmEditCamp.txtEmailTemplate.value
													/*END NEW*/
													var email	= document.frmEditCamp.txtEmail.value.replace(dquote,"''")
													var desc	= document.frmEditCamp.txtDesc.value.replace(dquote,"''")
													var date	= document.frmEditCamp.txtDispatchDate.value
													var time	= document.frmEditCamp.selExecHour.options[document.frmEditCamp.selExecHour.selectedIndex].value + ":"
													time		= time + document.frmEditCamp.selExecMin.options[document.frmEditCamp.selExecMin.selectedIndex].value + ":00"

													if (document.frmEditCamp.selExecHour.options[document.frmEditCamp.selExecHour.selectedIndex].value >= 12){
														time = time + "PM"	
													}else{
														time = time + "AM"	
													}

													var page = "modules/sms/bin/commsModule.asp"
													var data = "submitted=true&action=doUpdateCampaign&name=" + encodeURIComponent(name) + "&code=" + encodeURIComponent(code) //+ "&message=" + encodeURIComponent(message) 
													data = data + "&email=" + encodeURIComponent(email) + "&date=" + encodeURIComponent(date)+ "&time=" + encodeURIComponent(time) + "&desc=" + encodeURIComponent(desc) + "&campID=" + campID
													data = data + "&brand=" + encodeURIComponent(brand) + "&model=" + encodeURIComponent(model) + "&mdesc=" + encodeURIComponent(mdesc) + "&price=" + encodeURIComponent(price)
													data = data + "&min=" + encodeURIComponent(min) + "&enddate=" + encodeURIComponent(enddate) + "&campnum=" + encodeURIComponent(campnum)
													/*NEW*/
													//data = data + "&faxtemplate=" + encodeURIComponent(faxtemplate) + "&emailtemplate=" + encodeURIComponent(emailtemplate)
													/*END NEW*/
													//hiddenSubmit(page,data,null,4,null,true);
													
													var postLoad = function(oXML){
														var retValue = oXML.responseText;
														if (retValue == 0){
															alert("Sucessfully updated.");
														}
														else {	
															alert("Response was : " + oXML.responseText);
														}
														oXML = null;
													}
													//alert(data)
													
													hiddenSubmit(page,data,postLoad,0,null,null,true);
													
													//hiddenSubmit(page,data)
													//pagecontent.innerHTML = ret
													
													page = "modules/sms/campaignTarget.asp"
													data = "submitted=true&campaignID=" + campID + "&page=0"
													
													
													genericSubmit(page,data,null,null,'topcontent',completedSingleLoad,regularloadingstring,false);
													//genericSubmit(page,data)
												}
												else {
													alert("Please select a dispatch date.")
												}
											}
											else {
												alert("Please enter a campaign number.")
											}
										}
										else {
											alert("Please select a end date.")
										}
									}
									else {
										alert("Please enter a valid number as a minimum quantity.")
									}
								}
								else {
									alert("Please enter valid number as a price.")
								}
							}
							else {
								alert("Please enter a message description.")
							}
						}
						else {
							alert("Please enter a model.")
						}
					}
					else {
						alert("Please enter a brand.")
					}
				}
				else {
					alert("Please enter a reply e-mail address.")
				}
			}
			else {
				alert("Please select a campaign code.")
			}
		}
		else {
			alert("Please enter a description.")
		}
	}
	else {
		alert("Please enter a name.")
	}
}

//Display the campaign details for editing
//strPage is	main		0
//				edit		1
//				add User	2
//				add group	3
function displayCampaignPS(campID, intPage){
	var page = "modules/sms/campaignTarget.asp"
	var data = "submitted=true&campaignID=" + campID + "&page=" + intPage
	
	genericSubmit(page,data,null,null,'topcontent',completedSingleLoad,regularloadingstring,false);
	//genericSubmit(page,data)
}

function showSMSReport(){
	document.frmReport.submit()
}

//=======================END OF SMS CAMPAIGN FUNCTIONS================================

//=============================USER LINKING MANAGEMENT================================

//These functions manage the linking of:
//				- users to groups
//				- groups to user
//				- users to preferences
//				- preferences to user

//Receives code for query:
// code = 0 Users 
// code = 1 Groups
// code = 2 Preferences

function checkLinkType(code, prefFlag){

	if (document.frmMain.selMain.selectedIndex > 0){

		var page		= "modules/sms/bin/commsModule.asp"
		
		var dataToSendR	= "submitted=true&action=callGetStringArray&flag=1&mainID=" + document.frmMain.selMain.options[document.frmMain.selMain.options.selectedIndex].value
		dataToSendR		= dataToSendR + "&code=" + code + "&leftFlag=1&prefFlag=" + prefFlag

		var postLoadR = function (oXML) {
			var returnValR = oXML.responseText;
			populateBox(returnValR, document.frmLists.selRightList,1);
			hideLoading();
			oXML = null;
		}
		hiddenSubmit(page,dataToSendR,postLoadR,0,null,null,true);
		
		//var returnValR   = hiddenSubmit(page, dataToSendR);
		//populateBox(returnValR, document.frmLists.selRightList,1);

		var dataToSendL	= "submitted=true&action=callGetStringArray&flag=1&mainID=" + document.frmMain.selMain.options[document.frmMain.selMain.options.selectedIndex].value
		dataToSendL		= dataToSendL + "&code=" + code + "&leftFlag=0&prefFlag=" + prefFlag
		
		var postLoadL = function (oXML) {
			var returnValL = oXML.responseText;
			populateBox(returnValL, document.frmLists.selLeftList,1);
			hideLoading();
			oXML = null;
		}
		hiddenSubmit(page,dataToSendL,postLoadL,0,null,null,true);
		
		//var returnValL   = hiddenSubmit(page, dataToSendL);
		//populateBox(returnValL, document.frmLists.selLeftList,1);
		
		//alert(dataToSendR)
		//alert(dataToSendL)
		//pagecontent.innerHTML = returnValR
		//alert("pause")
		//pagecontent.innerHTML = returnValL
			
	} else {
		clearBox(document.frmLists.selLeftList)
		clearBox(document.frmLists.selRightList)
	}
	
}

//Function to return the users for the selected user type
function comms_checkUserTypes(userType, userListBox, leftBox, rightBox){
	var page = "modules/sms/bin/commsModule.asp";
	var DataToSend = "";
	if (userType==0) {
		//All users
		DataToSend = "submitted=true&action=callGetStringArray&flag=1&code=0";
	}
	else if (userType==1) {
		//Comms users
		DataToSend = "submitted=true&action=callGetStringArray&flag=1&code=12";
	}
	else if (userType==2) {
		//Users without preferences
		DataToSend = "submitted=true&action=callGetStringArray&flag=1&code=13";
	}

	var postLoad = function (oXML) {
		var returnVal = oXML.responseText;
		clearBox(leftBox);
		clearBox(rightBox);
		populateBox(returnVal, userListBox);
		hideLoading();
		oXML = null;
	}
	hiddenSubmit(page,DataToSend,postLoad,0,null,null,true);
	
	/*
	var returnVal = hiddenSubmit(page,DataToSend);	
	clearBox(leftBox);
	clearBox(rightBox);
	populateBox(returnVal, userListBox);
	*/
	//comms_clearCommsUsers()
}

//Clear all list boxes on the comms users page
/*
function comms_clearUsers() {
	clearBox(frmLists.selLeftList)
	clearBox(frmLists.selRightList)
	clearBox(frmListGroups.selLeftList)
	clearBox(frmListGroups.selRightList)
	comms_showCommsUserDetail(0, "")
}
*/

//=============================USED IN accgroupsedit.asp ==================================
/*
function checkLinkAccountGroupsEdit(boolBoth,code){

	if (document.frmMain.selMain.selectedIndex > 0){
		var page = "modules/sms/bin/commsModule.asp";
	
	if (boolBoth == true){
	
		//MEMBERS
			//Right Listbox (Not linked)
			var dataToSendR	= "submitted=true&action=callGetStringArray&flag=1&mainID=" + document.frmMain.selMain.options[document.frmMain.selMain.options.selectedIndex].value
			dataToSendR		= dataToSendR + "&code=10&leftFlag=0&prefFlag=null"

			var returnValR   = hiddenSubmit(page, dataToSendR)
	//		pagecontent.innerHTML = returnValR
	//		alert(dataToSendR)

			//Left Listbox (Linked)
			var dataToSendL	= "submitted=true&action=callGetStringArray&flag=1&mainID=" + document.frmMain.selMain.options[document.frmMain.selMain.options.selectedIndex].value
			dataToSendL		= dataToSendL + "&code=10&leftFlag=1&prefFlag=null"
			
			var returnValL   = hiddenSubmit(page, dataToSendL)
	//		pagecontent.innerHTML = returnVall
	//		alert(dataToSendL)

		populateBox(returnValL, document.frmLists.selLeftList1,1)
		populateBox(returnValR, document.frmLists.selRightList1,1)
		
		//SUPPLIERS
			//Right Listbox (Not linked)
			dataToSendR	= "submitted=true&action=callGetStringArray&flag=1&mainID=" + document.frmMain.selMain.options[document.frmMain.selMain.options.selectedIndex].value
			dataToSendR		= dataToSendR + "&code=11&leftFlag=0&prefFlag=null"

			returnValR   = hiddenSubmit(page, dataToSendR)
	//		pagecontent.innerHTML = returnValR
	//		alert(dataToSendR)

			//Left Listbox (Linked)
			dataToSendL	= "submitted=true&action=callGetStringArray&flag=1&mainID=" + document.frmMain.selMain.options[document.frmMain.selMain.options.selectedIndex].value
			dataToSendL		= dataToSendL + "&code=11&leftFlag=1&prefFlag=null"
			
			returnValL   = hiddenSubmit(page, dataToSendL)
	//		pagecontent.innerHTML = returnVall
	//		alert(dataToSendL)

		populateBox(returnValL, document.frmLists.selLeftList2,1)
		populateBox(returnValR, document.frmLists.selRightList2,1)
		
	//Just refresh one of the pairs of list boxes:
	}else{

			//Right Listbox (Not linked)
			dataToSendR	= "submitted=true&action=callGetStringArray&flag=1&mainID=" + document.frmMain.selMain.options[document.frmMain.selMain.options.selectedIndex].value
			dataToSendR		= dataToSendR + "&code="+code+"&leftFlag=0&prefFlag=null"

			returnValR   = hiddenSubmit(page, dataToSendR)

			//Left Listbox (Linked)
			dataToSendL	= "submitted=true&action=callGetStringArray&flag=1&mainID=" + document.frmMain.selMain.options[document.frmMain.selMain.options.selectedIndex].value
			dataToSendL		= dataToSendL + "&code="+code+"&leftFlag=1&prefFlag=null"
			
			returnValL   = hiddenSubmit(page, dataToSendL)

			if (code == 10){
				populateBox(returnValL, document.frmLists.selLeftList1,1)
				populateBox(returnValR, document.frmLists.selRightList1,1)
			}else{
				//code == 11
				populateBox(returnValL, document.frmLists.selLeftList2,1)
				populateBox(returnValR, document.frmLists.selRightList2,1)
			}
	}
	
	}else{
		//If selection = -1 then clear boxes (No associations)
		clearBox(document.frmLists.selLeftList1)
		clearBox(document.frmLists.selRightList1)

		clearBox(document.frmLists.selLeftList2)
		clearBox(document.frmLists.selRightList2)
	}
	
}
//=============================END OF accgroupsedit.asp ==================================
*/

function checkLinkTypeSpec(code, prefFlag, selectedID, leftBox, rightBox){
	if (selectedID != 0){

		var page = "modules/sms/bin/commsModule.asp";
		
		var dataToSendR	= "submitted=true&action=callGetStringArray&flag=1&mainID=" + selectedID;
		dataToSendR		= dataToSendR + "&code=" + code + "&leftFlag=1&prefFlag=" + prefFlag;
		
		var postLoadR = function (oXML) {
			var returnValR = oXML.responseText;
			//alert(returnValR);
			populateBox(returnValR, rightBox, 1);
			hideLoading();
			oXML = null;
		}
		hiddenSubmit(page,dataToSendR,postLoadR,0,null,null,true);
		
		//var returnValR   = hiddenSubmit(page, dataToSendR);
		//populateBox(returnValR, rightBox, 1);
		
		var dataToSendL	= "submitted=true&action=callGetStringArray&flag=1&mainID=" + selectedID;
		dataToSendL		= dataToSendL + "&code=" + code + "&leftFlag=0&prefFlag=" + prefFlag;
		
		var postLoadL = function (oXML) {
			var returnValL = oXML.responseText;
			//alert(returnValL);
			populateBox(returnValL, leftBox, 1);
			hideLoading();
			oXML = null;
		}
		hiddenSubmit(page,dataToSendL,postLoadL,0,null,null,true);
		
		//var returnValL = hiddenSubmit(page, dataToSendL);
		//populateBox(returnValL, leftBox, 1);
		
	
	} else {
		clearBox(leftBox);
		clearBox(rightBox);
	}
}

function refillListBox(code, objList){
	var page = "modules/sms/bin/commsModule.asp";
	var dataToSend = "submitted=true&action=callGetStringArray&flag=0&code=" + code + "&mainID=default&leftFlag=null&prefFlag=null";

	var postLoad = function (oXML) {
		var returnVal = oXML.responseText;
		populateBox(returnVal, objList, 1);
		hideLoading();
		oXML = null;
	}
	hiddenSubmit(page,dataToSend,postLoad,0,null,null,true);

	//var returnVal = hiddenSubmit(page,dataToSend);
	//populateBox(returnVal, objList, 1);
}

//Refill the list boxes used in CampaignsTarget.asp for Users or Groups
//Very similiar to function checkLinkType(code, prefFlag), the only difference being 
//	that refillListBoxCamp(code) doesnt get the mainID from a comboBox - its in .frmKeeper.campaignID
function refillListBoxCamp(code){

	var page		= "modules/sms/bin/commsModule.asp"
	
	var dataToSendR	= "submitted=true&action=callGetStringArray&mainID=" + document.frmKeeper.campaignID.value
	dataToSendR		= dataToSendR + "&code=" + code + "&prefFlag=null&leftFlag=1&flag=1"

	var postLoadR = function (oXML) {
		var returnValR = oXML.responseText;
		populateBox(returnValR, document.frmLists.selRightList, 1);
		hideLoading();
		oXML = null;
	}
	hiddenSubmit(page,dataToSendR,postLoadR,0,null,null,true);

	//var returnValR   = hiddenSubmit(page, dataToSendR);
	//populateBox(returnValR, document.frmLists.selRightList, 1);

	var dataToSendL	= "submitted=true&action=callGetStringArray&mainID=" + document.frmKeeper.campaignID.value
	dataToSendL		= dataToSendL + "&code=" + code + "&prefFlag=null&leftFlag=0&flag=1"
	
	var postLoadL = function (oXML) {
		var returnValL = oXML.responseText;
		populateBox(returnValL, document.frmLists.selLeftList, 1);
		hideLoading();
		oXML = null;
	}
	hiddenSubmit(page,dataToSendL,postLoadL,0,null,null,true);
		
	//var returnValL   = hiddenSubmit(page, dataToSendL);
	//populateBox(returnValL, document.frmLists.selLeftList, 1);
}

function refillComboBox(code, objList){

	var page = "modules/sms/bin/commsModule.asp";
	var dataToSend = "submitted=true&action=callGetStringArray&flag=1&code=" + code + "&mainID=default&leftFlag=null&prefFlag=null";
	
	var postLoad = function (oXML) {
		var returnVal = oXML.responseText;
		populateBox(returnVal, objList, 0);
		hideLoading();
		oXML = null;
	}
	hiddenSubmit(page,dataToSend,postLoad,0,null,null,true);
	
	/*var returnVal = hiddenSubmit(page,dataToSend);
	populateBox(returnVal, objList, 0);*/
	
	//if (returnVal == ""){
	//	returnVal = "0|^^| -- No options Available -- |^^|" + returnVal
	//}else{
		//returnVal = "0|^^| -- Select an option -- |^^|" + returnVal
	//}
	
}

function populateBox(qString, listObj, isListBox){

	//Declare Returned String Array
	var qArray = new Array()
	var i
	var valField
	var nameField
	var len
	var startPos
	//if a listbox is to be filled then the -- select an option should not be displayed.
	startPos = 0

	//alert(qString)
	qArray = qString.split("|^^|")

	if (isListBox){
		startPos = 2
	}

	clearBox(listObj)
	
	//Re populate List:
	for (i = startPos; i < qArray.length; i++){
		valField  = qArray[i]
		nameField = qArray[++i]
		addListOption(nameField, valField, listObj)
	}
}

function addListOption(field1, field2, listObj){
if (field1 != '' & field2 != ''){
		listObj.options[listObj.options.length] = new Option(field1, field2)
		if (field2 == SELECTED_INDEX_VALUE){
			listObj.options[listObj.options.length-1].selected = true
		}
	}else{
		//alert("Field Empty")
	}
}

function clearBox(listObj){
	//Clear the list:
	var len = listObj.options.length
	for (var i = 0; i < len; i++){
		listObj.options[0] = null
	}
}

//These functions either associate some(selected) or all properties (user/pref/comm) 
//	with a different property

//strType indicates the field(s) to be linked to the Main field
//	Many Comms to 1 User - "MC-2-1U"
//	Many Users to 1 Comm - "MU-2-1C"
//	Many Prefs to 1 User - "MP-2-1U"
//	Many Users to 1 Pref - "MU-2-1P"

//strType indicates the field(s) to be UN-linked to the Main field
//	Many Comms from 1 User - "MC-F-1U"
//	Many Users from 1 Comm - "MU-F-1C"
//	Many Prefs from 1 User - "MP-F-1U"
//	Many Users from 1 Pref - "MU-F-1P"

function alterLinks(strType, code, pFlag, listObj, filter){

	var counter	
	var strSels
	var mainID
	
	//Set the type of list used in linking:
//	if (right == true){
//		listObj = document.frmLists.selLeftList
//	}else{
//		listObj = document.frmLists.selRightList
//	}
	
	//String of selected ID's (linkID)
	strSels = ""
	
	//Get the ID of the main form
	if ((code == 5)||(code == 6)){
		mainID = document.frmKeeper.campaignID.value
	}else{
		mainID = document.frmMain.selMain.options[document.frmMain.selMain.selectedIndex].value
	}
	
	counter = 0
	//if useIf is true, all items must be concatenated
	if (filter == false){
		for (var i = 0; i < listObj.options.length; i++){
				counter = counter + 1
				strSels = strSels + "&linkID=" + listObj.options[i].value
		}
	}else{
		for (var i = 0; i < listObj.options.length; i++){
			if (listObj.options[i].selected){
				counter = counter + 1
				strSels = strSels + "&linkID=" + listObj.options[i].value
			}
		}
	}	
	//alert(strSels)

	if (counter > 0){

		var page = "modules/sms/bin/commsModule.asp";
		var dataToSend	= "submitted=true&action=setActiveLink&LinkType=" + strType + "&Counter=" + counter + "&MainID=" + mainID + strSels;
		
		var postLoad = function (oXML) {
			var returnVal = oXML.responseText;
			if ((code == 5)||(code == 6)){
				//Used with SMS Campaign User/Group Allocation
				refillListBoxCamp(code);
			}
			else {
				if ((code == 10)||(code == 11)){
					checkLinkAccountGroupsEdit(0,code);
				}
				else{
					checkLinkType(code,pFlag);
				}
			}
			hideLoading();
			oXML = null;
		}
		hiddenSubmit(page,dataToSend,postLoad,0,null,null,true);
		
		//var returnVal = hiddenSubmit(page, dataToSend);
		//alert(dataToSend)
		//alert(returnVal)
		//pagecontent.innerHTML = returnVal
		
		/*
		if ((code == 5)||(code == 6)){
			//Used with SMS Campaign User/Group Allocation
			refillListBoxCamp(code)
		}else{
			if ((code == 10)||(code == 11)){
				checkLinkAccountGroupsEdit(0,code)	
			}else{
				checkLinkType(code,pFlag)	
			}
		}
		*/
	}
	
	
	SELECTED_INDEX_VALUE = -1
}

//These functions either associate some(selected) or all properties (user/pref/comm) 
//	with a different property

//strType indicates the field(s) to be linked to the Main field
//	Many Comms to 1 User - "MC-2-1U"
//	Many Users to 1 Comm - "MU-2-1C"
//	Many Prefs to 1 User - "MP-2-1U"
//	Many Users to 1 Pref - "MU-2-1P"
//
//strType indicates the field(s) to be UN-linked to the Main field
//	Many Comms from 1 User - "MC-F-1U"
//	Many Users from 1 Comm - "MU-F-1C"
//	Many Prefs from 1 User - "MP-F-1U"
//	Many Users from 1 Pref - "MU-F-1P"

function alterLinks2(strType, code, pFlag, right, filter, selectedID, leftBox, rightBox){

	var counter	
	var strSels
	
	//String of selected ID's (linkID)
	strSels = ""

	//Set the type of list used in linking:
	if (right == true){
		listBox = leftBox
	}else{
		listBox = rightBox
	}
	
	counter = 0
	//if useIf is true, all items must be concatenated
	if (filter == false){
		for (var i = 0; i < listBox.options.length; i++){
				counter = counter + 1
				strSels = strSels + "&linkID=" + listBox.options[i].value
		}
	}else{	
		for (var i = 0; i < listBox.options.length; i++){
			if (listBox.options[i].selected){
				counter = counter + 1
				strSels = strSels + "&linkID=" + listBox.options[i].value
			}
		}
	}	
	
	if (counter > 0){
		var page		= "modules/sms/bin/commsModule.asp"
		var dataToSend	= "submitted=true&action=setActiveLink&LinkType=" + strType + "&Counter=" + counter + "&MainID=" + selectedID + strSels
		var postLoad = function (oXML) {
			var returnVal = oXML.responseText;
			checkLinkTypeSpec(code, pFlag, selectedID, leftBox, rightBox);
			hideLoading();
			oXML = null;
		}
		hiddenSubmit(page,dataToSend,postLoad,0,null,null,true);
		
		//var returnVal = hiddenSubmit(page, dataToSend)
		
		//checkLinkTypeSpec(code, pFlag, selectedID, leftBox, rightBox)

	}
}
//=========================END OF USER LINKING MANAGEMENT================================
