﻿
var TQSStatus = 
{
		Pending : "Pending",
		Waiting : "Waiting",
		Canceled : "Canceled",
		Active : "Active",
		Paused : "Paused",
		Closed : "Closed",
		Inactive : "Inactive"	
}

function getChoosenSurveyID(onlyone)
{   
    var arr = new Array();
    var sDelimiter = (onlyone) ? "" : "|";
    var arrInputs = document.getElementById('SurveyManagmentTable').getElementsByTagName('INPUT');
    for (var i = 0; i < arrInputs.length; i++)
    {
        if (arrInputs[i].checked)
        {
            arr[arr.length] = arrInputs[i].attributes["surveyid"].value;
            if (onlyone)
                break;
        }
    }
    return arr.join(sDelimiter);
}

function displaySurveyForTest(isurveyid)
{
    var sSurveyID;
    var sUrl = "";
    if (isurveyid == null)
    {
        sSurveyID = getChoosenSurveyID(true);
    }
    else
    {
        sSurveyID = isurveyid;
    }

    if (sSurveyID == "")
        return;

    displaySurvey(sSurveyID, sSurveyID , document.getElementById('surveyname_' + sSurveyID).innerHTML,'1','0',this,3);
}

function duplicateSurvey(isurveyid)
{
    var sSurveyID;
    var sUrl = "";
    if (isurveyid == null)
    {
        sSurveyID = getChoosenSurveyID(true);
    }
    else
    {
        sSurveyID = isurveyid;
    }

    if (sSurveyID == "")
        return;

    sUrl = "ManageSurveyActions.aspx?action=1&surveyid=" + sSurveyID;
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		return;
	}
	
	document.body.style.cursor = 'wait';
	xmlHttp.onreadystatechange=function()	
	{
		if (xmlHttp.readyState==4)
		{
			document.body.style.cursor = 'auto';
	        window.location.reload();
			xmlHttp = null;
		}
	};
	xmlHttp.open("GET",sUrl,true);
	xmlHttp.send(null);    
}

function freeSurvey(isurveyid)
{
    var sSurveyID;
    var sUrl = "";
    if (isurveyid == null)
    {
        sSurveyID = getChoosenSurveyID(true);
    }
    else
    {
        sSurveyID = isurveyid;
    }

    if (sSurveyID == "")
        return;

    sUrl = "ManageSurveyActions.aspx?action=3&surveyid=" + sSurveyID;
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		return;
	}
	
	document.body.style.cursor = 'wait';
	xmlHttp.onreadystatechange=function()	
	{
		if (xmlHttp.readyState==4)
		{
			document.body.style.cursor = 'auto';
	        window.location.reload();
			xmlHttp = null;
		}
	};
	xmlHttp.open("GET",sUrl,true);
	xmlHttp.send(null);    
}


function deleteSurvey(isurveyid)
{
    if(!confirm(GetLocalizedString(50) + '?')) return;
    var sSurveyID;
    var sUrl = "";
    if (isurveyid == null)
    {
        sSurveyID = getChoosenSurveyID(false);
    }
    else
    {
        sSurveyID = isurveyid;
    }

    if (sSurveyID == "")
        return;

    sUrl = "ManageSurveyActions.aspx?action=2&surveyid=" + sSurveyID;
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		return;
	}
	
	document.body.style.cursor = 'wait';
	xmlHttp.onreadystatechange=function()	
	{
		if (xmlHttp.readyState==4)
		{
			document.body.style.cursor = 'auto';
	        window.location.reload();
			xmlHttp = null;
		}
	};
	xmlHttp.open("GET",sUrl,true);
	xmlHttp.send(null);    
}

function PauseResume(isurveyid)
{
    var sSurveyID = isurveyid;;
    var sUrl = "";

    if (sSurveyID == "")
        return;

    sUrl = "ManageSurveyActions.aspx?action=4&surveyid=" + sSurveyID;
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		return;
	}
	
	document.body.style.cursor = 'wait';
	xmlHttp.onreadystatechange=function()	
	{
		if (xmlHttp.readyState==4)
		{
			document.body.style.cursor = 'auto';
	        window.location.reload();
			xmlHttp = null;
		}
	};
	xmlHttp.open("GET",sUrl,true);
	xmlHttp.send(null);    
}

