function openPopup(theURL, theName) {
	var theWidth = (arguments[2]) ? arguments[2] : 640;
	var theHeight = (arguments[3]) ? arguments[3] : 380;
	var opts = "fullscreen=no,menubar=no,width=" + theWidth + ",height=" + theHeight + ",status=yes,scrollbars=yes,resizable=yes,location=no,menubar=no,dependent=yes";
	var	wb = window.open(theURL, theName, opts);	
	if(wb)
	{
		wb.focus();	
	}
}

function selectSingleUserPopup(param) {
	if(!param)
	{
		var theUrl = "..\\usermanagment\\userslist.aspx?p=t";	
		openPopup(theUrl, "singlecontact");	
	}
	else
	{
		var theUrl = "..\\usermanagment\\userslist.aspx?p=t&m=" + param;	
		openPopup(theUrl, "singlecontact");	
	}
}

function selectStaticPage() {
		var theUrl = "..\\staticpages\\staticpageslist.aspx?p=t";	
		openPopup(theUrl, "StaticPage");	
}

function openNotice() {
	var theUrl = "..\\popup\\notice.html?t";	
	openPopup(theUrl, "singlecontact", "500", "400");	
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function HideDiv(theDiv)
{
	var theDiv = document.getElementById(theDiv);
	if(theDiv)
	{
		theDiv.style.visibility = 'hidden';
		theDiv.style.display = 'none';
	}
}

function pausecomp(millis)
{
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); }
	while(curDate-date < millis);
}


function goMessModify(theFieldName, theUrl)
{
	var checkedValue = '';
	var checkedCount = 0;
    var i=0;

	for (; i<document.mainform.elements.length; ++i) {
		var theObj = document.mainform.elements[i];
		if (theObj.name == theFieldName  && theObj.type == 'checkbox') {
			if (theObj.checked)
			{
				checkedValue = theObj.value;
				++checkedCount;
			}
		}
	}

	if (checkedCount < 1) {
		alert(notSelectedAnyText);
		return false;
	}
	if (checkedCount > 1) {
		alert(selectedManyText);
		return false;
	}

	theUrl += checkedValue;
	openInChildTab(theUrl, '');
}

function deleteConfirmation(theFieldName) {
	var checkedCount = 0;
    var i=0;
	for (; i<document.mainform.elements.length; ++i) {
		var theObj = document.mainform.elements[i];
		if (theObj.name == theFieldName  &&  theObj.type == 'checkbox') {
			if (theObj.checked) {
				++checkedCount;
			}
		}
	}

	if (checkedCount < 1) {
		alert(notSelectedAnyText);
		return false;
	}
	if (checkedCount > 1) {
		if (!confirm(massUpdateText)) {
			return false;
		}
	}
	if (checkedCount == 1) {
		if (!confirm(deleteText)) {
			return false;
		}
	}
	return true;
}


function onlyDigits(evt)
{    	
    var keycode;
     if (evt)
	    ;
    else if (window.event)
        evt = window.event;
    else if (event)
        evt = event;
    else
        return true;

    if (evt.charCode)
        keycode = evt.charCode;
    else if (evt.keyCode)
        keycode = evt.keyCode;
    else if (evt.which)
        keycode = evt.which;
    else
        keycode = 0;
    
    if(keycode == 8)
        return true;
        
    if(keycode >= 13 && keycode <= 57)
        return true;
    else
        return false;
}

function calcTotal(price, unitprice, resultID)
{
	var price = convertToInt(price, 0);
	var unitprice = convertToInt(unitprice, 0);
	var total = 0;
	total = price*unitprice;
	document.getElementById(resultID).innerHTML = total.numberFormat("#,###");
}

function calcVisaTotal(price)
{
	var price = convertToInt(price, 0);
	if(price<100)
		return 0;
		
	var total = 0;	
	if(100 <= price && price<=500)
	{
		total = price + varVisaCost100;
	}
	else if(500<price && price<=1000)
	{	
		total = price + varVisaCost500;		
	}
	else if(1000 < price)
	{
		var percentValue = (price*varVisaPercent)/100;
		var totalPercentValue = percentValue;
		if(percentValue > varVisaMax)
			totalPercentValue = varVisaMax;			
		
		total = price + totalPercentValue;		
	}
	else
	{
		return 0;
	}
	return total;	
}

function calcOnlinePaymentTotal(price)
{
	var price = convertToInt(price, 0);
		
	var total = 0;	
	var percentValue = (price*varOnlinePaymentChargePersent)/100;			
	if(onlinePaymentMinCommission)
	{
		if(percentValue < onlinePaymentMinCommission)
		{
			percentValue = onlinePaymentMinCommission;
		}
	}
	
	total = price + percentValue;
	return total;	
}

