//
function validatePassword(fieldRequired)
{
	if(document.getElementById(fieldRequired[0]).value != '')
	{
		var str = document.getElementById(fieldRequired[2]).innerHTML;
		str = str.replace("%", "");
		if(str < 80)
		{
			alert('Password strength should at least 80%');
			return false;
		}
		
		if(document.getElementById(fieldRequired[0]).value != document.getElementById(fieldRequired[1]).value)
		{
			alert('Retype Password does not match with the given Password');
			return false;
		}
		
	}
}


// show other city row
function hide_other()
{
	document.getElementById( 'other_city' ).style.display = 'none';
	document.getElementById( 'otherCity' ).disabled = true;
	document.getElementById( 'zip' ).className = '';
	document.getElementById( 'zip1' ).className = '';
}

// show other city row
function show_other()
{
	document.getElementById( 'other_city' ).style.display = '';
	document.getElementById( 'otherCity' ).disabled = false;
	document.getElementById( 'zip' ).className = 'blue_bdr1';
	document.getElementById( 'zip1' ).className = 'blue_bdr1';
}


// function to add category in multiple select box
function addCategory( text, id )
{
	var flag = true;
	
	// seed = document.listingFrm.seed;
	
	// get all listing categories
	feed = document.listingFrm.categories;
		
	// loop through all categories
	for ( i = 0; i < feed.length; i++ )
	{
		if ( feed.options[i].value == id )
			flag = false;
	}

	// check, if text, id, flag are exist
	if( text && id && flag )
	{
		feed.options[feed.length] = new Option( text, id );
		alert( 'Category "' + text + '" successfully added!' );
	}
	else
	{
		// check, if flag equal to true
		if ( !flag ) 
			alert( 'This category was already inserted' );
		else 
			alert('Please, select a valid category');
	}
	
	// call function to check maximum category qty
	if ( document.getElementById('moduleType').value == 'Listing' )
	{
		checkMaxCategory( feed.length, document.getElementById('totalPrice').value, document.getElementById('level').value, document.getElementById('path').value, document.getElementById('moduleType').value );
	}
}


// function to remove category from multiple select box
function removeCategory()
{
	// get listing categories
	feed = document.listingFrm.categories;
	
	// check, if selectedIndex is greater than equal to 0
	if ( feed.selectedIndex >= 0 ) 
		feed.remove( feed.selectedIndex );
	else
		alert( 'Please select a category first!' );

	// call function to check maximum category qty
	if ( document.getElementById('moduleType').value == 'Listing' )
	{
		checkMaxCategory( feed.length, document.getElementById('totalPrice').value, document.getElementById('level').value, document.getElementById('path').value, document.getElementById('moduleType').value );
	}
}
	

// function to display category path
function displayCategoryPath()
{
	// get listing categories
	feed = document.listingFrm.categories;
	
	// check, if selectedindex equal to -1
	if ( feed.selectedIndex == -1 )
		alert("Please select a category first!");
	else
	{
		var winl = ( ( ( screen.width ) / 2 ) - 350 );
		var wint = ( ( ( screen.height ) / 2 ) - 150 );
		var winprop = 'titlebar=no, toolbar=no, location=no, directories=no, status=no, width=700, height=60, left='+winl+', top='+wint+', menubar=no, scrollbars=yes, resizable=yes';
		window.open( "admin_category_path.html?id="+feed.options[feed.selectedIndex].value, "popup", winprop );
	}

	return;
}