function BuyTQS()
{
    var sSurveyID;
    var sUrl = "";
    sSurveyID = document.getElementById('paysurveyidlist').value;
    
    if (sSurveyID == "")
        return;

    sUrl = "ManageSurveyActions.aspx?action=5&surveyid=" + sSurveyID;
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		return;
	}
	
	document.body.style.cursor = 'wait';
	xmlHttp.onreadystatechange=function()	
	{
		if (xmlHttp.readyState==4)
		{
			document.body.style.cursor = 'auto';
	        window.location.reload();
			xmlHttp = null;
		}
	};
	xmlHttp.open("GET",sUrl,true);
	xmlHttp.send(null);    
}


function payment(isurveyid)
{
    var sSurveyID;
    var sUrl = "";
    if (isurveyid == null)
    {
        sSurveyID = getChoosenSurveyID(false);
    }
    else
    {
        sSurveyID = isurveyid;
    }

    if (sSurveyID == "")
        return;

    if (document.getElementById('paysurveyidlist') != null)  
        document.getElementById('paysurveyidlist').value = sSurveyID;
    
    sUrl = "PaySurvey.aspx?surveys=" + sSurveyID;
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		return;
	}
	
	document.body.style.cursor = 'wait';
	xmlHttp.onreadystatechange=function()	
	{
		if (xmlHttp.readyState==4)
		{
			document.body.style.cursor = 'auto';
			var sResp = xmlHttp.responseText;
			switch(sResp)
			{
				case "Error":
				{
				    if (document.getElementById('MessageDiv') != null) 
				        document.getElementById('MessageDiv').innerHTML = getError(95);
				    else
				        document.getElementById('PaySurveyDiv').innerHTML = getError(95);
 					break;
				}
				default:
				{
                    document.getElementById('PaySurveyDiv').innerHTML = sResp;
					break;
				}
			}
			xmlHttp = null;
			document.getElementById('PaySurveyDiv').scrollIntoView(true);
		}
	};
	xmlHttp.open("GET",sUrl,true);
	xmlHttp.send(null);    
}


function getExportHTML(isurveyid)
{
    var sSurveyID;
    var sUrl = "";
    if (isurveyid == null) sSurveyID = getChoosenSurveyID(true);
    else sSurveyID = isurveyid;
    if (sSurveyID == "") return;
    
    ToggleDetailsDiv('EditDetails');
    todiv = document.getElementById('clientdetails');
    todiv.innerHTML = loadwaitSurvey();   

    sUrl = "ManageSurveyActions.aspx?action=9&surveyid=" + sSurveyID + "&name=" + encodeURIComponent(document.getElementById('surveyname_' + sSurveyID).innerHTML);
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		return;
	}
	
	document.body.style.cursor = 'wait';
	xmlHttp.onreadystatechange=function()	
	{
		if (xmlHttp.readyState==4)
		{
			document.body.style.cursor = 'auto';
			todiv.innerHTML = xmlHttp.responseText;
			xmlHttp = null;
		}
	};
	xmlHttp.open("GET",sUrl,true);
	xmlHttp.send(null);    
}

function SetButtonsStatus(obj)
{
    
    var arrButtons = obj.getElementsByTagName('A');
    var sIDSelected = getChoosenSurveyID(false);
    var arrCBSelected = sIDSelected == '' ? new Array() : sIDSelected.split('|');
    
	toggleButton(arrButtons[1], arrCBSelected.length > 0 && containOny(TQSStatus.Pending, arrCBSelected)); //buy (activate)
	toggleButton(arrButtons[2], arrCBSelected.length > 0 && containOny(TQSStatus.Pending, arrCBSelected)); //delete
	toggleButton(arrButtons[3], arrCBSelected.length == 1); //duplicate
	toggleButton(arrButtons[4], arrCBSelected.length == 1 && containOny(TQSStatus.Active, arrCBSelected)); //export

	if (arrButtons.length > 5)
	    toggleButton(arrButtons[5], arrCBSelected.length == 1 && containOny(TQSStatus.Pending, arrCBSelected)); //free 
}

function containOny(sProp, oArr)
{
	var bReturn = true;
	for (var i = 0; i < oArr.length; i++)	
	{
		bReturn = bReturn && document.getElementById('ch' + oArr[i]).getAttribute('value') == sProp;
	}
	return bReturn;
}

