﻿jQuery(document).ready(function() {
	jQuery("#frmDialog").dialog({
		autoOpen: false,
		modal: true,
        hide: 'fade',
		minHeight: 20,
		height: 'auto',
		width: 470,
		resizable: false,
        position: ['center', 100],
        closeOnEscape: false,
		open: function(event, ui) {
			jQuery(this).parent().appendTo("#frmDialogContainer");
		}
	});
});

function addtocart(bookID, title, btn1, btn2) {
	openDialog(title);
	var jazik = getParameterByName('lang');
	if(jazik == "") { jazik = "mac"; }
	jQuery("#frmDialog").block({
		message: '<img src="grafika/jQuery/loader.gif" />',
		css: { border: '0px' },
		overlayCSS: { backgroundColor: '#ffffff', opacity: 50 }
	});
	jQuery("#frmDialog").load('shopping.asp?lang=' + jazik + '&action=add&item=' + bookID + '&count=1', function (responsedata) {
		jQuery('#frmDialog').html(responsedata); 
		jQuery("#frmDialog").unblock();
	});
	jQuery('#frmDialog').dialog('option', 'buttons', [
		{ text: btn1,
			click: function () {
				window.location = 'cartstatus.asp?lang=' + jazik;
			}
		},
		{ text: btn2,
			click: function () {
				setTimeout(function(){
					jQuery("#frmDialog").dialog('close');
				}, 500)
			}
		}
	]);
	var btn1 = jQuery('.ui-dialog-buttonset').children('button')[0];
	jQuery(btn1).addClass('bs_cart');
	var btn2 = jQuery('.ui-dialog-buttonset').children('button')[1];
	jQuery(btn2).addClass('bs_continue');
}

function addpackage2cart(packageID, title, btn1, btn2) {
	openDialog(title);
	var jazik = getParameterByName('lang');
	if(jazik == "") { jazik = "mac"; }
	jQuery("#frmDialog").block({
		message: '<img src="grafika/jQuery/loader.gif" />',
		css: { border: '0px' },
		overlayCSS: { backgroundColor: '#ffffff', opacity: 50 }
	});
	jQuery("#frmDialog").load('shopping.asp?lang=' + jazik + '&action=addPkg&item=' + packageID + '&count=1', function (responsedata) {
		jQuery('#frmDialog').html(responsedata); 
		jQuery("#frmDialog").unblock();
	});
	jQuery('#frmDialog').dialog('option', 'buttons', [
		{ text: btn1,
			click: function () {
				window.location = 'cartstatus.asp?lang=' + jazik;
			}
		},
		{ text: btn2,
			click: function () {
				setTimeout(function(){
					jQuery("#frmDialog").dialog('close');
				}, 500)
			}
		}
	]);
	var btn1 = jQuery('.ui-dialog-buttonset').children('button')[0];
	jQuery(btn1).addClass('bs_cart');
	var btn2 = jQuery('.ui-dialog-buttonset').children('button')[1];
	jQuery(btn2).addClass('bs_continue');
}

function removefromcart(bookID, title, message, btn1, btn2) {
	openDialog(title);
	jQuery("#frmDialog").html("<p>" + message + "</p>");
	var jazik = getParameterByName('lang');
	if(jazik == "") { jazik = "mac"; }
	jQuery('#frmDialog').dialog('option', 'buttons', [
		{ text: btn1,
			click: function () {
				jQuery("#frmDialog").block({
					message: '<img src="grafika/jQuery/loader.gif" />',
					css: { border: '0px' },
					overlayCSS: { backgroundColor: '#ffffff', opacity: 50 }
				});
				jQuery("#frmDialog").load('shopping.asp?lang=' + jazik + '&action=del&item=' + bookID, function (responsedata) {
					jQuery("#ShowItemsInCart").html(responsedata);
					jQuery("#frmDialog").unblock();
					jQuery("#frmDialog").dialog('close');
				});
			}
		},
		{ text: btn2,
			click: function () {
				setTimeout(function(){
					jQuery("#frmDialog").dialog('close');
				}, 500)
			}
		}
	]);

	var btn1 = jQuery('.ui-dialog-buttonset').children('button')[0];
	jQuery(btn1).addClass('bs_cart2');
	var btn2 = jQuery('.ui-dialog-buttonset').children('button')[1];
	jQuery(btn2).addClass('bs_continue2');
}

