// common js file v2.0

$('#searchForm').ready(function(){
	var p = $('#searchForm').offset();
	$('#searchHint').css('left',p.left).css('top',p.top+16);	
});

$(window).resize(function(){
	var p = $('#searchForm').offset();
	$('#searchHint').css('left',p.left).css('top',p.top+16);	
});

function checkPriceOverviewForm(frm) {
	var anum=/(^\d+$)|(^\d+\.\d+$)/;
	 if (!anum.test(frm.width.value) || !anum.test(frm.drop.value)) {
	 	alert('Please enter your sizes in the width and drop boxes. Also, please note that the sizes are in millimeters!');
	 	frm.width.focus();
	 	return(false);
	 } else {
	 	if (frm.width.value < 300) {
	 		alert('The width minimum value is 300 millimeters.');
	 		frm.width.focus();
	 		return(false);
	 	}
	 	if (frm.drop.value < 300) {
	 		alert('The drop minimum value is 300 millimeters');
	 		frm.drop.focus();
	 		return(false);
	 	}
	 	frm.submit();
	 }
}

function searchHint(act) {
	if (act == 'show') {
		$("#searchHint").slideDown(500);
	} else {
		$("#searchHint").slideUp(500);
	}
}

function showConvertor() {
	$('#convertorForm').fadeIn(500,function(){
			$.scrollTo('#convertorForm',{ speed:1500, queue:true, axis:'y' });
			$('#converterwidth').focus();
	});
}

function convert() {
	var anum=/(^\d+$)|(^\d+\.\d+$)/
    if (!anum.test($('#converterwidth').attr('value')) || !anum.test($('#converterdrop').attr('value'))) {
    	alert('Please use numeric values!');
    	$('#converterwidth').focus();
    } else {
		var typeofconv = $('#typeofconversion').attr('value');
		if (typeofconv == 'inch2mm') {
			$('#converterrwidth').attr('value',Math.round($('#converterwidth').attr('value')*25.4));
			$('#converterrdrop').attr('value',Math.round($('#converterdrop').attr('value')*25.4));
		} else {
			$('#converterrwidth').attr('value',Math.round($('#converterwidth').attr('value')*10));
			$('#converterrdrop').attr('value',Math.round($('#converterdrop').attr('value')*10));
		}
    }
}

function copyResults() {
	$('#width').attr('value',$('#converterrwidth').attr('value'));
	$('#drop').attr('value',$('#converterrdrop').attr('value'));
	hidePrice();
}

function is_numeric( mixed_var ) {
    return !isNaN( mixed_var );
}


function getPrice() {	
	var ok = 1;
	var errorMsg = '';	
	if ($("#width").attr('value') == undefined) {
		$('#widthHolder').addClass('error');
		errorMsg += '<li>Please enter your window width value in mm</li>';
		ok = 0;
	}
	if ($("#drop").attr('value') == undefined) {
		$('#dropHolder').addClass('error');
		errorMsg += '<li>Please enter your window drop value in mm</li>';
		ok = 0;
	}
	if ($("#width").attr('value') < 300) {
		$('#widthHolder').addClass('error');
		errorMsg += '<li>Your width value is too small. The minimum size is 300 mm</li>';
		ok = 0;
	}
	if ($("#drop").attr('value') < 300) {
		$('#dropHolder').addClass('error');
		errorMsg += '<li>Your drop value is too small. The minimum size is 300 mm</li>';
		ok = 0;
	}
	if (!is_numeric($("#width").attr('value'))) {
		$('#widthHolder').addClass('error');
		errorMsg += '<li>Your width value is not numeric.</li>';
		ok = 0;
	}
	if (!is_numeric($("#drop").attr('value'))) {
		$('#dropHolder').addClass('error');
		errorMsg += '<li>Your drop value is not numeric.</li>';
		ok = 0;
	}
	if (ok == 1) {
		$('#widthHolder').removeClass('error');
		$('#dropHolder').removeClass('error');
		$('#colourLoader').show();
		var pars = $("#colourForm").serialize();
		$.ajax({
			url:'/ajaxRequest.html?function=getPrice',
			dataType:'json',
			data:pars,
			success:function(data) {
				if (data.status == 'success') {
					$("#priceValue").html(data.priceValue);
					$("#containerPriceAll").slideDown(500);
					$('#getPriceButton').slideUp(500);
				} else {
					$("#containerError").html(data.priceValue).slideDown(500);
				}
				$('#colourLoader').hide();				
			},
			error:function() {
				alert('Unkwnown error. Please contact an administrator! Thank you!');
				$('#colourLoader').hide();
				hidePrice();
			}
		});
	} else {		
		$('#containerError').hide().html('<div id="errorMsg"><h3>Please note:</h3><ol>'+errorMsg+'</ol></div>').slideDown(1000,function(){hidePrice();});
	}
}