// function to collect all added categories and submit the form
function listingSubmit( requiredField, requiredLabel )
{
	var flag = 0;
	
	// validate title
	if ( document.listingFrm.title.value == '' )
	{
		alert('Title is required field.');
		flag = 1;
		document.listingFrm.title.focus();
		return false;
	}
//	// validate email
//	if ( document.listingFrm.email.value == '' || !verify_email(document.listingFrm.email.value) )
//	{
//		alert('Please enter a valid email address.');
//		flag = 1;
//		document.listingFrm.email.focus();
//		return false;
//	}
	
	// check required field has value or not
	if ( requiredField != '' && requiredLabel != '' )
	{
		// split required strings
		requiredFields = requiredField.split('|');
		requiredLabels = requiredLabel.split('|');
		
		for ( i = 0; i<requiredFields.length; i++ )
		{
			if(requiredLabels[i]=='Email')
			{
				if (  document.getElementById( requiredFields[i] ).value == '' || !verify_email( document.getElementById( requiredFields[i] ).value ) )
				{
					alert('Please enter a valid email address.');
					flag = 1;
					document.listingFrm.email.focus();
					return false;
				}
			}
			else if ( document.getElementById( requiredFields[i] ). value == '' )
			{
				alert( requiredLabels[i]+' is required field.' );
				flag = 1;
				document.getElementById( requiredFields[i] ).focus();
				return false;
			}
		}
	}
	
	
	// check, if any error has been occured
	if ( flag == 0 )
	{
		// get all categories
		feed = document.listingFrm.categories;
		
		// get all return categories
		return_categories = document.listingFrm.return_categories;
	
		// check, if length of return_categories greater than 0
		if ( return_categories.value.length > 0 ) 
			return_categories.value="";
		
		// loop through all categories
		for ( i = 0; i < feed.length; i++ )
		{
			if ( !isNaN( feed.options[i].value ) )
			{
				if( return_categories.value.length > 0 )
					return_categories.value = return_categories.value + "," + feed.options[i].value;
				else
					return_categories.value = return_categories.value + feed.options[i].value;
			}
		}
		
		// finally submit the form
		document.listingFrm.submit();
	}
}



// function to collect all added categories and submit the form
function eventSubmit()
{
	// validate title
	if ( document.listingFrm.title.value == '' )
	{
		alert('Title is required field.');
		document.listingFrm.title.focus();
		return false;
	}
	
	
	// validate startDate
	if ( document.listingFrm.startDate.value == '' )
	{
		alert('Start date is required field.');
		document.listingFrm.startDate.focus();
		return false;
	}
	
	
	// validate endDate
	if ( document.listingFrm.endDate.value == '' )
	{
		alert('End date is required field.');
		document.listingFrm.endDate.focus();
		return false;
	}
	
	
	// get all categories
	feed = document.listingFrm.categories;
		
	// get all return categories
	return_categories = document.listingFrm.return_categories;
	
	// check, if length of return_categories greater than 0
	if ( return_categories.value.length > 0 ) 
		return_categories.value="";
		
	// loop through all categories
	for ( i = 0; i < feed.length; i++ )
	{
		if ( !isNaN( feed.options[i].value ) )
		{
			if( return_categories.value.length > 0 )
				return_categories.value = return_categories.value + "," + feed.options[i].value;
			else
				return_categories.value = return_categories.value + feed.options[i].value;
		}
	}
		
	// finally submit the form
	document.listingFrm.submit();
}



// function to collect all added categories and submit the form
function articleSubmit()
{
	// validate title
	if ( document.listingFrm.title.value == '' )
	{
		alert('Title is required field.');
		document.listingFrm.title.focus();
		return false;
	}
	
	
	// validate smallAbstract
	if ( document.listingFrm.smallAbstract.value == '' )
	{
		alert('Abstract is required field.');
		document.listingFrm.smallAbstract.focus();
		return false;
	}
	
	
	// get all categories
	feed = document.listingFrm.categories;
		
	// get all return categories
	return_categories = document.listingFrm.return_categories;
	
	// check, if length of return_categories greater than 0
	if ( return_categories.value.length > 0 ) 
		return_categories.value="";
		
	// loop through all categories
	for ( i = 0; i < feed.length; i++ )
	{
		if ( !isNaN( feed.options[i].value ) )
		{
			if( return_categories.value.length > 0 )
				return_categories.value = return_categories.value + "," + feed.options[i].value;
			else
				return_categories.value = return_categories.value + feed.options[i].value;
		}
	}
		
	// finally submit the form
	document.listingFrm.submit();
}



