/*
  	Send to a Friend widget 
	(c) Sears Holdings Corporation
	
*/

// Open the div window
function send2friend(id,userDetail){
	
	// The HTML code for Send To a Friend  
	var stf_html = '<div class="friendTitle">Send to a friend</div>\n';
	stf_html += '<div id="closeWindowLink">\n';
	stf_html += '	<a href="javascript:;" class="closeWindow">close window</a>\n';
	//stf_html += '	<a href="javascript:;" onclick="send2friend(\'crumbWrapper\',\'\');">close window</a>\n';
	//stf_html += '	<a href="javascript:;" onclick="send2friend(\'crumbWrapper\',\'\');"><img src="'+imagePath+'/img/icons/closeWindow.gif" alt="Close Window Button" width="15" height="15" align="middle"></a>\n';   
	stf_html += '</div>\n';
	stf_html += '<form id="stfFormWrapper" name="sendToFriendForm" method="post" action="">\n';
	stf_html += '	<p>We\'ll send your friend(s) a link to this page and a personalized message.</p>\n';
	stf_html += '	<p>All fields in <strong>bold</strong> are required.</p>\n';
	stf_html += '	<div style="margin-right:10px;margin-top:10px;">\n';
	stf_html += '		<span>Your Name:</span> \n';
	stf_html += '		<input type="text" name="visitorName" size="10" value="'+userDetail[0]+'" style="width: 115px;" class="text">\n';
	stf_html += '	</div>\n';
	stf_html += '	<div style="margin-top:10px;">\n';
	stf_html += '		<span><strong>Your email:</strong></span> \n';
	stf_html += '		<input type="text" name="visitorEmail" size="10" value="'+userDetail[1]+'" style="width: 142px;" class="text">\n';
	stf_html += '	</div>\n';
	stf_html += '	<div style="margin-top: 15px;">\n';
	stf_html += '		<span><strong>Friend\'s email:</strong></span><br>\n';
	stf_html += '		<input type="text" name="friendsEmail" size="10" value="" style="width: 275px;" class="text"><br>\n';
	stf_html += '		<span style="font-size:9px;">Separate multiple emails by a semi-colon(;)</span>\n';
	stf_html += '	</div>';
	stf_html += '	<div style="margin-top: 15px;margin-bottom:15px;">\n';
	stf_html += '		<span>Message: (200 characters max)</span><br>\n';
	stf_html += '		<textarea name="message" cols="10" rows="4" style="width: 275px;" class="textField textAreaBox"></textarea>\n';
	stf_html += '	</div>\n';
	stf_html += '	<div id="sendFriendButt">\n';
	stf_html += '		<a href="javascript:;" onclick="if(submitFinish1()){showBox(\'sendToFriendThankyou\');hideBox(\'stfFormWrapper\');}">\n';
	stf_html += '		<img src="'+imagePath+'img/buttons/send.gif" width="97" height="22" alt="Send Email"></a>\n';
	stf_html += '	</div>\n';
	stf_html += '</form>\n';
	stf_html += '<div id="sendToFriendThankyou" style="display:none;">\n';
	stf_html += '	<p>An email has been sent to your friend(s) that includes a link to this page and your personalized message.</p>\n';
	stf_html += '</div>\n';
	
	if (!document.getElementById("send2friend")){
		
		//var wrapper = document.getElementById (id);
		var wrapper = document.getElementsByTagName("body")[0];
		if (browser == "Internet Explorer" && version < 6)
		{
			var div = document.createElement("div");
			var iframe = document.createElement('iframe');
			wrapper.appendChild(iframe);
			var d = document.createAttribute("id");
			d.value = 'stfCover';
			wrapper.getElementsByTagName("iframe")[0].setAttributeNode(d);
			wrapper.getElementsByTagName("iframe")[0].src = "javascript:;";
			
			wrapper.appendChild(div);
			
			var d = document.createAttribute("id");
			d.value = 'send2friend';
			wrapper.lastChild.setAttributeNode(d);
			
			var c = document.createAttribute("class");
			c.value = 'floatWindow';
			wrapper.lastChild.setAttributeNode(c);
			var s = document.createAttribute("style");
			s.value = 'z-index: 60; top: 250px; left: 336px;';
			wrapper.lastChild.setAttributeNode(s);
			
			var DivRef = document.getElementById("send2friend");
			DivRef.innerHTML = stf_html;
			var IfrRef = document.getElementById("stfCover");
            IfrRef.style.height = (DivRef.offsetHeight - 2) + "px";
            DivRef.style.height = DivRef.offsetHeight + "px";
		} 	
		else if (browser == "Internet Explorer" && version >= 6)
		{
			var div = document.createElement("div");
			wrapper.appendChild(div);
			
			var d = document.createAttribute("id");
			d.value = 'send2friend';
			wrapper.lastChild.setAttributeNode(d);
			
			var c = document.createAttribute("class");
			c.value = 'floatWindow';
			wrapper.lastChild.setAttributeNode(c);
			
			var DivRef = document.getElementById("send2friend");
			DivRef.innerHTML = stf_html;
			DivRef.style.left="336px";
			DivRef.style.top = "250px";
		} 
		else 
		{	
			var div = document.createElement("div");
			wrapper.appendChild(div);
			wrapper.lastChild.setAttribute("id","send2friend");
			wrapper.lastChild.setAttribute("class","floatWindow");
			wrapper.lastChild.setAttribute("style","z-index: 60; top: 250px; left: 336px;");
			
			var div = document.getElementById("send2friend");
			div.innerHTML = stf_html;
		}
		
		$('#send2friend').find('.closeWindow').click(function(){$('#send2friend').remove()});
	} 
	else if (document.getElementById("send2friend"))
	{
		
		var wrapper = document.getElementById (id);
		var div = document.getElementById ("send2friend");
		wrapper.removeChild(div);
		if (browser == "Internet Explorer" && version < 7)
		{
			var iframe = document.getElementById ("stfCover");
			wrapper.removeChild(iframe);
		}
		
	}
	
}


