function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}

function checkShopEdit()
{
     var d = $('#Data').serialize();
     var send = $.ajax
     ({
            type: "post",
            url: "/tmp.php",
            data: d,
            dataType: "json",
            success: msg
     });
}


function checkShopEditAdmin()
{
     var d = $('#Data').serialize();
     var send = $.ajax
     ({
            type: "post",
            url: "/tmp_admin.php",
            data: d,
            dataType: "json",
            success: msg
     });
}


function msg(oReq)
{
	var json = oReq;

	$('#err').html(json.err);
	$('#name').value = json.name;
	$('#shop_url').value = json.shop_url;
	$('#success_url').value = json.success_url;
	$('#success_url_method').selectedIndex = json.success_url_method;
	$('#fail_url').value = json.fail_url;
	$('#fail_url_method').selectedIndex = json.fail_url_method;
	$('#status_url').value = json.status_url;
	$('#status_url_method').selectedIndex = json.status_url_method;
	$('#notification_number').value = json.notification_number;
	$('#sms').value = json.sms;
	$('#notification_text').value = json.notification_text;
	$('#fees_payer').selectedIndex = json.fees_payer;
	$('#currency_id').selectedIndex = json.currency_id;
	$('#currency_exch').value = json.currency_exch;
	$('#secret_key').value = json.secret_key;
	scrollTo(0,200);
}


function removeEmail(num, shop_id)
{
	var d = num;
	d = d + "*" + shop_id + "*" + document.getElementById("e"+num).value; 
	var send = $.ajax
	({
        	type: "get",
	        url: "/tmp_remove.php",
        	data: "q=" + d,
	        dataType: "json",
	        success: refreshRemove
	});
}


function addEmail(shop_id)
{
	var d;
	d =  shop_id + "*" + document.getElementById("new_email").value; 
	var send = $.ajax
	({
        	type: "get",
	        url: "/tmp_add.php",
        	data: "q=" + d,
	        dataType: "json",
	        success: refreshRemove
	});
}


function refreshRemove(oReq)
{
	var json = oReq;

	var arr = new Array();
	arr = json.email;
	var txt = '';
	for (k in arr)
	{
		var tmp_k = k;
		if (IsNumeric(tmp_k))
		{
			txt = txt + '<span>' + 
				'<input type="text" name="notification_email[]" value="' + arr[k] + '" id="e' + (parseInt(k)+1) + '" class="form" >' + 
				'<span id="' + (parseInt(k)+1) + '" onClick="removeEmail(' + (parseInt(k)+1) + ', ' +  json.shop_id + ')"><img src="/img/icons/delete.png" style="cursor: pointer; cursor: hand; border:0;" title="Удалить"></span>' + 
				'</span> </br>';
		}
	}
	txt = txt + '<span><input type="text" name="notification_email[]" value="" id="new_email" class="form"  >' + 
				'<span onClick="addEmail(' + json.shop_id + ')"><img src="/img/icons/add.png" style="cursor: pointer; cursor: hand; border:0;" title="Добавить"></span>' + 
		     '</span> </br>';
	$('#emails').html(txt);

	$('#err').html(json.err);
	scrollTo(0,200);
}




function removeIcq(num, shop_id)
{
	var d = num;
	d = d + "*" + shop_id + "*" + document.getElementById("i"+num).value; 
	var send = $.ajax
	({
        	type: "get",
	        url: "/tmpicq_remove.php",
        	data: "q=" + d,
	        dataType: "json",
	        success: refreshRemoveIcq
	});
}


function addIcq(shop_id)
{
	var d;
	d =  shop_id + "*" + document.getElementById("new_icq").value; 
	var send = $.ajax
	({
        	type: "get",
	        url: "/tmpicq_add.php",
        	data: "q=" + d,
	        dataType: "json",
	        success: refreshRemoveIcq
	});
}


function refreshRemoveIcq(oReq)
{
	var json = oReq;

	var arr = new Array();
	arr = json.icq;
	var txt = '';
	for (k in arr)
	{
		var tmp_k = k;
		if (IsNumeric(tmp_k))
		{

			txt = txt + '<span>' + 
				'<input type="text" name="notification_icq[]" value="' + arr[k] + '" id="i' + (parseInt(k)+1) + '" class="form" >' + 
				'<span id="' + (parseInt(k)+1) + '" onClick="removeIcq(' + (parseInt(k)+1) + ', ' +  json.shop_id + ')"><img src="/img/icons/delete.png" style="cursor: pointer; cursor: hand; border:0;" title="Удалить"></span>' + 
				'</span> </br>';
		}
	}
	txt = txt + '<span><input type="text" name="notification_icq[]" value="" id="new_icq" class="form"  >' + 
				'<span onClick="addIcq(' + json.shop_id + ')"><img src="/img/icons/add.png" style="cursor: pointer; cursor: hand; border:0;" title="Добавить"></span>' + 
		     '</span> </br>';
	$('#icqs').html(txt);

	$('#err').html(json.err);
	scrollTo(0,200);
}

function genereteKEY(shop_id)
{
	var send = $.ajax
	({
        	type: "get",
	        url: "/tmp_generate_key.php",
        	data: "q=" + shop_id,
	        dataType: "json",
	        success: refreshGenerate
	});
}

function refreshGenerate(oReq)
{
	var json = oReq;

	secret_key = json.secret_key;
	var txt = '';
	txt = txt + secret_key;
//alert(txt);
//	$('#secret_key').html(txt);
	$('#secret_key').attr('value', txt);

	$('#err').html(json.err);
	scrollTo(0,200);
}

function showBlock()
{
	var elem = document.getElementById("link");
	elem.style.display="none";

	elem = document.getElementById("block1");
	elem.style.display="block";
}

function saveReferral(id)
{
	var d = document.getElementById('referral_id').value;
	if (d!='')
	{
		var data = id + '*' + d;
		var send = $.ajax
		({
		    type: "post",
	            url: "/tmp_referral.php",
  		    data: "q=" + data,
                    dataType: "json",
                    success: msg_referral
		});
	}
}

function msg_referral(oReq)
{
	var json = oReq;
	$('#err').html(json.err);
	var referral_id = json.referral_id;
	var status = json.status;
	if (status=='ok')
	{
		var elem = document.getElementById("link");
		elem.style.display="none";

		elem = document.getElementById("block1");
		elem.style.display="none";
	}
	
	scrollTo(0,200);
}

