/*
*	olsaddtogiftlist.js for Add to wish list Functionality in PDP page
*	Function to show add to wish list funtionality for expanding and hiding the div Tag
*/
// <![CDATA[
	//start DTCOM00002301 16 Sep 09 PA Modified
    $(window).bind('load', function(){
		$('span#addtowishlist').click(function() {
			$("#listpop").toggle();
		});
		$('#closepop').click(function() {
			$("#listpop").hide();
		});
		$('#createnewlink').click(function() {
			$("#createnew").toggle("normal");
		});
		$('#showmore').click(function() {
			$("#morelists").toggle("normal");
		});
  	});
  	//end DTCOM00002301 16 Sep 09 PA Modified

//function to makes a ajax call 
function validateNewGiftlistName(option)
	{
   	 	var vSkuId=document.getElementById("giftListSkuId").value;
		var vProdId=document.getElementById("giftListProductId").value;
		callAjax(vSkuId,vProdId,option);     	       
	}

//function to validate and makes a ajax call ,If validation Success
function callAjax(skuId,prodId,option) 
{ 
  
   var giftLists;
   var URL;
   var isValid=0;
   if(option=='newList')
	 {
		//creating a new wish list
		var newListName=document.getElementById("newlistname").value;
		//validate the newListName value
		isValid=validate(newListName,option);
		if(isValid==0)
		{
			//start DTCOM00002301 01 Oct 09 PA Modified-Fix for 4733
			URL = '/site/en_US/giftlist/fragments/olsaddtogiftlist.jsp?skuId='+ skuId +'&newListName='+encodeURIComponent(newListName)+'&prodId='+prodId;
			//end DTCOM00002301 01 Oct 09 PA Modified-Fix for 4733
		}
	 }
	 else
	 {
		//update the existing wish list by adding an Item
		giftLists=concatGiftListId();
		if(giftLists=="")
		{
			isValid=1;
			showError(option);
		}
		else
		{
			isValid=0;
			URL = '/site/en_US/giftlist/fragments/olsaddtogiftlist.jsp?skuId='+ skuId +'&giftlistIds='+giftLists +'&prodId='+prodId;
		}
		
	}
	if(isValid==0)
	 {
		//make an ajax call to post request
		ajaxPost(URL,option);
	 }
}
//start DTCOM00002301 01 Oct 09 PA Modified-Fix for 4740
//Function to show response of Ajax  
function updateStatic(xmlResponseText,option) {
	var xml =getXMLObject(xmlResponseText);
	//start DTCOM00002301 31 Jul 09 PA Modified
	if(xml!=null && xml.getElementsByTagName("code")[0]!=undefined){
	//end DTCOM00002301 31 Jul 09 PA Modified
		var code=xml.getElementsByTagName("code")[0].childNodes[0].data;
		if(code=="0")
		{
			//start DTCOM00002301 05 Aug 09 PA Modified
			if(option=="update")
			{
				$('#listpop').hide();
				//Start DTCOM00002301 09 Aug 09 PA Modified
				//end DTCOM00002301 05 Aug 09 PA Modified
				clearAll();
				document.getElementById("statusmessage").style.display = "block";
				var giftListId=xml.getElementsByTagName("giftListId")[0].childNodes[0].data;
				var viewListLink=document.getElementById("ViewListLink").value;
				var formedLink=viewListLink+"&giftListId="+giftListId;
				document.getElementById("viewLink1").href=formedLink;
				document.getElementById("viewLink1").style.display="inline";
				if(document.getElementById("viewLink2").style.display=="inline")
				{
					document.getElementById("viewLink2").style.display="none";
				}
				//end DTCOM00002301 09 Aug 09 PA Modified
			}
			
		}
		else if(code=="1")
		{
			$('#listpop').hide();
			clearAll();
			document.getElementById("statusmessage").style.display = "block";
			document.getElementById("viewLink2").style.display="inline";
			if(document.getElementById("viewLink1").style.display=="inline")
			{
				document.getElementById("viewLink1").style.display="none";
			}
		}
		else if(code=="2")
		{
			var errorMessage=xml.getElementsByTagName("errorMessage")[0].childNodes[0].data;
			if(document.getElementById("listname")!=undefined){
				if(document.getElementById("listname").style.display=="block")
				{
					document.getElementById("listname").style.display='none';
				}
			}
			if(document.getElementById("selecttext")!=undefined){
				if(document.getElementById("selecttext").style.display=="block")
				{
					document.getElementById("selecttext").style.display='none';
				}
			}
			if(document.getElementById("listnameBadWord")!=undefined){
				document.getElementById("listnameBadWord").style.display="block";
			}
			if(document.getElementById("errorBadWord")!=undefined){
				document.getElementById("errorBadWord").innerHTML=errorMessage;
			}
		}
		 // call the function to show the new added wish list to the user
		  if(code=="0" && option=='newList'){
			 showNewContent();
			}
	}
	//start DTCOM00002301 31 Jul 09 PA Modified
	else
	{
		if(document.getElementById("listnameBadWord")!=undefined){
			document.getElementById("listnameBadWord").style.display="block";
		}
	}
	//end DTCOM00002301 31 Jul 09 PA Modified
}
//end DTCOM00002301 01 Oct 09 PA Modified-Fix for 4740
//fucntion to post ajax request
function ajaxPost(strURL,option) {
 var xmlHttpReq = false;
       var self       = this;
       if (window.ActiveXObject) // IE
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
       else                      // Firefox / Mozilla / Safari
       {
            self.xmlHttpReq = new XMLHttpRequest();
            if (self.xmlHttpReq.overrideMimeType)
                 self.xmlHttpReq.overrideMimeType('text/xml');
       }
       self.xmlHttpReq.open('POST', strURL, true);
       self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
       self.xmlHttpReq.onreadystatechange = function()
       {
            if (self.xmlHttpReq.readyState == 4) {
            	//start DTCOM00002301 05 Oct 09 PA Modified-Fix for 4740
               updateStatic(extractXML(self.xmlHttpReq.responseText),option);
               //end DTCOM00002301 05 Oct 09 PA Modified-Fix for 4740
            }
       }
       self.xmlHttpReq.send(strURL);
 }
  
