/*
'Author					: Sonja Goosen
'Date Created			: 09-Mar-2005
'Last Changed by		: Stefan Kruger
'Date Changed			: 15-Feb-2007
'Version				: 1.0.15022007
'Comments:
'Date:			Desc:															
---------------------------------------------------------------------------------------------
'21-Dec-2006	Added data warehouse user functionality

'ToDoList:
'Date:			Desc:															Status:
'--------------------------------------------------------------------------------------------
*/

//==========================COMMS USERS=======================================
function comms_loadProductDepartments(category){
	if (category != ""){
		//alert("loading product dept : " + category);
		//submit to dwintegrationmodule
		var page = "/modules/sms/bin/dwintegrationmodule.asp";
		var data = "action=getProductDepartments&productcategory=" + encodeURIComponent(category);
		var completedLoadDepartment = function(oXML){
			//alert("Response : " + oXML.responseText);
			var myarray = (oXML.responseText).split(stefanregex);
			var thecombobox = document.getElementById("selProductDepartment");
			var initiallength = thecombobox.options.length;
			for (var k = 0;k < initiallength;k++){
				thecombobox.options[0] = null;
			}
			for (var k = 0;k < myarray.length;k++){
				thecombobox.options[k] = new Option(myarray[k],myarray[k]);
			}
			hideLoading();
			oXML = null;
		}
		//hiddenSubmit(page,data,whenDoneRunThis,modeDelimiter,div,syncmode);
		hiddenSubmit(page,data,completedLoadDepartment,0,null,null,true);
	}
}

function comms_loadProductSkus(department){
	var store = "";
	try {
		store = document.getElementById("selStore").value;
	}
	catch(ex) {
		store = "";
	}
	if (department != "" && store != "") {
		//alert("loading product skus : " + department);
		//submit to dwintegrationmodule
		var page = "/modules/sms/bin/dwintegrationmodule.asp";
		var data = "action=getProductSKUs&productdepartment=" + encodeURIComponent(department) + "&store=" + encodeURIComponent(store);
		var completedLoadSKU = function(oXML){
			//alert("Response : " + oXML.responseText);
			var myarray = (oXML.responseText).split(stefanregex);
			var thecombobox = document.getElementById("selProductSKU");
			var initiallength = thecombobox.options.length;
			for (var k = 0;k < initiallength;k++){
				thecombobox.options[0] = null;
			}
			for (var k = 0;k < myarray.length;k++){
				thecombobox.options[k] = new Option(myarray[k],myarray[k]);
			}
			hideLoading();
			oXML = null;
		}
		//hiddenSubmit(page,data,whenDoneRunThis,modeDelimiter,div,syncmode);
		hiddenSubmit(page,data,completedLoadSKU,0,null,null,true);
	}
}

function comms_loadCustomerList(campaignID){
	var page = "/modules/sms/bin/dwintegrationmodule.asp";
	var data = fetchInputFormData("frmDWUsers");
	data = "action=getCustomerList&" + data + "&campaignid=" + campaignID;
	
	if (document.frmDWUsers.txtCount.value != ""){
		//submit to dwintegrationmodule
		/*var completedCustomerListLoad = function(oXML){
			//alert(oXML.responseText);
			document.getElementById("resultsPane").innerHTML = oXML.responseText;
			hideLoading();
			oXML = null;
		}*/
		//genericSubmit(page,data,null,divToLoadItIn,completedSingleLoad,regularloadingstring,false);
		//alert("now loading customer list :\n" + data);
		//hiddenSubmit(page,data,completedCustomerListLoad,0,null,true);
		genericSubmit(page,data,null,null,"resultsPane",completedSingleLoad,regularloadingstring,false);
	}
	else {
		alert("Please enter a valid customer count limit.");
		try {
			document.frmDWUsers.txtCount.focus();
			document.frmDWUsers.txtCount.select();
		}
		catch (ex){}
	}
}

