// pickup owner
var lastObj;


function isBrowser(b) 
{

	browserOk = false; 
	versionOk = false; 
	browserOk = (navigator.appName.indexOf(b) != -1); 
	return browserOk ;
}

function openWin(url, target, params, Width, Height)
{
	Top = (screen.height - Height)/2;
	Left =	(screen.width - Width)/2;

	window.open(url, target, params + ",top=" + Top + ", left=" + Left + ", width=" + Width + ", height=" + Height); 
}


function closeWindow()
{
    window.open("", "_self");
    top.opener = null;
    top.close();
}   

function addToList(strValue, strList)
{
	if(strList == "")
	{
		return(strValue);	
	}
	else if(!inList(strValue, strList))
	{
		return(strList + "|" + strValue);	
	}
	else
	{
		return(strList);	
	}
}

function inList(strValue, strList)
{
	strList_arr = strList.split("|");
	for(i=0;i<strList_arr.length;i++)
	{
		if(strValue == strList_arr[i])
		{
			return(true);	
		}
	}
	return(false);
}

function removeFromList(strValue, strList)
{
	if(strList == "")
	{
		return("");	
	}
	else if(inList(strValue, strList))
	{
		var ret="";
		strList_arr = strList.split("|");
		for(i=0;i<strList_arr.length;i++)
		{
			if(strValue == strList_arr[i])
			{
				continue;
			}
			else
			{
				if(ret !="")
				{
					ret	+="|";
				}
				ret+= strList_arr[i];
			}
		}
		return(ret);
	}
	else
	{
		return(strList);	
	}
}

function clearOptions(obj)
{
	for(var i=obj.options.length-1; i>=0; i--)
	{
		obj.remove(i);
	}
}

function removeOptions(obj)
{
	for(var i=obj.options.length-1;i>=0;i--)
	{
		obj.remove(i);
	}
}

function RequestURL_GET(url, arg, processFunc) 
{
	var xmlhttp = null;
	if (window.ActiveXObject) 
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	else 
	{
		if (window.XMLHttpRequest) 
		{
			xmlhttp = new XMLHttpRequest();
		}
	}
	xmlhttp.open("GET", url, arg);

	if (arg) 
	{
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4){
				if(xmlhttp.status == 200) eval(xmlhttp.responseText);
			}
		}
	}
	else
	{
		xmlhttp.onreadystatechange = function () 
		{
			getReady(processFunc, arg, xmlhttp);
		};
	}
	xmlhttp.send(null);
}

function getReady(processFunc, arg, xmlhttp) 
{
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			eval(processFunc + "(\"" + xmlhttp.responseText +"\","+arg+");");
		}
		xmlhttp = null;
	}
}

function imagePreview(filectlname,imagectlname)
{
	if(isBrowser('Netscape'))
	{
		alert('Netscape can not support preview function.');
		return;
	}
	var imagectl = document.getElementsByName(imagectlname)[0];
	
	var filectl = document.getElementsByName(filectlname)[0];
	if(imagectl != null && filectl != null && filectl.value!="")
	{
		//imagectl.style.visibility="visible";
		imagectl.style.display='';
		imagectl.src = filectl.value ;
		DrawImage(imagectl);
	}

}
function imagePreview2(filectlname,imagectlname)
{
	if(isBrowser('Netscape'))
	{
		alert('Netscape can not support preview function.');
		return;
	}
	var imagectl = document.getElementsByName(imagectlname)[0];
	
	var filectl = document.getElementsByName(filectlname)[0];
	if(imagectl != null && filectl != null && filectl.value!="")
	{
		//imagectl.style.visibility="visible";
		imagectl.style.display='';
		imagectl.src = filectl.value ;
		DrawImageFree(imagectl,580,300);
	}

}
function imagePreviewFree(filectlname,imagectlname,width,height)
{
	if(isBrowser('Netscape'))
	{
		alert('Netscape can not support preview function.');
		return;
	}
	var imagectl = document.getElementsByName(imagectlname)[0];
	
	var filectl = document.getElementsByName(filectlname)[0];
	if(imagectl != null && filectl != null && filectl.value!="")
	{
		//imagectl.style.visibility="visible";
		imagectl.style.display='';
		imagectl.src = filectl.value ;
		DrawImageFree(imagectl,width,height);
	}

}