//function to clear text box value 
function clearValue(obj)
{
	var textBoxValue=obj.value;
	obj.value="";
}

//function to concat giftList id
function concatGiftListId()
{
	var giftLists='';
	if(document.getElementById("cnt")!=undefined)
	{
		var count = document.getElementById("cnt").value;
		for (i = 0; i < count; i++){
			if(document.getElementById("cb"+[i]).checked)
			{
				giftLists +=document.getElementById("cb"+[i]).value + ':';
		
			}
		}
	}
	return giftLists;
}

//function to show error
function showError(option)
{
	if(option=="newList")
	{
		if(document.getElementById("listname")!=undefined){
			document.getElementById("listname").style.display='block';
		}
		if(document.getElementById("listnameBadWord")!=undefined){
			if(document.getElementById("listnameBadWord").style.display=="block")
			{
				document.getElementById("listnameBadWord").style.display='none';
			}
		}
	}
	else
	{
		if(document.getElementById("selecttext")!=undefined){
			document.getElementById("selecttext").style.display='block';
		}
		if(document.getElementById("listnameBadWord")!=undefined){
			if(document.getElementById("listnameBadWord").style.display=="block")
			{
				document.getElementById("listnameBadWord").style.display='none';
			}
		}
	}
}

//function to validate new list Name
function validate(newListName,option)
{
	
	var isValid=0;
	var defaultValue=document.getElementById("defaultValue").value;
	if(newListName=="" || newListName.toLowerCase()==defaultValue.toLowerCase())
	{
		isValid=1;
		showError(option);
	}
	else
	{
		isValid=0;
	}
	return isValid;
}