function submitFinish1()
 {
 
 		var name = document.getElementById('stfFormWrapper').visitorName.value;
 	var vEmail = document.getElementById('stfFormWrapper').visitorEmail.value;
       var fEmail = document.getElementById('stfFormWrapper').friendsEmail.value;
        var msg = document.getElementById('stfFormWrapper').message.value;
        var locationurl = top.location.href;
        
        if(!nameValidation(name))
        {
        	return false;
        }
        
       function nameValidation(name){
       
       		var  regexpression = /^\s*[a-zA-Z\s][a-zA-Z0-9\s\.\-\\]*$/ ;
       		var yourName = name.replace( /^\s*/, "" );
	  		if(yourName.match(regexpression) || yourName.length == 0){
				return true;
			}	
			alert("The name you entered appears incorrect. Please check your information and try again.");
			return false;
       }
    		
	if((vEmail == '') || (fEmail == '')){
	    alert("Email address is required");
	    return false;
	}

	if (!validateEmailAddress(vEmail.toLowerCase(),vEmail))
        {
                
                return false;
        }

	// fEmail = removeSpaces(fEmail); 
         var vArray = fEmail.split(";");
                  
	 var count;
          for (count=0; count < vArray.length; count++)
          {
                var vElem = vArray[count];
                if (!validateEmailAddress(vElem.toLowerCase(),vElem))
                        {
                
                        return false;
                        }
          }

	if ((msg.length > 200))
	{
		alert("Message exceeds 200 characters");
	        return false;
        }

        var locationurl = top.location.href;
        var splitUrlArray = ""; 
        var splitUrlArray1 = "";
        var splitUrlArray2 = "";
        var mvVar="";
        if (locationurl.indexOf("mv") != null & locationurl.indexOf("mv") > 0) {
                splitUrlArray = locationurl.split("mv");
        	if(splitUrlArray !=null && splitUrlArray.length > 0 ){
        		splitUrlArray1 = splitUrlArray[0];
        		splitUrlArray2 = splitUrlArray[1];
        		if (splitUrlArray2.indexOf("&") != null & splitUrlArray2.indexOf("&") > 0) {
        		     if(splitUrlArray2.indexOf('&')> 0 ){ 
        		     	var startindex = splitUrlArray2.indexOf("&") ; 
        		     	var endindex = splitUrlArray2.length; 
        		     	var remUrl = splitUrlArray2.substring(startindex , endindex);
        		     	locationurl = splitUrlArray1+"mv=s2f"+remUrl;
        		     }
        		}else {
        		    locationurl = splitUrlArray1+"mv=s2f";
        		}
        	}
        	
        }else
        {
          if (locationurl.indexOf("?") != null & locationurl.indexOf("?") > 0) {	
          	locationurl = locationurl+"&mv=s2f";
          }else{
          	locationurl = locationurl+"?mv=s2f";
          }
        }
		var stringUrl = locationurl.replace(/&/g,"*");
		var visitorName  = document.getElementById('stfFormWrapper').visitorName.value;
		var visitorEmail = document.getElementById('stfFormWrapper').visitorEmail.value;
		var friendsEmail = document.getElementById('stfFormWrapper').friendsEmail.value;
		var message      = document.getElementById('stfFormWrapper').message.value;
		document.cookie = "userEmail="+visitorEmail+";"
		
		var ajaxurl = "SendToAFriendCmd?storeId="+storeId+"&visitorName="+visitorName+"&visitorEmail="+visitorEmail+"&friendsEmail="+friendsEmail+"&message="+message+"&url="+stringUrl;
    	ajaxCall(ajaxurl,'dummy');
         
  	return true;
}

function dummy(){
	
	return;
}