function DrawImageFree(ImgD,iwidth,iheight)
{
	var image=new Image();
	//var iwidth = 140;  
	//var iheight = 110;  
	image.src=ImgD.src;
	if(image.width>0 && image.height>0)
	{
		flag=true;
		if(image.width/image.height>= iwidth/iheight)
		{
			/*
			if(image.width>iwidth)
			{ 
				ImgD.width=iwidth;
				ImgD.height=(image.height*iwidth)/image.width;
			}
			else
			{
				ImgD.width=image.width; 
				ImgD.height=image.height;
			}
			ImgD.alt=image.width+"x"+image.height;
			*/
			
			ImgD.width=iwidth;
			ImgD.height=(image.height*iwidth)/image.width;
		}
		else
		{
			/*
			if(image.height>iheight)
			{ 
				ImgD.height=iheight;
				ImgD.width=(image.width*iheight)/image.height; 
			}
			else
			{
				ImgD.width=image.width; 
				ImgD.height=image.height;
			}
			ImgD.alt=image.width+"x"+image.height;
			*/
			ImgD.height=iheight;
			ImgD.width=(image.width*iheight)/image.height; 

		}
	}
} 

function isChecked(objForm, elementname, msg, confirmmsg)
{
	if(objForm.elements)
	{
		for (var i=0;i<objForm.elements.length;i++)
		{		
			var e = objForm.elements[i];
			if (e.name == elementname)
			{
				if(e.checked)
				{
					if(confirmmsg)
					{
						return(confirm(confirmmsg));	
					}
					else
					{
						return(true);	
					}
				}
			}
		}
		alert(msg);
		return(false);
	}
	else
		return false;
}

function changeChecked(objForm, elementname, curControl)
{
	if(objForm.elements)
	{
		for (var i = 0; i < objForm.elements.length; i++)
		{		
			var e = objForm.elements[i];
			if (e.name == elementname && e != curControl)
			{
				e.checked = false;
			}
		}
	}
}

function saveRichText(ctlname)
{
	var ctl = document.getElementsByName(ctlname)[0];
	if(ctl != null)
		ctl.value=WPro.myRichText.getCode();
}

function saveRichText2(ctlname)
{
	var ctl = document.getElementsByName(ctlname)[0];
	if(ctl != null)
		ctl.value=WPro.myRichText2.getCode();
}

//save content from richeditor to hidden
function saveContent(obj)
{
	//save content and rating
	if(lastObj == 0)
	{
		eval('document.form_individualreview.introduction').value = WPro.myRichText.getCode();
	}
	else
	{
		eval('document.form_individualreview.reviewelement' + lastObj).value = WPro.myRichText.getCode();
		eval('document.form_individualreview.rating' + lastObj).value = eval('document.form_individualreview.rating').value;
	}
	
	
	//display content and rating
	if(obj)
	{		
		lastObj = obj.value;
		if(lastObj == 0)
		{
			WPro.myRichText.setCode(eval('document.form_individualreview.introduction').value);

			clearOptions(eval('document.form_individualreview.rating'));

			eval('document.form_individualreview.rating').options[0] = new Option('N/A', '');
			eval('document.form_individualreview.rating').value = '';
		}
		else
		{
			
			if(eval('document.form_individualreview.reviewelement' + lastObj))
				WPro.myRichText.setCode(eval('document.form_individualreview.reviewelement' + lastObj).value);

			clearOptions(eval('document.form_individualreview.rating'));

			var maxscore = 0;
			if(eval('document.form_individualreview.maxscore' + lastObj))
				maxscore = (eval('document.form_individualreview.maxscore' + lastObj).value);

			var i = 0;
			for(i = 0; i < Number(maxscore) * 2 + 1; i++)
			{
				eval('document.form_individualreview.rating').options[i] = new Option(i * 0.5, i * 0.5);
			}

			if(!eval('document.form_individualreview.rating' + lastObj))
				eval('document.form_individualreview.rating').value = 0;
			else
			{
				if(eval('document.form_individualreview.rating' + lastObj).value == 'null')
				{
					eval('document.form_individualreview.rating').value = 0;
				}
				else
					eval('document.form_individualreview.rating').value = eval('document.form_individualreview.rating' + lastObj).value;
			}
				
		}
	}
}

