// standard ajax request
var http = false;
if(navigator.appName == "Microsoft Internet Explorer") {
  http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
  http = new XMLHttpRequest();
}


function forwardAjax(element,url) {
// Mozilla/Safari/IE7+
if (window.XMLHttpRequest) {
	http = new XMLHttpRequest();
	if(http.overrideMimeType){
		http.overrideMimeType('text/xml');
	}
}
// IE6-
else if (window.ActiveXObject) {
	http = new ActiveXObject("Microsoft.XMLHTTP");
}


	http.open("GET", url, true);
    http.onreadystatechange=function() {
    if(http.readyState == 4) {
      document.getElementById(element).innerHTML = http.responseText;
    }
  }
  http.send(null);
}


function buildForward(){
//alert('start');
var pageURL = window.location;
var pageTitle = document.title;
var forwardForm = document.getElementById('forwardHolder');
var forwardFormBuild = '<div id="messagebox" style="width:500px; height:350px; font-family:Arial; font-size:12px; padding-top:10px; padding-left:6px;">'+
'<div><strong>Your Message:</strong></div>'+
'<div style="padding-top:10px;">Take a look at what I found on Torrid.com!</div>'+
'<div id="pageTitle" style="padding-top:10px; font-weight:bold">'+pageTitle+'</div>'+
'<div id="pageUrl"><a href="'+pageURL+'">'+pageURL+'</a></div>'+
'<div id="forwardError" style="color:red; font-weight:bold; padding-top:7px;"></div>'+
'<div style="padding-top:7px;">'+
'<table width="450" border="0" cellpadding="4" cellspacing="0" style="font-family:Arial; font-size:12px;">'+
  '<tr>'+
    '<td width="191">*Your Name:</td>'+
    '<td width="309" align="right"><input type="text" id="forwardName" style="width:250px;" />'+
    '</td>'+
  '</tr>'+
  //'<tr>'+
    //'<td>*Your Email Address:</td>'+
    //'<td><input type="text" id="forwardEmail" style="width:250px;" /></td>'+
 // '</tr>'+
  '<tr>'+
    '<td>*Friend\'s Name:</td>'+
    '<td align="right"><input type="text" id="forwardFriendsName" style="width:250px;" /></td>'+
  '</tr>'+
  '<tr>'+
    '<td>*Friend\'s Email Address:</td>'+
    '<td align="right"><input type="text" id="forwardFriendsEmail" style="width:250px;" /></td>'+
  '</tr>'+
  '<tr>'+
    '<td>*Subject:</td>'+
    '<td align="right"><input id="forwardSubject" style="width:250px;" value="Look what your friend found at Torrid.com" /></td>'+
  '</tr>'+
  '<tr>'+
    '<td>*Message:</td>'+
    '<td align="right"><textarea id="forwardMessage" cols="45" rows="5" style="width:250px; height:50px;"></textarea></td>'+
  '</tr>'+
    '<tr>'+
    '<td></td>'+
    '<td align="right"><a href="javascript:forwardFormProcess()"><img src="/forward/send-email.gif" border="0" onMouseOver="this.src=\'/forward/send-email-roll.gif\'" onMouseOut="this.src=\'/forward/send-email.gif\'"/></a><a href="javascript:forwardFormClose()"><img src="/forward/close-email.gif" border="0" onMouseOver="this.src=\'/forward/close-email-roll.gif\'" onMouseOut="this.src=\'/forward/close-email.gif\'"/></a></td>'+
  '</tr>'+
'</table>'+
'</div>'+
'</div>';
forwardForm.style.display = 'none';
forwardForm.style.zIndex = "999";
forwardForm.style.position = 'absolute';
forwardForm.style.top = '200px';
forwardForm.style.left = '200px';
forwardForm.style.border = '#cccccc solid 3px';
forwardForm.style.backgroundColor = '#ffffff';
forwardForm.innerHTML=forwardFormBuild;
$("#forwardHolder").fadeIn("slow");
}


function forwardFormProcess(){
var yourname = document.getElementById('forwardName').value;
var friendsName = document.getElementById('forwardFriendsName').value;
var friendsemail = document.getElementById('forwardFriendsEmail').value;
var subject = document.getElementById('forwardSubject').value;
var message = document.getElementById('forwardMessage').value;
var forwardErrorDiv = document.getElementById('forwardError');

	//email check vars
	var at="@"
	var dot="."
	var lat=friendsemail.indexOf(at)
	var lstr=friendsemail.length
	var ldot=friendsemail.indexOf(dot)
	var emailError = "Invalid Email Address";

if(yourname == ""){
	forwardErrorDiv.innerHTML = "Error: missing your name.";
}else if (friendsName == ""){
	forwardErrorDiv.innerHTML = "Error: missing your friends name.";
}else if (friendsemail == ""){
	forwardErrorDiv.innerHTML = "Error: missing your friends email address.";
}else if (subject == ""){
	forwardErrorDiv.innerHTML = "Error: missing the subject line.";
}else if (friendsemail.indexOf(at)==-1){
	forwardErrorDiv.innerHTML = emailError;
}else if (friendsemail.indexOf(at)==-1 || friendsemail.indexOf(at)==0 || friendsemail.indexOf(at)==lstr){
	forwardErrorDiv.innerHTML = emailError;
}else if (friendsemail.indexOf(dot)==-1 || friendsemail.indexOf(dot)==0 || friendsemail.indexOf(dot)==lstr) {
	forwardErrorDiv.innerHTML = emailError;
}else if (friendsemail.indexOf(at,(lat+1))!=-1){
	forwardErrorDiv.innerHTML = emailError;
}else if (friendsemail.substring(lat-1,lat)==dot || friendsemail.substring(lat+1,lat+2)==dot) {
	forwardErrorDiv.innerHTML = emailError;
}else if (friendsemail.indexOf(dot,(lat+2))==-1) {
	forwardErrorDiv.innerHTML = emailError;
}else if (friendsemail.indexOf(" ")!=-1){
	forwardErrorDiv.innerHTML = emailError;
}else{
	forwardAjax("messagebox","/forward/forwardEmail.asp?name="+yourname+"&friendName="+friendsName+"&friendEmail="+friendsemail+"&subject="+subject+"&message="+message+"&url="+window.location+"&title="+document.title+"");


}

//end main
}

function forwardFormClose(){
 $("#forwardHolder").fadeOut("slow");

}


function socialpop(socialsite){
	var location = document.location.href;
	var title = "Torrid.com - "+document.title.replace("Torrid.com - Plus sizes - Product Details Page - ","");
	var winwidth = 626;
	var winheight = 436;

if (socialsite == "facebook"){
	window.open('http://www.facebook.com/sharer.php?u=l'+location+'&t='+title,'facebook','toolbar=0,status=0,width='+winwidth+',height='+winheight+',top=100,left=100');
	}
	else if(socialsite == "delicious"){
		window.open('http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url='+location+'&amp;title='+title,'delicious','toolbar=0,status=0,width='+winwidth+',height='+winheight+',top=100,left=100');
	}
	else if (socialsite == "myspace"){
		window.open('http://www.myspace.com/index.cfm?fuseaction=postto&d=testing&t='+title+'&c='+title+'&u='+location+'&l=Blog','myspace','toolbar=0,status=0,width='+winwidth+',height='+winheight+',top=100,left=100');
	}
	else if(socialsite == "twiter"){
		window.open("http://twitthis.com/twit?url="+location+"&title="+title,'twiter','toolbar=0,status=0,width='+winwidth+',height='+winheight+',top=100,left=100');
	}
}
