﻿// JavaScript Document

function StrTrim(str)
{
	return str.replace(/^\s\s*/,"").replace(/\s\s*$/,"");	
}

function changePageSize(PageSize,showType,pageurl,suburl,curparams)
{
	var Sortby = document.getElementById('Sortby').value;
	var sUrl = pageurl;
	var SubHrefUrl = suburl;
	if(SubHrefUrl != ""){
		sUrl += SubHrefUrl;
	}
	sUrl += "-"+PageSize+Sortby+"1"+showType;
	if(curparams != "")
		window.location = sUrl + "/" + curparams;
	else
		window.location = sUrl + "/";
}

function changeSortby(Sortby,showType,pageurl,suburl,curparams)
{
	var PageSize = document.getElementById('PageSize').value;
	var sUrl = pageurl;
	var SubHrefUrl = suburl;
	if(SubHrefUrl != ""){
		sUrl += SubHrefUrl;
	}
	sUrl += "-"+PageSize+Sortby+"1"+showType;
	if(curparams != "")
		window.location = sUrl + "/" + curparams;
	else
		window.location = sUrl + "/";
}

function checkTag()
{
	var subkeyword = $("SubKeyword").value;
	var minp = $("MinPrice").value;
	var maxp = $("MaxPrice").value;
	var pn = /^(-|\+)?\d+(\.\d+)?$/;
	if(trim(minp) == "" && trim(maxp) != ""){
		if(!pn.test(maxp)){						
			$("MaxPrice").value = "";
			$("MaxPrice").focus();
			return false;
		}else if(trim(maxp) < 0){												
			$("MaxPrice").value = "";
			$("MaxPrice").focus();
			return false;
		}
	}else if(trim(minp) != "" && trim(maxp) == ""){
		if(!pn.test(minp)){
			$("MinPrice").value = "";
			$("MinPrice").focus();
			return false;
		}
	}else if(trim(minp) != "" && trim(maxp) != ""){
		if(!pn.test(minp)){						
			$("MinPrice").value = "";
			$("MinPrice").focus();
			if(!pn.test(maxp)){						
				$("MaxPrice").value = "";
				$("MinPrice").focus();
				return false;
			}
			return false;
		}else if(trim(minp) < 0){
			if(!pn.test(maxp)){						
				$("MaxPrice").value = "";
				$("MaxPrice").focus();
				return false;
			}else if(trim(maxp) < 0){												
				$("MaxPrice").value = "";
				$("MaxPrice").focus();
				return false;
			}
		}
		if(!pn.test(maxp)){						
			$("MaxPrice").value = "";
			$("MaxPrice").focus();
			return false;
		}else if(trim(maxp) < 0){												
			$("MaxPrice").value = "";
			$("MaxPrice").focus();
			return false;
		}
	}
	if(minp != "" || maxp != "" || trim(subkeyword) != "" || $("videos").checked || $("discount").checked){
		dinoSearchEngne.saveViewSubmitCookie(minp,maxp,subkeyword,$("videos").checked,$("discount").checked);
		return true;
	}else{
		return false;	
	}	
}
function getSortName(sortId){
	switch(parseInt(sortId)){
		case 1: return 'Price';
		case 4: return 'Release Date';
		case 5: return 'Popularity';
		case 6: return 'Reviews';
		case 7: return 'Diggs';
		case 8: return 'Videos';
	}
}
function SaveCookieCheck(pageView,currCount,currSort,pageCount,currPageNum,searchType,proTypeID,productTypeName,ProductCount){
	if(checkTag()){		
		SetCookie("searchType",2);
		SetCookie("searchProType","productTypeID="+proTypeID+"&productTypeName="+productTypeName);
		SetCookie("searchViewData","PageShowCount=" + currCount +"&PageSort="+ currSort +"&PageSortName=" +getSortName(currSort)+"&PageCount="+ pageCount +"&PageNum="+ currPageNum +"+&PageProductCount="+ ProductCount +"&PageView=" + pageView);
	}
}
function uuid() {
            var ret = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
            .replace(/[xy]/g, function (c) {
                var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
                return v.toString(16);
            }).toUpperCase();
            return ret;
        }
