

//function to get the sku based on selected options
function getSku(index, pid) {	
	 
	var aOption = new Array();
	jQuery("select[id^='options_']").each(function(){
		var optionValue= jQuery(this).find("option:selected").val();
		var optionID = jQuery(this).attr("id");	
		productID = optionID.split("_")[1];

		if (productID == pid) {
			aOption.push(optionValue);
		}
     });

//	alert("pid="+pid+"&optionID=" + aOption.join("&optionID="));
    var dataString = "pid="+pid+"&optionID=" + aOption.join("&optionID=");
    jQuery.ajax({
        type: "POST",
        url: '/getsku.do',
        data: dataString,
        success: function(response) {
			//alert("response="+response);
            // set esku element value to the form
            if(response == null ||response == '' ){
                alert("Select Valid Option(s)");
                return false;
            } else {
                jQuery('input#esku').val(response);
                //valid option selected, show modal window
				showError("email_error", false);
		        showError("email_eqty", false);
       
	            fadeInScreen("#dialog1");
            }			
        }
    });
}

jQuery(document).ready(function() {
    //borrow this to show "send Message" button on detail, and only do this for detail page
    if(jQuery('#messagebutton').length){
      showError("messagebutton", false);
    }
    //for added item to basket
    var bFoundMessage = false;	
	jQuery(".addtobasket").each(function(){
		if (jQuery(this).text().replace(/\s/g, "").length > 0){
			bFoundMessage = true;
		}	
	});	
	if (bFoundMessage){	
	 //  e.preventDefault();
	   fadeInScreen("#dialog2"); 	
	}
			
	
	//if close button is clicked
	jQuery('.window .close').click(function (e) {
		e.preventDefault();
		
		jQuery('#mask').fadeOut(1000);
		jQuery('.window').fadeOut(1000);
	});
	
	jQuery('.basketwindow .basketclose').click(function (e) {
		e.preventDefault();
		
		jQuery('#mask').fadeOut(1000);
		jQuery('.basketwindow').fadeOut(1000);
	});		

	//submit form for notify email clicked
	jQuery(".emailmebutton").click(function() {

		// validate and process form
		// first hide any error messages
    	jQuery('.error').hide();

        var validQty = true;
        var validEmail = true;
		var qty = jQuery("input#eqty").val();
		if (!isValidQty(qty)) {
      		showError("email_eqty", true);
      		validQty = false;      		
    	}

		var email = jQuery("input#email").val();
		if(!isValidEmailAddress(email)){
       		showError("email_error", true);	
       		validEmail = false;      		
   	 	}
   	 	
   	 	if(validQty == false || validEmail == false){
   	 	   return false;
   	 	}

    	var sku = jQuery("input#esku").val();

    	//send data through ajax
		var emailData = 'email=' + email + '&qty=' + qty + '&sku=' + sku;
	//	alert (emailData);//return false;

		jQuery.ajax({
      	type: "POST",
      	url: "/emailwheninstock.do",
     	data: emailData,
      	error: function(){
       	 alert('Error sending email');
      	},
      	success: function() {		  
			jQuery('#mask').fadeOut(1000);
			jQuery('.window').fadeOut(1000);
      	}
     	});
	 
	});

});



//function for fadein screen
function fadeInScreen(divId){	 
		
		//Get the A tag		
		var id = divId; 
	
		//Get the screen height and width
		var maskHeight = jQuery(document).height();
		var maskWidth = jQuery(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		jQuery('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		jQuery('#mask').fadeIn(1000);
		jQuery('#mask').fadeTo("fast",0.8);
	
		//Get the window height and width
		var winH = jQuery(window).height();
		var winW = jQuery(window).width();
              
		//Set the popup window to center
		jQuery(id).css('top',  winH/2-jQuery(id).height()/2);
		jQuery(id).css('left', winW/2-jQuery(id).width()/2);
	
		//transition effect
		jQuery(id).fadeIn(1000);
}

//function to validare email
function isValidEmailAddress(emailAddress) {
 	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
 	return pattern.test(emailAddress);
}

//function to validate qty
function isValidQty(qty) {
 	var pattern = new RegExp("^[1-9]+[0-9]*$");
 	return pattern.test(qty);
}

function showError(layer, show){	
	if(show){
	  document.getElementById(layer).style.visibility="visible";
	} else {
	  document.getElementById(layer).style.visibility="hidden";	 
	}	
}
