// JavaScript Document
var currWishlistID;
function addWishList(cID){
	
	var textBox=jQuery('#txtWishList');
	var dropDown=jQuery('#drpWishList');
	
	if(textBox.val()==''){
		alert('Please Type a Name for the list.');
		return false;
	}
	//Check id Name exists already.
	var flag=true;
	/*if (dropDown.length > 0) { // make sure we found the select we were looking for
	
		// get the options and loop through them
		var $options = $('option', dropDown);
		var arrVals = [];
		$options.each(function(){
			var Val=$(this).text();
			if(Val.toLowerCase()==textBox.val().toLowerCase()){
				alert('Duplicate Name');
				flag= false;
			}
		});
	}
	if(!flag){
		return false;
	}*/

	//Prepare Data for Query String
	var strString="custID="+cID+"&listName="+escape(encodeURIComponent(textBox.val()));
	
	//send the data to ajax for updation.
	
	
	jQuery.ajax({
        url: 'ajax_addWishList.php',
        type: 'POST',
        async: true,
        dataType: 'text',
        cache: false,
        data: strString,
        error: function (xhr, desc, exceptionobj) {
            alert(desc);
        },
        success: function(json) {
			//duplicate  json chk
			var retVal=json.split('^^^');
			if(retVal[0]!='Error'){
				// Prepare the new Item values here
				addNewWishListItem(textBox.val(),json);
				//added by manish
				//set active
				jQuery('#drpWishList').val(json);
				setActiveWishlist(document.getElementById('drpWishList'));
				//manish end
			} else {
				alert(retVal[1]);
			}
        },
        beforeSend: function() {
            jQuery("#progessWish").show('slow');
        },
        complete: function() {
			jQuery("#progessWish").fadeOut('fast');
        }
		
    });
	//alert("lets Add");
	//addNewWishListItem(textBox.val(),21);
	//alert("is it addd?");
}

function addNewWishListItem(newText, NewID) {
	//alert(newText);
	//alert(NewID);
//TODO add the New Wishlist to the existing Select List.	
	//do comment
	//jQuery("select#drpWishList option[checked]").removeAttr("checked");
	jQuery("select#drpWishList").removeAttr("checked");
	/*
	var thisValue = NewID;
	var thisText  = newText;
	var thisOpt   = document.createElement('option');
	thisOpt.value = thisValue;
	thisOpt.selected = "selected";
	thisOpt.appendChild(document.createTextNode(thisText));*/
	//end of do comment
	var NON='"none"';
	var WISH='"wish_layer2"';
	/*
	PREVIOUS CODE COMMENTED ON ON 31/08/2009
	thisOpt = "<table style='font-family:Arial, Helvetica, sans-serif;font-size:12px;'  width='350' border='0' cellspacing='1' cellpadding='1'><tr><td><input type='radio' checked='checked' onclick='setActiveWishlist(this)' value='"+NewID+"' name='drpWishList' id='drpWishList'/></td><td>"+newText+"</td> <td></td><td></td><td align='center'>0</td></tr><table>";
	END OF PREVIOUS CODE 
	NEW CODE ON 31/08/2009
	CODE COMMENTED ON THE DATE OF 02/09/2009 PRMAOD
	*/
	
	thisOpt = "<table style='font-family:Arial, Helvetica, sans-serif;font-size:12px;'  width='350' border='0' cellspacing='1' cellpadding='1'><tr><td><input id='drpWishList' name='drpWishList' onclick='setActiveWishlist(this),document.getElementById("+WISH+").style.display = "+NON+"' checked='checked' type='radio'  value='"+NewID+"' name='drpWishList' id='drpWishList'></td><td>"+newText+"</td><td>&nbsp;</td><td>&nbsp;</td><td align='center'>0</td></tr><table>";
	
	/*thisOpt = "<table style='font-family:Arial, Helvetica, sans-serif;font-size:12px;'  width='350' border='0' cellspacing='1' cellpadding='1'>
	<tr>
	<td width='24'><input id='drpWishList' name='drpWishList' onclick='setActiveWishlist(this),document.getElementById("+WISH+").style.display = "+NON+"' checked='checked' type='radio'  value='"+NewID+"' name='drpWishList' id='drpWishList'></td>
	   <td width='148'>"+newText+"</td>
	   <td width='42'>JUST FOR TEST</td>
	   <td width='90'>&nbsp;</td>
	   <td width='50'>&nbsp;</td>
	   <td width='77' align='center'>0</td>
	   </tr></table>";*/
	   
	$("#inner_wishlist").append(thisOpt);
	jQuery('#txtWishList').val('');
	jQuery('#wishlist_name').val(newText);
	jQuery('#wish_hidden').val(NewID);
	//jQuery("select#drpWishList option[selected]").removeAttr("selected");
//jQuery("select#drpWishList option[value='thisValue']").attr("selected", "selected");

	//set as active wishlist
}