//fucntion to show the newly added wish list to user through Ajax call.
function showNewContent()
{
	var xmlHttpReq = false;
       var self       = this;
       if (window.ActiveXObject) // IE
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
       else                      // Firefox / Mozilla / Safari
       {
            self.xmlHttpReq = new XMLHttpRequest();
            if (self.xmlHttpReq.overrideMimeType)
                 self.xmlHttpReq.overrideMimeType('text/xml');
       }
	   var strURL='/site/en_US/giftlist/fragments/olsloadnewwishlistinpdp.jsp';
       self.xmlHttpReq.open('POST', strURL, true);
       self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
       self.xmlHttpReq.onreadystatechange = function()
       {
            if (self.xmlHttpReq.readyState == 4) {
            	//start DTCOM00002301 01 Oct 09 PA Modified-Fix for 4740
               loadContent(trim(self.xmlHttpReq.responseText));
               //end DTCOM00002301 01 Oct 09 PA Modified-Fix for 4740
            }
       }
       self.xmlHttpReq.send(strURL);
}

//function to load the new Content
function loadContent(resText)
{
	document.getElementById("loadContent").innerHTML=resText;
	//start DTCOM00002301 05 Aug 09 PA Modified
	$("#listpop").toggle();
	//end DTCOM00002301 05 Aug 09 PA Modified
}

//function to close the listpopdiv
function hideDiv(flag)
{
	if(flag=="true")
	{
		$("#listpop").hide();
		clearAll();
	}
	else
	{
		//start DTCOM00002301 08 Aug 09 PA Modified
		$("#listpop").hide();
		//end DTCOM00002301 08 Aug 09 PA Modified
		if(document.getElementById("listname")!=undefined){
			document.getElementById("newlistname").value=document.getElementById("defaultValueForNoList").value;
			document.getElementById("listname").style.display='none';
		}
		if(document.getElementById("listnameBadWord")!=undefined){
			document.getElementById("listnameBadWord").style.display='none';
		}
	}
	
}

//function to clear values
function clearAll()
{
	if(document.getElementById("listname")!=undefined){
		document.getElementById("newlistname").value=document.getElementById("defaultValue").value;
		document.getElementById("listname").style.display='none';
	}
	if(document.getElementById("selecttext")!=undefined){
		document.getElementById("selecttext").style.display='none';
	}
	if(document.getElementById("listnameBadWord")!=undefined){
		document.getElementById("listnameBadWord").style.display='none';
	}
	clearCheckBoxes();
	$("#createnew").hide();
}
//function to clear check box
function clearCheckBoxes()
{
	if(document.getElementById("cnt")!=undefined)
	{
		var count = document.getElementById("cnt").value;
		for (i = 0; i < count; i++){
			if(document.getElementById("cb"+[i]).checked)
			{
				document.getElementById("cb"+[i]).checked=false;
		
			}
		}
	}
}

//function to open create new list
function createNewList()
{
	$("#createnew").toggle("normal");
}
//function to handle enterKey
function handleEnterKey(e,callBackFun,option){
	var code = (e.type.toLowerCase().indexOf("key") != -1) ? e.keyCode || e.which : 0;
	var appVersion = navigator.appVersion;
	var isSafari = appVersion.indexOf('Safari');
	var isApple = appVersion.indexOf('Apple');
	if(code==13 || (code == 3 && isSafari > 0 && isApple > 0 ) ){
	callBackFun(option);
	return false;
	} else {
	return true;
	}
}
//start DTCOM00002301 01 Oct 09 PA Modified-Fix for 4740
//function to get xml object from response text
function getXMLObject(xmlResponseText)
{
	try //For Internet Explorer
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(xmlResponseText);
		return xmlDoc;
	}
	catch(e)
	{
		//For Firefox, Mozilla, Opera, etc.
		try 
		{
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(xmlResponseText,"text/xml");
			return xmlDoc;
		}
		catch(e) {
			return null;
		}
	}
}
//end DTCOM00002301 01 Oct 09 PA Modified-Fix for 4740
//Start DTCOM00002301 05 Oct 09 PA Modified-Fix for 4740
function extractXML(xmlResponseText)
{
	var xmlString;
	if(xmlResponseText!=undefined){
		var startNo=xmlResponseText.indexOf("<?xml");
		if(startNo!=undefined){
			xmlString=xmlResponseText.substring(startNo,xmlResponseText.length);
		}
	}
	return xmlString;
}
//End DTCOM00002301 05 Oct 09 PA Modified-Fix for 4740