function toggleButton(onjBtn, bEnable)
{
	onjBtn.className = bEnable ? onjBtn.className.replace(/ disabled/g, '') : onjBtn.className + ' disabled';
	if( onjBtn.getAttribute('hrefback') == null || onjBtn.getAttribute('hrefback') == "") onjBtn.setAttribute('hrefback', onjBtn.getAttribute('href'));
	onjBtn.setAttribute('href', bEnable ? onjBtn.getAttribute('hrefback') : 'javascript:void(0)');
	
	//onjBtn.getAttribute() ? onjBtn.setAttribute();
}

function SetListButtonsStatus()
{
	var arrToolbars = document.getElementById('QSListButtonsDiv');
	var arrToolbars1 = document.getElementById('QSListButtonsDiv1');
    SetButtonsStatus(arrToolbars);
    SetButtonsStatus(arrToolbars1);
}

function verifyVat(vatnumber, vatname)
{
 	if ( (!ValidateTextLen(0, 50, vatnumber)) )
 	{
        document.getElementById('MessageDiv').innerHTML = getError(93);
        return;
    }
    
 	if ( (!ValidateTextLen(0, 50, vatname)) )
 	{
        document.getElementById('MessageDiv').innerHTML = getError(94);
        return;
    }

    if ((vatname=="") && (vatnumber==""))
    {
        document.getElementById('MessageDiv').innerHTML = getError(93);
        return;
    }
    sUrl = "ManageSurveyActions.aspx?action=8&vatnumber=" + vatnumber + '&vatname=' + vatname;
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		return;
	}
	
	document.body.style.cursor = 'wait';
	xmlHttp.onreadystatechange=function()	
	{
		if (xmlHttp.readyState==4)
		{
			document.body.style.cursor = 'auto';
			var sResp = xmlHttp.responseText;
			switch(sResp)
			{
				case "Error":
				{
				    document.getElementById('MessageDiv').innerHTML = getError(95);
 					break;
				}
				default:
				{
                    payment(document.getElementById('paysurveyidlist').value);
					break;
				}
			}
			document.getElementById('MessageDiv').scrollIntoView(true);
			xmlHttp = null;
		}
	};
	xmlHttp.open("GET",sUrl,true);
	xmlHttp.send(null);    
}


function verifyCoupon(surveyid, coupon)
{
 	if ( (!ValidateTextLen(1, 21, coupon)) )
 	{
        document.getElementById('MessageDiv').innerHTML = getError(92);
        return;
    }
            
    sUrl = "PaySurvey.aspx?survey=" + surveyid + '&coupon=' + coupon;
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		return;
	}
	
	document.body.style.cursor = 'wait';
	xmlHttp.onreadystatechange=function()	
	{
		if (xmlHttp.readyState==4)
		{
			document.body.style.cursor = 'auto';
			var sResp = xmlHttp.responseText;
			switch(sResp)
			{
			    case "refresh":
			    {
			        window.location.reload();
			        break;
			    }
				default:
				{
				    document.getElementById('MessageDiv').innerHTML = sResp;
					break;
				}
			}
			document.getElementById('MessageDiv').scrollIntoView(true);
			xmlHttp = null;
		}
	};
	xmlHttp.open("GET",sUrl,true);
	xmlHttp.send(null);    
}

function setTryFreeBanner()
{
	var oTryFreeBanner = document.getElementById('QSCPTryDivBottom');
	if(oTryFreeBanner != null)
	{
		var oLastButton = document.getElementById('QSListButtonsDiv1').lastChild;
		oTryFreeBanner.style.marginLeft = getXCoordinate(oLastButton) - getXCoordinate(oLastButton.parentNode) + 'px';
		oTryFreeBanner.style.visibility = "visible";	
	}
}

///////////////////////////////////////////////////
function pageInit()
{
	var sAlertPlaceId = 'lblErrMessage';
	var oErrorMessageSpan = document.getElementById(sAlertPlaceId);
	if(oErrorMessageSpan != null) if(oErrorMessageSpan.innerHTML.length > 0) oErrorMessageSpan.style.display = 'block'; 
	sorttable.init();
	setTryFreeBanner();
}
addEvent(window, 'load', pageInit);
///////////////////////////////////////