function changeQuantity(objID) {
	var kolicina = jQuery('#objQuantity' + objID).val();
	var jazik = getParameterByName('lang');
	if(jazik == "") { jazik = "mac"; }
	
	jQuery.get('shopping.asp?lang=' + jazik + '&action=newQt&item=' + objID + '&count=' + kolicina, function (responsedata) {
		jQuery("#ShowItemsInCart").html(responsedata);
	});
}

function changeCurrency(newCurrency) {
	var jazik = getParameterByName('lang');	
	if(jazik == "") { jazik = "mac"; }
	jQuery.get('shopping.asp?lang=' + jazik + '&action=newCurrency&currency=' + newCurrency, function (responsedata) {
		jQuery("#ShowItemsInCart").html(responsedata);
	});
}

function changeLocation(newLocation) {
	var jazik = getParameterByName('lang');	
	if(jazik == "") { jazik = "mac"; }
	jQuery.get('shopping.asp?lang=' + jazik + '&action=newLocation&location=' + newLocation, function (responsedata) {
		jQuery("#ShowItemsInCart").html(responsedata);
	});
}

function changeProvider(newProvider) {
	for(i=0; i<=5; i++){
		jQuery("#provajder_" + i).hide();
	}
	jQuery("#provajder_" + newProvider).show();
}

function openDialogAndBlock(title) {
	openDialog(title);
	jQuery("#frmDialog").block({
		message: '<img src="grafika/jQuery/loader.gif" />',
		css: { border: '0px' },
		overlayCSS: { backgroundColor: '#ffffff', opacity: 50 } 
	});
}

function openDialog(title) {
	jQuery("#frmDialog").dialog("option", "title", title);			
	jQuery("#frmDialog").dialog('open');
}

function unblockDialog() {
	jQuery("#frmDialog").unblock();
}

function closeDialog() {
	jQuery("#frmDialog").dialog('close');
}

function closeDialogPause() {
    setTimeout(function(){
        $("#frmDialog").dialog('close');
    }, 500)
}

