var POST_URL = "http://www.abazias.com/webserv-20/jsonSimple-test2.ashx";

function NewWindow( mypage, myname, w, h, scroll ) 
{
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	
	window.opener = this.window;
	
	var mywin = window.open(mypage, myname, winprops); 
	mywin.focus() 
}

function getCookie_v2( cookieName )
{
    //check to see if cookie is already set.
	if( document.cookie.indexOf( cookieName ) != -1 )
	{
	    cookieName += "=";
	    
        var c = "";
		var contentArray = document.cookie.split(';');
		
		if( contentArray.length > 1 )
		{
		    for(var i=0;i < contentArray.length;i++)
		    {    
		        if (contentArray[i].indexOf( cookieName ) == 1 )
		        {
				    var currVal = contentArray[i].replace( cookieName, '' );
					currVal = currVal.replace(/^\s+|\s+$/g,"");
		            return( currVal );
		        }
		    }
		}
		else
		{
		    var currVal = contentArray[i].replace( cookieName, '' );
			currVal = currVal.replace(/^\s+|\s+$/g,"");
		    return( currVal );
		}
	}
	
	return( "" );
}

function AddJewelryItemToCart()
{    
    // Check to see if we need to check javascript
    if( document.add_item_cart.Metal.value == "none" && document.add_item_cart.Size.value == "none" )
    {
		document.add_item_cart.submit();
    }

 	// metal check first
    if( document.add_item_cart.Metal.value == "" ) 
    {
        alert( "Please select what metal you would like for this item." );
        // focus on the metal here: document.add_item_cart.Metal.focus();
		return;
    }
    
    if( document.add_item_cart.Size != "none" )
    {
        CheckSize = 1;
    }
    
    if( CheckSize == 1 )
    {
        if( document.add_item_cart.Size.value == "" ) 
        {
            alert( "Please select a size for this item." );
            // focus on the size here: document.add_item_cart.Size.focus();
			return;
        }
    }
    
	document.add_item_cart.submit();
}

function OpenRingSizingWin() 
{   
    var scroll = "yes";
    w = 500;
    h = 420;
 
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
       
    mypage = "/bandSize.asp";
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	window.opener = this.window;
	
	var mywin = window.open(mypage, "ring_size_win", winprops); 
	mywin.focus() 
}

function OpenNoResizeWin() 
{   
    var scroll = "no";
    w = 440;
    h = 445;
 
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
       
    mypage = "/noResize.asp";
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	window.opener = this.window;
	
	var mywin = window.open(mypage, "no_resize_win", winprops); 
	mywin.focus() 
}

function SubmitJewelryRequest( stockNum )
{             
    var isError = false;
    var userFirst = $( "nc_first_name" );
    var userPhone =  $( "nc_phone_number" );
    var userEmail = $( "nc_email_address" );
    var userComments = $( "nc_comments" );
    var EmailRegex = "^[a-zA-Z0-9\-\_\.]+\@[a-zA-Z0-9\-\_\.]+\.[a-zA-Z0-9]+$";

    // Validate form info prior to sending
    if( userFirst.value == "" || userFirst.value == "Name" ) 
    {
        alert( "Please enter in your name in the area provided." );
        userFirst.focus();
        isError = true;
    }
    else if( userEmail.value.search( EmailRegex ) == -1 ) 
    {
        alert( "The email address you have entered is invalid.  Please try again." );
        userEmail.focus();
        isError = true;
    }
    
    if( ! isError  )
    {
        var currPhone = "";
        var currComments = "";
        
        if( userPhone.value != "Phone (Optional)" ){
            currPhone = userPhone.value;
        }

        if( userComments.value != "Comments (Optional)" ){
            currComments = userComments.value;
        }

        // Set cookies for future use
        setCookie( "nc_first_name", userFirst.value )
        setCookie( "nc_phone_number", currPhone )
        setCookie( "nc_email_address", userEmail.value )

		var xmlData = "function=AddNewJewelryRequest"
				    + "&firstName=" + userFirst.value
                    + "&email=" + userEmail.value
                    + "&phone=" + currPhone
        			+ "&comments=" + currComments
        			+ "&stockNum=" + stockNum;

        
		new Ajax.Request(POST_URL, {
			method: 'post',
			postBody: xmlData,
			onSuccess: 			
			function (msg) {
				if(msg.responseText != "" )
				{
		            alert( "Your question has been submitted successfully. \n\nA jewelry specialist will be in contact with you as soon as possible." );
					HideRequestForm();
		        }
			},
		   onFailure: 
			function (msg) {
				alert( "There was an error processing your request.  Please try again.");
			}
		});
    }
}