function calcVisaTotalToman(price, currentcy, resultCurrencyID, resultTomanID)
{
	
	var currencyPrice = calcVisaTotal(price);
	var totalToman = 0;
	
	if(currentcy == "D")
	{
		totalToman = currencyPrice * varDollarPriceRate;
		document.getElementById("lbl" + resultTomanID).innerHTML = totalToman.numberFormat("#,###");
		document.getElementById("lbl" + resultCurrencyID).innerHTML = currencyPrice + " Dollar";
		
		document.getElementById("hid" + resultTomanID).value = totalToman;
		document.getElementById("hid" + resultCurrencyID).value = currencyPrice + " Dollar";		
	}
	else if(currentcy == "E")
	{
		totalToman = currencyPrice * varEuroPriceRate;
		document.getElementById("lbl" + resultTomanID).innerHTML = totalToman.numberFormat("#,###");
		document.getElementById("lbl" + resultCurrencyID).innerHTML = currencyPrice + " Euro";
		
		document.getElementById("hid" + resultTomanID).value = totalToman;
		document.getElementById("hid" + resultCurrencyID).value = currencyPrice + " Euro";		
	}
}

function calcOnlinePaymentToman(price, currentcy, resultCurrencyID, resultTomanID)
{	
	
	var currencyPrice = calcOnlinePaymentTotal(price);
	var totalToman = 0;
	
	if(currentcy == "D")
	{
		totalToman = currencyPrice * varDollarPriceRate;
		document.getElementById("lbl" + resultTomanID).innerHTML = totalToman.numberFormat("#,###");
		document.getElementById("lbl" + resultCurrencyID).innerHTML = currencyPrice + " Dollar";
		
		document.getElementById("hid" + resultTomanID).value = totalToman;
		document.getElementById("hid" + resultCurrencyID).value = currencyPrice + " Dollar";		
	}
	else if(currentcy == "E")
	{
		totalToman = currencyPrice * varEuroPriceRate;
		document.getElementById("lbl" + resultTomanID).innerHTML = totalToman.numberFormat("#,###");
		document.getElementById("lbl" + resultCurrencyID).innerHTML = currencyPrice + " Euro";
		
		document.getElementById("hid" + resultTomanID).value = totalToman;
		document.getElementById("hid" + resultCurrencyID).value = currencyPrice + " Euro";		
	}
}

function calcClickCount(initValue, clickCost, clickCountId)
{
	var varInitValue = convertToInt(initValue, 0);
	var varClickCost = convertToInt(clickCost, 0);
	if(varInitValue == 0 || varClickCost == 0)
	{
		document.getElementById(clickCountId).value = 0;
	}
	
	var totalClick = varInitValue/varClickCost;
	totalClick = convertToInt(totalClick, 0);
	document.getElementById(clickCountId).value = totalClick;
}



function convertToInt(strvalue, defaultValue)
{
	var ret = parseInt(strvalue);
	if(!isNaN(ret))
	{	
		return ret;
	}
	else
	{
		return defaultValue;
	}
}

function convertToFloat(strvalue, defaultValue)
{
	var ret = parseFloat(strvalue);
	if(!isNaN(ret))
	{	
		return ret;
	}
	else
	{
		return defaultValue;
	}
}

function editField(fieldID)
{
	theViewObj = document.getElementById('view_' + fieldID);
	theEditObj = document.getElementById('edit_' + fieldID);
	theEdlitLinkObj = document.getElementById('edlnk_' + fieldID);
	theCancelLinkObj = document.getElementById('cnlnk_' + fieldID);
	
	hideObject(theViewObj, theEdlitLinkObj);
	visibleObject(theEditObj, theCancelLinkObj);								
}

function cancelField(fieldID, lblField)
{
	theViewObj = document.getElementById('view_' + fieldID);
	theEditObj = document.getElementById('edit_' + fieldID);
	theEdlitLinkObj = document.getElementById('edlnk_' + fieldID);
	theCancelLinkObj = document.getElementById('cnlnk_' + fieldID);									
	thelblField = document.getElementById(lblField);
	thetxtField = document.getElementById(fieldID);
	
	thetxtField.value = thelblField.innerText;
	visibleObject(theViewObj, theEdlitLinkObj);
	hideObject(theEditObj, theCancelLinkObj);								
}

function hideObject()
{
  for (i=0 ; i < arguments.length; i++) 
  {
	arguments[i].style.visibility = "hidden";
	arguments[i].style.display = "none";
  }
}

function visibleObject()
{
  for (i=0 ; i < arguments.length; i++) 
  {
	arguments[i].style.visibility = "visible";
	arguments[i].style.display = "block";
  }								
}							
				