var count = 1;

function redirect(i) { if(i != "") this.location = i; }

function startList() {
	var ua = navigator.userAgent.toLowerCase();
	var is_pc_ie = ( (ua.indexOf('msie') != -1 ) && ( ua.indexOf('win') != -1 ) && ( ua.indexOf('opera') == -1 ) && ( ua.indexOf('webtv') == -1 ) );
	var is_mac_ie = ( (ua.indexOf('msie') != -1 ) && ( ua.indexOf('mac') != -1 ));
	/*  MRR Comment Out as we are not using old nav code
	if(is_pc_ie){
		navRoot = document.getElementById("mainNav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";

				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
	*/
}

function MM_preloadImages() { //v3.0
    var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function entryStyle() {

    if(document.getElementById("videoEntry").checked) {
        document.getElementById("videoDIV").style.display = "block";
		document.getElementById("photoDIV").style.display = "none";
    } else if(document.getElementById("photoEntry").checked)  {
        document.getElementById("photoDIV").style.display = "block";
		document.getElementById("videoDIV").style.display = "none";
    }
}

function init() {

    var filename = location.pathname.substring(location.pathname.lastIndexOf('/')+1);

    if(filename == "project_library_experienced.aspx") {
        initLibraryMenu(0);
    } else if(filename == "project_library_beginner.aspx") {
        initLibraryMenu(1);
    }


	 if(filename == "project_library_main.aspx" || filename == "project_library_paint.aspx" || filename == "project_library_repair.aspx" || filename == "project_library_lawn.aspx" ) {
	  //document.getElementById("searchprojects").style.display = "block";
	   }
	  else if(filename =="default.aspx" || filename == "" || filename =="default_noflash.aspx") {

	  } else {
	  	//document.getElementById("searchprojects").style.display = "none";
	  }



    if(filename =="default.aspx" || filename == "" || filename =="default_noflash.aspx") {
        //document.getElementById("homeIdeaHouse").style.display = "none";
	}

   else if(filename.substr(0,13) == "project_lawn_" || filename.substr(0,15) == "project_repair_" || filename.substr(0,14) == "project_paint_" || filename.substr(0,17) == "project_interior_") {
        document.getElementById("rightContentProjectOnly").style.display = "block";
		document.getElementById("ProjectLibrarySection").style.display = "block";
		//document.getElementById("rightContentCoupon").style.display = "block";
		insertFeedbackLink(filename.slice(0, filename.indexOf('.')));
    }
    else if(filename.substr(0,8) == "contest_" ) {
        document.getElementById("rightContentProjectOnly").style.display = "none";
		document.getElementById("rightContentContestOnly").style.display = "block";
		document.getElementById("ProjectLibrarySection").style.display = "none";
		/*****************************************************************/
		/* Added on 8-4-09 for share/save on contest details page */
		insertFeedbackLink(filename.slice(0, filename.indexOf('.')));
		/*****************************************************************/		
    }
	else if(filename.substr(0,12) == "initial_Flow" || filename.substr(0,12) == "initial_flow") {
		//document.getElementById("rightContentProjectOnly").style.display = "none";
		//document.getElementById("ProjectLibrarySection").style.display = "none";
		//document.getElementById("rightContentCoupon").style.display = "none";
		/*****************************************************************/
		/* Added on 4-8-09 */
		insertFeedbackLink(filename.slice(0, filename.indexOf('.')));
		/*****************************************************************/
    }
	else {
		//document.getElementById("rightContentProjectOnly").style.display = "none";
		//document.getElementById("ProjectLibrarySection").style.display = "block";
		//document.getElementById("rightContentCoupon").style.display = "block";
	}
}

function checkCommentSize(obj) {
	if(document.all || document.getElementById) {
		var count = document.getElementById("size");
		var commentSize = document.getElementById(obj.id);
		if(commentSize.value.length > 100) commentSize.value = commentSize.value.substr(0, 100);
		else count.innerHTML = commentSize.value.length;
	}
}

function external() {
    if (document.getElementsByTagName) {
	var a;
	a = document.getElementsByTagName('a');
	for (i = 0; i<a.length; i++) {
        if (a[i].getAttribute('href') && a[i].getAttribute('rel') == 'external'){
    		a[i].target = '_blank';
			}
		}
	 }
}

function clearText(thefield){
    //if (thefield.defaultValue==thefield.value) {
        thefield.value = "";
    //}
}

function controlParentNav(parentID, action) {
    if(document.getElementById) {
        x = document.getElementById(parentID);
        if(action) { x.className = parentID + "Highlight"; }
        else { x.className = ""; }
    }
}

function flagDropdown(id, flag) {
	if(document.getElementById) {
		var x = document.getElementById(id);
		
		if(flag) {
			x.style.display = "block";
		}else{
			x.style.display = "none";
		}

	}
}

function validateForm(a) {
    /*************************************************************************/
	/* Call if Optin checkbox is checked                                     */
	optinCheckboxChecker("PrintCouponDefault1_chkOptin");
	/*************************************************************************/
	if(document.all || document.getElementById) {

	    var email_filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var err = new Boolean();
		err = false;

        for(var i = 0; i < a.length; i++) {
		    var x = a[i];
		    var xn = x.n;

		    if(xn == "zipcode" && (x.value.length != 5 || isNaN(x.value))) {
			    x.value = "Zip code required.";
			    err = true;
		    }

		    if(xn == "email" && (x.value.length == 0 || x.value == "Email required.")) {
			    x.value = "Email required.";
			    err = true;
		    } else if(xn == "email" && !email_filter.test(x.value)) {
			    x.value = "Invalid email address.";
			    err = true;
	        }

	        if(xn == "search" && (x.value.length == 0 || x.value == "Site Search" || x.value == "Enter Search Term" || x.value == "Project")) {
			    x.value = "Enter Search Term";
			    err = true;
		    }
		}

		if(err) return(false);
		else return(true);
	}

}


/**********************************************************************************************/
//Addition: 7-16-09
//Function validates form on initial_flow.aspx page
//Requirement: email and Opin should always be submitted concurrently
//NL
/**********************************************************************************************/
function validateInitialFlow()
{
    try
    {
        var err = false;
        var eMail = document.getElementById("ctl00_ContentPlaceHolder1_Email");
        var chkB =  document.getElementById("ctl00_ContentPlaceHolder1_chkOptin");
        var zip = document.getElementById("ctl00_ContentPlaceHolder1_zipcode");
        var errMsg = "";
        var email_filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if(chkB.checked && eMail.value == "")
        {
            err = true;
            errMsg = "Please provide a valid email address so that you can receive future offers and project tips from True Value Hardware.";
        }
        else if(!chkB.checked && eMail.value != "")
        {
            err = true;
            errMsg = "You've provided an email address to receive future offers and project tips, but you also need to check the box to confirm your interest.";
        }
        else
        {
            if(eMail.value != "" && !email_filter.test(eMail.value))
            {
                err = true;
                errMsg = "Invalid email address.";
            }
        }
        if(zip.value.length > 0)
        {
            if(zip.value.length != 5 || isNaN(zip.value))
            {
                err = true;
                errMsg = "Invalid Zip code.";
            }
         }
        if(err)
        {
            alert(errMsg);
            return false;
        }
        else return true;
   }
   catch(err)
   {
        //alert("some error");
   }
}


function initLibraryMenu(n) {

    if(document.getElementById) {

        if(n == 0) {
            var obj1 = document.getElementById("i1").getElementsByTagName("li");
            var obj2 = document.getElementById("i2").getElementsByTagName("li");
            var obj3 = document.getElementById("i3").getElementsByTagName("li");

            for(var i = 0; i < obj1.length; i++) {
                var x = obj1[i];
                x.onmouseover = function() { this.parentNode.style.display='block'; }
                x.onmouseout = function() { this.parentNode.style.display='none'; }
            }

            for(var i = 0; i < obj2.length; i++) {
                var x = obj2[i];
                x.onmouseover = function() { this.parentNode.style.display='block'; }
                x.onmouseout = function() { this.parentNode.style.display='none'; }
            }

            for(var i = 0; i < obj3.length; i++) {
                var x = obj3[i];
                x.onmouseover = function() { this.parentNode.style.display='block'; }
                x.onmouseout = function() { this.parentNode.style.display='none'; }
            }
        } else if(n == 1) {
            var obj4 = document.getElementById("i4").getElementsByTagName("li");
            var obj5 = document.getElementById("i5").getElementsByTagName("li");
            var obj6 = document.getElementById("i6").getElementsByTagName("li");

            for(var i = 0; i < obj4.length; i++) {
                var x = obj4[i];
                x.onmouseover = function() { this.parentNode.style.display='block'; }
                x.onmouseout = function() { this.parentNode.style.display='none'; }
            }

            for(var i = 0; i < obj5.length; i++) {
                var x = obj5[i];
                x.onmouseover = function() { this.parentNode.style.display='block'; }
                x.onmouseout = function() { this.parentNode.style.display='none'; }
            }

            for(var i = 0; i < obj6.length; i++) {
                var x = obj6[i];
                x.onmouseover = function() { this.parentNode.style.display='block'; }
                x.onmouseout = function() { this.parentNode.style.display='none'; }
            }
        }
    }
}

function swapTipIn() {
	x=document.getElementById('toolbox');
	x.style.display='none';
	if(count % 2 == 0) {
		x.className = 'open1';
		x.style.display='block';
	}
	else {
		x.className = 'open2';
		x.style.display='block';
	}
	count++;

}

function swapTipOut() {
	x=document.getElementById('toolbox');
	x.style.display='none';
	x.className = 'closed';
	x.style.display='block';
}

function insertFeedbackLink(s) {
	if(document.getElementsByTagName && document.getElementById) {
		var x = document.getElementsByTagName("h1");
		var a = document.getElementsByTagName("a");
		for(var i = 0; i < x.length; i++) {
			if(x[i].className == "projectTitle") {
				var str = x[i].innerHTML;
				str = str.substring(str.indexOf(">")+1, str.lastIndexOf("<"));
				break;
			}
		}
		/*******************/
		/* Added on 4-8-09 */
		if (s == "initial_Flow")
		{ str = "Save $5 on any $25 purchase at True Value";
		  var DiggBodyText = "Save $5 on any $25 purchase at True Value";
		}
		/* Added on 8-4-09 for share/save on contest details page */
		else if (s == "contest_details")
		{ str = "True Value DIY Drama Contest";
		  var DiggBodyText = "Enter the True Value DIY Drama Contest";
		}
		/*******************/
		else
		{
		  var DiggBodyText = "True Value Project";
		}
		/*******************/
		for(var v = 0; v < a.length; v++) {
			if(a[v].className == "iFlowLink") {
				a[v].href = "initial_Flow.aspx?pageType=" + s;
			}
			if(a[v].className == "iFlowEmailLink") {
				a[v].href = "email_friends.aspx?msg=" + escape(str) + "&pageName=" + s + ".aspx";
			}
			if(a[v].className == "iFlowDiggLink") {
				//a[v].href = "http://digg.com/submit?url=http%3A%2F%2Fwww.startrightstarthere.com%2F" + s + ".aspx&title=" + escape(str) + "&bodytext=" + escape(DiggBodyText);
				a[v].href = "http://digg.com/submit?url=http%3A%2F%2Fdev.b2comm.com%2Ftruevalue_september09_beta907_contest%2F" + s + ".aspx&title=" + escape(str) + "&bodytext=" + escape(DiggBodyText);
			}
			//if(a[v].className == "iFlowFacebookLink") {
			//	a[v].href = "http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.startrightstarthere.com%2F" + s + ".aspx";
			//}
			if(a[v].className == "iFlowDeliciousLink") {
				//a[v].href = "http://del.icio.us/post?url=http://www.startrightstarthere.com/" + s + ".aspx&title=" + escape(str);
				a[v].href = "http://del.icio.us/post?url=http://dev.b2comm.com/truevalue_september09_beta907_contest/" + s + ".aspx&title=" + escape(str);
			}
			if(a[v].className == "iFlowGoogleLink") {
				//a[v].href = "http://www.google.com/bookmarks/mark?op=add&bkmk=http%3A%2F%2Fwww.startrightstarthere.com%2F" + s + ".aspx&title=" + escape(str);
				a[v].href = "http://www.google.com/bookmarks/mark?op=add&bkmk=http%3A%2F%2Fdev.b2comm.com%2Ftruevalue_september09_beta907_contest%2F" + s + ".aspx&title=" + escape(str);
			}
		}

		//document.getElementById("rightContentProjectFeedback").innerHTML = "<a href='feedback.aspx?projectTitle=" + escape(str) + "'>Tell us what you think.</a>";
	}
}

// enables a form field
function enableEdit(fld)
{
    textbox = document.getElementById(fld).disabled = false;
}

// converts textbox/textarea (all lower or upper case) to proper sentence casing
// Ex. [THIS IS A SENTENCE.]  becomes [This is a sentence.]
function sentenceCase(fld) {
    var theText = document.getElementById(fld).value;
    theText = theText.toLowerCase().replace(/(^\s*\w|[\.\!\?]\s*\w)/g,function(c){return c.toUpperCase()});
    document.getElementById(fld).value = theText;
}
// For Facebook share & save
function fbs_click() {
	var filename = location.pathname.substring(location.pathname.lastIndexOf('/')+1);
	u=location.href;
	t=document.title;
	/* Added on 4-23-09 */
	if(document.getElementsByTagName && document.getElementById) {
		var x = document.getElementsByTagName("h1");
		for(var i = 0; i < x.length; i++) {
			if(x[i].className == "projectTitle") {
				var str = x[i].innerHTML;
				str = str.substring(str.indexOf(">")+1, str.lastIndexOf("<"));
				break;
			}
		}
		if (filename.slice(0, filename.indexOf('.')) == "initial_Flow")
		{ t = "Save $5 on any $25 purchase at True Value";
		}
		else
		{
		  t = str;
		}
	}
	/*******************/

	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

// Email a friend - transfers Your Name value to beginning of Message value
function updateMessage() {
	if (document.getElementById("ctl00_ContentPlaceHolder1_Emailer1_txtFromName").value) {
		document.getElementById("ctl00_ContentPlaceHolder1_Emailer1_txtMessage").value = document.getElementById("ctl00_ContentPlaceHolder1_Emailer1_txtFromName").value + document.getElementById("ctl00_ContentPlaceHolder1_Emailer1_txtMessage").value;
		}
}


//rating
function $(v,o) { return((typeof(o)=='object'?o:document).getElementById(v)); }
function $S(o) { return((typeof(o)=='object'?o:$(o)).style); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function abPos(o) { var o=(typeof(o)=='object'?o:$(o)), z={X:0,Y:0}; while(o!=null) { z.X+=o.offsetLeft; z.Y+=o.offsetTop; o=o.offsetParent; }; return(z); }
function XY(e,v) { var o=agent('msie')?{'X':event.clientX+document.body.scrollLeft,'Y':event.clientY+document.body.scrollTop}:{'X':e.pageX,'Y':e.pageY}; return(v?o[v]:o); }

star={};
var isRated = false;
var initialvalue = 0;
star.mouse=function(e,o) { if(star.stop || isNaN(star.stop)) { star.stop=0;
	document.onmousemove=function(e) {

	    if (!isRated)
	    {
	        var n=0;
	        var p=abPos($('starCur'+n)), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y; star.num=o.id.substr(4);
            if(oX<1 || oX>84 || oY<0 || oY>19) { star.stop=1; star.revert(); }

		    else {


			    $S('starCur'+n).width=oX+'px';
			    $S('starUser'+n).color='#111';

			    $('starUser'+n).innerHTML=Math.round(oX/84*100)+'%';
			    var tnk = document.getElementById('starUser0ThankYou');
			    if (tnk != null) {
			        if (Math.round(oX/84*100) < 20){
			            tnk.innerHTML = 'Not my thing';
			        }else if(Math.round(oX/84*100) < 40){
			           tnk.innerHTML = 'Might try it';
			        }else if(Math.round(oX/84*100) < 60){
			           tnk.innerHTML = 'Like it';
			        }else if(Math.round(oX/84*100) < 80){
			           tnk.innerHTML = 'Love it';
			        }else  tnk.innerHTML = 'Can\'t wait to start';

			    }

		    }

	    }
	};
} };

star.update=function(e,o) {
    var n=0;
    var v=parseInt($('starUser'+n).innerHTML);
    n=o.id.substr(4);
    var starC = document.getElementById('starCur0');
    starC.title=v;
	//req=new XMLHttpRequest(); req.open('GET','/test.aspx?vote='+(v/100),false); req.send(null);

    var tnk = document.getElementById('starUserThankYou');

    tnk.innerHTML = 'Thank you for rating';

    var tnk2 = document.getElementById('starUser0ThankYou');
    tnk2.innerHTML = '';
    xmlhttp=null;
    if (window.XMLHttpRequest)
      {// code for all new browsers
      xmlhttp=new XMLHttpRequest();
      }
    else if (window.ActiveXObject)
      {// code for IE5 and IE6
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    if (xmlhttp!=null)
      {
         var sPath = window.location.pathname;
         var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
         var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

      xmlhttp.onreadystatechange=state_Change;
      xmlhttp.open("POST",'responseHTTP.aspx?vote='+(v)+ '&url=' + sPage,true);
      xmlhttp.send(null);
      }
    else
      {
      alert("Your browser does not support XMLHTTP.");
      }
   isRated = true;

};

star.revert=function() {

    var n=0, v=parseInt($('starCur'+0).title);

	$S('starCur'+n).width=Math.round(v*84/100)+'px';
	$('starUser'+n).innerHTML=(v>0?Math.round(v)+'%':'');
	$('starUser'+n).style.color='#888';
	document.onmousemove='';
		var tnk = document.getElementById('starUser0ThankYou');
	tnk.innerHTML='';

};

star.num='cur0';

function state_Change()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = OK
       //alert(xmlhttp.responseText);
      var numOfRating = document.getElementById("ctl00_right_content_totVoting");
      numOfRating.innerHTML = xmlhttp.responseText + " total ratings";

    }
  else
    {
    alert("Problem saving rating data");
    }
  }
}
function initVal(val1, val2){
    var cur = document.getElementById("starCur0");
    cur.title = val1;
    cur.style.width = val2;

}

function setNum(){
	old_num = 0;
}
function faq(num,this_link){
	//if a section is already open, close it
	if (old_num != 0){
		document.getElementById("faq_"+old_num).className = "faq_hide";
		document.getElementById("arrow_faq_" + old_num).src = "images/img_arrow.gif";
		if (num == old_num) {
			old_num = 0;
		// if not, open new one
		} else {
			document.getElementById("faq_"+num).className = "faq_show";
			document.getElementById("arrow_faq_" + num).src = "images/img_arrow_down.gif";
			old_num = num;
		}
		// if no section is opne, open it and store it's number
	} else {
		document.getElementById("faq_" + num).className = "faq_show";
		document.getElementById("arrow_faq_" + num).src = "images/img_arrow_down.gif";
		old_num = num;
	}
}

function hide_faq(){
	for (n=1;n<=10;n++){
		document.getElementById("faq_"+n).className = "faq_hide";
		document.getElementById("arrow_faq_" + n).src = "images/img_arrow.gif";
	}
}

/*****************************************************************************/
/*Added: 5/22/09                                                             */
/*Function calls tracker(google) when user Opt-in is selected                */
/*****************************************************************************/
function optinCheckboxChecker(chkboxname)
{
    var chkBx = document.getElementById(chkboxname)
    if (chkBx != null)
    {
        if (chkBx.checked==true) urchinTracker('SRSHcheckbox'); //alert("check box checked"); //
    }
}
/*****************************************************************************/