function saveRUUIDInCookie(){
	if(ReadCookie("RUUID") == ""){	
		SetCookie("RUUID",uuid(),0.33);
	}
}
function clearRUUIDInCookie(){
	if(ReadCookie("RUUID") != ""){	
		SetCookie("RUUID","",-1);
	}
}
function setViewSearchCookie(minp,maxp,subkeyword,videos,discount){
	var strDate = "";
	var priceRange = "";
	if(trim(minp)!="" && trim(maxp)!=""){
		if(parseInt(minp) < parseInt(maxp))
	 		priceRange = minp +"-"+ maxp;
		else
			priceRange = maxp +"-"+ minp;
	}else if(trim(minp)!=""){
		priceRange = minp;
	}else{
		priceRange = maxp;
	}
	if(videos)
		videos = 1;
	else
		videos = 0;;
	if(discount)
		discount = 1;
	else
		discount = 0;
	strDate = "pricRange=" + priceRange
			+ "&IsDiscount=" + discount
			+ "&IsVideos=" + videos;
	SetCookie("searchViewData1",strDate);			
	SetCookie("keywords",trim(subkeyword));
	SetCookie("currUrl",document.location);
}
function insertSearchData(searchTime,searchCount,searchKeyID){
	var st = ReadCookie("searchType");
	var strData = "";
	if((st == "0" || st == "1" || st == "2") && document.location !=ReadCookie("currUrl")){
			strData = "userAgent=" + navigator.userAgent 
					+ "&PCUID=" + ReadCookie("PCUID")
					+ "&UUID=" + ReadCookie("UUID") 
					+ "&RUUID=" + ReadCookie("RUUID")
					+ "&searchType=" + st
					+ "&searchTime=" + searchTime
					+ "&searchCount=" + searchCount 
					+ "&searchKeyID=" + searchKeyID
					+ "&keywords=" + ReadCookie("keywords")
					+ "&" + ReadCookie("searchProType")
					+ "&currUrl=" + ReadCookie("currUrl");
	}
	if((st == "1" || st == "2") && document.location !=ReadCookie("currUrl")){
	strData = strData + "&" + ReadCookie("searchViewData")
			 + "&" + ReadCookie("searchViewData1");
	}
		if(strData != ""){
		jQuery.ajax({url:jsMainSiteUrl+'SearchStatistics.cfm',
			type:'get',
			dataType:'text',
			data:strData,
			error:function(XMLHttpRequest,text,dd){this;},
			success:function(data){}
			   });
		SetCookie("currUrl",document.location);
		}
}
function showAllPage(){
	document.getElementById("pageDiv1").style.display = "none";
	document.getElementById("pageDiv2").style.display = "";	
}

function IsMaxLength(obj){
	var Mlength = obj.getAttribute ? parseInt(obj.getAttribute("Maxlength")) : "";
	if(obj.getAttribute && obj.value.length > Mlength){
		obj.value = obj.value.substring(0,Mlength);
		alert("Your feedback is too long.");
	}
}

function checkFeedbackForm(){
	var content = document.getElementById("feedback_content").value;
	var useremail=document.getElementById('uemail').value;
	var aspace=useremail.replace(/^\s+/,'').replace(/\s+$/,'');
	var email_pattern = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if(trim(content) == "" || content == null){
		alert("Please enter your feedback.");
		return false;
	}
	else if(!email_pattern.test(useremail) && aspace!="" && StrTrim(useremail)!="E-mail address...")
	{
		alert("Please enter a valid email address.");
		document.getElementById('uemail').value="";
		return false;
	}
	else if(StrTrim(useremail)=="E-mail address..." || StrTrim(useremail)=="")
	{
		alert("Please Input a email address.");	
		return false;
	}
	else{
		return true;
	}
}