function comms_saveCustomerList(sql,campaignID){
	var page = "/modules/sms/bin/dwintegrationmodule.asp";
	var data = fetchInputFormData("frmUserDataToSave") + "&action=saveCustomerList&sql=" + encodeURIComponent(sql) + "&campaignid=" + campaignID;
	
	//submit to dwintegrationmodule
	var completedCustomerListSave = function(oXML){
		alert(oXML.responseText);
		//document.getElementById("resultsPane").innerHTML = oXML.responseText;
		displayCampaignPS(campaignID,4);
		hideLoading();
		oXML = null;
	}
	//alert("saving customer list :\n" + data);
	hiddenSubmit(page,data,completedCustomerListSave,0,null,null,true);
}


//==========================COMMS USERS=======================================

//Function to update the users for customer account listbox
function comms_checkUsersForCustomerAccount(accountNo, userListBox){
	if (accountNo != 0) {
		var page = "modules/sms/bin/commsModule.asp";
		var DataToSend = "submitted=true&action=UsersForCustomerAccount&flag=1&accountNo=" + encodeURIComponent(accountNo);
		//alert(DataToSend);
		
		var postLoad = function (oXML) {
			var returnVal = oXML.responseText;
			populateBox(returnVal, userListBox);
			hideLoading();
			oXML = null;
		}
		hiddenSubmit(page,DataToSend,postLoad,0,null,null,true);
	
		//var returnVal = hiddenSubmit(page,DataToSend);
		//populateBox(returnVal, userListBox);
	}
	else {
		clearBox(userListBox)
	}
	comms_clearCommsUsers()
}

//Clear all list boxes on the comms users page
function comms_clearCommsUsers() {
	clearBox(frmLists.selLeftList)
	clearBox(frmLists.selRightList)
	clearBox(frmListGroups.selLeftList)
	clearBox(frmListGroups.selRightList)
	comms_showCommsUserDetail(0, "")
}

//Populate the preferences and group list boxes
function comms_populateCommsUserChannels(userID/*, accountNo*/) {
	//User details 
	comms_showCommsUserDetail(userID/*, accountNo*/)
	//preferences
	checkLinkTypeSpec(2,0, userID, frmLists.selLeftList, frmLists.selRightList)
	//groups
	checkLinkTypeSpec(1,0, userID, frmListGroups.selLeftList, frmListGroups.selRightList)
}

//Display the comms user details for editing
function comms_showCommsUserDetail(userID/*, accountNo*/){
	var page = "modules/sms/commsuserdetail.asp";
	var dataToSend = "submitted=true&action=view&userID=" + userID /*+ "&accountNo=" + accountNo*/;
	
	var postLoad = function (oXML) {
		var returnVal = oXML.responseText;
		document.all("DetailEdit").innerHTML = returnVal;
		hideLoading();
		oXML = null;
	}
	hiddenSubmit(page,dataToSend,postLoad,0,null,null,true);
	
	//var returnVal = hiddenSubmit(page, dataToSend);
	//document.all("DetailEdit").innerHTML = returnVal;
}

//Update the comms user details
function comms_updateCommsUser(userID, accountNo){
	//check email
	//if (isValidEmail(frmEditDetail.txtEmail.value)) {
		var page = "modules/sms/commsuserdetail.asp"
		var dataToSend = "submitted=true&action=update&userID=" + userID + "&accountNo=" + accountNo + "&txtEmail=" + frmEditDetail.txtEmail.value + "&txtPhone=" + frmEditDetail.txtPhone.value + "&txtPhoneCode=" + /*frmEditDetail.txtPhoneCode.value + */"&txtFax=" + frmEditDetail.txtFax.value + "&txtCell=" + frmEditDetail.txtCell.value
		
		var postLoad = function (oXML) {
			var returnVal = oXML.responseText;
			document.all("DetailEdit").innerHTML = returnVal;
			hideLoading();
			oXML = null;
		}
		hiddenSubmit(page,dataToSend,postLoad,0,null,null,true);
		
		//var returnVal = hiddenSubmit(page, dataToSend);
		//document.all("DetailEdit").innerHTML = returnVal;
	//}
	//else {
	//	frmEditDetail.txtEmail.focus();
	//	alert('Please enter a valid email address');
	//}
	
}

//returns true if val is a valid internet e-mail address
function isValidEmail(val){
	re = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	r = val.match(re);
	if (r == null) {
		return false;
	}
	else {
		return true;
	}
}
//=========================END OF COMMS USERS================================