/*
Redirects to anothe URL.
Acceps a variable number of parameters, appends them together to form a URL, and the redirects.
*/
function RedirectTo() {
	var link = "";
	for( var i=0 ; i < arguments.length; i++ ) { 
		link += arguments[i];
	}
		
	//document.location = link;
	window.open(link, "_blank");
}

function BackFromUserData()
{
	var urlFrom = document.location;
	var link = 'manageuserdata.aspx?urlFrom=' + escape( urlFrom );
	document.location = link;
}

function BackFromUserInfo()
{
	var urlFrom = document.location;
	var link = 'userinfo.aspx?urlFrom=' + escape( urlFrom );
	document.location = link;
}

/*
function WriteReview( type, id ) {
	var urlFrom = document.location;
	var link = '#';
    if( type=='prod' )
		link = 'write_product_review-' + id + '.html?urlFrom=' + urlFrom;
	else if( type=='merc' )
		link = 'write_merchant_review.html?merchantId=' +id + '&urlFrom=' + urlFrom;

	if( link != "#" )
		document.location = link;
}
*/
function WriteProductReview( categoryName, productFPN ) {
	var urlFrom = document.location;
	var link = 'write_product_review-' + categoryName + '-' + productFPN + g_defaultExt + '?urlFrom=' + escape( urlFrom );

	document.location = link;
}

function WriteMerchantReview( merchantId ) {
	var urlFrom = document.location;
	var link = 'write_merchant_review' + g_defaultExt + '?merchantId=' + merchantId + '&urlFrom=' + escape( urlFrom );
	
	document.location = link;
}

//function WriteTopicComment( TopicId , TopicTitle , ReviewId) {
//	var urlFrom = document.location;
//	var link;
//	var sTitle = TopicTitle.replace("'", "\\'");
//	if ((TopicId == null) || (TopicId == -1))
//	{
//		link = 'write_comment.aspx?ReviewId=' + ReviewId + '&TopicTitle=' + sTitle + '&urlFrom=' + escape( urlFrom );
//	}
//	else
//	{
//		if ((sTitle == null) || (sTitle == ''))
//		{
//			link = 'write_comment.aspx?TopicId=' + TopicId +  '&urlFrom=' + escape( urlFrom );
//		}
//		else
//		{
//			link = 'write_comment.aspx?TopicId=' + TopicId + '&TopicTitle=' + sTitle + '&urlFrom=' + escape( urlFrom );
//		} 
//	}
//	document.location = link;
//}

function WriteTopicComment( TopicId , ReviewId) {
	var urlFrom = document.location;
	var link;
	//var sTitle = TopicTitle.replace("'", "\\'");
	if ((TopicId == null) || (TopicId == -1))
	{
		link = 'write_comment.aspx?reviewid=' + ReviewId + '&urlFrom=' + escape( urlFrom );
	}
	else
	{

			link = 'write_comment.aspx?TopicId=' + TopicId +  '&urlFrom=' + escape( urlFrom );
		}
	document.location = link;
}

function WriteTopic( CategoryId , FixedProductName ) {
	var urlFrom = document.location;
	var link;
	if ((FixedProductName == '') ||(FixedProductName == null))
		link = 'write_topic_category.aspx?' + g_CatIdParamName + '=' + CategoryId + '&urlFrom=' + escape( urlFrom );
	else
		link = 'write_topic_product.aspx?' + g_CatIdParamName + '=' + CategoryId + '&name=' + FixedProductName + '&urlFrom=' + escape( urlFrom );
	
	document.location = link;
}

function GoToTopic( TopicId , FixedProductName , CategoryId ) {
	var urlFrom = document.location;
	var link;
	if ((FixedProductName == null) || (FixedProductName == ''))
		link = 'topic_category.aspx?' + g_CatIdParamName + '=' + CategoryId + '&TopicId=' + TopicId;
	else
		link = 'topic.aspx?' + g_CatIdParamName + '=' + CategoryId + '&TopicId=' + TopicId + '&name=' + FixedProductName ;
	
	document.location = link;
}

function GoToTopicList ( CategoryId , FixedProductName )
{
	var urlFrom = document.location;
	var link = (FixedProductName == null) ? 
	'topic_list.aspx?CategoryId=' + CategoryId :
	'topic_list.aspx?CategoryId=' + CategoryId + '&name=' + FixedProductName ; 
	
	document.location = link;
}

