// open a popup window whenever the user is going to epinions to write a review
// show the popup only once per session
// then reidrect to epinions write a review page
// in case of shopping.com , show a different popup
function goToEpinions(prdId,nopopup)
{
	var hostName = location.host;
	var popupInd = getCookie('rvpopind');
	if ( !popupInd && !nopopup )
	{
		setCookie('rvpopind',1);
		var popupFileName;
		if ( hostName.indexOf('.shopping.') != -1 )
		{
			popupFileName = '/xProductReviews/DT-EP-pop_shop.html';
		}
		else
		{
			popupFileName = '/xProductReviews/DT-EP-pop.html';
		}
		var win = window.open(popupFileName,'',"status=no,resizable=no,width=495,height=420");
	}
	var url = 'http://www.epinions.com/content/write.html/tnode_~' + prdId;
	document.location.href=url;
}


function goToUserEp (user,nopopup)
{
	var popupInd = getCookie('usrpopind');
	if ( !popupInd && !nopopup )
	{
		setCookie('usrpopind',1);
		var win = window.open('/xProductReviews/DT-EP-USER-pop.html','',"status=no,resizable=no,width=495,height=420");
	}
	var url = 'http://www.epinions.com/user-' + user;
	document.location.href=url;
}

// build the url for sorting the product or merchant reviews
function sortReviews(prdName,mrId,zeroDeals)
{
	var theForm = document.frmSort;
	var selectedIndex = theForm.sortby.selectedIndex;
	var sortVal = theForm.sortby.options[selectedIndex].value;
	var newLocation;
	if ( prdName )
	{
		newLocation = '/xPR-' + prdName + '~S-' + sortVal;
		if (zeroDeals == 1)
		{
			newLocation	= newLocation + '~ZD-' + zeroDeals ;
		}
	}
	else
	{
		newLocation = '/xMR-~MRD-' + mrId + '~S-' + sortVal;
	}
	document.location.href=newLocation;
}

function storeDirSubmit ()
{
	var theForm = document.frmStoreDir;
	var searchTerm = theForm.storeSearch.value;
	var newLocation = '/xSD-' + searchTerm;
	document.location.href=newLocation;
}

function userStatusPopup (status)
{
	var win;
	if ( status == 'adv' )
	{
		 win = window.open('/xProductReviews/memberAdv.html','',"status=no,resizable=no,width=495,height=200");
	}
	else if ( status == 'top' )
	{
		 win = window.open('/xProductReviews/memberTopRev.html','',"status=no,resizable=no,width=495,height=180");
	}
	else
	{
		 win = window.open('/xProductReviews/memberLead.html','',"status=no,resizable=no,width=495,height=180");
	}
}

function getCookie(NameOfCookie)
{
	// First we check to see if there is a cookie stored.
	// Otherwise the length of document.cookie would be zero.
	if (document.cookie.length > 0) 
	{ 
		// Second we check to see if the cookie's name is stored in the
		// "document.cookie" object for the page.

		// Since more than one cookie can be set on a
		// single page it is possible that our cookie
		// is not present, even though the "document.cookie" object
		// is not just an empty text.
		// If our cookie name is not present the value -1 is stored
		// in the variable called "begin".

		begin = document.cookie.indexOf(NameOfCookie+"="); 
		if (begin != -1) // Note: != means "is not equal to"
		{ 
			// Our cookie was set. 
			// The value stored in the cookie is returned from the function.
			begin += NameOfCookie.length+1; 
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end)); 
		} 
	}
	return null; 
	// Our cookie was not set. 
	// The value "null" is returned from the function.
}