function getParameterByName(name) {
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function srediIsporaka(lokacija,jazik) {
	var value = jQuery("#isporaka").is(':checked');
	
	if(value == true){
		jQuery("#ispime").attr('disabled',false);
		jQuery("#ispprezime").attr('disabled',false);
		jQuery("#ispadresa").attr('disabled',false);
		jQuery("#ispmesto").attr('disabled',false);
		jQuery("#ispfah").attr('disabled',false);
		if(lokacija=='mac' && jazik=='mac'){
			jQuery("#ispdrzava").attr('disabled',true);
			jQuery("#ispdrzava").val("Македонија");
		} else if(lokacija=='mac' && jazik=='eng'){
			jQuery("#ispdrzava").attr('disabled',true);
			jQuery("#ispdrzava").val("Macedonia");
		} else {
			jQuery("#ispdrzava").attr('disabled',false);
		};
		jQuery("#drzava").attr('disabled',false);
    } else {
		jQuery("#ispime").attr('disabled',true);
		jQuery("#ispprezime").attr('disabled',true);
		jQuery("#ispadresa").attr('disabled',true);
		jQuery("#ispmesto").attr('disabled',true);
		jQuery("#ispfah").attr('disabled',true);
		jQuery("#ispdrzava").attr('disabled',true);
		if(lokacija=='mac' && jazik=='mac'){
			jQuery("#drzava").attr('disabled',true);
			jQuery("#drzava").val("Македонија");
		} else if(lokacija=='mac' && jazik=='eng'){
			jQuery("#drzava").attr('disabled',true);
			jQuery("#drzava").val("Macedonia");
		} else {
			jQuery("#drzava").attr('disabled',false);
		};
	};
}

function checkForm() {
	jQuery("#imagreska").html('0');
	jQuery("#checkout1 input").each(function () {
		jQuery("#s" + jQuery(this).attr('id')).removeClass("frmerr");
		jQuery("#s" + jQuery(this).attr('id')).addClass("frmnone");
		jQuery("#" + jQuery(this).attr('id')).css("background-color","");
		
		switch (jQuery(this).attr('id'))
		{
			case 'email':
				checkMail(jQuery(this).attr('id'));
				break;			
			default:
				checkString(jQuery(this).attr('id'));
		}
    });
	
	checkCombo('drzava');
	checkCombo('ispdrzava');

	if(!(jQuery("#soglasnost").attr('checked'))) { 
		jQuery("#ssoglasnost").removeClass("frmnone");
		jQuery("#ssoglasnost").addClass("frmerr");
		jQuery("#tdsoglasnost").css("background-color","#fcf");
		jQuery("#imagreska").html('1');
	} else {
		jQuery("#ssoglasnost").addClass("frmnone");
		jQuery("#ssoglasnost").removeClass("frmerr");
		jQuery("#tdsoglasnost").css("background-color","");
	}
	
	var value = jQuery("input:radio[@name='rgProvajder']:checked").val();
	if(value > 0){
		jQuery("#sprovajder").addClass("frmnone");
		jQuery("#sprovajder").removeClass("frmerr");
		jQuery("#provajder_0").css("background-color","");
		jQuery("#provajder_0").css("display","none");
	} else {
		jQuery("#sprovajder").removeClass("frmnone");
		jQuery("#sprovajder").addClass("frmerr");
		jQuery("#provajder_0").css("background-color","#fcf");
		jQuery("#provajder_0").css("display","block");
		jQuery("#imagreska").html('1');
	}
	
	if(jQuery("#imagreska").html()=="0"){ 
		jQuery('#c1').submit();
	}
}

function checkString(fieldID) {
	if(fieldID.substring(0,3)=="isp"){ 
		if(!(jQuery("#isporaka").attr('checked'))) { return; }
	}
	if(jQuery("#" + fieldID).val() == "") {
		jQuery("#s" + fieldID).removeClass("frmnone");
		jQuery("#s" + fieldID).addClass("frmerr");
		jQuery("#" + fieldID).css("background-color","#fcf");
		jQuery("#imagreska").html('1');
	}
}

function checkCombo(fieldID) {
	if(fieldID.substring(0,3)=="isp"){ 
		if(!(jQuery("#isporaka").attr('checked'))) { return; }
	}
	if(jQuery("#" + fieldID).val() == "-") {
		jQuery("#s" + fieldID).removeClass("frmnone");
		jQuery("#s" + fieldID).addClass("frmerr");
		jQuery("#" + fieldID).css("background-color","#fcf");
		jQuery("#imagreska").html('1');
	} else {
		jQuery("#s" + fieldID).addClass("frmnone");
		jQuery("#s" + fieldID).removeClass("frmerr");
		jQuery("#" + fieldID).css("background-color","");
	}
}

function checkMail(fieldID) {
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if( !emailReg.test( jQuery("#" + fieldID).val() ) || jQuery("#" + fieldID).val() == "" ) {
		jQuery("#s" + fieldID).removeClass("frmnone");
		jQuery("#s" + fieldID).addClass("frmerr");
		jQuery("#" + fieldID).css("background-color","#fcf");
		jQuery("#imagreska").html('1');
	}
}

function casys(tmpstring) {
	$.post("test.php", { name: "John", time: "2pm" },
   		function(data) {
     	alert("Data Loaded: " + data);
   });
}

