
//<GENERAL file="ajax_Verification.cfm" description="">
//<history author="Marina Arzumanyan" email="marzumanyan@mediacommerce.com" date="11 Jan 2007" type="Create"
//			comments="Fuctions used for username, station verification using AJAX" />			
			

var xmlHttp;
var addedStations = new Array();
var addedStationIDs = new Array();
var stationIdHolder = new Array();
var stationCallHolder = new Array();
var formCallLetters;
var formbandID;
var storedStationID;

function verification(act)
{
	var postVar;
	
	if(act == 'username'){
		postVar= document.getElementById('username').value;
	}
	
	else if(act == 'zip')
	{
	   postVar= document.getElementById('zipcode').value;
	
	}
	//Check if we have the station in our DB. Allow ad if exists
	else if(act == 'station')
	{
	   postVar= document.getElementById('callband').value;
	   
	
	}
	//Check if we have the station in our DB. Don't allow if exists
	else if(act == 'station2')
	{
		var myCallLen = document.getElementById('callLetters').value.length;
		postVar = myCallLen;
		if (myCallLen > 4){
			alert('Call Letter is too long');
			return false;
		}
		
		postVar = document.getElementById('callLetters').value+'-'+document.getElementById('bandID').value;
		storedStationID = document.getElementById('stationID').value;

	}
	if(postVar)
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
		alert ("Browser does not support HTTP Request");
		return
		} 
		var url="/admin/ajax_Verification.cfm";
		url=url+"?postVar="+postVar;
		url=url+"&action="+act;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=stateChangedReg; 
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
} 



function stateChangedReg() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		var responses= xmlHttp.responseText.split("|");
		//remove after. for debugging only
		//document.getElementById('test').innerHTML=xmlHttp.responseText;
		//
		var action = responses[1];
	
		if (action == 'username')
		{
			var alreadyExists = responses[2];
			var username = responses[3];
			if (alreadyExists > 0)
			{
				alert("The user name "+username+" is already in our system. Please select different username.");
				document.getElementById('username').value = '';
			}
		}	
	
		if (action == 'station')
		{
			var alreadyExists = responses[2];
			var station = responses[3];
			var stationID = responses[4];
			if (alreadyExists == 0)
			{
				alert("Station "+station+" is not in our system. Please check the station name or call us.");
			}
			else 
			{
				addStation(stationID);
			}
		}	
		if (action == 'station2')
		{
			var alreadyExists = responses[2];
			var station = responses[3];
			var stationID = responses[4];
			
			if (alreadyExists > 0 && storedStationID!=stationID)
			{
				alert("Station "+station+" already exists in our system.");
				
				if (formCallLetters && formbandID){
				document.getElementById('callLetters').value = formCallLetters;
				document.getElementById('bandID').value = formbandID;
				}
				else{
				document.getElementById('callLetters').value = '';
				document.getElementById('bandID').value = 0;
				}
				
			}
		
		}	
		if(action == 'zip')
		{
			if(responses[2]*1 == 1)
			{
				document.getElementById('city').value = responses[4];
				var stateOpts = document.getElementById('stateID').options;
				for(i=0; i<stateOpts.length; i++)
				{
					if(stateOpts[i].value == responses[5]){
						document.getElementById('stateID').selectedIndex = i;
					}
					
				}
				restoreThisStyle(document.getElementById('city'));
				restoreThisStyle(document.getElementById('stateID'));

			}
			else{
				document.getElementById('city').value = '';
				document.getElementById('stateID').selectedIndex = 0;
			}
		}
	//if (action == 'agenciesForAdvertiser'){
	//var agencyIDs = responses[2];
	
	//var agencyNames = responses[3];
	
	// polulateOptions(agencyIDs,agencyNames);
	
	//}	
	} 
} 

function GetXmlHttpObject()
{ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest)
	{
	objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
	objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp
}

