// SVN: $Id: commonFunction.js,v 1.25 2009/05/18 14:10:32 bhavinm Exp $

//check() function checks and display alert if all checkboxes are unchecked in indexSuccess.php of member module in admin side
function check(oCheckbox, ssCheckbox)
{  
    var snCounter = 0;
    var anCommonId = eval('document.getElementsByName("' + ssCheckbox + '")');

       
    	
	for( x=0; x<anCommonId.length; x++ )
    {
		var ssChecked = anCommonId[x];
		
		
		if (oCheckbox.id == 'bDelete' || oCheckbox.id == 'bStatus' || oCheckbox.id == 'bDelete1' || oCheckbox.id == 'bStatus1')
        {
		  if ( ssChecked.checked ) 
		  	snCounter++;
       }
	}

	  
	if(snCounter < 1 && (oCheckbox.id == 'bDelete' || oCheckbox.id == 'bDelete1'))
    {
		 if( oCheckbox.id == 'bDelete' || oCheckbox.id == 'bDelete1' )
		 	alert('Please select atleast one record to delete!');
		return false;
    }
	else
	{
		var ssConfirm;
		if(oCheckbox.id == 'bStatus' || oCheckbox.id == 'bStatus1')
			   ssConfirm = confirm('Are you sure want to change status of selected records?');  
		if(oCheckbox.id == 'bDelete' || oCheckbox.id == 'bDelete1')
			   ssConfirm = confirm('Are you sure want to delete selected records?');  	
		 
	  if(ssConfirm == true)
		return true;
	  else
		return false;    
    }
}







function check1(osForm,ssCheckbox)
{            
    var snCounter = 0;
	
	var snX = document.getElementById('flag'); // varibale take for check hidden box value

	if(snX.value == 1)	   // works if hidden textbox value is 1 for confirm message  display on delete button link
	{
		for( x=0; x<osForm.elements.length; x++ )
		{
		 	// if any checked button are check or not
		   var ssChecked = osForm.elements[x];
		   if ( ssChecked.name == ssCheckbox )
		   {
			  if ( ssChecked.checked ) 
			  snCounter++;
		   }
		}

		
		if(snCounter < 1)
		{
			 alert(ssAllUnSelectDeleteMsg);
			 return false;
		}
		else 
		{
			 var ssConfirm = confirm(ssAllConfirmDeleteMsg);
			 if(ssConfirm == true)
				return true;
			 else
				return false;    

		}
	}
	else  
	{	// works if hidden textbox value is 0 for confirm message not display on edit button link
			return true;
	}
	

}  

//functions for paging call after select box submit
function pagingjoinedfunction(snPagingval,ssPageUrl)
{ 
       location.href = ssPageUrl+"?pagingJoinedGroup="+snPagingval;
 }   
// functions for paging call after select box submit
function pagingfunction(snPagingval,ssPageUrl)
{		
       location.href = ssPageUrl+"?paging="+snPagingval;
}	


function checkAll(oCheckbox, ssCheckbox)
{   

    var anCommonId = eval('document.getElementsByName("' + ssCheckbox + '")');

	if(oCheckbox.checked) 
	{
		document.getElementById('inactivateIds').value = '';
		for( snX = 0; snX < anCommonId.length; snX++ )
		{
			anCommonId[snX].checked = true;
		}
	}else
	{	
		var anId = new Array();
		for( snX = 0; snX < anCommonId.length; snX++ )
		{
			anCommonId[snX].checked = false;
			anId.push(anCommonId[snX].value);
			
		}
		document.getElementById('inactivateIds').value = anId;
	}
 
}  