function AddJewelryMessage( stockNum )
{             
    var isError = false;
    var message = $( "JewelryMessage" );

    // Validate form info prior to sending
    if( message.value == "" || message.value == "Your Message" ) 
    {
        alert( "Please enter in your message area provided." );
        message.focus();
        isError = true;
    }
   
	if( ! isError  )
    {
		var currStock = document.add_item_cart.ItemNumber.value;
		var resultString = "function=SaveJewelryMessage"
						+ "&message=" + message.value
						+ "&stockNum=" + currStock;
						 
		var returnVal = "";
		new Ajax.Request(POST_URL, {
			method: 'post',
			postBody: resultString,
			onSuccess: 			
			function (msg) {
				if(msg.responseText != "" )
				{
		            alert( "Your personal note has been added.  This information will be added to your order once it is placed." );
					HideMessageForm();
		        }
			},
		   onFailure: 
			function (msg) {
				alert( "There was an error processing your request.  Please try again.");
			}
		});        				   
    }
}


msgDivOpen = 0;
rqDivOpen = 0;

function ShowRequestForm()
{
	var currLocation = $("ShowRequestFormDiv").cumulativeOffset();
	var rx = currLocation.left;
	var ry = currLocation.top;
	rqDivOpen = 1;
	HideMessageForm();
	$('nc_first_name').value = getCookie_v2( "nc_first_name");
	$('nc_phone_number').value = getCookie_v2( "nc_phone_number");
	$('nc_email_address').value = getCookie_v2( "nc_email_address");

	$('RequestDiv').style.display = "block";
	$("RequestDiv").style.left = rx + "px";
	$("RequestDiv").style.top = ( ry - $("RequestDiv").offsetHeight + $("ShowRequestFormDiv").offsetHeight + 18 ) + "px";
	$("ShowRequestFormDiv").innerHTML = "";

}

function HideRequestForm()
{
	rqDivOpen = 1;
	$('RequestDiv').style.display = "none";
	$("ShowRequestFormDiv").innerHTML = "Request More Info";
}

function ShowMessageForm()
{
	var currLocation = $("ShowMessageDiv").cumulativeOffset();
	var x = currLocation.left;
	var y = currLocation.top;
	msgDivOpen = 1;
	HideRequestForm();

	$("MessageDiv").style.left = ( x - $("MessageDiv").getWidth() + 5 ) + "px";
	$("MessageDiv").style.top = ( y - $("MessageDiv").getHeight()/2 + 25 ) + "px";
	$('MessageDiv').style.display = "block";
	$('ShowMessageDiv').onclick = function() {HideMessageForm();}
}

function HideMessageForm()
{
	msgDivOpen = 0;
	$('MessageDiv').style.display = "none";
	$('ShowMessageDiv').onclick = function() {ShowMessageForm();}
}

function HighlightPriceDiv(currId)
{
	if( $(currId).className != "PriceDivSelected" ) {
		$(currId).className = "PriceDivHover";
		if( $(currId + "_size") ) {
			$(currId + "_size").className = "SelectSizeOn";
		}
	}
}