function saveRating(obj)
{

	//save rating
	if(lastObj != 0)
	{
		eval('document.form_customerreview.rating' + lastObj).value = eval('document.form_customerreview.rating').value;
	}
	//display rating
	if(obj)
	{		
		lastObj = obj.value;
		if(lastObj == 0)
		{
			clearOptions(eval('document.form_customerreview.rating'));

			eval('document.form_customerreview.rating').options[0] = new Option('N/A', '');
			eval('document.form_customerreview.rating').value = '';
		}
		else
		{
			clearOptions(eval('document.form_customerreview.rating'));

			var maxscore = (eval('document.form_customerreview.maxscore' + lastObj).value);

			var i = 0;
			for(i = 0; i < Number(maxscore) * 2 + 1; i++)
			{
				eval('document.form_customerreview.rating').options[i] = new Option(i * 0.5, i * 0.5);
			}
			eval('document.form_customerreview.rating').options[i] = new Option('N/A', null);
			
			if(eval('document.form_customerreview.rating' + lastObj).value == 'null' || eval('document.form_customerreview.rating' + lastObj).value == '')
			{
				eval('document.form_customerreview.rating').value = null;
			}
			else
			{
				eval('document.form_customerreview.rating').value = eval('document.form_customerreview.rating' + lastObj).value;
			}
			
		}
	}
}

//get content when review section is changed in individual review section
function onchange_review_section_i()
{
	var reviewsectionid = eval('document.form_individualreview.reviewsectionid').value;
	var reviewsectionid_old = eval('document.form_individualreview.reviewsectionid_old').value;
	var individualreviewid = eval('document.form_individualreview.individualreviewid').value;

	if(!reviewsectionid_old || reviewsectionid_old == 0 || confirm('The Store List, Review Elements and Ratings will be changed accordingly, and the existing content will be removed. \nDo you want to continue?'))
	{
		
		var url = "./ajax_review_section_changed_i.php?reviewsectionid=" + reviewsectionid + "&reviewsectionid_old=" + reviewsectionid_old + "&individualreviewid=" + individualreviewid;
		
		//window.open(url);
		
		RequestURL_GET(url, true, "ProcessXML");
		
		//reset old section id to new one
		eval('document.form_individualreview.reviewsectionid_old').value = reviewsectionid;
		
	}
	else
	{
		eval('document.form_individualreview.reviewsectionid').value = eval('document.form_individualreview.reviewsectionid_old').value;
	}
}

function loadRatings(ctrl, maxscore)
{
	var i = 0;
	for(i = 0; i < Number(maxscore) + 1; i++)
	{
		ctrl.options[i] = new Option(i, i);
	}

}

function checkCustomerReviewData()
{
	
	document.getElementById('submittedby_error').innerHTML = '';
	document.getElementById('emailaddress_error').innerHTML = '';
	document.getElementById('overallscore_error').innerHTML = '';

	var result = true; 
	if(document.getElementById('submittedby').value == '')
	{
		result = false;
		document.getElementById('submittedby_error').innerHTML = '<br>Please input your name here.';
	}
	if(document.getElementById('emailaddress').value == '')
	{
		result = false;
		document.getElementById('emailaddress_error').innerHTML = '<br>Please input your email address here.';
	}
	if(document.getElementById('overallscore').value == '')
	{
		result = false;
		document.getElementById('overallscore_error').innerHTML = '<br>Please input overall score for this store here.';
	}
	
	if(result == false)
	{
		launch_link(document.getElementById('submit_customer_review_link'));
	}

	return result;
}

function onlyNumber(e)
{
    var key;
    iKeyCode = window.event?e.keyCode:e.which;

    if( !((((iKeyCode >= 48) && (iKeyCode <= 57)) || ((iKeyCode >= 96 ) && (iKeyCode <= 105))) || (iKeyCode == 13) || (iKeyCode == 46) || (iKeyCode == 45) || (iKeyCode == 37) || (iKeyCode == 39) || (iKeyCode == 8)))
    {   
        if(getOs()=="MSIE")
        {
            e.returnValue=false;
        }
        else
        {
            e.preventDefault();
        }
    }
} 

function launch_link(ctrl)
{
	if(getOs()=="MSIE")
	{
		ctrl.click();
	}
	else
	{
		var m_event = document.createEvent( "MouseEvents")
		m_event.initEvent("click", true, true);
		ctrl.dispatchEvent(m_event);
	}
}

function getOs()
{
    var OsObject = "";
   if(navigator.userAgent.indexOf("MSIE")>0) {
        return "MSIE";
   }
   if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){
        return "Firefox";
   }
   if(isSafari=navigator.userAgent.indexOf("Safari")>0) {
        return "Safari";
   }
   if(isCamino=navigator.userAgent.indexOf("Camino")>0){
        return "Camino";
   }
   if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){
        return "Gecko";
   }
  
} 