// function to collect all added categories and submit the form
function classifiedSubmit()
{
	// validate title
	if ( document.listingFrm.title.value == '' )
	{
		alert('Title is required field.');
		document.listingFrm.title.focus();
		return false;
	}
	
	
	// get all categories
	feed = document.listingFrm.categories;
		
	// get all return categories
	return_categories = document.listingFrm.return_categories;
	
	// check, if length of return_categories greater than 0
	if ( return_categories.value.length > 0 ) 
		return_categories.value="";
		
	// loop through all categories
	for ( i = 0; i < feed.length; i++ )
	{
		if ( !isNaN( feed.options[i].value ) )
		{
			if( return_categories.value.length > 0 )
				return_categories.value = return_categories.value + "," + feed.options[i].value;
			else
				return_categories.value = return_categories.value + feed.options[i].value;
		}
	}
		
	// finally submit the form
	document.listingFrm.submit();
}



// function for replacing unwanted characters from urlFriendly
function url_friendly( val )
{
	val = val.toLowerCase();
	val = val.replace( / /g, '_' );
	document.getElementById( 'urlFriendly' ).value = val;
}



// function for replacing unwanted characters from urlFriendly and include .html extension
function url_friendlyHTML( val, fromPage )
{
	val = val.toLowerCase();
	val = val.replace( / /g, '_' );
	document.getElementById( 'pageName' ).value = val;
	pageNameSearch( document.getElementById( 'pageName' ).value, fromPage );
}


