// JavaScript Document

// FieldName, WarningName, RemainingName, MaxChars
function CheckFieldLength(fn,wn,rn,mc) {
  var len = fn.value.length;
  if (len > mc) {
    fn.value = fn.value.substring(0,mc);
    len = mc;
  }
  document.getElementById(wn).innerHTML = len;
  document.getElementById(rn).innerHTML = mc - len;
}


// Jump Menu
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_jumpMenuGo(selName,targ,restore){ //v3.0
  var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ,selObj,restore);
}


// Property List Sorting - 
function selectOrderChange() {
   CategoryselIdx = document.frmSort.Category.selectedIndex;
   
   if(document.frmSort.Order.options.length > 2) {
	   if(CategoryselIdx > 0) {
		   document.frmSort.Order.options[2].selected = true;
	   }
   }

   if(document.frmSort.Order.options.length > 3) {
		if(CategoryselIdx > 0) {
		   document.frmSort.Order.options[3].selected = true;
	   }
   }  
}

// Auto Tabbing - Mobile Number
function TabNext(obj,event,len,next_field) {
	var phone_field_length = 0;	
	
	if (event == "down") {
		phone_field_length=obj.value.length;
		}
	else if (event == "up") {
		if (obj.value.length != phone_field_length) {
			phone_field_length=obj.value.length;
			if (phone_field_length == len) {
				next_field.focus();
			}
		}
	}
}


// http://www.somacon.com/p143.php
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}	
}


function mosDHTML(){
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=this.agent.indexOf("Opera 5")<-1
	this.ie5=(this.ver.indexOf("MSIE 5")<-1 && this.dom && !this.opera5)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")<-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6
	this.mac=this.agent.indexOf("Mac")<-1
	this.ns6=(this.dom && parseInt(this.ver) <= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6||this.ie5||this.ie4||this.ns4||this.ns6||this.opera5);

	this.activeTab = '';
	this.onTabStyle = 'ontab';
	this.offTabStyle = 'offtab';

	this.setElemStyle = function(elem,style) {
		document.getElementById(elem).className = style;
	}
	this.showElem = function(id) {
		if ((elem = document.getElementById(id))) {
			elem.style.visibility = 'visible';
			elem.style.display = 'block';
		}
	}
	this.hideElem = function(id) {
		if ((elem = document.getElementById(id))) {
			elem.style.visibility = 'hidden';
			elem.style.display = 'none';
		}
	}
	this.cycleTab = function(name) {
		if (this.activeTab) {
			this.setElemStyle( this.activeTab, this.offTabStyle );
			page = this.activeTab.replace( 'tab', 'page' );
			this.hideElem(page);
		}
		this.setElemStyle( name, this.onTabStyle );
		this.activeTab = name;
		page = this.activeTab.replace( 'tab', 'page' );
		this.showElem(page);
	}
	return this;
}
var dhtml = new mosDHTML();


function updateMobileNo() {

	var mobile_counrycode =  document.getElementById('LMobileNoCountryCode').value;
	var mobile_prefix =  document.getElementById('LMobileNoMobileCode').value;
	var mobile_postfix =  document.getElementById('LMobileNoMobileNumber').value;
	var mobile = "";
	
	mobile = "+" + mobile_counrycode + "(" + mobile_prefix + ")" + mobile_postfix;
	document.getElementById('LMobileNo').value = mobile;
}


function validateSearchPriceRange(form)
{  
  if(form.PriceMax.value.length>0){
  	if(isNumeric(form.PriceMax.value)==true){
	  if (form.PriceMax.value.indexOf(".")> -1) {
		alert( "Price should be a numeric value." );
		form.PriceMax.focus();
		return false ;
	  }
	  if (form.PriceMax.value<0){
		alert( "Price should be a numeric value." );
		form.PriceMax.focus();
		return false ;
	  }
	}else{
		alert( "Price should be a numeric value." );
		form.PriceMax.focus();
		return false ;
	}	
  }
   
   if(form.PriceMin.value.length>0){
   	if(isNumeric(form.PriceMin.value)==true){
	  if (form.PriceMin.value.indexOf(".")> -1) {
		alert( "Price should be a numeric value." );
		form.PriceMin.focus();
		return false ;
	  }
	  if (form.PriceMin.value<0){
		alert( "Price should be a numeric value." );
		form.PriceMin.focus();
		return false ;
	  }
	}else{
		alert( "Price should be a numeric value." );
		form.PriceMin.focus();
		return false ;
	}	
  }
  	  if (parseInt(form.PriceMax.value) < parseInt(form.PriceMin.value)){
    	alert( "Please enter valid Price Range.\nMinimum Price should be less than the Maximum Price." );
		form.PriceMax.focus()
    	return false ;
  	  }


  return true ;
}

function isNumeric(value) {
  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
} 


//------
function validatePriceRange(form)
{  
  if(form.TSPriceMax.value.length>0){
  	if(isNumeric(form.TSPriceMax.value)==true){
	  if (form.TSPriceMax.value.indexOf(".")> -1) {
		alert( "Price should be a numeric value." );
		form.TSPriceMax.focus();
		return false ;
	  }
	  if (form.TSPriceMax.value<0){
		alert( "Price should be a numeric value." );
		form.TSPriceMax.focus();
		return false ;
	  }
	}else{
		alert( "Price should be a numeric value." );
		form.TSPriceMax.focus();
		return false ;
	}	
  }
   
   if(form.TSPriceMin.value.length>0){
   	if(isNumeric(form.TSPriceMin.value)==true){
	  if (form.TSPriceMin.value.indexOf(".")> -1) {
		alert( "Price should be a numeric value." );
		form.TSPriceMin.focus();
		return false ;
	  }
	  if (form.TSPriceMin.value<0){
		alert( "Price should be a numeric value." );
		form.TSPriceMin.focus();
		return false ;
	  }
	}else{
		alert( "Price should be a numeric value." );
		form.TSPriceMin.focus();
		return false ;
	}	
  }
  	  if (parseInt(form.TSPriceMax.value) < parseInt(form.TSPriceMin.value)){
    	alert( "Please enter valid Price Range.\nMinimum Price should be less than the Maximum Price." );
		form.TSPriceMax.focus()
    	return false ;
  	  }


  return true ;
}

