
/* Javascript used to dynamically produce VAS personalization
   author: glconley
     date: 01/19/2010	
*/
/* The following line effectivly includes an external javascript file used to make the ajax call */

document.write('<script type="text/javascript" language="javascript" src="/JavaScripts/HTTPDom.js"></script>'); 


/* This is the main entry point - pass in the SKU (TR340+BLK+OB) and the form.  Hidden fields get added to the form
   which are used to contain the values entered by the user for the vas options 
   
   03/19/2010 add JSONLineData to method - this is a JSON object which will contain existing
              VAS information harvested from the line item table.  This is used in cases
			  where the VAS is being edited.
   */ 
/*
 $(document).ready(function() {   
   	$('.vasInputText').bind('blur',function (event){
		var ok=checkForReservedPhrase(this);
		if (ok==false) {
			this.focus();
			}
		});
	});
*/	
function addVASToForm(skuAlias, inputForm, JSONLineData) {	
	maxWidth=0;	
	var divVAS = document.getElementById('vasPrompt');
	if (divVAS==null) {
		return false;
		}

	while (divVAS.hasChildNodes()) {
    	$("#vasPrompt").empty();
	}
	//var Response = httpRequestGet("gaWeb.dll/Ajax?method=clearCache"+'&ts~='+new Date().getTime()+'&ms~='+Math.random());
	var itemHeight=0;
	var optionData='?method=GetVASInJSON&skuAlias='+skuAlias;
	var Response = httpRequestGet("gaWeb.dll/Ajax"+optionData+'&ts='+new Date().getTime()+'&ms='+Math.random());
	
	if (Response=='') return false;
	if (Response.substring(0,5) == 'ERROR') {
		return false;
		}
      else {
	  var vasObject = eval('('+Response+')');
	  if (vasObject != null) {
        var vasCount=0;
		$(divVAS).height(45);
		divVAS.innerHTML='<strong>Customize</strong>';
		for (i=0;i<=vasObject.vasTypes.length-1;i++) {
			for (x=0;x<=vasObject.vasTypes[i].vasItems.length-1;x++) {
				var vasItem=vasObject.vasTypes[i].vasItems[x];
				//vasCount=vasCount+1;
                //createHiddenInputFieldVasItem( vasObject.vasTypes[i].vasTypeName, vasItem, x, inputForm);
				createHiddenInputFieldVasItem( vasObject.vasTypes[i].vasTypeName, vasItem, vasCount, inputForm);
				// Create Optional Prompt with Check Box and group Item Group in Div 03/03/2010
				var divItem=document.createElement('div');
				divItem.setAttribute('id',"vasItem_"+i);
				divItem.setAttribute('name',"vasItem_"+i);
				divItem.style.height="60px";
				divItem.setAttribute('class',"vasItem");
				//divVAS.appendChild(divItem);
				if (vasObject.vasTypes[i].vasItems[x].requiredFlag=='F') {
					var chkBox=document.createElement("input");
					chkBox.setAttribute('type','checkbox');
					chkBox.setAttribute('name','chkBox_'+x); //glc 07/29/2010 was chkBoxSwitch_ but dll expects chkBox_
					chkBox.setAttribute('id','chkBoxSwitch_'+x);
					chkBox.setAttribute('value','Y');
					chkBox.setAttribute('enabled','true');
					chkBox.setAttribute('class','vasReqdCheckbox');
					chkBox.setAttribute('onclick','toggleDiv(this);');
					
					/*
					$(chkBox).attr({
						type : "checkbox",
						name : "chkBoxSwitch_"+x,
						id	 : "chkBoxSwitch_"+x,
						enabled : true,
						checked : false,
						value : 'Y'
						
					})
					*/
					//onclick : "toggleDiv(this);"
					divVAS.innerHTML += '<br /> ';
					divVAS.appendChild(chkBox);      
					
					var chkBoxSwitch=document.createElement("input");
					$(chkBoxSwitch).attr({
						type : "hidden",
						name : "chkBox_"+x,
						id	 : "chkBox_"+x,
						value : "Y"
					})
//					inputForm.appendChild(chkBox);
					inputForm.appendChild(chkBoxSwitch);      
//					inputForm.appendChild(chkBox);
		
					
					$(chkBox).width("15px");
					//$(chkBox).attr("value","Y");
					divVAS.innerHTML += vasObject.vasTypes[i].vasItems[x].promptText;
					var vasReqd=document.createElement("input");
					$(vasReqd).attr({
						name  : "vasReqd_"+x,
						id	  : "vasReqd_"+x,
						type  : "hidden",
						value :	"Y"
					})
					divVAS.appendChild(vasReqd);
//					$("divVAS").width("50px");
					//alert(navigator.appName);
					if (navigator.appName=='Opera')
						document.getElementById("chkBox_"+x).onclick="toggleDiv(this);";
					}
  				for (z=0;z<=vasItem.attributes.length-1;z++) {
					// Add Hidden Field
//					vasCount=vasCount+1;	
					if (vasItem.attributes[z].webPromptType=="hidden")
						createHiddenInputField(vasItem.attributes[z], x, inputForm);
						
					// If there is a JSONLineData object populated - set the default value
					if (JSONLineData != null) {
						for (vasIndx=0;vasIndx<JSONLineData.ATTRIBUTES.length-1;vasIndx++) 
							if (JSONLineData.ATTRIBUTES[vasIndx].attributeMAP==vasItem.attributes[z].attributeMAP && JSONLineData.ATTRIBUTES[vasIndx].Value != '') {
								vasItem.attributes[z].defaultValue=JSONLineData.ATTRIBUTES[vasIndx].Value;
								$('#chkBoxSwitch_'+x).attr({checked:true});	
								var chkBox=document.getElementById('chkBoxSwitch_'+x);
								toggleDiv(chkBox);
								break;
							}
					
					}
						
					// Dispatch to Appropriate Constructor
					if (vasItem.attributes[z].promptType=='Select') 
						writeSelect(vasItem.attributes[z], x, divItem,vasItem.requiredFlag);
				      else
						writePrompt(vasItem.attributes[z], x, divItem,vasItem.requiredFlag);
					
					// glc 07/28/2010 add required class if attribute is required
					//vasItem_0_ATTRIBUTE10
					/*
					if (vasItem.requiredFlag=='Y') {
						var elementName='#vasItem_'+x+'_'+vasItem.attributes[z].attributeMAP;
						$(elementName).addClass("required");
						};
					*/	
				itemHeight += 45; /* increment size of div group with border 03/03/2010 */
				
				}
			$(divItem).height(itemHeight);	/* set height and append Item Group to VAS div 03/03/2010 */
			divVAS.appendChild(divItem);
			vasCount +=1;
			}
	
		}
		
	toggleDiv(chkBox);
	createHiddenInputFieldVasCount( vasCount, inputForm);	
	
	
	}		
	itemHeight=$(divVAS).height()+3;
	$(divVAS).height(itemHeight); /* set height of item group 03/03/2010 */
}
/* Supporting method to create hidden inputs as containers for hidden VAS */
function createHiddenInputField(vasItemAttribute, cnt, inputForm) {
	var hiddenField=document.createElement('input');
	hiddenField.name='vasItem_'+cnt+'_'+vasItemAttribute.attributeMAP;
	hiddenField.id='vasItem_'+cnt+'_'+vasItemAttribute.attributeMAP;
	hiddenField.type="hidden";
	hiddenField.value=vasItemAttribute.defaultValue;
	inputForm.appendChild(hiddenField);
		
	var hiddenField=document.createElement('input');
	hiddenField.name='vasAttrName_'+cnt+'_'+vasItemAttribute.attributeMAP;
	hiddenField.id='vasAttrName_'+cnt+'_'+vasItemAttribute.attributeMAP;
	hiddenField.type="hidden";
	hiddenField.value=vasItemAttribute.attributeName;
	inputForm.appendChild(hiddenField);

//	inputForm.appendChild(hiddenField);
	
}

function createHiddenInputFieldVasCount( cnt, inputForm) {
      var hiddenFieldc=document.createElement('input');
      hiddenFieldc.name='vasCount';
      hiddenFieldc.id='vasCount';
      hiddenFieldc.value=cnt;
      hiddenFieldc.type='hidden';
      inputForm.appendChild(hiddenFieldc);
}

function createHiddenInputFieldVasItem(vasType, vasItem, cnt, inputForm) {
	  // Create Type
      var hiddenField=document.createElement('input');
      hiddenField.name='vasItem_'+cnt+'_'+'vasType';
      hiddenField.id='vasItem_'+cnt+'_'+'vasType';
      hiddenField.value=vasType;
      hiddenField.type="hidden";
      inputForm.appendChild(hiddenField);
	  
      // Create SKU
      var hiddenField=document.createElement('input');
      hiddenField.name='vasItem_'+cnt+'_'+'vasSku';
      hiddenField.id='vasItem_'+cnt+'_'+'vasSku';
      hiddenField.value=vasItem.vasSku;
      hiddenField.type='hidden';
      inputForm.appendChild(hiddenField);
      // Create SKU Desc
      var hiddenFielddesc=document.createElement('input');
      hiddenFielddesc.name='vasItem_'+cnt+'_'+'vasDesc';
      hiddenFielddesc.id='vasItem_'+cnt+'_'+'vasDesc';
      hiddenFielddesc.value=vasItem.vasDescription;
      hiddenFielddesc.type='hidden';
      inputForm.appendChild(hiddenFielddesc);
}

/* Creates select prompts */
function writeSelect(vasItemAttribute, cnt, divItem, requiredFlag) {
	var selectedIndex=-1;
    var divVAS=document.getElementById('vasPrompt');
	var divAttr=document.createElement('div');
	setDiv('attr',vasItemAttribute.attributeMAP,divAttr);
	
	// create div for prompt and populate
    var divPrompt=document.createElement('div');
	setDiv('prompt',vasItemAttribute.attributeMAP,divPrompt);
	divPrompt.setAttribute('height','auto');
    
	// create div for select and populate
    var divSelect=document.createElement('div');
	setDiv('select',vasItemAttribute.attributeMAP,divSelect);
	
	var selectOptions=document.createElement('select');	

	if ($.browser.msie==true) {
		if (requiredFlag=='Y') 
		  selectOptions.className='required vasSelectText'
	     else	
		  selectOptions.className='vasSelectText';
		}
	   else {
		if (requiredFlag=='Y') 
			selectOptions.setAttribute("class","vasSelectText required")
		   else
		   selectOptions.setAttribute("class","vasSelectText");
		}


	
	selectOptions.name='vasItem_'+cnt+'_'+vasItemAttribute.attributeMAP;
	selectOptions.id='vasItem_'+cnt+'_'+vasItemAttribute.attributeMAP;

	var optionsNew=document.createElement('option');
	optionsNew.text='select '+vasItemAttribute.prompt;
	optionsNew.value='';
	selectOptions.options.add(optionsNew);
	
	for (v=0;v <= vasItemAttribute.values.length-1;v++) {
		var optionsNew=document.createElement('option');
		optionsNew.text=vasItemAttribute.values[v].valueDescription;
		optionsNew.value=vasItemAttribute.values[v].valueName;
		selectOptions.options.add(optionsNew);
	}
	for (v=0;v <= vasItemAttribute.values.length-1;v++) {
		if (vasItemAttribute.values[v].valueName==vasItemAttribute.defaultValue) {	
			
			//if ($.browser.version != '6.0')
		//		optionsNew.setAttribute("selected","selected");
	//		   else	
			selectedIndex=v+1;
			break;	
		}
	}
    
		

	//divPrompt.innerHTML += '&nbsp;'+vasItemAttribute.prompt;	
	divSelect.appendChild(selectOptions);
	//divSelect.innerHTML += '<br />';
	divAttr.appendChild(divPrompt);	
	divAttr.appendChild(divSelect);
	//divAttr.innerHTML += '<br />';
	divItem.appendChild(divAttr);  /* Append divAttr to Item Group 03/03/2010 */
//	divVAS.appendChild(divAttr);

	if (selectedIndex > -1)
		if ($.browser.version=='6.0')
			selectOptions.setAttribute('selectedIndex',selectedIndex)
		   else
			selectOptions.options[selectedIndex].setAttribute("selected","selected");

    // Add Select Prompt

	addHeights(divVAS,45);
	}

}
/* Adds height to the div objects so that the container is always large enough to hold the content 
Converted to JQuery 03/03/2010 */
function addHeights(divObj,height) {
	var curHeight=$(divObj).height();
	$(divObj).height(curHeight + height);
}
/* Adds input tag for non selectable prompts */
function writePrompt(vasItemAttribute, cnt, divItem, requiredFlag) {
//	var divItem = document.getElementById('vasItem_'+cnt);
    var divVAS = document.getElementById('vasPrompt');
	var divAttr=document.createElement('div');
	setDiv('attr',vasItemAttribute.attributeMAP,divAttr);


	// create div for prompt and populate
    var divPrompt=document.createElement('div');
	setDiv('prompt',vasItemAttribute.attributeMAP,divPrompt);
    // create div for select and populate
    var divInput=document.createElement('div');
	setDiv('input',vasItemAttribute.attributeMAP,divInput);
	// Create Input Object
	var inputObj=document.createElement('input');
	inputObj.name='vasItem_'+cnt+'_'+vasItemAttribute.attributeMAP;
	inputObj.id='vasItem_'+cnt+'_'+vasItemAttribute.attributeMAP;
	inputObj.setAttribute("maxlength",vasItemAttribute.maxLength);

	
	if (vasItemAttribute.defaultValue!='null')
		inputObj.value=vasItemAttribute.defaultValue
	   else
	    inputObj.value='';
		
	// add ajax reference to test glc 06/28/2010

	if ($.browser.msie==true) {
		if (requiredFlag=='Y') 
		  inputObj.className='required vasInputText'
	     else	
		  inputObj.className='vasInputText';
		}
	   else {
		if (requiredFlag=='Y') 
			inputObj.setAttribute("class","vasInputText required")
		   else
		   inputObj.setAttribute("class","vasInputText");
		}
	
	//inputObj.setAttribute("onblur","checkForReservedPhrase(this);");
	//inputObj.className="testForReservedPhrase";
	//inputObj.setAttribute("class","testForReservedPhrase");
	inputObj.setAttribute("onfocus","this.select();");	
	/*
	$('#'+inputObj.id).bind('blur',function(event){
		var ok=checkForReservedPhrase(this);
		if (ok==false) {
			this.focus();
			}
		
	});	
	*/
	divPrompt.innerHTML += '&nbsp;'+vasItemAttribute.prompt;	
	divInput.appendChild(inputObj);
	
	divAttr.appendChild(divPrompt);	
	divAttr.appendChild(divInput);
	
	divItem.appendChild(divAttr);
//	divVAS.appendChild(divAttr);
//	divVAS.appendChild(divItem); 
    addHeights(divVAS,45);
	divItem.setAttribute('height',$(divItem).height() + $(divAttr).height());


	
}