function GoToLogin()
{
	var urlFrom = document.location;
	//alert(urlFrom);
	var link = 'login.aspx?ReturnUrl=' + escape( urlFrom );
	document.location = link;
}

function GoToRegister()
{
	var urlFrom = document.location;
	//alert(urlFrom);
	var link = 'registration.aspx?ReturnUrl=' + escape( urlFrom );
	document.location = link;
}

function GoToBasket()
{
	var link = 'myshoppydoo.aspx';
	document.location = link;
}

function AddToBasket(sFixedProductName , catId , operation)
{
	var urlFrom = document.location;
	var link;
	//alert(operation);
	if (operation == null)
		link = 'tobasket.aspx?name=' + sFixedProductName + '&' + g_CatIdParamName + '=' + catId + '&urlFrom=' + escape(urlFrom);
	else
		link = 'tobasket.aspx?name=' + sFixedProductName + '&' + g_CatIdParamName + '=' + catId + '&urlFrom=' + escape(urlFrom) + '&operation=' + operation;
	document.location = link;
}

function OpenCloseBox( id )
{
	//alert(id);
	var appo = id.replace(/\$/g , '_');
	var ph = document.getElementById(appo);
	ph.style.display = (ph.style.display == 'block') ? 'none' : 'block';
}

function ResetErrorMessage( id1 , id2 , id3 )
{
	//alert(id1 + ' ' + id2 + ' ' + id3);
	var el1 = document.getElementById(id1);
	if (el1 != null)
	{
//		alert(el1.innerText);
		el1.innerText = '';
//		el1.innerHTML = '';
	}
	var el2 = document.getElementById(id2);
	if (el2 != null)
	{
//	    alert(el2.innerText);
		el2.innerText = '';
//		el2.innerHTML = '';
	}
	var el3 = document.getElementById(id3);
	if (el3 != null)
	{
//		alert(el3.innerText);
		el3.innerText = '';
//		el3.innerHTML = '';
	}
}

// Used for notifications 
//********************************************************************************************

// checkbox checked or not
function CheckNotification(value , idToSearch)
{
	var chk = document.getElementById(idToSearch);
	if (chk) 
		chk.checked = value;
}

function CheckNotificationDefined(value) {
	CheckNotification(value , 'chkNotify');
}

// add or remove notification according to checkbox value (Topic operations)
function AddRemoveNotificationForTopic(currentCheBox , TopicId)
{
	//alert(''+current.checked);
	var urlFrom = document.location;
	
	(currentCheBox.checked) ?
	window.open('totopic.aspx?topicid=' + TopicId + '&operation=add&UrlFrom=' + escape(urlFrom) , '_blank' , 'height=250,width=450,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes') :
	window.open('totopic.aspx?topicid=' + TopicId + '&operation=remove&UrlFrom=' + escape(urlFrom) , '_blank' , 'height=250,width=450,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes');
}

// add or remove notification according to checkbox value (Question/Answers operations)
function AddRemoveNotificationForQA(current , QuestionId , UserName , UserEmail)
{
	//alert(''+current.checked);
	var urlFrom = document.location;
	//var link;
	
	(current.checked) ?
	window.open('answernotify.aspx?questionid=' + QuestionId + '&operation=add&UrlFrom=' + escape(urlFrom) , '_blank' , 'height=250,width=450,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes') :
	window.open('answernotify.aspx?questionid=' + QuestionId + '&operation=remove&UrlFrom=' + escape(urlFrom) , '_blank' , 'height=250,width=450,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes');
}

// login from topic page

function WriteComment(IsAuthenticated , TopicUrl)
{
	var link;
	if (IsAuthenticated)
	{
		link = TopicUrl;
	}
	else
	{
		link = 'login.aspx?ReturnUrl=' + escape( TopicUrl );
	}
//	var urlFrom = document.location;
	document.location = link;
}



// SHOW - HIDE 
//#########################

// obj                  -- object that is supposed to be clicked
// show                 -- current visibility status
// objInterested		-- element to hide/show
// alignRight			-- true if you want to align right objInterested to obj
function showhide(obj , show , objInterested, alignRight)
{
	var x = new getObj(objInterested);
	x.style.visibility = (!show) ? 'hidden' : 'visible';
	setLyr(obj,objInterested,alignRight);
	
	/*if( show )
		setTimeout(function() {showhide(obj , false , objInterested)} , 5000);*/
}