function hidePrice() {
	$('#containerPriceAll').slideUp(500);
	$('#getPriceButton').slideDown(500);
}
function hideError() {	
	$('#containerError').slideUp(500);
	$('#getPriceButton').slideDown(500);
}
function clearContainer(id) {
	if ($('#containerOption'+id).html() != '') {
		$('#containerOption'+id).empty();
	}
}
function showGallery(galleryID,optionID) {
	$('#option'+optionID+'Loader').show();
	$('#containerOption'+optionID).empty();
	$.ajax({
		url: '/ajaxRequest.html?function=loadGallery&galleryID='+galleryID+'&optionID='+optionID,
		dataType:'html',
		success:function(data) {
			$('#option'+optionID+'Loader').hide();
			$('#containerOption'+optionID).hide().html(data).slideDown(500);
		}
		
	});
}

function gotoFromSelect(sel) {
	location.href = sel.options[sel.selectedIndex].value;
}

function helpWith(id) {
	var left = Math.floor((screen.availWidth - 370) / 2);
	var top = Math.floor((screen.availHeight - 400) / 2);
	window.open ("/optionhelp-"+id+"/","optionHelp","status=1,scrollbars=1,width=370,height=400,top=" + top + ",left=" + left);
}

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 checkMail(x)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return(true);
	else return(false);
}

function freeSample(id) {
	$('#colourLoader1').show();
	$.ajax({
		url:'/ajaxRequest.html?function=freeSample&id='+id,
		dataType:'json',
		success: function(data) {
			if (data.status == 'success') {
				location.href = '../../../view-my-cart.html';
			} else {
				alert(data.reason);				
			}
			$('#colourLoader1').hide();
		},
		error: function(data) {
			$('#colourLoader1').hide();
			alert('Unkwnown error. Please contact an administrator! Thank you!');
		}
	});
}

function addToCart() {
	$('#colourLoader').show();
	var pars = $("#colourForm").serialize();
	$.ajax({		
		url: '/ajaxRequest.html?function=addColourToCart',
		data: pars,
		dataType:'json',
		success: function(data){ 
			$('#colourLoader').hide();
			if (data.status == 'success') {
				location.href = '../../../view-my-cart.html';
			} else {
				alert(data.reason);
			}
		},
		error: function(data) {
			$('#colourLoader').hide();
			alert('Unkwnown error. Please contact an administrator! Thank you!');
		}
	});
}

function recalculate() {
	$('#cartLoader').show();
	var pars = $('#cartForm').serialize();
	$.ajax({
		url: '/ajaxRequest.html?function=updateCartQuantity',
		data: pars,
		dataType:'json',
		success:function(data) {
			$('#cartLoader').hide();
			if (data.status == 'success') {
				location.href = 'view-my-cart.html';
			} else {				
				alert(data.reason);
			}
		},
		error:function() {
			$('#cartLoader').hide();
			alert('Unkwnown error. Please contact an administrator! Thank you!');
		}
	});
}

function removeItem(id,type) {
	var ask = confirm("Are you sure about deleting this item?");
	if (ask) {
		$('#cartLoader').show();
		$.ajax({
			url:'/ajaxRequest.html?function=remove'+type+'&id='+id,
			dataType:'json',
			success: function(data) {
				$('#cartLoader').hide();
				if (data.status == 'success') {
					location.href = 'view-my-cart.html';
				} else {
					alert(data.reason);
				}
			},
			error: function() {
				$('#cartLoader').hide();
				alert('Unkwnown error. Please contact an administrator! Thank you!');
			}
		});
	}
}

function showDetails(id) {
	$.ajax({
		url: '/ajaxRequest.html?function=getColourDetails&id='+id,
		dataType:'json',
		success: function(data) {
			$("#colourDetails"+id).hide().html(data.details).slideDown(500);
		},
		error: function() {
			$('#colourDetails'+id).slideUp(500);
			alert('Unkwnown error. Please contact an administrator! Thank you!');
		}
	});

}