function uncheck(oCheckbox, ssCheckbox)
{   
	var inactivateIds = document.getElementById("inactivateIds");

	var anCheck= eval('document.getElementsByName("' + oCheckbox.name + '")');
	var anCheckbox = eval('document.getElementById("' + ssCheckbox + '")');
	
	smStrId = oCheckbox.id;
	snId = smStrId.split("_");
	snId = snId[2]; //Fetch the ID
	
	ssInactiveIds = inactivateIds.value;
	anInactiveIds = new Array();
	if(ssInactiveIds != '')	anInactiveIds = ssInactiveIds.split(",");	//Convert String to Array
	
	if(oCheckbox.checked) 
	{
		anInactiveIds  = removeItems(anInactiveIds, snId ); // Remove Item from the Array
		
		var snFlag = 0;
		for(snX = 0; snX < anCheck.length; snX++ )
		{

			if(!anCheck[snX].checked)
			{
				var snFlag = 1;
			}
		}
		if(snFlag == 0)
		{
			anCheckbox.checked = true;
		}
	}
	else
	{			
		 anInactiveIds.push(snId);
		
		anCheckbox.checked = false;
	}
	inactivateIds.value = anInactiveIds.join(",")	// Convert Array to String and assign to the hidden variable	
	
}  

function removeItems(array, item) {
var i = 0;
while (i < array.length) {
if (array[i] == item) {
array.splice(i, 1);
} else {
i++;
}
}
return array;
}


// Check any membership radio button checked or not?
function checkradio(snRadio)
{
	var anCommonId = eval('document.getElementsByName("' + snRadio + '")');
	var Flag = 'Off';

	for( snX = 0; snX < anCommonId.length; snX++ )
	{
	   if(anCommonId[snX].checked == true)
	   		{
				Flag = 'On';
				break;
			}
	}

     if(Flag == 'On')
	 	return true;
		
	 alert("Please select any Records");
		return false;	
}

function checkvalidHeaderPosition(amPositionValues)
{

	var anPositionId = eval('document.getElementsByName("' + amPositionValues + '")');
	var snFlag = 1;
	var regexp = /^[0-9]+$/;
	
		for( x=0; x<anPositionId.length; x++ )
		{
			snValue = anPositionId[x].value;
			if(regexp.test(snValue))
			{
				snFlag = 1;
			}
			else
			{
				snFlag = 0;
				break;
			}
	
		}
	

		if(snFlag == 0)
		{
			alert('Please Select valid Position!');
			return false;
		}
				
			return true;
}


function checkHandlingFees(amPositionValues)
{
	var anPositionId = eval('document.getElementsByName("' + amPositionValues + '")');
	var snFlag = 1;
	//var regexp = /^[0-9]+$/;
	var regexp = /^([\$]?)([0-9]*\.?[0-9]{0,2})$/;
	
	for( x=0; x<anPositionId.length; x++ )
	{
		snValue = anPositionId[x].value;
		
		if(regexp.test(snValue))
			snFlag = 1;
		else
		{	
			snFlag = 0;
			break;
		}
	}

	if(snFlag == 0)
	{
		alert('Invalid value for Handling Fee. (' + snValue + ') Please correct and retry.');
		
		return false;
	}
				
	return true;
}


function openwinurl(url) {
	window.open(url);
}


function addUserFieldVar()
{ 
	var sUserField = document.getElementById("product_code").value;	
	var snIdDiv = document.getElementById("snIdDiv");
	snIdDiv.innerHTML = "<input type='textbox' value='--"+sUserField+"--' readonly size='30'>";	
}