// Set position of objToBePositioned relative to obj
// obj                        -- object supposed to be clicked
// objToBePositioned    -- object supposed to be shown
function setLyr(obj,objToBePositioned,alignRight)
{
	var newX = findPosX(obj);
	var newY = findPosY(obj);
	
	// Fabio - I use this if i have an exception to position the object
	//if (lyr == 'testP') newY -= 50;
	
	var x = new getObj(objToBePositioned);
	x.style.top = ( newY + 20 )+ 'px';
	
	var width = 0;
	if( alignRight )
	{
		var re = new RegExp( '[\\d]+' );
		width = re.exec(x.style.width);
	}
	x.style.left = (newX - width) + 'px';
}

// find X coordinate of obj
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

// find Y coordinate of obj
function findPosY(obj)
{
	var curtop = 0;
	//var printstring = '';
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			//printstring += ' element ' + obj.tagName + ' has ' + obj.offsetTop;
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	//window.status = printstring;
	return curtop;
}

// Gets exact obj name depending on browser type
function getObj(name)
{
 if (document.getElementById)
 {
	   this.obj = document.getElementById(name);
	   this.style = document.getElementById(name).style;
 }
 else if (document.all)
 {
	   this.obj = document.all[name];
	   this.style = document.all[name].style;
 }
 else if (document.layers)
 {
	   if (document.layers[name])
	   {
	   	this.obj = document.layers[name];
	   	this.style = document.layers[name];
	   }
	   else
	   {
	    this.obj = document.layers.testP.layers[name];
	    this.style = document.layers.testP.layers[name];
	   }
 }
}
//#########################

//********************************************************************************************

// objclicked                    -- object that is supposed to be clicked
// show							-- current visibility status// hide	
// objInterested				-- element to hide/show
function showhidePanelData(isAuthenticated , objclicked , show , objInterested)
{
	//alert(isAuthenticated);
	if (!isAuthenticated)
	{
		GoToLogin();
	}
	else
	{
		//alert(objInterested);
		//alert(objclicked);
		var appo1 = objInterested.replace(/\$/g , '_');
		var appo2 = objclicked.replace(/\$/g , '_');
		var x = new getObj(appo1);
		var obj_clicked = new getObj(appo2);
//		hide = !hide;
//		x.style.visibility = (show) ? 'hidden' : 'visible';
//		obj_clicked.style.visibility = (show) ? 'visible' : 'hidden';
		x.style.display = (show) ? 'none' : 'block';
		obj_clicked.style.display = (show) ? 'block' : 'none';
	}
}

function WriteQuestion( CategoryId , FixedProductName ) {
	var urlFrom = document.location;
	var link;
	if ((FixedProductName == '') ||(FixedProductName == null))
		link = 'write_question_category.aspx?' + g_CatIdParamName + '=' + CategoryId + '&urlFrom=' + escape( urlFrom );
	else
		link = 'write_question_product.aspx?' + g_CatIdParamName + '=' + CategoryId + '&name=' + FixedProductName + '&urlFrom=' + escape( urlFrom );
	
	document.location = link;
}

///////////////////////////////////////////////////////////////////////////////////////////////
//
function getObject(obj) {
  var theObj;
  if(document.all) {
    if(typeof obj=="string") {
      return document.all(obj);
    } else {
      return obj.style;
    }
  }
  if(document.getElementById) {
    if(typeof obj=="string") {
      return document.getElementById(obj);
    } else {
      return obj.style;
    }
  }
  return null;
}

// Count characters in a texbox (use on key Up event)
//
// TextBoxId                          -- Id of textbox or textarea where write in
// TextDisplayId                     -- Id of the control where displying number of characters left
// text                                 -- Text format to display
// maxChar                           -- Max number of characters (used to count down)
function Count(TextBoxId , TextDisplayId , text , maxChar) 
{
  //alert(TextBoxId);
  var ObjAdjust1 = TextBoxId.replace(/\$/g , '_');
  var ObjAdjust2 = TextDisplayId.replace(/\$/g , '_'); 
  var TextBoxObj = getObject(ObjAdjust1);
  var TextToShowObj=getObject(ObjAdjust2);
  //alert( TextBoxObj.value);
  var longitud = maxChar - TextBoxObj.value.length;
  if(longitud <= 0) 
  {
	 longitud = 0;
	 text = '<span class="disable"> ' + text + ' </span>';
	 TextBoxObj.value=TextBoxObj.value.substr( 0 , maxChar);
  }
  TextToShowObj.innerHTML = text.replace("{CHAR}" , longitud); 
}
//
///////////////////////////////////////////////////////////////////////////////////////////////




