﻿/// <reference path="jquery-1.3.vsdoc.js" />
var path = $("[id$=m_hidPath]").val();
function delPdf(a_lPdfID) {
    $.ajax({
        type: "POST",
        url: path + "/Product/Actions.aspx",
        dataType: "json",
        success: onDelPdfSuccess,
        error: onDelPdfError,
        data: {
            pdfID: a_lPdfID,
            action: "delPdf"
        }
    });

    function onDelPdfSuccess(response, statusText) {
        if (response.ok) {
            $("[id$=m_inPdf"+a_lPdfID+"]").parent().hide();
        }
        else {
            statusBar.showError(response.status);
        }
    };

    function onDelPdfError(xhr, textStatus, errorThrown) {
        statusBar.showError('Wystąpił błąd');
    };
    
    return false;
};

function showMoreDescription()
{
    var lbButton = $("[id$=m_lbShowMoreDesc]");
    var pDesc = $("[id$=m_pDesc]");
    var spanDesc = $("[id$=m_spanDesc]");
    if (lbButton.text() == "więcej")
    {
        lbButton.text("ukryj");
        pDesc.hide();
        spanDesc.show();
        return false;
    }
    else (lbButton.text() == "ukryj")
    {
        lbButton.text("więcej");
        pDesc.show();
        spanDesc.hide();
        return false;
    }
};


function delPhoto(a_lPhotoID) {
    $.ajax({
        type: "POST",
        url: path + "/Product/Actions.aspx",
        dataType: "json",
        success: onDelPhotoSuccess,
        error: onDelPhotoError,
        data: {
            photoID: a_lPhotoID,
            action: "delPhoto"
        }
    });

    function onDelPhotoSuccess(response, statusText) {
        if (response.ok) {
            $("[id$=m_imgPhoto"+a_lPhotoID+"]").parent().hide();
        }
        else {
            statusBar.showError(response.status);
        }
    };

    function onDelPhotoError(xhr, textStatus, errorThrown) {
        statusBar.showError('Wystąpił błąd');
    };
    
    return false;
};

function askPriceOrProduct(a_lProductID, a_sTitle, a_iWidth, a_iHeight)
{
    var lProductID = a_lProductID;
    var sTitle = a_sTitle;
    var iWidth = a_iWidth;
    var iHeight = a_iHeight;
    $("[id$=divQuestion]").show();
    $("[id$=divWaitingPro]").show();
    $("[id$=divQuestion]").prependTo($(document.body)).addClass("div-to-remove");
            var windowWidth = document.documentElement.clientWidth;  
            var windowHeight = document.documentElement.clientHeight;  
            $("[id$=divQuestion]").css({  
                            "position": "absolute",  
                              "top": getScrollXY()[1] + windowHeight/2-iHeight/2,  
                              "left": windowWidth/2-iWidth/2,
                              "width": iWidth + "px"
            });
    $.ajax({
        type: "POST",
        url: path + "/Product/Actions.aspx",
        dataType: "json",
        success: onAskPriceOrProductSuccess,
        error: onAskPriceOrProductoError,
        data: {
            productID: lProductID,
            paymentType: "askForProduct"
        }
    });
    
    function onAskPriceOrProductSuccess(response, statusText) {
        $("[id$=divWaitingPro]").hide();
        if (response.ok) {
            $('[id$=m_taDesc]').val(''); 
            $('[id$=m_labWrongMail]').text('');
            $("[id$=m_hidProductID]").val(lProductID);
            $("[id$=m_hidTitleQuestion]").val(sTitle);
            if ($('[id*=divComAddress]', $('[id$=divContentQuestion]')).length > 0)
            {
                $('[id*=divComAddress]', $('[id$=divContentQuestion]')).remove();
            }
            $("[id$=divComAddress" + lProductID + "]").clone().prependTo($("[id$=divContentQuestion]"));
            $("[id$=divComAddress" + lProductID + "]", $("[id$=divContentQuestion]")).show();
            $("[id$=m_h3Question]").text(sTitle);
        }
        else {
            statusBar.showError(response.status);
        }
    };

    function onAskPriceOrProductoError(xhr, textStatus, errorThrown) {
        $("[id$=divWaitingPro]").hide();
        $("[id$=divQuestion]").hide();
        statusBar.showError('Wystąpił błąd');
    };
    
    return false;
};

function SetQuestion()
{
    $("[id$=m_hidQuestionContent]").val($("[id$=m_taDesc]").val());
    $("[id$=m_hidEmail]").val($("[id$=m_tbEmail]").val());
    $("[id$=m_hidName]").val($("[id$=m_tbName]").val());
    $("[id$=m_hidPhone]").val($("[id$=m_tbPhone]").val());
    $("[id$=m_hidNewsletter]").val($("[id$=m_chbNewsletter]").attr('checked'));
    $("[id$=divWaitingPro]").show();
    ValidateForm();
}

function ValidateForm()
{
    var email = $("[id$=m_tbEmail]");
    var emailInfo =$("[id$=m_labWrongMail]");
    var name = $("[id$=m_tbName]");
    var nameInfo =$("[id$=m_labWrongName]");
    var phone = $("[id$=m_tbPhone]");
    var phoneInfo =$("[id$=m_labWrongPhone]");
    var bReturn = true;
    
    
    //email not valid
    if(email.val().length <= 1){  
        emailInfo.text("Musisz podać email!");
        bReturn = false;  
    }
    else if(!isValidEmailAddress(email.val())){  
        emailInfo.text("Niepoprawny email!");
        bReturn = false;  
    }  
    else
    {  
        emailInfo.text("");  
    }

    if(name.val().length <= 1){  
        nameInfo.text("Musisz podać imię i nazwisko!");
        bReturn = false; 
    }
    else{  
        nameInfo.text("");  
    }

    if(phone.val().length <= 1){  
        phoneInfo.text("Musisz podać telefon!");
        bReturn = false;  
    } 
    else
    {  
        phoneInfo.text("");  
    }
    return bReturn;
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^([\w.])([\w-\.])+\@(([\w-])+\.)[a-z]{2,}/);
	return pattern.test(emailAddress);
	}  