//checkprint() function checks and display alert if all printinng checkboxes are unchecked in indexSuccess.php of all module in admin side
function checkprint(oCheckbox, ssCheckbox, ssPageUrl)
{  
    var snCounter = 0;
    var anCommonId = eval('document.getElementsByName("' + ssCheckbox + '")');
    var anOrderIds = new Array();

	
	var snI = 0;
	for( x=0; x<anCommonId.length; x++ )
    {
		var ssChecked = anCommonId[x];
		
		
		if(ssChecked.checked == true)
		{
			anOrderIds[snI] = ssChecked.value;
			snI++;
		}
		
		if (oCheckbox.id == 'bshipping' || oCheckbox.id == 'binvoices')
        {
		  if ( ssChecked.checked ) 
		  	snCounter++;
       }
	   
	  
	}
 
	
	if(snCounter < 1 && (oCheckbox.id == 'binvoices' || oCheckbox.id == 'bshipping'))
    {
		 if( oCheckbox.id == 'bshipping' )
			 	alert('Please select atleast one record to generate shipping address!');
		 if( oCheckbox.id == 'binvoices' )
			 	alert('Please select atleast one record to generate invoices!');
		 
		 return false;
		
    }
	else
	{
		//  location.href = ssPageUrl+"?paging="+snPagingval;
		//location.href = ssPageUrl+"?paging=10";
		//window.open(ssPageUrl);
		
		/*
		  
form = document.createElement("form");
form.method = "GET";
form.action = "http://www.your.link.url.com";
form.target = "_blank";
document.body.appendChild(form);
form.submit();		  

document.myform.submit();
		  
		 */
//		alert(anOrderIds);
		
		form = document.createElement("form");
		form.method = "POST";
		
		if(ssCheckbox == 'id_order_print_addresses[]')
			form.action = ssPageUrl+"?smIdOrdersShipping="+anOrderIds;
		else if(ssCheckbox == 'id_order_print_invoices[]')
			form.action = ssPageUrl+"?smIdOrdersInvoices="+anOrderIds;
		
		form.target = "_blank";
		document.body.appendChild(form);
		form.submit();		  
		
//		window.open(ssPageUrl);
		
		return false;
	}
	
}

function singleinvoiceprint(snOrderId,snIdCustomer,ssPageUrl)
{
		var form = document.createElement("form");
		
		form.method = "POST";
		
		form.action = ssPageUrl+"?smIdOrdersInvoices="+snOrderId+"&id_customer="+snIdCustomer;
		
		form.target = "_blank";
		document.body.appendChild(form);
		form.submit();		  
		
		return false;
		
}

function invoiceprint(ssPageUrl)
{
		var form = document.createElement("form");
		form.method = "POST";
		
		form.action = ssPageUrl;
		
		form.target = "_blank";
		document.body.appendChild(form);
		form.submit();		  
		
		return false;
}



function open_win()
{
		myWindow=window.open('','','width=500,height=300');
		myWindow.document.write('<p><center>Keywords may be separated by AND and/or OR statements for greater control of the search results.</center></p><p><center>For example, Microsoft AND mouse would generate a result set that contain both words. However, for <u>mouse OR keyboard</u>, the result set returned would contain both or either words.</center></p><p><center>Exact matches can be searched for by enclosing keywords in double-quotes.<br /><br />For example, &quot;notebook computer&quot; would generate a result set which match the exact string.<br /><br /> Brackets can be used for further control on the result set.<br /> <br />For example, Microsoft and (keyboard or mouse or &quot;visual basic&quot;).<br /></center></p>');
		myWindow.focus();
}



//Admin Side banner add/edit time
function change_baner_validation()
{

 var ssSection = document.getElementById('banner_section').value;
 var snPosition = document.getElementById('banner_position').value;
 	
	 if(ssSection == 'primary')
	 {
		document.getElementById('limit_primary').style.display = 'block';
		document.getElementById('tr_banner_position').style.display = '';
		document.getElementById('banner_position').value = '';
		document.getElementById('limit_secondary').style.display = 'none';
		
	 }
	 else
	 {
		document.getElementById('limit_primary').style.display = 'none';
		document.getElementById('tr_banner_position').style.display = 'none';
		document.getElementById('banner_position').value = 1;
		document.getElementById('limit_secondary').style.display = 'block';
	 }

}

function bcategory(url)
{ 
   window.location = '/blog/index?bcategory_title='+url;   
}