function setCookie(NameOfCookie, value, expiredays) 
{

	// Three variables are used to set the new cookie. 
	// The name of the cookie, the value to be stored,
	// and finally the number of days until the cookie expires.
	// The first lines in the function convert 
	// the number of days to a valid date.

	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

	// The next line stores the cookie, simply by assigning 
	// the values to the "document.cookie" object.
	// Note the date is converted to Greenwich Mean time using
	// the "toGMTstring()" function.

	document.cookie = NameOfCookie + "=" + escape(value) + 
	((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function normelizeStr(str)
{
	var newStr = str;
	var strValidChars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz&_-.';	
	// remove all the characters that their ascii code is bigger than 255
	var stringLen = newStr.length;
	var i;
	var tempStr = "";
	for ( i=0 ; i<stringLen ; i++ )
	{
		if (strValidChars.indexOf(newStr.charAt(i)) != -1)
		{
			tempStr = tempStr + newStr.charAt(i);
		} else {
			tempStr = tempStr + " ";
		}
	}
	return (tempStr.toLowerCase());
}

function storeDirSubmit() 
{

	var theForm = document.frmStoreDir;
	if (theForm.storeSearch.value == '') 
	{
		alert('Please enter a store to search on.');
		return;
	}
	else if (theForm.storeSearch.value.length < 3)
	{
		alert('Please enter at least 3 characters.');
		return;
	}
	else 
	{	
		theForm.storeSearch.value = normelizeStr(theForm.storeSearch.value);
		var searchTerm = theForm.storeSearch.value;
		searchTerm = searchTerm.replace(/\s/gi,"_");
		var newLocation = '/xSD-' + searchTerm;
		document.location.href=newLocation;
	}
}

function chkWriteRevForm ()
{
	var bOption = -1
	var i;
	for (i=0; i < document.merchreview.orating.length; i++) 
	{
		if (document.merchreview.orating[i].checked) 
		{
			bOption = i
		}
	}

	if (bOption == -1) {
		alert("Please select an Overall Rating for this merchant on a scale from 1 to 5")
		return;
	}
	else if (document.merchreview.nickname.value == '') {
		alert('Please enter a Nick Name to appear with your review.');
		return;
	}
	else if (document.merchreview.nickname.value.length < 5) {
		alert('Please enter at least 5 characters for your Nick Name.');
		return;
	}
	else if (document.merchreview.nickname.value.length > 1000) {
		alert('Please enter no more than 20 characters for your Nick Name.');
		return;
	}
	else if (document.merchreview.email.value == '') {
		alert('Please enter a valid Email address.');
		return;
	}
	else if (document.merchreview.email.value.indexOf("@")==-1){ 
		alert("Please enter a valid Email address.");
	    return;
  	}
	else if (document.merchreview.email.value.indexOf(".")==-1){ 
		alert("Please enter a valid Email address.");
	    return;
  	}
	else if (document.merchreview.reviewtext.value==''){ 
		alert("Please enter a breif review.");
	    return;
  	}
	else if (document.merchreview.reviewtext.value.length < 1){ 
		alert("Please enter at least 100 characters within you review.");
	    return;
  	}	
	else if (document.merchreview.reviewtext.value.length > 50000){ 
		alert("Please enter no more than 500 characters within you review.");
	    return;
  	}		
	else if (document.merchreview.terms.checked == "") {
		alert("You must agree to DealTime's legal terms.  Please check the box that says 'I agree to the above terms.'")
		return;
	}	
	else {	
		//document.merchreview.agree.value='';
		document.merchreview.submit();
	}

}


function checkStep1UK () {
	var theElement = document.frm_step1.reviewtext;
	var len = theElement.value.length;
	var retVal = true; 
	if ( len < 99 ) {
		retVal = false;
		alert('Please enter at least 100 characters for your review');
	}
	return retVal;
}

function checkStep2UK () {
	var theForm = document.frm_step2;
	var retVal = false;
	for (var i=0; i < theForm.orating.length; i++) 
	{
		if (theForm.orating[i].checked) 
		{
			retVal = true;
		}
	}
	if ( !retVal ) {
		alert("Please select an Overall Rating for this merchant on a scale from 1 to 5")
	}
	return retVal;
}

function checkStep31UK () {
	var theForm = document.frm_step31;
	var retVal = true;
	if ( theForm.nickname.value == '' ) {
		alert('Please enter a Nick Name to appear with your review.');
		retVal = false;
	} else if ( theForm.email.value == '' ) {
		alert('Please enter a valid Email address.');
		retVal = false;
	} else if (theForm.email.value.indexOf("@")==-1){ 
		alert("Please enter a valid Email address.");
		retVal = false;
	} else if (theForm.email.value.indexOf(".")==-1){ 
		alert("Please enter a valid Email address.");
		retVal = false;
	} else if (theForm.terms.checked == "") {
		alert("You must agree to Shopping.com's legal terms.  Please check the box that says 'I agree to the above terms.'")
		retVal = false;
	}
	return retVal;
	
}