//Function to validate the form,fieldRequired,fieldDescription
function validate(formobj,fieldRequired,fieldDescription,email)
{
	
	var alertMsg = "Following fields are missing or incorrect :\n";
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++)
	{
		var obj     = formobj.elements[fieldRequired[i]];
		if (obj)
		{
			switch(obj.type)
			{
				case "select-one":
				if(obj.options.length > 1)
				{
					if (obj.selectedIndex == 0 || obj.options[obj.selectedIndex].text == ""){
						alertMsg += " - " + fieldDescription[i] + "\n";
					}
				}
				break;
				
				case "select-multiple":
				if (obj.selectedIndex == -1)
				{
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
				
				case "text":
				case "password":
				case "textarea":
				if (trim_string(obj.value) == "" || trim_string(obj.value) == null)
				{
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				
				if(obj.id == 'conf_password' && trim_string(obj.value) != "")
				{
					tempMsg = confirm_password(formobj);
					if(tempMsg != '')
						alertMsg += tempMsg; 
				}
				break;
				
				case "file":
				if (trim_string(obj.value) == "" || trim_string(obj.value) == null)
				{
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				/*else
				{
					msg = extension_check(obj.value);
					if(msg != '')
					alertMsg += msg;
				}*/
				break;
				default:
			}
			if (obj.type == undefined)
			{
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++)
				{
					if (obj[j].checked)
					{
						blnchecked = true;
					}
				}
				if (!blnchecked)
				{
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
		
		// check valid email
		if( email == 1 && ( obj.name == 'userEmail' || obj.name == 'email' || obj.name == 'emailAddress' || obj.name == 'listing_email' || obj.name == 'event_email' || obj.name == 'banner_email' || obj.name == 'classified_email' || obj.name == 'article_email' || obj.name == 'new_account_email' || obj.name == 'contact_email' || obj.name == 'support_email' || obj.name == 'payment_email' || obj.name == 'committed_email' || obj.name == 'claim_email' || obj.name == 'general_email' || obj.name == 'realEstateEmail' ) )
		{
			if( obj.value != '' )
			{
				if( !verify_email( obj.value ) )
					alertMsg += ' - Enter valid E-Mail address'; 
			}
		}
	}
	
	if (alertMsg.length == l_Msg)
	{
		return true;
	}
	else
	{
		alert(alertMsg);
		return false;
	}
}



// function to check extension of a file
function extension_check(str){
	str = str.substring(str.lastIndexOf('.'),str.length);
	str = str.toLowerCase();
	if(str != '.jpg' && str != '.xls'){
		msg = ' - File Type is not allowed'; 
	}else{
		msg ='';
	}
	return msg;
}



//check for password is equal to confirm password
function confirm_password(formobj)
{
	var msg ='';
	var password = formobj.elements['password'];
	var conPass  = formobj.elements['conf_password'];
	if(trim_string(password.value) != trim_string(conPass.value))
	{
		msg = ' - Confirm password \n';
	}
	return msg;
}



// trim white space
function trim_string (str) 
{
  while (str.charAt(0) == ' ')
    str = str.substring(1);
  while (str.charAt(str.length - 1) == ' ')
    str = str.substring(0, str.length - 1);
  return str;
}



// verify email
function verify_email(str){
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str))){
		return false;
	}
	else{
		return true;
	}
}


function drop(){
	if(confirm('Are you sure to delete?'))
		return true;
	else
		return false;
}



function change_status(obj){
	chkFlag = 0;
	if(obj){
		for(i=0; i< obj.elements.length; i++){
			if(obj.elements[i].type=='checkbox'){
				if(obj.elements[i].checked == true){
					chkFlag = 1;
					break;
				}
			}
		}
	}
	else{
		alert('Error in script');
		return false;
	}
	if(chkFlag == 1){
		return status();
	}
	else{
		alert('Please select at least one record to change status!');
		return false;
	}
}

function drop_validate(obj){
	chkFlag = 0;
	if(obj){
		for(i=0; i< obj.elements.length; i++){
			if(obj.elements[i].type=='checkbox'){
				if(obj.elements[i].checked == true){
					chkFlag = 1;
					break;
				}
			}
		}
	}
	else{
		alert('Error in script');
		return false;
	}
	if(chkFlag == 1){
		return drop();
	}
	else{
		alert('Please select at least one record to delete!');
		return false;
	}
}

//Function to check or uncheck all the check box in a form.
function checkUncheck( obj )
{
	var set = document.getElementById( 'setAll' );
	if( set.checked == true )
	{
		for( i = 0; i < obj.elements.length; i++ )
		{
			if ( obj[i].type == "checkbox" )
				obj[i].checked = true;
		}
	}
	else
	{
		for( i = 0; i < obj.elements.length; i++ )
		{
			if ( obj[i].type == "checkbox" )
				obj[i].checked = false;
		}
	}
}
 
 
// select all elements of a form
function checkAll( form )
{
	var frm = document.getElementById( form );
	for( i = 0; i < frm.elements.length; i++ )
	{
		if ( frm.elements[i].type == "checkbox" )
			frm.elements[i].checked = true;
	}
}

// uncheck ALL elemnts of a form
function uncheckAll( form )
{
	var frm = document.getElementById( form );
	for( i = 0; i < frm.elements.length; i++ )
	{
		if ( frm.elements[i].type == "checkbox" )
			frm.elements[i].checked = false;
	}
}
 




function popup(url,title,w,h,r,s,m,t){
	//if vars aren't provided, use these defaults
	if (title==null) {
		title = 'popup';
	} if (w==null) {
		w=600;
	} if (h==null) {
		h=570;
	} if (r==null) {
		r='yes';
	} if (s==null) {
		s='yes';
	} if (m==null) {
		m='no';
	} if (t==null) {
		t='no';
	}
	vars = 'width='+w+',height='+h+',resizable='+r+',scrollbars='+s+',menubar='+m+',toolbar='+t;
	var newWin = window.open(url,title,vars);
	newWin.focus();
	return false;
}






// Function to validate user name field
function validateSearch()
{
	if(document.getElementById('userName').value=='' || document.getElementById('userName').value.length < 3 )
	{
		alert("Please enter search value atleast three character");
		document.getElementById('userName').focus();
	}
	else
		show_promo_user(document.getElementById('userName').value, 'admin_search.html');
}


//character counter function.
function char_counter(element,span,message,charCount)
{
  var elementObj=getObject(element);
  var spanObj=getObject(span);
  var remaining=charCount - elementObj.value.length;
  if(remaining <= 0)
  {
    remaining=0;
    message='<span class="disable"> '+message+' </span>';
    elementObj.value=elementObj.value.substr(0,charCount);
  }
  spanObj.innerHTML = message.replace("{CHAR}",remaining);
}



function getObject(obj)
{
  var theObj;
  if(document.all)
  {
    if(typeof obj=="string")
	{
      return document.all(obj);
    }
	else
	{
      return obj.style;
    }
  }
  if(document.getElementById)
  {
    if(typeof obj=="string")
	{
      return document.getElementById(obj);
    }
	else
	{
      return obj.style;
    }
  }
  return null;
}

var characterMsg	= '{CHAR} characters left.';















/*
// Function to get options list (send request)
function get_option_list( idval )
{   
	
	var Digital = new Date();
	var seconds = Digital.getSeconds();
	var url = "edit_records_ajax.php?pId="+idval+"&sec="+seconds; 
	xmlHttp = GetXmlHttpObject( option_list_response );
	xmlHttp.open("GET", url , true );
	xmlHttp.send( null );
}

// Function to get states ( handle response )
function option_list_response()
{ 
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" )
		document.getElementById( "optionListTxt" ).innerHTML = xmlHttp.responseText;
	else if( xmlHttp.readyState != 4 )
		document.getElementById( "optionListTxt" ).innerHTML = 'Loading ......';
} 


// Function to Edit record of group options (send request)
function edit_group_options( frm , groupId , productId )
{   
	// declare variables
	var Digital = new Date();
	var seconds = Digital.getSeconds();
	var url;
	var val = '';
	var status = document.getElementById('optionStatus').value;
	var price = document.getElementById('productPrice').value;
	// fetch all options
	for ( var i = 0; i < frm.elements.length; i++ )
		if ( frm.elements[i].type == "checkbox" )
			if ( frm.elements[i].checked == true )
				val += frm.elements[i].value + '|';
	
	// remove last pipe character
	val = val.substring( 0, val.length - 1 );
	
	// send request
	url = "edit_records_ajax.php?groupId="+groupId+"&sec="+seconds+"&productId="+productId+"&value="+val+"&price="+price+"&status="+status; ; 
	xmlHttp = GetXmlHttpObject( edit_response );
	xmlHttp.open("GET", url , true );
	xmlHttp.send( null );
}


// Function to Edit record (send request)
function edit_record( idval )
{   
	var Digital = new Date();
	var seconds = Digital.getSeconds();
	var val	=	document.getElementById('Txt'+idval).value;
	var url = "edit_records_ajax.php?recId="+idval+"&sec="+seconds+"&value="+val; 
	xmlHttp = GetXmlHttpObject( edit_response );
	xmlHttp.open("GET", url , true );
	xmlHttp.send( null );
}

// Function to edit record ( handle response )
function edit_response()
{ 
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" )
		alert(xmlHttp.responseText);
} 



// Function to get states (send request)
function get_states( idval )
{   
	var Digital = new Date();
	var seconds = Digital.getSeconds();
	var url = "get_states.php?CatID="+idval+"&sec="+seconds; 
	xmlHttp = GetXmlHttpObject( state_response );
	xmlHttp.open("GET", url , true );
	xmlHttp.send( null );
}

// Function to get states ( handle response )
function state_response()
{ 
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" )
		document.getElementById( "stateTxt" ).innerHTML = xmlHttp.responseText;
	else if( xmlHttp.readyState != 4 )
		document.getElementById( "stateTxt" ).innerHTML = 'Loading ......';
} 


// Function to set default autoresponders
function autoresponder( userId, responderId )
{   
	var Digital = new Date();
	var seconds = Digital.getSeconds();
	var url = "default_autoresponder.php?userId="+userId+"&responderId="+responderId+"&sec="+seconds; 
	xmlHttp = GetXmlHttpObject( responder_response );
	xmlHttp.open("GET", url , true );
	xmlHttp.send( null );
}

// Function to autoresponder ( handle response )
function responder_response()
{ 
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" )
		alert( xmlHttp.responseText );
	else if( xmlHttp.readyState != 4 )
		;
} 


// Function for initialilize GetXmlHttpObject
function GetXmlHttpObject( handler )
{ 
	var objXmlHttp=null
	
	if (navigator.userAgent.indexOf("Opera")>=0)
	{
		alert("This example doesn't work in Opera") 
		return 
	}
	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5") >= 0 )
		{
			strName="Microsoft.XMLHTTP"
		} 
		try
		{ 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
		} 
		catch(e)
		{ 
			alert("Error. Scripting for ActiveX might be disabled") 
			return 
		} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp
	}
}
*/