function ClearPriceDiv(currId)
{
	if( $(currId).className != "PriceDivSelected" ) 
	{
		$(currId).className = "PriceDiv";
		if( $(currId + "_size") ) {
			$(currId + "_size").className = "SelectSize";
		}
	}
}

function DoDropDownUpdateJewelry( currMenu )
{   
	var selectedSize = $(currMenu).value;
	if( selectedSize != "" ) 
	{
		document.add_item_cart.Size.value = selectedSize;
	}
}

function SelectPriceDiv(currId)
{
	$$("div[class^=PriceDiv]").each(function(e){
		$(e).className = "PriceDiv";
		if($(e.id + "_size")) {
			$(e.id + "_size").className = "SelectSize";	
		}
	});

	$(currId).className = "PriceDivSelected";
	if( $(currId + "_size") ) {
		$(currId + "_size").className = "SelectSizeOn";
		var currSize = ""
		if( $( "Size-" + currId.substr(currId.indexOf('_') - 1,currId.length) ) )
		{
			currSize = $( "Size-" + currId.substr(currId.indexOf('_') - 1,currId.length) ).value;
		}
		document.add_item_cart.Size.value = currSize;
	}
	var currMetal = currId.substr(currId.indexOf('_') - 1,currId.length);
	if( currMetal.search( /&(wg|yg|pd|pl|si|ti|kp)$/ != -1 ) )
	{
		document.add_item_cart.Metal.value = currMetal;
	}
}

function OpenSpamWin()
{
    document.getElementById("weHateSpam").style.display = "block";
}

function CloseSpamWin()
{
    document.getElementById("weHateSpam").style.display = "none";
}

function RateJewelryComment_v2(EntryId, Vote)
{
    var xmlData = "function=AddJewelryCommentRating&EntryId=" + EntryId + "&Vote=" + Vote;
	new Ajax.Request(POST_URL, {
			method: 'post',
			postBody: xmlData,
			onSuccess: 			
			function (msg) {
				if(msg.responseText != "" )
				{
		            alert("Your vote has been saved.  Thank you.");
		        	document.getElementById("comment_vote_" + EntryId).innerHTML = "";
		        }
			},
		   onFailure: 
			function (msg) {
				alert( "There was an error processing your request.  Please try again.");
			}
	});     

    return( true );
}


function SubmitJewelryComment_v2( StockNum )
{             
    var isError = false;
    var userFirst = document.getElementById( "jc_FirstName" );
    var userInitial =  document.getElementById( "jc_LastInitial" );
    var userComments = document.getElementById( "jc_Comments" );

    // Validate form info prior to sending
    if( userFirst.value == "" ) 
    {
        alert( "Please enter in your first name in the area provided." );
        userFirst.focus();
        isError = true;
		return(false);
    }
    if( userComments.value == "" ) 
    {
        alert( "Please enter your comments." );
        userComments.focus();
        isError = true;
		return(false);
    }
    if( userInitial.value == "" ) 
    {
        alert( "Please enter your last initial." );
        userInitial.focus();
        isError = true;
		return(false);
    }

    if( StockNum == "" ) 
    {
        isError = true;
		return(false);
    }
  

    if( ! isError  )
    {
        
		var xmlData = "function=AddJewelryComment"
					+ "&FirstName=" + userFirst.value
                    + "&Initial=" + userInitial.value
        			+ "&comments=" + userComments.value
        			+ "&StockNum=" + StockNum;				   
        
		new Ajax.Request(POST_URL, {
			method: 'post',
			postBody: xmlData,
			onSuccess: 			
			function (msg) {
				if(msg.responseText != "" )
				{
		            alert( "Your comment has been submitted.  Thank you!" );
					$('jc_FirstName').value = "";
					$('jc_LastInitial').value = "";
					$('jc_Comments').value = "";
		        }
			},
		   onFailure: 
			function (msg) {
				alert( "There was an error processing your request.  Please try again.");
			}
		});      
    }
}