var tags;


//Function to set Permalink value
function setPermalink(){
    document.permalink.url.value = location.href;
}
    
    
//Face Book Link Code
function fbs_click() {
    u=location.href;t=document.title;window.open("http://www.jeep.com/en/experience/bounce.html?item="+encodeURIComponent('http://www.facebook.com/sharer.php?u=')+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;
}


//Link function for delicious
function delicious_click(){
    u=location.href;t=document.title;window.open("http://www.jeep.com/en/experience/bounce.html?item="+encodeURIComponent('http://del.icio.us/post?url=')+encodeURIComponent(u),'delicious');return false;
    
}

//Link function for Digg
function digg_click(){
    
    //var pageData = feeds.getArticleByGUID(guid);
    
    //Get Text of description
    var descStorage = new Element('div', {});
	descStorage.setHTML(document.title);
	var metaDesc = descStorage.getText().substring(0, 150) + "...";
   
    u=location.href;t=document.title;window.open("http://www.jeep.com/en/experience/bounce.html?item="+encodeURIComponent('http://digg.com/submit?url='+u+'&bodytext='+metaDesc+'&title='+document.title+'&media=news&topic=autos'),'digg');return false;
}

//Share News Story Javascript

function setFormValidationRequirements(arg_formObj) {

    
    // Only proceed if rssDoc is not Null
	if (!arg_formObj) {
		setTimeout("setFormValidationRequirements("+arg_formObj+");", 1000);
		return;
	}
		
		
    // these will be validated all the time
    arg_formObj.FromName.required = "Your Name";
    arg_formObj.FromAddress.required  = "Your Email Address";
    arg_formObj.ToName.required = "Your Friends Name";
    arg_formObj.ToAddress.required = "Your Friends Email Address";
    
    // these will be validated only if filled in:
    arg_formObj.FromAddress.subtype = "email";
    arg_formObj.ToAddress.subtype = "email";
   
}


function submitForm(){
    document.tellafriend.submit();
}

function submitTellaFriend(form_id){
	
	// track
	linkTrack('community_share_nav', 'share_article');
	
    //var pageData = feeds.getArticleByGUID(guid);
    
    var validation = validateForm(form_id);
	
	// figure out which page and message to write
	if(document.title.indexOf('Urban Ranger') != -1){
		var bodyMessage = "&bodytext=This message was sent from "+form_id.FromName.value+" " + "<" + form_id.FromAddress.value + ">.\nThis name and address were not verified.\n\n\n\nHello, hello.\n\nYour valued buddy recommends a dose of The Urban Ranger. It's like a reality show-but with better people and Jeep(R) vehicles. Can't beat that.\n\nWatch Now >" + document.location +"\n\n\n\nThis e-mail was sent from www.jeep.com. Please do not reply to this e-mail as it is unmonitored.\n\nJeep e-mail Headquarters, 880 W. Long Lake Road, Troy MI 48098,(800) 946-0600";	
	} else {
		var bodyMessage = "&bodytext=This message was sent from "+form_id.FromName.value+" " + "<" + form_id.FromAddress.value + ">.\nThis name and address were not verified.\n\n\n\nHi there,\n\nA friend of yours just perused the \"Have Fun Out There\" site. And like all good sites, it needed to be shared. (Aren't you honored?)\n\nWatch Now >" + document.location +"\n\n\n\nThis e-mail was sent from www.jeep.com. Please do not reply to this e-mail as it is unmonitored.\n\nJeep e-mail Headquarters, 880 W. Long Lake Road, Troy MI 48098,(800) 946-0600";	
	}
	
    $('form_messages').setStyle('display','none');
    
    if(validation){
        var params = "";
        params += "URLFail=/flash/forms/error.html";
        params += "&URLOK=/flash/forms/thankyou.html";
        //params += "&ecardhost="+location.hostname;
        //params += "&ecardtarget=/en/experience/news/articles/";
        params += "&URLTemplate=mailer%2Ftemplates%2Fsaf_by_parameters_nogreet.txt";
        params += "&params="+escape("&guid=" + guid);
        //params += "&MailFormat=text%2Fhtml";
        params += "&Subject=Check This Out: " + document.title;
        params += bodyMessage;
        params += "&prelinktext=";
        params += "&salutation=";
        params += "&FromName="+form_id.FromName.value;
        params += "&ToName=";
        params += "&FromAddress="+form_id.FromAddress.value;
        params += "&ToAddress="+form_id.ToAddress.value;
        makePOSTRequest('/mailer/oo_mailer_encoding.jsp', params, 'tellaFriendResult(http_request[i].responseXML, http_request[i].responseText)');
        submitClicked = false;
        form_id.ToAddress.value = "";
        form_id.ToName.value = "";
    }else{
        return false;
    }
 }
  
 function tellaFriendResult(xmlObj, xmlText){
	var was_success = xmlText.split("true");
    if (was_success[1]) {
		 $('form_messages').setHTML('<b>MESSAGE SENT</b> <br />Your friend will now get this link sent to them. Thanks for your interest.<br /><br /><a class="submitButton" href="javascript:void(0);" onclick="return closeMessage()">OK</a>');
		   document.tellafriend.ToAddress.value = "";
    } else {
         $('form_messages').setHTML('<b>ERROR</b> <br />We are unable to tell your friend about this link. Please make sure you have completed all of the required fields and try again.<br /><br /><a class="submitButton" href="javascript:void(0);" onclick="return closeMessage()">OK</a>');
    }
    
     $('form_messages').setStyle('display','block');
     $('tell_a_friend').setStyle('display','none');
  }


function closeMessage(){
    $('form_messages').setStyle('display','none');
    $('tell_a_friend').setStyle('display','block');
}



function setShareForms(){

    global.initAccordion("shareNews","div.shareNewsAccordion","a.shareNewsToggler", false, true);
	setFormValidationRequirements(document.tellafriend);
	setPermalink();
	
	$('tellafriend').addEvent('submit', function(e) {
        submitTellaFriend(document.tellafriend);
        new Event(e).stop();
    });
}

onload_register('setShareForms()');