function popStationDivs(){
	
	var toPopulate = document.getElementById('addedstations');
	toPopulate.innerHTML = '';
	//add to the hidden field
	document.getElementById('stationList').value = '';
	document.getElementById('stationList').value = stationIdHolder.join(',');
	if(stationIdHolder.length == 0)
	{
		document.getElementById('stListDiv').style.display = 'none';
	}
	for(i=0; i<stationIdHolder.length;i++)
	{
		toPopulate.innerHTML = toPopulate.innerHTML + '<div>' + stationCallHolder[i] +' <a href="javascript:deleteStation('+stationIdHolder[i]+')" class="capLink">Remove</a></div>'
	}
}

//add new station
function addStation(stationID)
{
	var dupFlag = true;
	//check for duplicates
	for(i=0; i<stationIdHolder.length;i++)
	{
		if(stationIdHolder[i] == stationID){
			dupFlag = false;
		}
	}
	//add to array
	if(dupFlag){
		stationIdHolder[stationIdHolder.length] = stationID;
		stationCallHolder[stationCallHolder.length] = document.getElementById('callband').value;
		document.getElementById('stListDiv').style.display = 'block';
		popStationDivs();
	}
	
	
	document.getElementById('callband').value = '';

}
//delete station from list
function deleteStation(id)
{   
   // if station is deleted from saved list, make savedStationList empty, so, we will use stationList only
	document.getElementById('savedStationList').value = '';
	var delIndex;
	for(i=0; i<stationIdHolder.length; i++)
	{
		if(stationIdHolder[i] == id)
		{
			delIndex = i;
		}
	}
	stationIdHolder.splice(delIndex,1);
	stationCallHolder.splice(delIndex,1);
	popStationDivs();
	
}
//shows input box for entering station name
function addFieldforStation()
{
	var style2 = document.getElementById('inp').style;
	style2.display = style2.display? "":"block";
}
//check if entered username is different from stored one. Used for edit User
function checkNewName(a,b)
{
	var uName = document.getElementById('username').value;
	if (uName.toUpperCase() != b.toUpperCase())
	verification(a);
}
//check if entered station is different from stored one. Used for edit station
function editStationName(a,b,c)
{
	var cL = document.getElementById('callLetters').value;
	var bd = document.getElementById('bandID').value;
	//set global variables to stored values
	formCallLetters = b;
	formbandID = c;
	if (cL != b || bd != c)
	verification(a);
}

//this function checks for call signs on keyUp

function getCallSign(e,el)
{
	el.value = el.value.toUpperCase();
	string = el.value;
	charArray = string.split('');
	el.value = el.value.replace(/[^A-Za-z,-]/g,"");
	//restrict to four options only for call letters may start only with four possible letters
	if(el.value.length == 1){
		
		if(string != 'W' && string != 'K' && string != 'C' && string != 'X'){
			el.value = '';
			alert('Station Call Letters May Not Start With "' + string + '"');
			return
		}
	
	}
	else{
		var callstr = el.value.split("-");
		if (callstr.length==2){
		
			if(callstr[1].length==2){
				el.value = callstr[0]+"-"+callstr[1];
				verification('station');
			}
		}
	}
	

}

function getZip(el){
   if (us == true){
		el.value = el.value.replace(/[^0-9,-]/g,"");
		if(el.value.length == 5){
			verification('zip');
		}
   }
//alert(el.value);

}
//Marina:functions that executes after ajax request. Analize response and give message id name is not unique. 
//used together with ajax_uniqueCheck.cfm
function uniqueCheck(){
			var responses= xmlHttp.responseText.split("|");
	        var alreadyExists = responses[1];
			var uniqueItem = responses[2];
			var uniqueValue = responses[3]; 
			if (alreadyExists != 0)
			{
				alert("The "+uniqueItem+" '"+uniqueValue+"'"+" is already in our system. Please select different "+uniqueItem+".");
				document.getElementById(uniqueItem).value = '';
			}

}
//Marina:check if entered name is different from stored one. e - new name, f - old name (a,b,c,d match the globalAjaxRequest parameters).
function requestUniqueCheck(a,b,c,d,e,f)
{   
	var e = trim(e);
	var f = trim(f);
	if (f.toUpperCase() != e.toUpperCase())
	globalAjaxRequest(a,b,c,d);
}

