///////////////Begin Timer Functions/////////////////////////

var timerID = 0;
var tStart  = null;

function ManageFrontEndSession(){
	//alert(document.all.item("ElapsedTime").value + '\n' + document.all.item("SessionTimeout").value)
	if (document.all.item("ElapsedTime").value == document.all.item("SessionTimeout").value){
		document.all.item("SessionUserID").value = "";
		Stop();
		return;
	}
}

function UpdateTimer() {
   if(timerID) {
      clearTimeout(timerID);
      clockID  = 0;
   }
   if(!tStart) tStart = new Date();
   var   tDate = new Date();
   var   tDiff = tDate.getTime() - tStart.getTime();
   tDate.setTime(tDiff);
   document.all.item("ElapsedTime").value = "" + tDate.getMinutes() + "." + tDate.getSeconds();
   timerID = setTimeout("UpdateTimer()", 1000);
   ManageFrontEndSession();
}

function Start(){
	if (document.all.item("SessionUserID").value != ""){
		tStart   = new Date();
		document.all.item("ElapsedTime").value = "00:00";
		timerID  = setTimeout("UpdateTimer()", 1000);
	}
}


function Stop() {
   if(timerID) {
      clearTimeout(timerID);
      timerID  = 0;
   }
   tStart = null;
}

function Reset() {
   tStart = null;
   document.theTimer.theTime.value = "00.00";
}

//////////////End Timer Functions///////////////////////////
function GoBack(){
	//alert()
	window.history.go(-1);
	//return;
}

function SelectTextboxText(obj){
	obj.select()
}

function GoToStyle()
{
	if (Trim(document.all("txtSearchByStyle").value) == "" ||
			document.all("txtSearchByStyle").value == "Enter Style Number")
	{
		alert("Please enter the style you want to search");
		document.all("txtSearchByStyle").focus();
		document.all("txtSearchByStyle").select();
	}
	else
	{
		//The below line of code will take a user directly to the CMS3 page and show the style that matches the search
		//document.location.href = "cms3.asp?StyleNum=" + document.all("txtSearchByStyle").value;
		
		//The below line of code will take a user to a stylelist page. The user can see a list of all the styles that exist in the system and 
		//Click on one of the styles that he would like to go to. Click the link in the list will take the user to the CMS3 page where the details 
		// of the Style will be shown. 
		// This is still not a requirement and hence the below line is commented. 
		//alert()
		document.location.href = "Stylelist.asp?StyleNum=" + document.all("txtSearchByStyle").value;
	}
}

function ValidateLogin(){
	//alert("in Validate")
	//alert();
	if (document.userlogin.elements("SignOut").value != 1){
		//validate form:
		strUserName = Trim(document.userlogin.elements("txtLogin").value)
		strPassword = Trim(document.userlogin.elements("txtPassword").value)
		//alert(strUserName)
		//alert("in out block")
		//if (strUserName == "Enter UserName" || strUserName == "" || !checkmail(strUserName)){
		if (strUserName == "Enter UserName" || strUserName == "" ){
			//alert("in IN block 1")
			alert("The value entered in UserName is invalid")
			document.userlogin.elements("txtLogin").focus();
			document.userlogin.elements("txtLogin").select();
			return;
		}
		if (strPassword == "Enter Password" || strPassword == "" || !checkpasswordlength(strPassword)){
			//alert("in IN block 2")
			alert("The value entered in Password is invalid.\nPasswords have to be 8 characters or less ")
			document.userlogin.elements("txtPassword").focus();
			document.userlogin.elements("txtPassword").select();
			return;
		}
	}
	document.userlogin.submit()
}

function checkmail(strEmail){
	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
	var returnval=emailfilter.test(strEmail)
	return returnval
}

function checkpasswordlength(strPassword){
	//alert(strPassword.length )
	if (strPassword.length < 6 || strPassword.length > 25){		
		return false;
	}else{
		return true;
	}
}


//this function is called on the OnChange event of the Segment dropdown in the left navigation bar. 
function fxnSegmentSelect(intVal){
	//alert(intVal)
	if (intVal == 9){
		strURL = "http://www.allbilt.com"
		NewWindow(strURL,"Allbilt",800,600,1)
		//window.open(strURL,null,"height=600,width=800");
	}else{	
		strURL = "cms1.asp?CatID=" + intVal;
		window.document.location = strURL;	
	}
	return;
}

function NewWindow(mypage,myname,w,h,scroll){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
	window.open(mypage,myname,settings)
	return;
}

function ShowItemDetail(val){
//	alert(val)
	window.document.location = val

}

function validatefields(){
	document.frmUser.SaveUser.value = 1 
	//alert("This step will add the user and the page will be refreshed with the saved users info")
	/*
	If Main Dealer account then
	    CompanyName 
	 */
	
}

function dealerwindow(stateId)
{
	//var	features = "resize=yes,scroll=yes,titlebar=no,status=no,width=400,height=300";
	var	features = "resize=yes,scrollbars=yes,titlebar=no,status=no,width=400,height=300";
	alert(stateId)
	if (stateId == "None") {
		//var state = document.DealerLocator.state_name.value;
		var state = document.DealerLocator.state_name.options[document.DealerLocator.state_name.selectedIndex].value;
		if(state!=""){
			alert(state)
			window.open("dealerlist.asp?stateid=" + state, 'dealerlist', features);
		}
	}
	else {
		window.open("dealerlist.asp?stateid=" + stateId, 'dealerlist', features);
	}		
}	

function changeNews(strNewsID)
	{
		//alert()
		//var txt = document.editNews.NewsList.value;
		var result = strNewsID.split("|");
		if (result != null) {
			var id = result[0];
			var newsdate = result[1];
			var newsDesc = result[2];
			var newsDesc2 = newsDesc.replace(/~/g, "'");
		} 
	//var	features = "resize=yes,scroll=yes,titlebar=no,status=no,width=400,height=300";
	var	features = "resize=yes,scrollbars=yes,titlebar=no,status=no,width=400,height=400";
	//alert(features)
	window.open("newsStory.asp?id=" + id, 'NewsStory', features);
		
	}


//Begin javascript Trim Functions////////////////////////////////////
function Trim(s) 
   {
   	// Remove leading spaces and carriage returns
   	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
   	 { s = s.substring(1,s.length); }
     
   	// Remove trailing spaces and carriage returns
	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
   	 { s = s.substring(0,s.length-1); }
   	return s;
   }
//End Trim Functions ///////////////////////////////////////////////



function txtSearchByStyleOnKeyPress(ikeyCode){
   if (ikeyCode == 13){ 
		//alert('Enter has been pressed');  
		GoToStyle();
   } 
}

function txtPasswordOnKeyPress(ikeyCode){
   if (ikeyCode == 13){ 
		//alert('Enter has been pressed');  
		ValidateLogin();
   } 
}