/* Assigns CSS class attributes to the div tags */
function setDiv(elementType,attributeName,divObj) {
	divObj.id=elementType+'_'+attributeName;
	divObj.name=elementType+'_'+attributeName;
	if (elementType=='select') 
		divObj.className='vasSelect'
	   else if (elementType=='prompt')
	    divObj.className='vasPrompt'
	   else if (elementType=='value')
	    divObj.className='vasValue'
	   else if (elementType=='input')
	    divObj.className='vasInput'
	   else if (elementType=='attr')
	    divObj.className='vasAttr';
}
// Enables / Disables the Item Group 03/02/2010
function toggleDiv(chkBox) {
	if (chkBox==null) return;
	var id='vasItem_'+chkBox.id.split('_')[1];
	if (chkBox.checked==true) {
		$(".vasSelectText").removeAttr('disabled');
		$(".vasInputText").removeAttr('disabled');
		$(".vasSelectText").addClass('required');
		$(".vasInputText").addClass('required');
		$('#chkBox_'+chkBox.id.split('_')[1]).attr('value','Y');
		/*
		$('#'+id+' *').removeAttr('disabled');
		$('#'+id).removeAttr('disabled');
		$('#'+id).attr('disabled','');
		$('#chkBox_'+chkBox.id.split('_')[1]).attr('value','Y');
		*/
		}
	   else {
		$(".vasSelectText").attr('disabled','disabled');
		$(".vasInputText").attr('disabled','disabled');
		$(".vasSelectText").removeClass('required');
		$(".vasInputText").removeClass('required');

		/*
		$(".vasInputText").removeAttr('disabled');
		
		$('#'+id+' *').attr('disabled',true);
		$('#'+id).attr('disabled',true);
		*/
		$('#chkBox_'+chkBox.id.split('_')[1]).attr('value','');
	   }

}
//function checkForReservedPhrase(phraseObj) {
function checkForReservedPhrase(phrase) {
	var Response = httpRequestGet('gaWeb.dll/Ajax?method=IsReservedPhrase&reservedPhrase='+phrase);
	if (Response=='Y') {
			alert('Warning! '+phrase+' is a reserved phrase / word and thus cannot be used!');
	//		setTimeout(function(){document.getElementById(phraseObj.id).focus()},10);
			return Response;
			};
		 	
};