function displayInput(textvalue)
{ 
   var ssText = textvalue.value;
   //alert(document.getElementById('selectTextDiv')); exit;
  // document.form1.address2.disabled=false;


    if(ssText == 'Virtual Product')
    {
		document.getElementById('selectTextDiv').style.display = '';
		document.getElementById('selectTextsDiv').style.display = '';
		document.getElementById('product_weight').readOnly = true;
		document.getElementById('product_weight').value = 0;
		document.getElementById('selectTextWeightDiv').style.display = 'none';		
    }
    if(ssText == 'Combine')
    {
		document.getElementById('selectTextDiv').style.display = '';
		document.getElementById('selectTextsDiv').style.display = '';	
		document.getElementById('product_weight').readOnly = false;
		document.getElementById('selectTextWeightDiv').style.display = '';
    }	  
    if(ssText == 'Physical Product')
    {
		document.getElementById('selectTextDiv').style.display = 'none';
		document.getElementById('selectTextsDiv').style.display = 'none';
		document.getElementById('product_weight').readOnly = false;
		document.getElementById('selectTextWeightDiv').style.display = '';
    }
    
}


function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
			

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// Textbox display when user select fieldname.
function addUserField()
{ 
	var sUserField = document.getElementById("username").value;	
	var snIdDiv = document.getElementById("snIdDiv");
	
	snIdDiv.innerHTML = "<input type='textbox' value='"+sUserField+"' readonly size='30'>";	
}


//at admin side in all module auto filled dynamic Seo_Url Field
function setSeoUrl(ssValue,ssTextBoxId)
{ 
	ssValue = ssValue.replace(/^\s+|\s+$/g, '');   // Remove space from start/end
	
	/*ssValue = ssValue.replace(/"/,'');  
	ssValue = ssValue.replace(/(/,'');  
	ssValue = ssValue.replace(/)/,'');  
	ssValue = ssValue.replace(/@/,'');
	ssValue = ssValue.replace(/^/,'');      
	ssValue = ssValue.replace(/%/,'');      
	ssValue = ssValue.replace(/./,'');        
	ssValue = ssValue.replace(/,/,'');        
	ssValue = ssValue.replace(/?/,'');
	ssValue = ssValue.replace(/'/,'');        
	ssValue = ssValue.replace(/:/,''); */
	ssValue = ssValue.replace(/\%|\@|\#|\$|\^|\(|\)|\.|\,|\?|\'|\:|\"|\’|\“|\!|\*|\||\`|\~|\/|\\|\{|\}|\;|\<|\>|\+|\=|\[|\]/g,''); 
      
	ssTextbox = document.getElementById(ssTextBoxId);
	
	ssValue = ssValue.replace(/\lndpge/g,'');
	ssValue = ssValue.replace(/\-lp/g,'');
	
	

	
	
	
	ssTextbox.value = ssValue.replace(/\s+/g,'_');   //Replace space with Hyphen
	


}

//At client side when logged in member select the upgrade membership
function redirectMembership(snValue)
{
	
	/*  for local
	if(snValue == 5 || snValue == 6)   //Regular and audio new letter account
		location.href = '/Economic_Forecast_Newsletters';

	if(snValue == 4)		//For standard free account
		location.href = '/store';

	if(snValue == 7)		//For financial advisor network
		location.href = '/Advisors_Store';
	*/
	
	//  for live
	if(snValue == 5 || snValue == 6)   //Regular and audio new letter account
		location.href = '/Economic_Forecast_Newsletters';

	if(snValue == 4)		//For standard free account
		location.href = '/store';

	if(snValue == 7)		//For financial advisor network
		location.href = '/Join_the_Advisors_Network';
		
		
	
}//for login
function getLoginDetail(ssLogin)
{
	$k=jQuery.noConflict();	
	//alert("hello");
	var loadUrl = "/common/getLoginButtonDisplay?ssLoginDisplay="+ssLogin; 
	$k("#ssHsdentLoginBtnID").load(loadUrl);
}