//Marina: this function is used together with ajax_activate.cfm.
//We should send tablename,idname and id value. In that table isActive will be changed to 1(0) if it was 0(1) for the specified id.
//ims scr must have id that consists from idname and idvalue.
//Example: <img src="/images/#iif(isActive is 1,DE('active'),DE('inactive'))#1.gif" class="actionIcon1" title="#iif(isActive is 0,DE('Activate'),DE('Deactivate'))# Timepart" onclick="globalAjaxRequest('GET','tablename=L_TimeParts&ID='+#getTimeParts.TimepartID#+'&idname=TimepartID','ajax_activate.cfm','activate_deactivate');" id="timepartID#timepartID#">

				
function activate_deactivate(){
	var responses= xmlHttp.responseText.split("|");
    var actstatus = responses[1];
	var idname = responses[2];
	var id = responses[3];
	if (actstatus == 0)
	{document.getElementById(idname + id).src = '../images/inactive1.gif';
	 document.getElementById(idname + id).title='Activate '+idname.split("ID")[0];
	}
	else
	{document.getElementById(idname + id).src = '../images/active1.gif';
	 document.getElementById(idname + id).title='Deactivate '+idname.split("ID")[0];
	}
}

function getNewOptions(){

	var responses = xmlHttp.responseText.split("|");
	var newvalue = responses[1].split(",");
	var newtext = responses[2].split(",");    
 	for(var i=0; i < newvalue.length; i++){
 		document.getElementById('company').options[i+1] = new Option(newtext[i],newvalue[i]);
 
 	}
}

//populate options for select agency account manager (dsp_addCompany.cfm)
function getNewOptions_aam(){
 
    var responses = xmlHttp.responseText.split("|");
	var newvalue = responses[1].split(",");
	var newtext = responses[2].split(",");  
	var aamID = responses[3];
	var action = responses[4];
	/*if (aamID != 0){
		document.getElementById('selectAAM').style.display='block';
	}
	else{
		document.getElementById('selectAAM').style.display='none';
	}*/
	//clear all options from previously selected agencyID
	document.getElementById('aamID').options.length = 0;
	document.getElementById('aamID').options[0] = new Option('-select account manager-', 0);
 	for(var i=0; i < newvalue.length; i++){
	    if (newvalue[i]!=''){
 		document.getElementById('aamID').options[i+1] = new Option(newtext[i],newvalue[i]);
 		}
 	}
	//when add new advertiser take its agency fees as default fees
	if(action=='ADD'){
		var transFeeStand = responses[5];
		var transFeeFlight = responses[6];
		var comFeeStand = responses[7];
		var comFeeFlight = responses[8];
	//Marina: political advertisers will not have fields transFeeStand and comFeeStand
		if (document.getElementById('transFeeStand')){
		for(var i=0; i < document.getElementById('transFeeStand').options.length; i++){
		    if (i==transFeeStand){
	 		document.getElementById('transFeeStand').options[i].selected = true;
	 		}
	 	}
		}
		if (document.getElementById('transFeeFlight')){
		for(var i=0; i < document.getElementById('transFeeFlight').options.length; i++){
		    if (i==transFeeFlight){
	 		document.getElementById('transFeeFlight').options[i].selected = true;
	 		}
	 	}
		}
		if (document.getElementById('comFeeStand')){
		for(var i=0; i < document.getElementById('comFeeStand').options.length; i++){
		    if (i==comFeeStand){
	 		document.getElementById('comFeeStand').options[i].selected = true;
	 		}
	 	}
		}
		if (document.getElementById('comFeeFlight')){
		for(var i=0; i < document.getElementById('comFeeFlight').options.length; i++){
		    if (i==comFeeFlight){
	 		document.getElementById('comFeeFlight').options[i].selected = true;
	 		}
	 	}
		}
	}
}