function hideCD(id) {
	$("#"+id).slideUp(500,function(){$("#"+id).empty();});
}

function hide(id) {
	$("#"+id).hide();
}

function show(id) {
	$("#"+id).show();
}

function differentDeliveryAddress(act) {
	if (act == 'show') {
		$('#differentDeliveryHolder').slideDown(500);
	} else {
		$('#differentDeliveryHolder').slideUp(500);
		$('#dname').attr('value','');
		$('#daddress').attr('value','');
		$('#dcity').attr('value','');
		$('#dpostalcode').attr('value','');
	}
}

function requestSamples() {
	var ok = 1;
	// check fields
	var fields = new Array();
	fields[0] = 'name';fields[1] = 'address';fields[2] = 'city';fields[3] = 'postalcode';fields[4] = 'email';fields[5] = 'telephone';
	$.each(fields,function(i,d) {
			if ($("#"+d).attr("value") == undefined) {
				$("#"+d).addClass("error");
				ok = 0;
			} else {
				$("#"+d).removeClass("error");
			}
	});	
	if (ok) {
		$("#samplesLoading").show();
		pars = $("#samplesForm").serialize();
		$.ajax({
			url: '/ajaxRequest.html?function=requestSamples',
			data: pars,
			dataType: 'json',
			success: function(data) {
				$("#samplesLoading").hide();
				if (data.status == 'success') {
					location.href = 'thanks-samples-request.html';
				} else {
					alert(json.reason);
				}
			},
			error: function() {
				$("#samplesLoading").hide();
				alert('Unkwnown error. Please contact an administrator! Thank you!');
			}
		});
	} else {
		alert('Please fill all the fileds marked with * from the form!');
	}
}

function sendContact() {
	var ok = 1;
	// check fields
	var fields = new Array();
	fields[0] = 'name';fields[1] = 'telephone';fields[2] = 'email';fields[3] = 'subject';fields[4] = 'question';
	$.each(fields,function(i,d) {
			if ($("#"+d).attr("value") == undefined) {
				$("#"+d).addClass("error");
				ok = 0;
			} else {
				$("#"+d).removeClass("error");
			}
	});	
	if (ok) {
		$("#contactControls").slideUp(200);
		$("#contactLoader").slideDown(200);
		$('#contactForm').submit();
	} else {
		alert('Please fill all the fileds marked with * from the form!');
	}
}

function gotoPayment() {
	var ok = 1;
	// check fields
	var fields = new Array();
	fields[0] = 'name';fields[1] = 'address';fields[2] = 'city';fields[3] = 'postalcode';fields[4] = 'telephone';fields[5] = 'email';
	$.each(fields,function(i,d) {
			if ($("#"+d).attr("value") == undefined) {
				$("#"+d).addClass("error");
				ok = 0;
			} else {
				$("#"+d).removeClass("error");
			}
	});
	// check if delivery address != cardholder address
	if ($('input[name=saddropt]:checked').val() == 'differentaddress') {
		var fields = new Array();
		fields[0] = 'dname';fields[1] = 'daddress';fields[2] = 'dcity';fields[3] = 'dpostalcode';
		$.each(fields,function(i,d) {
				if ($("#"+d).attr("value") == undefined) {
					$("#"+d).addClass("error");
					ok = 0;
				} else {
					$("#"+d).removeClass("error");
				}
		});
	}
	if (ok) {
		$("#orderControls").slideUp(200);
		$("#orderLoader").slideDown(200);
		var pars = $("#orderForm").serialize();
		$.ajax({
			url:'/ajaxRequest.html?function=prepareForPayment',
			data: pars,
			type: 'POST',
			dataType: 'json',
			success: function(data) {
				if (data.status == 'success') {					
					$('#orderForm').attr('action','http://payments.flamingoblinds.co.uk');
					$('#orderForm').submit();
				} else {
					$("#orderControls").slideDown(200);
					$("#orderLoader").slideUp(200);
					alert(data.reason);
				}
			},
			error: function() {
				$("#orderControls").slideDown(200);
				$("#orderLoader").slideUp(200);
				alert('Please fill all the fileds marked with * from the form!');
			}
		});
	} else {
		alert('Please fill all the fileds marked with * from the form!');
	}
}


function filterSearchResults(a,b) {
	if (a.checked) {
		$(".bid"+b).show(500);
	} else {
		$(".bid"+b).hide(500);
	}
}