/*
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 UploadUserImage( categoryId, productFPN ) {
	var urlFrom = document.location;
	var link = 'upload_product_picture' + g_defaultExt 
	    + "?" + g_CatIdParamName + "=" + categoryId 
	    + '&' + g_FPNParamName + "=" + productFPN 
	    + '&urlFrom=' + escape( urlFrom );

	document.location = link;
}

function PriceGraph(prod_id,cat_id) {
    window.open('pricegraph.aspx?prodId='+prod_id+'&catId='+cat_id+'&type=0','_blank','toolbar=no,menubar=no,location=no,resizable=no,status=no,width=840,height=450'); 
}

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(urlFrom)
{
    if (urlFrom == null)
    {
        urlFrom = document.location;
    } 
    var link = '/login.aspx?ReturnUrl=' + escape( urlFrom );
	document.location = link;
}

//function GoToLogin()
//{
//	var urlFrom = document.location;
//	//alert(urlFrom);
//	 GoToLogin(urlFrom);
//}

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 , sourceURL)
{
	var urlFrom = document.location;
	var link;
	//alert(sFixedProductName);
	if (operation == null)
		link = '../../tobasket.aspx?name=' + sFixedProductName + '&' + g_CatIdParamName + '=' + catId + '&producturl=' + escape(sourceURL) + '&urlFrom=' + escape(urlFrom);
	else
		link = '../../tobasket.aspx?name=' + sFixedProductName + '&' + g_CatIdParamName + '=' + catId + '&producturl=' + escape(sourceURL) + '&urlFrom=' + escape(urlFrom) + '&operation=' + operation;
	document.location = link;
}

//*******************************************************
//
// Show / Hides a Panel (typically a DIV element) that contains a textbox
// TextBox content if (exists) will be deleted
function OpenCloseBox( id , txtToResetId)
{
	//alert(id);
	var appo = id.replace(/\$/g , '_');
	var ph = document.getElementById(appo);
	ph.style.display = (ph.style.display == 'block') ? 'none' : 'block';
	var TextBox = new getObject(txtToResetId);
	if (TextBox)
	{
		if (ph.style.display == 'block')
			TextBox.value ='';
	}
}
//*******************************************************

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 = GoToLogin( 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';
	if (obj) {
	    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);
       if (this.obj)
       {
            this.style = this.obj.style;
       }
    }
    else if (document.all)
    {
       this.obj = document.all[name];
        if (this.obj)
       {
            this.style = document.all[name].style;
       } 
    }
    else if (document.layers)
    {
       if (document.layers[name])
       {
   	       this.obj = document.layers[name];
           if (this.obj)
           {	
   	            this.style = document.layers[name];
   	       } 
       }
       else
       {
           this.obj = document.layers.testP.layers[name];
           if (this.obj)
           { 
               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 ShowOrderDataPanel(objInterested)
{
    var e=document.getElementsByTagName("input");
	var panel = document.getElementById(objInterested);
	
	var isThree = false;
	var Cond1 = false;
	var Cond2 = false;
	for(var i=0;i<e.length;i++)
	{
		if (e[i].id.indexOf('lstRate') != - 1)
		{	
		    if (e[i].checked)
		    { 
//			    panel.style.visibility = (e[i].value > 2) ? 'collapse' : 'visible';
                Cond1 = (e[i].value <= 2);
			    isThree = (e[i].value == 3); 
			}
		}
	}
	for(var i=0;i<e.length;i++)
	{
		if (e[i].id.indexOf('rdbBuyAdvice') != - 1)
		{
		    if ((e[i].checked) && (e[i].value == 'false') && (isThree))
		   {   
		        Cond2 = true;
		   }
		}
	}
	if ((Cond1) || (Cond2))
	{
	    panel.style.visibility = 'visible';
	}
	else
	{
	    panel.style.visibility = 'collapse';
	}
}

function Trim(str) 
{ 
	return str.replace(/^\s+|\s+$/, '')
}

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); 
}
//
///////////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////////////
// GET and SET Cookie value

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) 
{
	var prefix = name + '=';
	var cookieStartIndex;
	if (document.cookie)
	{
		cookieStartIndex = document.cookie.indexOf(prefix);
	}
	else
	{
		cookieStartIndex = -1;
	}
	if (cookieStartIndex == -1)
		return null;
	var cookieEndIndex = document.cookie.indexOf(';', cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1)
		cookieEndIndex = document.cookie.length;
	return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name					Name of the cookie
 * value					Value of the cookie
 * [expiredays]		Expiration date of the cookie (default: end of current session)
 * [path]				Path where the cookie is valid
 * [domain]				Domain where the cookie is valid			
 * [secure]				Boolean value indicating if the cookie transmission requires a secure transmission
 */
function SetCookie(name, value, expiredays, path, domain, secure) 
{
	var caution = false;
	var ExpireDate = new Date();
	var expires = ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	expires = expires.toLocaleString();
	//alert(expires);
	var curCookie = name + '=' + escape(value) + ((expiredays) ? '; expires=' +  expires : '') + ((path) ? '; path=' + path : '') + ((domain) ? '; domain=' + domain : '') + ((secure) ? '; secure' : '');
	if (!caution || (name + '=' + escape(value)).length <= 4000)
	{
		document.cookie = curCookie;
	}
	else
	{
		if (confirm('Cookie exceeds 4KB and will be cut!'))
			document.cookie = curCookie;
	}
}

//************************************************************************
//* Cheks if a specified cookie exists
//*
//* name  Name of the desired cookie.
//*
//************************************************************************
function checkCookie(name)
{
	var obj = getCookie(name);
	if (obj !=null)
	{
		//alert('cookie not null');
	}
	else 
	{
		//alert('cookie NULL');
		SetCookie(name , 'opened' , 5 , '/' , null , null);
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////