function openWishList(navLink){
	
	//var List=$jQuery("#wish_hidden").val();//document.getElementById('drpWishList');
	var List=jQuery("#wish_hidden");
	
	/*if (List.selectedIndex<=0){
		alert("You need to select a Wishlist from the dropdown");
		return false;
	}*/
	//var listVal=List[List.selectedIndex].value;
	var listVal=List.val();
	

	
	navLink.href=navLink.href+listVal;
	
	return true;
}

function setActiveWishlist(wishList){
	var strString='';
	
	currWishlistID=wishList.value;
	strString="currWishID="+currWishlistID;
	jQuery.ajax({
        url: 'ajax_addWishList.php',
        type: 'POST',
        async: true,
        dataType: 'text',
        cache: false,
        data: strString,
        error: function (xhr, desc, exceptionobj) {
            alert(desc);
        },
        success: function(json) {
			/*if(json==''){
				//alert('Error in registering the wishlist as ACTIVE Wishlist.');
				alert('Please select the name of wishlist to set as Active Wishlist');
			}*/
			//alert(json);
			jQuery("#wishlist_name").val(json);
			
        },
        beforeSend: function() {
            jQuery("#progessWish").show('slow');
        },
        complete: function() {
			jQuery("#progessWish").fadeOut('fast');
			jQuery("#wish_check").val('y');
			jQuery("#wish_hidden").val(currWishlistID);
			//alert(json);
			//alert('hi');

        }
    });
}

//new function added by manish 
//09-08-03

function addWishListNote(wID){
	//alert(wID);
	var textBox=jQuery('#wishlist_note');
	//alert(textBox);
	var dropDown=jQuery('#drpWishList');
	if(textBox.val()==''){
		alert('Please Enter Description for the wishlist');
		return false;
	}
	//Check id Name exists already.
	var flag=true;
	


	//Prepare Data for Query String
	var strString="wishID="+wID+"&Notes="+escape(encodeURIComponent(textBox.val()));
	
	//send the data to ajax for updation.
	jQuery.ajax({
        url: 'ajax_WishListNote.php',
        type: 'POST',
        async: true,
        dataType: 'text',
        cache: false,
        data: strString,
        error: function (xhr, desc, exceptionobj) {
            alert(desc);
        },
        complete: function() {
			jQuery("#progessWishNote").html("Successfully Added!");
			jQuery("#progessWishNote").show("slow");
			//jQuery("#progessWishNote").hide("slow");

        }
    });
}

//save as yours

function addasYours(cID,wID){
	
	var textBox=jQuery('#your_winame');
	var listNotes = jQuery('#save_wishdesc');
	var dropDown=jQuery('#drpWishList');
	
	
	//Check id Name exists already.
	var flag=true;
	if (dropDown.length > 0) { // make sure we found the select we were looking for
	
		// get the options and loop through them
		var $options = $('option', dropDown);
		var arrVals = [];
		$options.each(function(){
			var Val=$(this).text();
			if(Val.toLowerCase()==textBox.val().toLowerCase()){
				alert('Duplicate Name');
				flag= false;
			}
		});
	}
	if(!flag){
		return false;
	}

	//Prepare Data for Query String
	var strString="custID="+cID+"&listName="+escape(encodeURIComponent(textBox.val()))+"&listNotes="+escape(encodeURIComponent(listNotes.val()))+"&wID="+wID;
	//alert("Test");
	//send the data to ajax for updation.
	jQuery.ajax({
        url: 'ajax_addWishListasYours.php',
        type: 'POST',
        async: true,
        dataType: 'text',
        cache: false,
        data: strString,
        error: function (xhr, desc, exceptionobj) {
            alert(desc);
        },
        success: function(json) {
			var retVal=json.split('^^^');
			if(retVal[0]!='Error'){
				// Prepare the new Item values here
				addNewWishListItem(textBox.val(),json);
			} else {
				alert(retVal[1]);
			}
        },
        beforeSend: function() {
            jQuery("#progessWish").show('slow');
        },
        complete: function() {
			jQuery("#progessWish").fadeOut('fast');
        }
    });
}


