﻿
function pageLoad() 
{
    AdvertService.set_defaultSucceededCallback(AdvertCallback);
    AdvertCommentService.set_defaultSucceededCallback(AdvertCommentCallback);
    
    AdvertService.GetRating($get('hdAdvertID').value);

    if ($get('divComments'))
        ListComments();
        
    //LoadMap();
}

function GetServerVar(clientID)
{
    return $get($get("hdModuleID").value + "_" + clientID);
}

function ImposeMaxLength(e, txt, maxLen)
{
    if (!e) e = window.event;
    
    var key = e.keyCode || e.which;
    if ((key > 32 && key < 41) || (key == 8) || (key == 46))
        return true;
    
    if (txt.value.length > maxLen+1)
        txt.value = txt.value.substring(0,maxLen);
    
    return (txt.value.length <= maxLen);
}

function DisplayReportAdvert()
{
	if ($get('divReportAdvert').style.display == "block")
	{
    	$get('divReportAdvert').style.display = "none";
    	$get('divSendFriend').style.display = 'none';
    	$get('divContactSeller').style.display = 'none';
		GetServerVar('aReportAdvert').className = '';
		GetServerVar('aSendLinkFriend').className = '';
	}
	else
	{
    	$get('divReportAdvert').style.display = "block";
    	$get('divSendFriend').style.display = 'none';
    	$get('divContactSeller').style.display = 'none';
		GetServerVar('aReportAdvert').className = 'Selected';
		GetServerVar('aSendLinkFriend').className = '';		
	}
}

function DisplaySendFriend()
{
	if ($get('divSendFriend').style.display == "block")
	{
    $get('divReportAdvert').style.display = 'none';
    $get('divSendFriend').style.display = 'none';
    $get('divContactSeller').style.display = 'none';
   	GetServerVar('aReportAdvert').className = '';
	GetServerVar('aSendLinkFriend').className = '';
	}
	else
	{
    $get('divReportAdvert').style.display = 'none';
    $get('divSendFriend').style.display = 'block';
    $get('divContactSeller').style.display = 'none';
   	GetServerVar('aReportAdvert').className = '';
	GetServerVar('aSendLinkFriend').className = 'Selected';		
	}
}

function DisplayContactSeller()
{
	if ($get('divContactSeller').style.display == 'none')
	{
	    $get('divReportAdvert').style.display = 'none';
        $get('divSendFriend').style.display = 'none';
        $get('divContactSeller').style.display = 'block';
	}
	else
	{
	    $get('divReportAdvert').style.display = 'none';
        $get('divSendFriend').style.display = 'none';
        $get('divContactSeller').style.display = 'none';		
	}
}

function ReportAdvert()
{
    if (Page_ClientValidate('ReportAdvert'))
        AdvertService.ReportAdvert($get('hdAdvertID').value,$get('hdUserID').value, GetServerVar('txtReason').value, window.location.href);
}

function SendFriend()
{
    if (Page_ClientValidate('SendFriend'))
    {
        AdvertService.SendFriend(GetServerVar('txtName').value, GetServerVar('txtEmail').value, GetServerVar('txtFriendsEmail').value, GetServerVar('txtFriendComment').value, window.location.host, window.location.href, $get('hdAdvertID').value, $get('hdTabID').value);
        
    }
}

function ContactSeller()
{
    if (Page_ClientValidate('ContactSeller'))
    {
        var captcha = document.getElementsByName(($get("hdModuleID").value + "_captchaSeller").replace(/_/g, "$"))[0];
        AdvertService.ContactSeller(GetServerVar('txtName2').value, GetServerVar('txtEmail2').value, GetServerVar('txtNumber').value, $get('hdTabID').value, $get('hdAdvertID').value, $get('hdAdvertUserID').value, GetServerVar('txtMessage').value, "", window.location.host, window.location.href,true);
        $get('aContact').style.display = '';
        
    }
}

function Rate(rating)
{
    AdvertService.Rate($get('hdAdvertID').value, rating);
    $get('aGood').style.display = 'none';
    $get('aBad').style.display = 'none';
    
    if (rating)
    {
        var good = parseInt($get('spnGood').innerHTML);
        $get('spnGood').innerHTML = good+1;
    }
    else
    {
        var bad = parseInt($get('spnBad').innerHTML);
        $get('spnBad').innerHTML = bad+1;
    }
}

function ListComments()
{
    AdvertCommentService.List($get('hdAdvertID').value);
   
    ShowIndicator();
}

function AddComment()
{
    var comment = $get('txtComment');
    
    if (comment.value.length > 0)
    {
        AdvertCommentService.Add($get('hdTabID').value, $get('hdUserID').value, $get('hdAdvertID').value, $get('hdAdvertUserID').value, comment.value, window.location.host,$get('hdUserDisplayName').value);
        comment.value = '';
    }
}

function DeleteComment(commentID)
{
    if(confirm ("Are you sure you wish to delete this comment?"))
    {
        AdvertCommentService.Delete($get('hdUserID').value, $get('hdAdvertID').value, commentID);
        var comment = $get('divComment' + commentID);
        comment.parentNode.removeChild(comment);
        $get('divTotal').innerHTML = parseInt($get('divTotal').innerHTML) - 1;
    }
}

function BuildComment(i,template,pageUrl)
{
    i.CreateDate = i.CreateDate.format('dd MMM yyyy HH:mm tt');
    var deleteStyle = 'none';
    if ($get('hdIsEditable').value == "True")
        deleteStyle = 'inline-block';
        
    return parseTemplate(template, {i: i, extra: { DeleteStyle: deleteStyle  }});	
}

function AdvertCommentCallback(result, userContext, methodName)
{
    var template = $get("CommentTemplate").innerHTML;
                
    switch(methodName)
    {
        case ("List"):
        {
            $get('divTotal').innerHTML = result.length;
            BuildGrid(template, result, $get("divComments"), BuildComment, "No comments found");
            
            break;
        }
        
        case ("Add"):
        {
            if ($get("divComments").innerHTML == "No comments found")
                $get("divComments").innerHTML = "";
                
            $get("divComments").innerHTML += BuildComment(result, template, '');
            $get('divTotal').innerHTML = parseInt($get('divTotal').innerHTML) + 1;
            break;
        }
    }
}

function AdvertCallback(result, userContext, methodName)
{
    switch(methodName)
    {
        case ("GetRating"):
        {
            if ($get('hdRated').value != "false")
            {
                $get('aGood').style.display = 'none';
                $get('aBad').style.display = 'none';
            }
            
            $get('spnGood').innerHTML = result.Good;
            $get('spnBad').innerHTML = result.Bad;
            
            break;
        }
        
        case ("ReportAdvert"):
        {
            $get('spnReport').innerHTML = result;
            break;
        }
        
        case ("SendFriend"):
        {
            $get('spnMessage').innerHTML = result;
            break;
        }
        
        case ("ContactSeller"):
        {
            $get('spnSellerMessage').innerHTML = result;
            break;
        }
    }
}