function validate_copy(){
	var textBox=jQuery('#wishlist_name');
	var listNotes = jQuery('#save_wishdesc');
	var dropDown=jQuery('#drpWishList');
	var loginc =jQuery('#loginc');
	//alert(loginc);
	//alert(loginc.val());
	if(loginc.val()=='n'){
		alert('Please login (or create an account) in order to save this wishlist as your own - (you must rename the list)');
		return false;
	}
	
	if(textBox.val()==''){
		alert('Please Type a Name for the list.');
		return false;
	}

	//Check id Name exists already.
	var flag=true;
	if (dropDown.length > 0) { // make sure we found the select we were looking for
	
		// get the options and loop through them
		var $options = $('option', dropDown);
		var arrVals = [];
		$options.each(function(){
			var Val=$(this).text();
			if(Val.toLowerCase()==textBox.val().toLowerCase()){
				alert('This name already exists in your wishlists');
				flag= false;
			}
		});
	}
	if(!flag){
		return false;
	}
}

function validate_login_wishlist(){
	
	var loginc =jQuery('#loginc');
	var wishc =jQuery('#wish_check');
	if(loginc.val()=='n'){
		alert('In order to use the Wishlist function you must Login [or create an account if you have not previously created one]');
		return false;
	}
	//new functionality for check the default wishlist
	if (loginc.val()=='y'){
		if(wishc.val() == 'n'){
		alert('Please select or create a wishlist');
		return false;
		}
		if(wishc.val() == 'c'){
		alert('its working');
		return true;
		}
	}
	
}



function add_comment(cid){

	//alert(cid);
	//alert(cid);
var comment = jQuery('#comments_'+cid);
var oldcomment = jQuery('#oldcomment_'+cid);
var comment_by = jQuery('#comment_by');
var comment_date = jQuery('#comment_date');
//alert(comment.val());
//alert(oldcomment.html());

//alert(comment_by.val());
//alert(comment_date.val());

var newcomment = oldcomment.html()+"<br>"+comment_date.val()+" "+comment_by.val()+" - "+comment.val();
//alert(newcomment);
//alert(comment.val());
//Prepare Data for Query String

var loginc =jQuery('#loginc');
	var wishc =jQuery('#wish_check');
	if(loginc.val()=='n'){
		alert('In order to add comments to Wishlist function you must Login [or create an account if you have not previously created one]');
		return false;
	}
	else {
	var strString="recID="+cid+"&comment="+escape(encodeURIComponent(newcomment))+"";
	//alert("Test");
	//send the data to ajax for updation.
	jQuery.ajax({
        url: 'ajax_addComment.php',
        type: 'POST',
        async: true,
        dataType: 'text',
        cache: false,
        data: strString,
        error: function (xhr, desc, exceptionobj) {
            alert(desc);
        },
        success: function(json) {
			var retVal=json.split('^^^');
			if(retVal[0]!='Error'){
				// Prepare the new Item values here
			//	addNewWishListItem(textBox.val(),json);
			//alert('Comment saved');
			} else {
				alert(retVal[1]);
			}
        },
		 beforeSend: function() {
            jQuery("#progessWish_"+cid).show('slow');
        },
        complete: function() {
			jQuery("#progessWish_"+cid).fadeOut('fast');
			jQuery('#oldcomment_'+cid).html(newcomment);
        }
    });
	}
}


//save as shared

function wishlist_share_view(wID,cID){
	
	var loginc =jQuery('#loginc');
	var dropDown=jQuery('#drpWishList');
	var wish_name =jQuery('#wish_name');
	if(loginc.val()=='n'){
		alert('In order to Save the file as Shared, you must Login [or create an account if you have not previously created one]');
		return false;
	}
	else {
	var strString="wID="+wID+"&type=V"+"&cID="+cID;
	//alert(strString);
	//alert("Test");
	//send the data to ajax for updation.
	jQuery.ajax({
        url: 'ajax_addShared.php',
        type: 'POST',
        async: true,
        dataType: 'text',
        cache: false,
        data: strString,
        error: function (xhr, desc, exceptionobj) {
            alert(desc);
        },
        success: function(json) {
			var retVal=json.split('^^^');
			if(retVal[0]!='duplicate'){
				// Prepare the new Item values here
			//	addNewWishListItem(textBox.val(),json);
			alert('Saved as shared');
			} else {
				//alert(retVal[1]);
				alert("This wishlist is already shared");
			}
        },
		 beforeSend: function() {
            jQuery("#progessWish_s").show('slow');
        },
        complete: function() {
			jQuery("#progessWish_s").fadeOut('fast');
			
			var flag=true;
	if (dropDown.length > 0) { // make sure we found the select we were looking for
	
		// get the options and loop through them
		var $options = $('option', dropDown);
		var arrVals = [];
		$options.each(function(){
			var Val=$(this).text();
			if(Val.toLowerCase()==wish_name.val().toLowerCase()){
				alert('Duplicate Name');
				flag= false;
			}
			else{
			
			addNewWishListItem(wish_name.val(),wID);
			setActiveWishlist(wID);
			}
		});
	}
	
			
        }
    });
	}
	
	
}
